2017-04-17 1 views
1

처음에 다른 질문이 있습니다. No broadcasting for tf.matmul in TensorFlow
하지만이 질문은 내 문제를 해결하지 못합니다.4D 3D tensors의 tf.matmul에 대한 방송이 없습니다.

제 문제는 행렬의 배치가 다른 벡터 배치를 곱하는 것입니다.

x=tf.placeholder(tf.float32,shape=[10,1000,3,4]) 
y=tf.placeholder(tf.float32,shape=[1000,4]) 

X 10 * 1000 matrices.Each 매트릭스 형상이다이다 [3,4]
Y 1000 vectors.Each 벡터 도형이다 vectors.There에 배치되어있다 matrices.There의 배치 인 [4]
x의 희미한 1과 y의 희미한 0은 동일합니다. tf.matmul 방송을 지원했다면, 나는

y=tf.reshape(y,[1,1000,4,1]) 
result=tf.matmul(x,y) 
result=tf.reshape(result,[10,1000,3]) 

을 쓸 수 그러나 내가

위에 언급 한 문제의 접근 방식을 사용하는 경우
방송 tf.matmul 지원하지 않습니다
(여기서 1000)
x=tf.reshape(x,[10*1000*3,4]) 
y=tf.transpose(y,perm=[1,0]) #[4,1000] 
result=tf.matmul(x,y) 
result=tf.reshape(result,[10,1000,3,1000]) 

결과는 [10,1000,3]이 아닌 모양 [10100031000]입니다.
중복을 제거하는 방법을 모르겠다.
방송을 지원하는 tf.matmul과 동일한 결과를 얻는 방법?

답변

1

직접 해결합니다.

x=tf.transpose(x,perm=[1,0,2,3]) #[1000,10,3,4] 
x=tf.reshape(x,[1000,30,4]) 
y=tf.reshape(y,[1000,4,1]) 
result=tf.matmul(x,y) #[1000,30,1] 
result=tf.reshape(result,[1000,10,3]) 
result=tf.transpose(result,perm=[1,0,2]) #[10,1000,3] 
관련 문제