TensorFlow placeholder

placeholder 允许在用session.run()运行结果的时候给输入一个值

import tensorflow as tf

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1, input2)

with tf.Session() as sess:
    print(sess.run(output, feed_dict={input1: [4.], input2: [5.]}))        #传入的feed_dict是一个字典值

运行结果:

多次操作可以通过一次feed完成执行

import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

c = tf.multiply(a, b)
d = tf.subtract(a, b)
#init = tf.global_variables_initializer()
with tf.Session() as sess:
    #sess.run(init)
    result = sess.run([c, d], feed_dict={a: [1., 3., 5., 7.], b: [2., 4., 6., 8.]})
    print(result)
    print(result[0])

在run方法中首先传入了一个两次运算(tensor) ,第二个参数是feed_dic, 返回的result 是由两个运算结果组成的列表。 当然在python中 可以通过unpack赋值给两个变量 res1, res2=sess.run([c, d], feed_dict={a: [1., 3., 5., 7.], b: [2., 4., 6., 8.]})

原文地址:https://www.cnblogs.com/francischeng/p/9690385.html