ⵜⴰⵙⴻⴽⴽⵉⵔⵜ ⵏ Einstein Sumation ⴷ ⴰⴱⵔⵉⴷ ⵉⴳⴻⵊⴷⴰⵏⴻⵏ ⵢⴻⵔⵏⴰ ⴷ ⴰⵊⴻⵀⴷⴻⵏ ⵉ ⵓⵙⴻⵎⵔⴻⵙ ⵏ ⵜⵎⴻⵥⵔⵉⵡⵉⵏ ⵏ tensor, ⵢⴻⵜⵜⵡⴰⵙⴻⵇⴷⴰⵛ ⵙ ⵡⴰⵟⴰⵙ ⴷⴻⴳ ⵜⴼⵉⵣⵉⴽⵜ ⴷ ⵓⵍⵎⴰⴷ ⵏ ⵜⵎⴰⵛⵉⵏⵉⵏ. ⵢⴻⵜⵜⴰⴵⴵⴰⴰⵖ ⴰⴷ ⵏⴰⵔⵓ ⵍⴻⵃⵙⴰⴱⴰⵜ ⵉⵅⵓⵚⵚⴻⵏ ⵖⴻⴼ ⵢⵉⵜⴻⵏⵙⵓⵔⴻⵏ ⵙ ⵜⴰⵍⵖⴰ ⵏ ⵜⵎⴻⵥⵥⵓⵖⵜ. ⴰⴷ ⴷⵏⴻⴱⴷⴻⵔ ⵍⵙⴰⵙ ⵖⴻⴼ ⵓⵙⴷⵓⴽⴽⴻⵍ ⵏ Einstein, ⴰⵎⴻⴽ ⴰⵔⴰ ⵜⵏⴻⵙⵙⴻⵅⴷⴻⵎ ⴷⴻⴳ Python ⵙ Numpy ⴷ Tensorflow, ⴰⴷ ⴷⵏⴻⴼⴽ ⵉⵎⴻⴷⵢⴰⵜⴻⵏ ⴰⵔⴰ ⴷⵢⴻⵙⵙⴽⴻⵏ ⴰⵙⴻⵇⴷⴻⵛⵉⵙ.
ⵉⵍⵓⴳⴰⵏ ⵏ ⵓⵙⴷⵓⴽⴽⴻⵍ ⵏ Einstein
ⵜⴰⵙⴻⴽⴽⵉⵔⵜ ⵏ ⵓⵙⴷⵓⴽⴽⴻⵍ ⵏ Einstein (Einsum) ⵜⴻⵜⵜⵡⴰⵙⴱⴻⴷⴷ ⵖⴻⴼ ⵜⵎⵓⵖⵍⵉ ⵏ ⵓⵙⴷⵓⴽⴽⴻⵍ ⵖⴻⴼ ⵢⵉⴹⵔⵉⵙⴻⵏ ⵢⴻⵜⵜⵡⴰⵄⴻⵔⴹⴻⵏ ⴷⴻⴳ ⵜⵎⴻⵙⵍⴰⵢⵉⵏ ⵏ tensor. ⵢⴻⵜⵜⵡⴰⴱⵏⴰ ⵖⴻⴼ ⵙⵉⵏ ⵏ ⵢⵉⵍⵓⴳⴰⵏⴰ :
1. ⴰⵙⴻⵎⵔⴻⵙ ⵖⴻⴼ ⵢⵉⴹⵔⵉⵙⴻⵏ ⵢⴻⵜⵜⵡⴰⵄⴻⵔⴹⴻⵏ: Ma ⵢⴻⵍⵍⴰ ⴰⴹⵔⵉⵙ ⵢⴻⵜⵜⴱⴰⵏⴷ ⵙⵏⴰⵜ ⵏ ⵜⵉⴽⴽⴰⵍ ⴷⴻⴳ ⵢⵉⵡⴻⵏ ⵏ ⵓⵎⵢⴰⴳ, ⵢⴻⵜⵜⵡⴰⵙⵙⵓⵎⵎⴻⵍ ⵖⴻⴼ .
2. ⵉⴹⵔⵉⵙⴻⵏ ⵉⵍⴻⵍⵍⵉⵢⴻⵏ: ⵉⴹⵔⵉⵙⴻⵏ ⵉ ⴷⵢⴻⵜⵜⴱⴰⵏⴻⵏ ⵢⵉⵡⴻⵜ ⴽⴰⵏ ⵏ ⵜⵉⴽⴽⴻⵍⵜ ⴷ ⵉⴹⵔⵉⵙⴻⵏ ⵉⵍⴻⵍⵍⵉⵢⴻⵏ ⵢⴻⵔⵏⴰ ⵜⵜⴳⴻⵏⵙⵉⵙⴻⵏⴷ ⵉⵅⴻⴼ ⵏ ⵜⴻⵏⵙⵓⵔ ⵏ ⵓⵙⵍⵓⴳⴻⵏ .
ⴰⴷ ⴷⵏⴻⵙⵎⴻⴽⵜⵉ ⴰⵢⴰ ⵙ ⵓⵎⴻⴷⵢⴰ ⵏ ⵓⵙⵏⴻⵔⵏⵉ ⵏ ⵙⵏⴰⵜ ⵏ ⵜⵎⴰⵜⵔⵉⵢⵉⵏ A ⴷ B : ⵜⴰⵎⴰⵜⵔⵉⵙⵜ C ⵉ ⴷⵢⴻⵜⵜⵡⴰⵙⵏⵓⵍⴼⴰⵏ ⵜⴻⵜⵜⵡⴰⵙⵙⴻⵏ ⵙ .
ⴷⴻⴳ Pitun, ⴰⵎⴰ ⴷ ⵜⵉⵎⴽⴻⵔⴹⵉⵡⵉⵏ ⵏ Numpy ⴰⵎⴰ ⴷ ⵜⵉⵎⴽⴻⵔⴹⵉⵡⵉⵏ ⵏ Tensorflow ⵜⵜⴰⴽⴻⵏⵜⴷ ⵜⴰⵖⵓⵍⵜ ⵏ ⴻⵉⵏⵙⵓⵎ.
ⵏⵓⵎⴱⵉ
import numpy as np
# Define two matrices A and B
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Perform matrix multiplication using einsum
C = np.einsum('ij,jk->ik', A, B)
print(C)
# [[19 22]
# [43 50]]
ⴷⴻⴳ ⵓⵎⴻⴷⵢⴰ ⵏ ⵜⴼⴻⵍⵡⵉⵜ, ⵉⵊ,ⵊⴽ->ⵉⴽ
ⴷ ⵜⴰⵖⴻⵛⵜ ⵏ ⴻⵉⵏⵙⵓⵎ:
ⵉⵊ
ⵢⴻⵜⵜⴳⴻⵏⵙⵉⵙⴷ ⵜⵉⵡⵜⵉⵍⵉⵏ ⵏ ⵎⴰⵜⵔⵉⵅ A
ⵊⴽ
ⵢⴻⵜⵜⴳⴻⵏⵙⵉⵙⴷ ⵜⵉⵡⵜⵉⵍⵉⵏ ⵏ ⵎⴰⵜⵔⵉⵅ B
->ⵉⴽ
ⵢⴻⵙⵙⴽⴰⵏⴰⵢⴷ ⵉⵏⴷⴻⵅ ⵏ ⵜⵎⴰⵜⵔⵉⵜ ⵏ ⵓⵙⵍⵓⴳⴻⵏ C
ⵜⴰⵎⵀⴻⵍⵜ ⵜⴻⵜⵜⴽⴻⵎⵎⵉⵍ ⵖⴻⴼ ⵓⵎⴹⵉⵇ ⵊ
ⴰⵎ ⵡⴰⴽⴽⴻⵏ ⴰⵔⴰ ⵢⵉⵍⵉ ⵓⴽⴰⵔⴰⵎⵏⵏⵉⴹⴻⵏ ⴷⴻⴳ Tensorflow
import tensorflow as tf
# Define two matrices A and B
A = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
B = tf.constant([[5, 6], [7, 8]], dtype=tf.float32)
# Perform matrix multiplication using einsum
C = tf.einsum('ij,jk->ik', A, B)
print(C)
# tf.Tensor(
# [[19. 22.]
# [43. 50.]], shape=(2, 2), dtype=float32)
ⵓⴳⴰⵔ ⵏ ⵉⵎⴻⴷⵢⴰⵜⴻⵏ
ⴰⴼⴰⵔⵉⵙ ⵏ ⴷⴰⵅⴻⵍ ⵏ ⵢⵉⵙⴻⴽⴽⵉⵍⴻⵏ
ⴰⴼⴰⵔⵉⵙ ⵏ ⴷⴰⵅⴻⵍ (ⴰⴼⴰⵔⵉⵙ ⵏ ⵜⵏⴻⵇⴹⵉⵏ) ⵏ ⵙⵉⵏ ⵏ ⵢⵉⴼⴻⵔⴷⵉⵙⴻⵏ ⴰ ⴷ ⴱ ⵢⴻⵜⵜⵡⴰⵙⴱⴻⴳⴳⴻⵏⴷ ⵙ
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.einsum('i,i->', a, b)
print(c) # Output: 32
ⴰⴼⴰⵔⵉⵙ ⵏ ⴱⴻⵕⵕⴰ ⵏ ⵉⵎⵙⴻⴹⵔⵓⵢⴻⵏ
ⴰⴼⴰⵔⵉⵙ ⵏ ⴱⴻⵔⵔⴰ ⵏ ⵙⵉⵏ ⵏ ⵢⵉⴼⴻⵔⴷⵉⵙⴻⵏ ⴰ ⴷ ⴱ ⵢⴻⵜⵜⵓⵏⴻⴼⴽ ⵙ:
C = np.einsum('i,j->ij', a, b)
print(C)
# Output
# [[4 5 6]
# [8 10 12]
# [12 15 18]]
ⴰⵙⵉⵡⴻⴹ ⵏ Matrix
ⴰⵙⴻⴽⵛⴻⵎ ⵏ ⵜⵎⴰⵜⵔⵉⵜ A ⵢⴻⵣⵎⴻⵔ ⴰⴷ ⴷⵢⴻⵜⵜⵡⴰⴼⴽ ⵙ ⵓⴱⴻⴷⴷⴻⵍ ⵏ ⵢⵉⴹⵔⵉⵙⴻⵏⵉⵙ
A_transpose = np.einsum('ij->ji', A)
print(A_transpose)
# Output
# [[1. 3.]
# [2. 4.]]
ⴰⴹⴰⵔ ⵏ Matrix
ⴰⴹⴰⵔ ⵏ ⵎⴰⵜⵔⵉⵅ A ⴷ ⴰⴳⵎⵓⴹ ⵏ ⵢⵉⴼⴻⵔⴷⵉⵙⴻⵏⵉⵙ ⵏ ⵜⵖⴻⵔⵖⴻⵔⵜ:
trace = np.einsum('ii->', A)
print(trace)
# Output: 5.0
ⴰⵙⵏⴻⵔⵏⵉ ⵏ Matrix ⵏ ⵜⴼⴻⵔⴽⵉⵜ
ⴻⵉⵏⵙⵓⵎ ⴷ ⴰⵢⴻⵏ ⵉ ⴷⵢⴻⵜⵜⴰⵡⵉⵏ ⵏⵏⴼⴻⵄ ⵍⴰⴷⵖⴰ ⵉ ⵜⵎⴻⵥⵔⵉⵡⵉⵏ ⵏ ⵜⴼⴻⵔⴽⵉⵜ. ⴰⴷ ⴷⵏⵉⵏⵉ ⴱⴻⵍⵍⵉ ⵏⴻⵙⵄⴰ ⵢⵉⵡⴻⵜ ⵏ ⵜⴼⴻⵔⴽⵉⵜ ⵏ ⵜⵎⴰⵜⵔⵉⵢⵉⵏ A ⴷ B, ⵢⴻⵔⵏⴰ ⵏⴻⴱⵖⴰ ⴰⴷ ⵏⴻⵙⵙⴻⵎⵔⴻⵙ ⵜⵉⵎⴰⵜⵔⵉⵢⵉⵏ ⵉ ⴷⵢⴻⵜⵜⵡⴰⵙⵇⴻⴷⵛⴻⵏ ⴷⴻⴳ ⵜⴼⴻⵔⴽⵉⵜⵏⵏⵉ:
A = np.random.rand(3, 2, 2)
B = np.random.rand(3, 2, 2)
# Perform batch matrix multiplication
C = np.einsum('bij,bjk->bik', A, B)
print(C)
ⴷⴰⴳⵉ, ⴱ
ⵢⴻⵜⵜⴳⴻⵏⵙⵉⵙⴷ ⵍⵇⵉⴷⴰⵔ ⵏ ⵜⴼⴻⵔⴽⵉⵜ.
ⵍⴼⴰⵢⴷⴰⵜ ⵏ ⵓⵙⴻⴽⵍⴻⵙ ⵏ Einsum
1. ⵜⴰⴳⵣⴻⵎⵜ: Tagzemt ⵏ Einsum ⴷ ⵜⴰⵎⴻⵥⵢⴰⵏⵜ, ⵢⴻⵔⵏⴰ ⵜⴻⵣⵎⴻⵔ ⴰⴷ ⴷⵜⴻⵙⵙⴽⴻⵏ ⵜⵉⵎⵀⴰⵍ ⵙ ⵜⵖⴻⵣⴼⴰⵏⵜ
2. ⴼⵍⴻⵅⵉⴱⵉⵍⵉⵜⵢ: Yezmer ⴰⴷ ⵢⴻⵃⵔⴻⵣ ⴰⵟⴰⵙ ⵏ ⵜⵖⴰⵡⵙⵉⵡⵉⵏ ⵏ tensor ⵢⴻⵎⴳⴰⵔⴰⴷⴻⵏ ⵡⴰⵔ ⵎⴰ ⵢⴻⵜⵜⴱⴻⴷⴷⵉⵍ ⵙ ⵜⵖⴰⵡⵍⴰ ⵏⴻⵖ ⵢⴻⵜⵜⴱⴻⴷⴷⵉⵍⴻⵏ ⵜⵉⴼⴻⵍⵡⵉⵢⵉⵏ .
3. ⴻⴼⴼⵉⵛⵉⴻⵏⵛⴻ: ⴰⵟⴰⵙ ⵏ ⵜⴻⵎⴽⴰⵔⴹⵉⵢⵉⵏ ⵉ ⵢⴻⵙⵙⴻⵎⵖⴰⵔⴻⵏ ⵜⵉⵎⵀⴰⵍ ⵏ ⴻⵉⵏⵙⵓⵎ ⵙ ⴷⴰⵅⴻⵍ, ⵢⴻⵣⵎⴻⵔ ⴰⴷ ⴷⵢⴰⵡⵉ ⵙ ⵜⵖⴰⵔⴰ ⵢⴻⵍⵀⴰⵏ.