This paper investigates the effect of data integrity attacks on the central control of the microgrids (MGs), which can lead to severe blackouts and load shedding. It assesses this cyber attack from the steady state and optimal scheduling point of view. In order to stop the cyber hacking, a new deep learning-based framework has been developed based on the generative adversarial networks (GANs). In this framework, two networks compete with each other, wherein the first network generates fake data, and the second one is responsible for the data classification. In order to get into the most optimal features, a new optimization method based on a modified teaching-learning based optimization (TLBO) algorithm is also devised to reinforce the GAN model and help a better matching training process. In addition, a new modification is introduced for TLBO to avoid premature convergence and provide high population diversity. To show the effectiveness of the proposed framework, a real dataset of several smart metering devices in a MG has been tested. Results illustrate the high performance of the proposed framework, comparing to the well-known conventional detection frameworks with hit rate of 93.11%, miss rate of 6.89%, false alarm rate of 7.76% and correct reject rate of 92.24%.