我从Lasagne的官方github加载了mnist_conv.py示例。
最后,我想预测一下我自己的例子。我在官方文档中看到"lasagne.layers.get_output()“应该处理numpy数组,但它不起作用,我不知道该怎么做。
下面是我的代码:
if __name__ == '__main__':
output_layer = main() #the output layer from the net
exampleChar = np.zeros((28,28)) #the example I would predict
outputValue = lasagne.layers.get_output(output_layer, exampleChar)
print(outputValue.eval())但它给了我:
TypeError: ConvOp (make_node) requires input be a 4D tensor; received "TensorConstant{(28, 28) of 0.0}" (2 dims)我知道它需要一个4D张量,但我不知道如何修正它。
你能帮帮我吗?谢谢
发布于 2015-07-19 19:14:30
首先,您尝试将单个“图像”传递到您的网络中,因此它具有维度(256,256)。
但它需要一个三维数据列表,即图像,在theano中是作为4D张量实现的。
我不明白你的完整代码,你打算如何使用千层面的接口,但是如果你的代码写得很好,根据我到目前为止所看到的,我认为你应该首先将你的(256,256)数据转换成像(1,256,256)这样的单通道图像,然后从列表中使用更多的(1,256,256)数据来创建一个列表,比如[(1,256,256), (1,256,256), (1,256,256)],或者从这个例子中创建一个列表,比如[(1,256,256)]。前者是(3,1,256,256),后者是(1,1,256,256) 4D张量,它将被千层面接口接受。
发布于 2015-08-14 18:08:46
正如您在错误消息中所写的,输入应该是形状为(n_samples, n_channel, width, height)的4D张量。在MNIST的例子中,n_channels是1,width和height是28。
但您输入的是2D张量,形状为(28, 28)。您需要添加新的轴,您可以使用exampleChar = exampleChar[None, None, :, :]执行此操作
exampleChar = np.zeros(28, 28)
print exampleChar.shape
exampleChar = exampleChar[None, None, :, :]
print exampleChar.shape输出
(28, 28)
(1, 1, 28, 28)注意:我认为您可以使用np.newaxis而不是None来添加轴。而且exampleChar = exampleChar[None, None]也应该可以工作。
https://stackoverflow.com/questions/31499761
复制相似问题