A man has denied attempting to murder a toddler by stepping in front of a moving train while carrying a child on his shoulders, a court heard.