必威体育Betway必威体育官网
当前位置:首页 > IT技术

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

时间:2019-09-02 00:43:24来源:IT技术作者:seo实验室小编阅读:81次「手机版」
 

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]]}))

这里写图片描述

这样就没有出错了。

相关阅读

HTML5之placeholder属性以及如何更改placeholder属性

今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中

关于ContentPlaceHolder与Content控件

定义:ContentPlaceHolder 控件:在 ASP.NET 母版页中定义内容区域。Content控件:保存文本、标记和服务器控件以呈现给母版页中的 Cont

分享到:

栏目导航

推荐阅读

热门阅读