炼数成金Tensorflow学习笔记之2.3_fetch_and_feed
代码及分析
"""
Created on Sat Mar 14 10:06:12 2020
@author: 寒火qwer
"""
import tensorflow as tf
input1 = tf. constant( 3.0 )
input2 = tf. constant( 2.0 )
input3 = tf. constant( 5.0 )
add = tf. add( input2, input3)
mul = tf. multiply( input1, add)
with tf. Session( ) as sess:
result = sess. run( [ add, mul] )
print ( result)
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: 8 . , input2: 2 . } ) )
tf.multiply (x,y,name=None) 作用:乘法,相同位置的元素相乘(也是可以广播的)tf.placeholder (dtype,shape=None,name=None) 作用:在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。 dtype :数据类型。常用的是tf.float32,tf.float64等数值类型 shape :数据形状。默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)