2017-03-27 2 views
1

내가 가지고있는 다음과 같은 기능 : 시도가 keras 그것을 평가 때와Keras 람다 함수 내적 mistmatch

x = K.variable(np.array(np_x)) 
y = K.variable(np.array(np_x)) 
obj = transpose_dot 
objective_output = obj((x, y)) 
print('-----------------') 
print (K.eval(objective_output)) 

결과를 작동

def transpose_dot(vects): 
    x, y = vects 
    # <x,x> + <y,y> - 2<x,y> 

    return K.dot(x, K.transpose(y)) 

:

[[ 1. 1. 1. 2.] 
[ 1. 2. 2. 4.] 
[ 1. 2. 2. 4.] 
[ 2. 4. 4. 8.] 

그러나,의 기능으로 사용하려고 할 때레이어가 작동하지 않습니다.

self.fn() if output_subset is None else\ 
ValueError: Shape mismatch: x has 2 cols (and 4 rows) but y has 4 rows (and 2 cols) 
Apply node that caused the error: Dot22(Reshape{2}.0, Reshape{2}.0) 
Toposort index: 11 
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)] 
Inputs shapes: [(4, 2), (4, 2)] 
Inputs strides: [(8, 4), (8, 4)] 
Inputs values: ['not shown', 'not shown'] 
Outputs clients: [[Reshape{4}(Dot22.0, MakeVector{dtype='int64'}.0)]] 

내가 여기에 누락 어떤 생각과

np_x = [[1, 0], [1, 1], [1, 1], [2, 2]] 
features = np.array([np_x]) 
test_input = Input(shape=np.array(np_x).shape) 
dot_layer= Lambda(transpose_dot, output_shape=(4,4))([test_input, test_input]) 
x = Model(inputs=test_input, outputs=dot_layer) 
x.predict(features, batch_size=1) 

결과?

편집 : 나는 내 실수를 발견 https://github.com/fchollet/keras/에서 사람의 도움으로 오류 메시지

+0

오류 메시지가 무엇입니까? 당신은 람다 라인에 누락되었습니다 ... –

+0

@ NassimBen, 오류 메시지를 추가, 기본적으로 그것은 모양에 대해 불평하지만'x는 2 cols (그리고 4 rows)이지만 y는 4 rows (그리고 2 cols)를가집니다. ' – oak

답변

0

의 추가 출력. 함수는 get (n, m)을 기대합니다. 그러나 람다 함수를 사용할 때 (샘플, n, m) 얻을 것으로 기대합니다.