我是TF的新手。作为一个实验,我试图修改MNIST示例,直接实现非线性,而不是使用预定义函数。在
MNIST输入数据在程序中显示为大小张量(10784),其中10是批大小。当这些数据通过我的(修改过的)程序传递时,我得到一个形状错误(“必须具有相同的秩”),除非代码的其余部分也引用了批大小。这与我在教程中的预期相反,在教程中暗示批大小只需要考虑占位符,而othersize通常可以忽略。我原以为大部分代码可以编写成一次处理一个数据点的样子。在
这是代码的相关部分。mkmodule是从中的inference()的修改版本调用的mnist.py(从提供的示例中)反过来从完全连接的_饲料.py. mkmodule()的theinput
参数是一批图像,大小(10784)。在
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
,否则线性增加,用乘法表示。也就是说,在伪代码中
这是密码
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
不需要它?在
那会很麻烦的。我想我一定是误会了什么。在
目前没有回答
相关问题 更多 >
编程相关推荐