How to fix this? AttributeError: ‘NoneType’ object has no attribute ‘update_state’

I have problem with this: AttributeError: ‘NoneType’ object has no attribute ‘update_state’

My code:

latent_dim = 100
batch_size = 32
my_discriminator = keras.models.Sequential([ keras.Input(shape=(784,)), keras.layers.Dense(128, activation=“relu”),keras.layers.Dense(64, activation=“relu”),keras.layers.Dense(1, activation=“sigmoid”)])
my_discriminator.compile(optimizer=‘adam’, loss=‘binary_crossentropy’, metrics=[‘accuracy’])
my_generator = keras.models.Sequential([keras.Input(shape=(latent_dim,)),keras.layers.Dense(128,activation=“relu”),keras.layers.Dense(64,activation=“relu”),keras.layers.Dense(784,activation=“tanh”)])

(x_train,*),(*,_) = keras.datasets.mnist.load_data()
x_train = (x_train.astype(“float32”) / 127.5) - 1
x_train = x_train.reshape(x_train.shape[0],-1)

gan_input = keras.Input(shape=(latent_dim,))
gan_output = my_discriminator(my_generator(gan_input))
my_gan = keras.Model(inputs=gan_input,outputs=gan_output)
my_gan.compile(optimizer=‘adam’,loss=‘binary_crossentropy’)

for i in range(100):
real_img = x_train[numpy.random.choice(x_train.shape[0], batch_size, replace=False)]
noise = numpy.random.normal(0,1,(batch_size,latent_dim))
fake_img = my_generator(noise)
real_labels = numpy.ones((batch_size,1), dtype=“float32”)
fake_labels = numpy.zeros((batch_size,1), dtype=“float32”)
my_discriminator.trainable = True
d_real = my_discriminator.train_on_batch(real_img, real_labels)  # Here is the problem 
d_fake = my_discriminator.train_on_batch(fake_img, fake_labels)

my_discriminator.trainable = False
noise = numpy.random.normal(0,1,(batch_size,latent_dim))
misleading_labels = numpy.ones((batch_size, 1))
g_loss = my_gan.train_on_batch(noise, misleading_labels)

Hi @NYLY, I have tried to execute the above given code in colab and did not face any error. Please refer to this gist for working code example. Thank You.