2017-12-01 1 views
1

내 Keras 모델에서 길쌈 레이어를 통과 한 후 내 데이터가 어떻게 표시되는지 확인하려고합니다. 나는 Theano 백엔드를 사용하고 있습니다. 내가 가진 코드는 Keras Github에서에서 함께 자갈길되었습니다Keras 모델에서 레이어의 출력을 얻으려고 할 때 IndexError가 발생했습니다.

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

는 내가 여기서 할 노력하고있어 내 네트워크의 첫 번째 레이어 (A Conv2D 층)하는 기능을 컴파일합니다. test_data 인수는 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 

이 무엇을 의미합니까 :이 함수를 호출 할 때

그러나, 나는 비밀 스택 트레이스를 얻을? 함수를 잘못 호출하고 있습니까?

답변

0
귀하의 기능은 다음과 같은 모델을 사용하여 나를 위해 작동

: 레이어 0은 입력 층이 아닌 Conv2D이지만, 코드는 또한 내가 그렇게 tensorflow 백엔드를 사용하고 층 (1) 작동

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 

주 그 차이가 당신의 모델 이냐 아니면 theano 백엔드인지 나는 모른다.

+0

흥미 롭습니다. 나는 tensorflow로 시도 할 것이다. –

+0

Keras 및 TensorFlow의 어떤 버전을 사용하고 있습니까? 파이썬 2 또는 3을 사용하고 있습니까? –

+0

Keraas 2.0.5, TensorFlow 1.2.1, Python 2 –

관련 문제