深度学习手记(二)之占位符Placeholder

TensorFlow是一种符号式编程,它里面有各种计算流图和变量,今天来介绍一种占位符,它的好处是可以避免生成大量常量来提供输入数据,提高了计算图的利用率。其实,今天介绍这个Placeholder占位符还有一个原因:就是使用它经常会出现下面问题:
(1)ValueError: Cannot feed value of shape (2,) for Tensor ‘input_2:0’, which has shape ‘(?, 2)’

(2)InvalidArgumentError (see above for traceback): Matrix size-incompatible: In[0]: [2,3], In[1]: [1,2]
[[Node: MatMul_2 = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device=”/job:localhost/replica:0/task:0/cpu:0”](MatMul_1, _arg_input_2_0_1)]]

(3)InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor ‘Placeholder_5’ with dtype float and shape [100,784]
[Node: Placeholder_5 = Placeholder[dtype=DT_FLOAT, shape=[100,784], _device=”/job:localhost/replica:0/task:0/cpu:0”]]
这三类问题的原因是不样的,前面两种问题的答案,我现在已经知道,但,第三种问题目前还不知道是怎么回事,希望牛人解答。
我们来说前两种问题。


第一种:

import tensorflow as tf

input_1 = tf.placeholder(tf.float32, shape=(None, 2), name="input_1")
input_2 = tf.placeholder(tf.float32, shape=(None, 2), name="input_2")
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
output = tf.matmul(input_1, input_2)
a = tf.matmul(input_1, w1)
y = tf.matmul(input_2, a)
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init) 
    print(sess.run(y, feed_dict={input_1: [[1., 2.], [3., 4.]], input_2: [3, 4]}))

上面代码就会出现:
这里写图片描述
造成这种情况的原因是你给出的shape类型不对。input_2的type应该是一个二维数组,用[[3, 4]]表示才对。


第二种:

import tensorflow as tf

input_1 = tf.placeholder(tf.float32, shape=(None, 2), name="input_1")
input_2 = tf.placeholder(tf.float32, shape=(None, 2), name="input_2")
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
output = tf.matmul(input_1, input_2)
a = tf.matmul(input_1, w1)
y = tf.matmul(a, input_2)
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(y, feed_dict={input_1: [[1., 2.], [3., 4.]], input_2: [[3, 4]]}))

上面代码就会出现:
这里写图片描述
其实,造成这种问题的原因也很简单。问题就出在matmul函数,这个函数是表示两个矩阵相乘,那么就要明白两个矩阵相乘的条件是什么,一定是第一个矩阵的列数(column)和第二个矩阵的行数(row)相同。tf.matmul(input_1, w1)得到的a的shape为(2,3)那么它与input_2(1,2)就没有办法相乘,只有input_2(1,2)能与a(2,3)相乘。所以该这样操作:

import tensorflow as tf

input_1 = tf.placeholder(tf.float32, shape=(None, 2), name="input_1")
input_2 = tf.placeholder(tf.float32, shape=(None, 2), name="input_2")
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
output = tf.matmul(input_1, input_2)
a = tf.matmul(input_1, w1)
y = tf.matmul(input_2, a)
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(y, feed_dict={input_1: [[1., 2.], [3., 4.]], input_2: [[3, 4]]}))

这里写图片描述
这样就没有出错了。

猜你喜欢

转载自blog.csdn.net/llh_1178/article/details/79563723