As a ubiquitous aspect of modern information technology, data compression has a wide range of applications. Therefore, quantum autoencoder which can compress quantum information into a reduced space is fundamentally important to achieve atomatical data compression in the field of quantum information. Such a quantum autoencoder can be implemented through training the parameters of a quantum device using machine learning. In this paper, we experimentally realize a universal two-qubit unitary gate and achieve a quantum autoencoder by applying machine learning. Also, this quantum autoencoder can be used to discriminate two groups of nonorthogonal states.