728x90

Einstein summation

numpy, torch, tensorflow ๋“ฑ ๋‹ค์–‘ํ•œ Tensor Type์˜ ํ˜•ํƒœ๋ฅผ ๋ณ€ํ˜•ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ํ˜„์žฌ๋Š” einops์™€ ์ฝ”๋“œ๋ฅผ ํ•จ๊ป˜ ์ž‘์„ฑํ•˜๋ฉด ์ •๋ง ๋ชจ๋“  ์ด๊ฐ€ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์„ ์„ค๋ช…์„œ ๊ฐ™์€ ๋ชจ๋ธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Example)

 

Attention์—์„œ energy๋ฅผ ๊ตฌํ• ๋•Œ Q ์™€ K^T์˜ ํ–‰๋ ฌ๊ณฑ์‹œ Key ๋ถ€๋ถ„์˜ Transpose๋ฅผ ์‰ฝ๊ฒŒ ํ‘œํ˜„ ๊ฐ€๋Šฅ

quries = torch.tensor(np.array([i for i in range(0,240)]).reshape(1,2,6,20),dtype=torch.float64)
keys = torch.tensor(np.array([i*2 for i in range(0,240)]).reshape(1,2,6,20),dtype=torch.float64)
energy = torch.einsum('bhqd, bhkd -> bhqk', quries, keys)
print(energy.size())
print(f'att {F.softmax(energy, dim=-1).size()}')
#torch.Size([1, 2, 6, 6])
#att torch.Size([1, 2, 6, 6])

 

 

Vision in Transformer:  1 X 3 X 224 X 224์˜ ์ด๋ฏธ์ง€์—์„œ 16 X 16์˜ kernel์„ ๊ฑฐ์นœ Shape๋ฅผ ํ‘œํ˜„ํ• ๋•Œ๋„ ์ง๊ด€์ ์œผ๋กœ ์ฝ”๋“œ์— ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

b (h  k1) (w k2) c -> b (h w) (c k1 k2)

 

patch_size = 16 # paper default
## 1, 3, 224 ,224 -> 1, (14x16=224) (14x16=224) -> 1, (14 14) (16 16 3)
patches = rearrange(x, 'b c (h k1) (w k2) -> b (h w) (k1 k2 c)',k1=patch_size, k2= patch_size)

 

๋ฐ˜์‘ํ˜•
๋‹คํ–ˆ๋‹ค