Computer keeps shutting down
I have 27-inch iMac with Retina 5K display purchased Nov 2018. Currently running Mojave 10.14.6 and use mostly page layout and photo editing apps. Sometimes, with no warning, the screen goes black and the computer restarts. Applications tell me they were not shut down properly, and all the windows reopen themselves. This also happens when the computer is in sleep mode. When I wake it I have to log in as if it had been shut down. Again, same message from the applications that were open when computer had a problem. How do I find out what the problem is so I can try to fix it?
iMac 27" 5K, macOS 10.14