1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import tensorflow as tf import numpy as np x_input = np.array([[1,2,3,4,5]]) y_input = np.array([[10]]) x = tf.placeholder(tf.float32, [None, 5]) y = tf.placeholder(tf.float32, [None, 1]) W = tf.Variable(tf.zeros([5, 1])) b = tf.Variable(tf.zeros([1])) y_pred = tf.matmul(x, W)+b loss = tf.reduce_sum(tf.pow((y-y_pred), 2)) train = tf.train.GradientDescentOptimizer(0.0001).minimize(loss) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) for i in range(10): feed_dict = {x: x_input, y: y_input} sess.run(train, feed_dict=feed_dict) sess = tf.Session() sess.run(init) for i in range(10): feed_dict = {x: x_input, y: y_input} _, loss_value = sess.run([train, loss], feed_dict=feed_dict) print(loss_value) 100.0 97.77255 95.594696 93.46538 91.38347 89.34794 87.357765 85.41191 83.5094 81.64925 |