커뮤니티
Python/TensorFlow
제목:    keras.layes.dot, tf.tensordot
  1103   김윤중

SDSD

  • Understand batch matrix multiplication, Keras
    • Keras dot, tf dot
    • tf.matmul,  K.batxh_dot
    • 2차원에서는 K.dot와 tf.matmul  동일
      • from keras import backend as K
        a = K.ones((3,4))
        b = K.ones((4,5))
        c = K.dot(a, b)
        print(c.shape) #(3,5)
        mport tensorflow as tf
        a = tf.ones((3,4))
        b = tf.ones((4,5))
        c = tf.matmul(a, b)
        print(c.shape) #(3,5)
    • 고차원이면 Note that this behavior is specific to Keras dot. It is a reproduction of Theano behavior.
      • from keras import backend as K
        a = K.ones((2,3,4))
        b = K.ones((7,4,5))
        c = K.dot(a, b)
        print(c.shape) #(2,3,7,5)
    • Matrix multiplication when tensors are matrices
      • from keras import backend as K
        a = K.ones((9, 8, 7, 4, 2))
        b = K.ones((9, 8, 7, 2, 5))
        c = K.batch_dot(a, b)
        print(c.shape) #(9, 8, 7, 4, 5) 
        import tensorflow as tf
        a = tf.ones((9, 8, 7, 4, 2))
        b = tf.ones((9, 8, 7, 2, 5))
        c = tf.matmul(a, b)
        print(c.shape) #(9, 8, 7, 4, 5) 

frfrom keras.layers.merge import dot
import tensorflow as tf
from keras.layers import Input, Dense, Masking, Dropout, LSTM, Bidirectional, Activation

ZX

u=tf.constant(
    [[1,2,3],
     [2,3,3]],dtype=float) #2,3 (B,F)
y=tf.constant(
    [[[3,2,3] ,
      [4,2,3]] ,
     [[5,2,3] ,
      [6,2,3]]],dtype=float) #2,2,3  (B,T,F)
   #2,2,3 
alpha1 = dot([y,u], axes=-1)  # (2,2) dot(-1)(B,F)(B,T,F)=>(B,T)    
alpha = Activation('softmax')(alpha1)    
#(?,T)

z = dot([y,alpha], axes=1)    # (2,3)   dot(1)(B,F)(B,T,F)=>(B,T)

with tf.Session() as sess:
    u_,y_,alpha1_,alpha_,z_=sess.run([u,y,alpha1,alpha,z])
    print('u:{}\n,y:{}\n,alpha1:{}\n,alpha:{}\n,z:{}\n'.format(u_,y_,alpha1_,alpha_,z_))
    print('u:{}\n,y:{}\n,alpha1:{}\n,alpha:{}\n,z:{}\n'.format(u_.shape,y_.shape,alpha1_.shape,alpha_.shape,z_.shape))
"""
u:(2,3)   (B,F)
[[1. 2. 3.]
 [2. 3. 3.]]
y:(2,2,3)   (B,T,F)
[[[3. 2. 3.]
  [4. 2. 3.]]
 [[5. 2. 3.]
  [6. 2. 3.]]]
alpha1:(2,2)=dot([u, y], axes=-1)  (B,F)(B,T,F)=>(B,T)
[[16. 17.]
 [25. 27.]]
alpha:(2,2)
[[0.26894143 0.7310586 ]
 [0.11920291 0.880797  ]]
z:(2,3)=dot([alpha, y], axes=1)    dot([(B,T) (B,T,F)],1)=>(B,F)
[[3.7310586 2.        3.       ]
 [5.880797  1.9999999 3.       ]]

dot([(B,T) (B,T,F)],1)=>(B,F)
dot([(B,T) (B,T,F)],-1)=>(B,T)
"""

 

vu = tf.tensordot(v, u_omega, axes=1, name='vu')  # (B,T) shape (B,T,A)*(A)=(B,T)
 alphas = tf.nn.softmax(vu, name='alphas')           # (B,T) shape (B,T)=(B,T)

v=tf.constant(
[[[111. 112. 113. 114.]
  [121. 122. 123. 124.]
  [131. 132. 133. 134.]]
 [[211. 212. 213. 214.]
  [221. 222. 223. 224.]
  [231. 232. 233. 234.]]], dtype=tf.float32)
(2,3,4)
[[[111. 112. 113. 114.]
  [121. 122. 123. 124.]
  [131. 132. 133. 134.]]
 [[211. 212. 213. 214.]
  [221. 222. 223. 224.]
  [231. 232. 233. 234.]]]

u=tf.constant( [1 1 1 1], dtype=float32)
(4,)
[1. 1. 1. 1.]

vu=tf.tensordot(v,u,axes=1)
(2, 3)
[[450 490 530]
 [850 890 930]]

A.B=|A| |B| cos(th)=trans(A)B
동일한 각도 때 최대값, 90도이면 0, 반대방향이면 -최대값
(a1,