从Lasagne (python深度神经网络框架)获取输出



我从Lasagne的官方github中加载了mnist_conf .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)

我知道它需要一个四维张量,但我不知道如何修正它。

你能帮我吗?由于

首先,您尝试将单个"image"传递到您的网络中,因此它具有维度(256,256)

但它需要一个三维数据列表,即图像,在这里被实现为四维张量。

我没有看到你的完整代码,你打算如何使用千层面的接口,但如果你的代码写得正确,从我所看到的到目前为止,我认为你应该首先将你的(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)四维张量,将被千层面界面接受。

如您的错误信息所写,输入期望为形状为(n_samples, n_channel, width, height)的4D张量。在MNIST情况下,n_channels为1,widthheight为28。

但是你输入的是一个二维张量,形状为(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]应该也工作。

最新更新