占位符以外的张量流代码取决于批量大小

时间:2016-09-02 11:01:30

标签: python tensorflow

我是TF新手。作为实验,我尝试修改MNIST示例,直接实现非线性而不是使用预定义函数。

MNIST输入数据在程序中显示为尺寸张量(10,784),其中10是批量大小。当这个数据通过我的(修改过的)程序传递时,我得到一个形状错误("必须具有相同的等级"),除非代码的其余部分引用批量大小。这与我对教程的期望相反,其中暗示批量大小仅需要考虑占位符,并且通常可以忽略其他化。我曾预料到大多数代码都可以写成,就像它一次处理一个数据点一样。

以下是代码的相关部分。 mkmodule是从mnist.py(来自提供的示例)中的inference()的修改版本调用的,而mnist.py又是从fully_connected_feed.py调用的。 mkmodule()的theinput争论是一批图像,大小(10,784)。

def mknonlin(theinput,nactive):
    print('theinput shape vs nactive',theinput.get_shape(), nactive)
    a = tf.Variable(tf.ones([nactive]),name='a')
    c = tf.Variable(tf.zeros([nactive]))
    amul = tf.mul(a,(theinput-c),name='mul-a')
    zeros = tf.zeros([nactive])
    print('a',a.get_shape(), 'c',c.get_shape(),   'amul', amul.get_shape())
    result = tf.select(tf.greater(theinput,c), amul, zeros)
    return result

def mkmodule(thename,theinput,insize,outsize):
    print('module',thename,'input shape=',theinput,insize,'output size=',outsize)
    W = tf.Variable(tf.truncated_normal([insize,outsize], stddev=1.), name='W')
    activation = mknonlin(tf.matmul(theinput,W), outsize)
    return activation

算法的精度是乘以大小(784,32)的权重矩阵W,然后 如果激活较少,则通过元素非线性传递激活 而不是常数c并且线性增加,否则由乘法表示。也就是说,在伪代码中,

if input > c
   elementwise_activation = a * (input - c)
else
   elementwise_activation = 0

这是代码

amul = tf.mul(a,(theinput-c),name='mul-a')

和选择

result = tf.select(tf.greater(theinput,c), amul, zeros)

无论如何,这里的算法只是一个轻率的实验。请只关注张量形状。

运行时,它会从tf.select

中产生以下错误
ValueError: Shapes (10, 32) and (32,) must have the same rank

print语句的输出是:

module module1 input shape= Tensor("Placeholder:0", shape=(10, 784), dtype=float32) 784 output size= 32

theinput shape vs nactive (10, 32) 32

a (32,) c (32,) amul (10, 32)

我主要感到mul的输出大小(10,32)而不是大小32。 这是否意味着图表中的某些变量(例如此处为zero)应将批量大小作为维度之一?但其他变量如a不需要吗?

那会很混乱。我想我一定是误会了。

0 个答案:

没有答案