In view of the difficulty of machine abnormal sound detection under the condition that abnormal sound samples are difficult to collect, this paper proposes an unsupervised abnormal sound detection model based on a self-coding model, which effectively improves the accuracy of abnormal sound detection under this condition. In this paper, location coding in Transformer is replaced with relational awareness self-attention to improve the representation capability of location coding. Secondly, the relevance scores in multi-head attention are mixed to enhance the understanding of context in the attention matrix. At the same time, Layer Normalization was replaced with Batch Normalization to speed up model training, and improved Transformer was introduced into the encoders and decoders of self-coding models. Finally, the improved self-coding model is used for unsupervised learning of the machine's normal sound to obtain the potential feature distribution of its normal sound. ToyADMOS and MIMII open data sets are used for experiments. Compared with traditional autoencoders and two improved self-coding models, The AUC score of toycar, Toycar, fan, slider and valve machines increased by 2.1%, 1.97%, 3.06%, 0.34% and 2.99%, respectively.