|
| 1 | +# Lab 7 Learning rate and Evaluation |
| 2 | +import tensorflow as tf |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +x_data = np.array([[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5], |
| 6 | + [1, 7, 5], [1, 2, 5], [1, 6, 6], [1, 7, 7]], dtype=np.float32) |
| 7 | +y_data = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], |
| 8 | + [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]], dtype=np.float32) |
| 9 | + |
| 10 | +X = tf.placeholder("float", [None, 3]) |
| 11 | +Y = tf.placeholder("float", [None, 3]) |
| 12 | + |
| 13 | +W = tf.Variable(tf.zeros([3, 3])) |
| 14 | +# Softmax |
| 15 | +hypothesis = tf.nn.softmax(tf.matmul(X, W)) |
| 16 | +# tf.nn.softmax computes softmax activations |
| 17 | +# softmax = exp(logits) / reduce_sum(exp(logits), dim) |
| 18 | + |
| 19 | +# Cross entropy cost |
| 20 | +cost = tf.reduce_mean(-tf.reduce_sum(Y * |
| 21 | + tf.log(hypothesis), reduction_indices=1)) |
| 22 | + |
| 23 | +# Changed learning_rate to 10 |
| 24 | +optimizer = tf.train.GradientDescentOptimizer( |
| 25 | + learning_rate=10.).minimize(cost) |
| 26 | + |
| 27 | +init = tf.global_variables_initializer() |
| 28 | + |
| 29 | +# Launch graph |
| 30 | +with tf.Session() as sess: |
| 31 | + sess.run(init) |
| 32 | + |
| 33 | + for step in range(2001): |
| 34 | + sess.run(optimizer, feed_dict={X: x_data, Y: y_data}) |
| 35 | + if step % 200 == 0: |
| 36 | + print(step, sess.run(cost, feed_dict={ |
| 37 | + X: x_data, Y: y_data}), sess.run(W)) |
0 commit comments