Hey @Xiaojian_Deng,
Welcome to the community. The issue lies with the position of calling the detach method, as you can easily see from your code.
I am assuming that you are well familiar with the concept of detach method, but if you are not, then let me give you a 1-line summary. We call this method when we don’t want to update the associated weights with an object. In this case, the object is the fake images, and the weights are of the generator.
Now if you carefully look at the code, you will find that we are using these fake/generated images, for updating the discriminator as well as the generator. Now, though we want to call detach on the fake images when we are updating the discriminator (since we don’t want to update the generator in this case), we don’t want the same thing to happen when we are updating the generator. And hence, when you changed the position of detach method, the generator didn’t update at all, and hence, led to empty squares.
Hope this helps