I don't think so - it's not illegal. The whole point of "earnest money" is to give the seller an assurance that a buyer will not back out but for the buyer, earnest money keeps the seller from accepting a higher offer that might be made after their offer has been accepted. I believe the wording is such that a potential buyer can back out for any reason; something could turn up on inspection they don't like or their circumstances could change before closing that makes the purchase untenable.
It's not a particularly nice or honest thing to do, to put in an offer, pay the earnest money and know all along that you intend to back out, but I don't think it's illegal. I would also think that your realtor would not want to work with you knowing that it's your intention as the realtor doesn't want to be associated with that sort of dishonesty.