If you are only in the offer stage and your offer has not been accepted by the seller you only have an offer not a contract. If the seller accepted and you put up earnest money that may be a different issue. Review your offer in this case to see what can be done. If you need help understanding talk to your agent to find out. If after review it looks like you may have an issue you may need legal help from a real estate attorney.
If you submitted a written offer and the seller had not yet accepted the agreement you generally can withdraw your offer up until the time that it has been signed by the seller and delivered back to you or your buyer agent. If the offer had been accepted and acknowledged/delivered back to you there may be a home inspection clause as part of the purchase and the outcome of that process may allow you to walk away from the sale. If your change of mind all occured within the first few days I would normally recommend to the seller to just let you out of the sale because why would a seller try to proceed with a sale when the buyer is considering everything possible to get out of the sale? If it was just a very short time then the seller would have difficulty proving damages and should just let you go and begin marketing again. However, if the sale was accepted and fully in progress then it is a different story if you are trying to get out of the sale.. If seller never signed and accepted the sale you would normallyhave the right to withdraw (this advice is without having actual contract to review).
If this is for a regular home, and if you've elected a contingency period AND are within the timeframe, then yes, you probably have the right to a refund of your deposit. However, Seller must agree to terminate the agreement in writing. If Seller disagrees, the funds may be held in escrow for 365 days while the parties negotiate an agreement. Bottom line - check your contingencies!