We propose a beamforming algorithm based on waveform diversity for hyperthermia treatment of breast cancer using an ultrasonic array. The introduced array has a structure with a network connecting the feeding nodes and the array elements, and the objective of the algorithm is to train the weight matrix of the network to minimize the difference between the generated beam pattern and the ideal one. The training procedure of the algorithm, which is inspired by the idea of machine learning, comprises three parts: forward calculation, comparison, and backward calculation. The forward calculation maps the weight matrix to the beam pattern, and in the comparison step, the generated beam pattern is modified based on the error, and finally, the backward calculation maps the modified beam pattern to a refined weight matrix which performs better than the original one. An optimal weight matrix is obtained by iterative training. The effectiveness of the algorithm is demonstrated by using numerical simulations.