在编写TF程序里,经常会有一些待输入的参数,但是在建立模型时,需要使用到它,那么就需要使用占用符的方式来写入计算公式里,也就是建立到模型里的关系。
下面就是一个使用占位符的例子:
#python 3.5.3
#2017-03-13 蔡军生 http://blog.csdn.net/caimouse
#
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.Session() as sess:
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
输出结果如下:
======== RESTART: D:/work/csdn/tensorflow/line/TF_1.0_placeholder1.py ========
[[ 250.13143921 259.39517212 253.62782288 ..., 259.89556885
252.14181519 248.71147156]
[ 259.01690674 262.79354858 260.86987305 ..., 265.65951538
265.07470703 249.14387512]
[ 272.46600342 274.78189087 269.41098022 ..., 282.14144897
271.10812378 266.05484009]
...,
[ 256.34280396 265.10443115 261.69354248 ..., 265.37792969
258.57714844 250.93035889]
[ 259.50469971 265.48266602 259.76217651 ..., 269.34823608
264.03543091 253.12931824]
[ 247.76449585 258.01040649 256.93270874 ..., 262.97912598
255.13204956 245.21099854]]
>>>