def main(): # We treat images as sequences of pixel rows. train, test = sets.Mnist() _, rows, row_size = train.data.shape num_classes = train.target.shape[1] data = tf.placeholder(tf.float32, [None, rows, row_size]) target = tf.placeholder(tf.float32, [None, num_classes]) dropout = tf.placeholder(tf.float32) model = SequenceClassification(data, target, dropout) sess = tf.Session() sess.run(tf.global_variables_initializer()) for epoch in range(10): for _ in range(100): batch = train.sample(10) sess.run(model.optimize, { data: batch.data, target: batch.target, dropout: 0.5 }) error = sess.run(model.error, { data: test.data, target: test.target, dropout: 1 }) print('Epoch {:2d} error {:3.1f}%'.format(epoch + 1, 100 * error))
weight = tf.random.truncated_normal([in_size, out_size], stddev=0.01) bias = tf.constant(0.1, shape=[out_size]) return tf.Variable(weight), tf.Variable(bias) @staticmethod def _last_relevant(output, length): batch_size = tf.shape(input=output)[0] max_length = int(output.get_shape()[1]) output_size = int(output.get_shape()[2]) index = tf.range(0, batch_size) * max_length + (length - 1) flat = tf.reshape(output, [-1, output_size]) relevant = tf.gather(flat, index) return relevant if __name__ == '__main__': # We treat images as sequences of pixel rows. train, test = sets.Mnist() _, rows, row_size = train.data.shape num_classes = train.target.shape[1] data = tf.compat.v1.placeholder(tf.float32, [None, rows, row_size]) target = tf.compat.v1.placeholder(tf.float32, [None, num_classes]) model = VariableSequenceClassification(data, target) sess = tf.compat.v1.Session() sess.run(tf.compat.v1.initialize_all_variables()) for epoch in range(10): for _ in range(100): batch = train.sample(10) sess.run(model.optimize, {data: batch.data, target: batch.target}) error = sess.run(model.error, {data: test.data, target: test.target}) print('Epoch {:2d} error {:3.1f}%'.format(epoch + 1, 100 * error))