2017-05-09 4 views
0

여기에 간단한 배치를 만들려고합니다. 배치의 각 샘플마다 반복됩니다. Keras backend.repeat_elements가 작동하지 않습니까?

balanceMatrix = np.array([[[5,10,10],[1,1,1],[1,1,1]]]) 
print(balanceMatrix.shape) 

balanceMatrix = K.constant(balanceMatrix) 
print(K.shape(balanceMatrix).eval()) 

지금까지, 나는 모양 예상 매트릭스 (1,3,3-)를 너무 좋아 가지고 여기

는 행렬이다. 이제 배치 (예 : 60000 개의 샘플)의 모든 샘플에 대해 반복 할 수 있습니다. keras documentation에서, 나는이해야 할 모든이있다 :

balanceMatrix = K.repeat_elements(balanceMatrix, 60000,axis=0) 
print(K.shape(balanceMatrix).eval()) 

을하지만 이것은 단순히 이해할 수없는 다음과 같은 오류가 발생합니다 :

IndexError        Traceback (most recent call last) 
<ipython-input-28-4356baf13de8> in <module>() 
    20 balanceMatrix = K.constant(balanceMatrix) 
    21 print(K.shape(balanceMatrix).eval()) 
---> 22 balanceMatrix = K.repeat_elements(balanceMatrix, 60000,axis=0) 
    23 print(K.shape(balanceMatrix).eval()) 
    24 

c:\users\ut65\appdata\local\programs\python\python35\lib\site-packages\keras\backend\theano_backend.py in repeat_elements(x, rep, axis) 
    743  if hasattr(x, '_keras_shape'): 
    744   y._keras_shape = list(x._keras_shape) 
--> 745   repeat_dim = x._keras_shape[axis] 
    746   if repeat_dim is not None: 
    747     y._keras_shape[axis] = repeat_dim * rep 

IndexError: tuple index out of range 

을 무슨 일을? 알다시피, 나는 np.repeat(balanceMatrix,60000,axis=0)으로 먼저 그 다음 케라 텐서를 만들 수 있지만, 케라 스 옵션도 제대로 작동하지 않아야합니까?

답변

1

내가 K.variable 여기에 도움이 될 믿습니다

balanceMatrix = K.variable(value=balanceMatrix) 
+0

감사합니다 :) - 당신은 이유를 알고 있습니까? --- 테스트 후, numpy 옵션은 빠른 속도입니다 ... –

+0

@ 대니얼은 여행 중이었고 모바일에 많은 것을 쓸 수 없었습니다./정확한 이유는 모르지만 나는 과거. 내 생각 엔 Keras가 * inplace * 구조를 반복하려고하고 상수를 사용하면 충돌이 발생합니다 (확실하지는 않습니다). 그리고 네, numpy 반복 구조가 거의 항상 빨라질 것입니다. 그러나 numpy로 반복 한 다음 모든 것을 Keras로 옮기는 것은 Keras로 이동 한 다음 거기에서 반복하는 것과 비슷한 성능을 가져야합니다 (충분히 큰 반복을 위해). –