It will be too late to undo the the tagging, Emma. The "Edit > Undo" command is only working when applied right away after making a mistake.
In Photos for Mac it can easily happen, that more photos have been selected than you are currently seeing in the Browser. Photos hidden behind the border of the window can remain selected. And one you click the favourite heart ♡, all selected photos will be added to the favorites.
To avoid tagging errors, always look at the upper right corner of the Photos Window, where you can see, how many photos are selected, before you are batch changing anything.
It will be hard to fix the thousands of Favorites, but save the few, that you want to be favorites.
- Either restore the previous version of your Photos Library from a Time Machine backup. Photos for Mac: Restore a Photos library from a Time Machine backup . Before you do this, you will have to export all new photos, that have been added after you made the backup, to a folder on your disk, so you can reimport them.
- Or look through your favourites album and collect all photos, that should be favorites in a separate album. Then select all photos in the Favorites album at once with ⌘A and click the ♡ in the toolbar to remove the Favourite heart. Then open the album, where you saved the true favorites, enter again ⌘A and click again the ♡ in the toolbar to mark your favorites.
Just a hunch - have you enabled the shared iCloud Photos Library and are sharing your Photos Libbrary with someone? Any person who has been invited to your Shared iCloud Photos Library can favourite the photos, add titles and captions, even delete photos.If you are sharing your library, you have to discuss with the members how the shared photos will be handled, so you do not mess up each others work.