Get output from Lasagne (python deep neural network framework)

牧云@^-^@ 提交于 2019-12-02 08:30:40

问题


I loaded the mnist_conv.py example from official github of Lasagne.

At the and, I would like to predict my own example. I saw that "lasagne.layers.get_output()" should handle numpy arrays from official documentation, but it doesn't work and I cannot figure out how can I do that.

Here's my code:

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())

but it gives me:

TypeError: ConvOp (make_node) requires input be a 4D tensor; received "TensorConstant{(28, 28) of 0.0}" (2 dims)

I understand that it expects a 4D tensor, but I don't have any idea how to correct it.

Can you help me? Thanks


回答1:


First you try pass a single "image" into your network, which so it has the dimension (256,256).

But it need a list of 3 dimensional data i.e. images, which in theano is implemented as 4D tensor.

I don't see your full code, how you intended to use lasagne's interface, but if your code is written properly, from what I saw so far, I think you should convert your (256,256) data first to a one single channel image like (1,256,256), then make a list from either use more (1,256,256) data passed in a list e.g. [(1,256,256), (1,256,256), (1,256,256)], or make a list from this single example like [(1,256,256)]. Former you get and then pass a (3,1,256,256), latter a (1,1,256,256) 4D tensor, which will be accepted by lasagne interface.




回答2:


As written in your error message, the input is expected to be a 4D tensor, of shape (n_samples, n_channel, width, height). In the MNIST case, n_channels is 1, and width and height are 28.

But you are inputting a 2D tensor, of shape (28, 28). You need to add new axes, which you can do with exampleChar = exampleChar[None, None, :, :]

exampleChar = np.zeros(28, 28)
print exampleChar.shape 
exampleChar = exampleChar[None, None, :, :]
print exampleChar.shape

outputs

(28, 28)
(1, 1, 28, 28)

Note: I think you can use np.newaxis instead of None to add an axis. And exampleChar = exampleChar[None, None] should work too.



来源:https://stackoverflow.com/questions/31499761/get-output-from-lasagne-python-deep-neural-network-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!