在tensorflow中用张量这种数据结构来表示所有的数据类型,因为tensorflow特殊的数据计算和处理形式,图进行计算是,可以从外界传入数据。二TensorFlow并不能直接对传入的数据进行处理,因此使用paceholder保留一个数据的位置,之后可以在Tensorflow会话运行的时候进行赋值。
实例代码:
import tensorflow as tf
input1 = tf.placeholder(tf.int32)
input2 = tf.placeholder(tf.int32)
output = tf.add(input1,input2)
sess = tf.Session()
print(sess.run(output,feed_dict={input1:99,input2:2}))
print(output.dtype)
print(sess.run(output,feed_dict={input1:[100,200],input2:[1,1]}))
print(output.dtype)
输出结果为:
101
<dtype: 'int32'>
[101 201]
<dtype: 'int32'>
在会话过程中,input1为一个张量,被赋予的值时99,input2为2。run()启动数据流图进行计算,输出结果为99+2=101。