forward_propagation.py 789 B

12345678910111213141516171819202122
  1. import tensorflow as tf
  2. # define two variables w1 and w2 as weight matrices, use seed to guarantee we get constant result.
  3. w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
  4. w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))
  5. # define input eigenvector as a constant vector
  6. # x = tf.constant([[0.7, 0.9]])
  7. # use placeholder to store data in a constant place rather than create a large number of variables
  8. x = tf.placeholder(tf.float32, shape=[3, 2], name="input")
  9. # forward propagation to receive the output
  10. a = tf.matmul(x, w1)
  11. y = tf.matmul(a, w2)
  12. with tf.Session() as sess:
  13. # sess.run(w1.initializer)
  14. # sess.run(w2.initializer)
  15. sess.run(tf.global_variables_initializer())
  16. print (sess.run(y, feed_dict={x: [[0.7, 0.9], [0.1, 0.4], [0.5, 0.8]]}))