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

1586010002-jmsa.png

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

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值