1. 程式人生 > >【Tensorflow】placeholder & feed_dict

【Tensorflow】placeholder & feed_dict

placeholder佔位。

feed_dict 字典型別。向佔位符送資料。

import tensorflow as tf
w1 = tf.Variable(tf.random_normal([2,3],stddev=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1))

x = tf.placeholder(tf.float32,shape=(1,2),name="input")
a = tf.matmul(x,w1) # 矩陣乘法
y = tf.matmul(a,w2)

sess = tf.Session()
sess.run(tf.global_variables_initializer()) #初始化模型的引數
print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))  #注意[[]]

輸出:

[[-2.2629368]]