一、張量
張量本身隻是一個計算的過程,不會存儲結果;例如:
a = tf.constant(30) b = tf.constant(20) c = a + b print(c)
結果:Tensor(“add:0”, shape=(), dtype=int32)
二、會 話(session)
前面所說的張量隻是一個過程,若要獲得計算結果,則需建立會話,運作張量的流程:
sess = tf.Session() d = sess.run(c) print(d)
結果:50
三、向前傳播
import tensorflowas tf
#輸入層(設有3個輸入變量1x3) in_layer = tf.random_normal([1,3],mean=-1, stddev=4)
#設定輸入層到隐含層随機權重(設隐含層有3個神經元3x3) in_to_hd_w1 = tf.Variable(tf.random_normal([3,3],mean=-1, stddev=4))
#隐含層3個神經元的值(1x3) hd_layer = tf.matmul(in_layer,in_to_hd_w1)
#隐含層到輸出層随機權重(3x1) hd_to_out_w2 = tf.Variable(tf.random_normal([3,1],mean=-1, stddev=4))
#僅有一個輸出值(1x1) out_layer = tf.matmul(hd_layer,hd_to_out_w2)
sess = tf.Session()
#注:變量必須要有初始值,in_to_hd_w1與in_to_hd_w2需初始話 sess.run(in_to_hd_w1.initializer); sess.run(hd_to_out_w2.initializer);
#輸出值 print(sess.run(out_layer))