This sounds a lot likemight be The Bridge by Iain Banks.
From iain-banks.net
The man who wakes up in the extraordinary world of a bridge has amnesia, and his doctor doesn't seem to want to cure him. Does it matter? Exploring the bridge occupies most of his days. But at night there are his dreams. Dreams in which desperate men drive sealed carriages across barren mountains to a bizarre rendezvous; an illiterate barbarian storms an enchanted tower under a stream of verbal abuse; and broken men walk forever over bridges without end, taunted by visions of a doomed sexuality.
There is a wikipedia page (I'll addgives away too much IMO): wikipedia
The beginning of the book is more about the bridge than a train. The main character has sessions with some sort of psychiatrist. Later on, he goes on a train journey (the train remaining on the bridge which seems to go on forever).