本节主要详述tensorflow的使用feed来对变量赋值,以下代码在python3.5,tensorflow1.5.0,numpy1.13.3下测试通过,想学习的小伙伴可以直接拷贝运行,一块学习提高呀。
需要用到feed来赋值的操作可以通过tf.placeholder()说明,以创建占位符。
代码:
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:[7.0], input2:[2.0]})运行结果:
[array([ 14.], dtype=float32)]代码:
with tf.Session() as sess:
result = sess.run(output, feed_dict={input1:[7.0], input2:[2.0]})
print type(result)
print result运行结果:
<type 'numpy.ndarray'>
[ 14.]代码:
with tf.Session() as sess:
result = sess.run(output, feed_dict={input1:7.0, input2:2.0})
print type(result)
print result运行结果:
<type 'numpy.float32'>
14.0代码:
with tf.Session() as sess:
print sess.run([output], feed_dict={input1:[7.0, 3.0], input2:[2.0, 1.0]})运行结果:
[array([ 14., 3.], dtype=float32)]代码:
with tf.Session() as sess:
print sess.run(output, feed_dict={input1:[7.0, 3.0], input2:[2.0, 1.0]})运行结果:
[ 14. 3.]如果代码有看不懂的小伙伴,可以留言或者私信博主(* ̄︶ ̄)。
2470

被折叠的 条评论
为什么被折叠?



