尝试在Keras模型中获取图层输出时的IndexError

时间:2017-12-01 20:57:26

标签: python numpy keras

我试图通过我的Keras模型中的卷积层看到我的数据是什么样的。我正在使用Theano后端。我的代码已经从Keras Github拼凑而成:

test_data

我在这里尝试做的是为我网络中的第一层(Conv2D层)编译一个函数。 np.ndarray参数是Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/theano/compile/function_module.py", line 884, in __call__ self.fn() if output_subset is None else\ File "/usr/local/lib/python3.5/dist-packages/theano/gof/op.py", line 872, in rval r = p(n, [x[0] for x in i], o) File "/usr/local/lib/python3.5/dist-packages/theano/tensor/nnet/abstract_conv.py", line 1626, in perform conv_out = self.conv(img, kern, mode="valid", dilation=self.filter_dilation) File "/usr/local/lib/python3.5/dist-packages/theano/tensor/nnet/abstract_conv.py", line 1531, in conv dilated_kern[n, im0, ...], IndexError: index 1 is out of bounds for axis 1 with size 1 。我的模型正确加载,我已经准确地训练了它。

但是,当我调用此函数时,我会得到一个神秘的堆栈跟踪:

onFailure

这是什么意思?我是否错误地调用了我的功能?

1 个答案:

答案 0 :(得分:0)

您的功能适用于我使用以下型号:

a = Input(shape=(224,224,3))
b = Conv2D(8, 3, strides=(2,2))(a)
model = Model(inputs=a, outputs=b)
model.compile(optimizer='sgd', loss='mse')

def get_layer0_outputs(model, test_data):
    output = model.layers[0].output
    inputs = [K.learning_phase()] + model.inputs
    func = K.function(inputs, [output])
    return func([0] + [test_data])

print get_layer0_outputs(model, np.zeros((1, 224, 224, 3)))[0].shape

请注意,第0层是输入层而不是Conv2D,但代码也适用于第1层。我使用张量流后端,所以我不知道差异是你的模型还是theano后端。

相关问题