Rats eats their babies after giving birth because they are tired and exhausted. Often times the weaker or almost dead rats are eaten. The mother rat does it to recover it's energy. Giving birth is a exhaustive process so replenishment is necessary and that need to be done fast. If the mother rat doesn't get food on time then it can die.
Male rat can also eat other rat babies. This is done to make female pregnant and produce their own babies. Without this the female may not want to mate. This behavior is common in nature and is unsightly.