Einstein summation or einsum
So today I was studying the paper dEFEND: Explainable Fake News Detection and their code, where there was a line using einsum, short for Einstein summation.
What is einsum?
So basically, you give it an equation, and it will give you output according to the equation. For example, if we have two matrices m1 and m2, m1 has shape ij and m2 has shape jk, so after multiplication it will have shape ik, so the equation becomes,
ij,jk → ik
In TensorFlow, it becomes like this:
tf.einsum('ij,jk→ik',m1,m2)
But if we have three matrix multiplications like the paper code,
tf.einsum('btd,dD,bDn->btn', m1, m2, m3)
So in the above line, the first matrix multiplication is performed between m1 of shape btd and m2 of shape dD, and the output will be of shape btD, which will be multiplied with m3 of shape bDn, and the output will become of shape btn. If you want to read further about it, please go to the TensorFlow documentation.
Subscribe to my newsletter
Read articles from Rashid Ul Haq directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rashid Ul Haq
Rashid Ul Haq
I am a passionate AI and machine learning expert with extensive experience in deep learning, TensorFlow, and advanced data analytics. Having completed numerous specializations and projects, I have a wealth of knowledge and practical insights into the field. I am sharing my journey and expertise through detailed articles on neural networks, deep learning frameworks, and the latest advancements in AI technology.