У меня есть такая функция:
def train(models, X_train, noise_plot, dir_result="D:\gan\result", epochs=10000, batch_size=100):
combined, discriminator, generator = models
nlatent_dim = noise_plot.shape[1]
half_batch = int(batch_size / 2)
history = []
for epoch in range(epochs):
# ---------------------
# Train Discriminator
# ---------------------
# Select a random half batch of images
idx = np.random.randint(0, X_train.shape[0], half_batch)
imgs = X_train[idx]
noise = get_noise(half_batch, nlatent_dim)
# Generate a half batch of new images
gen_imgs = generator.predict(noise)
# Train the discriminator q: better to mix them together?
d_loss_real = discriminator.train_on_batch(imgs, np.ones((half_batch, 1)))
d_loss_fake = discriminator.train_on_batch(gen_imgs, np.zeros((half_batch, 1)))
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# ---------------------
# Train Generator
# ---------------------
noise = get_noise(batch_size, nlatent_dim)
# The generator wants the discriminator to label the generated samples
# as valid (ones)
valid_y = (np.array([1] * batch_size)).reshape(batch_size,1)
# Train the generator
g_loss = combined.train_on_batch(noise, valid_y)
history.append({"D":d_loss[0],"G":g_loss})
if epoch % 100 == 0:
# Plot the progress
print ("Epoch {:05.0f} [D loss: {:4.3f}, acc.: {:05.1f}%] [G loss: {:4.3f}]".format(
epoch, d_loss[0], 100*d_loss[1], g_loss))
if epoch % int(epochs/100) == 0:
plot_generated_images(noise_plot,
path_save=dir_result+"/image_{:05.0f}.png".format(epoch),
titleadd="Epoch {}".format(epoch))
if epoch % 1000 == 0:
plot_generated_images(noise_plot,
titleadd="Epoch {}".format(epoch))
return(history)
И когда я пытаюсь обучить модель:
history = train(models=_models, X_train=X_train, noise_plot=noise, dir_result=dir_result,epochs=10000, batch_size=100)
Все переменные инициализируются. Но у меня есть следующие ошибки:
![enter image description here](https://i.stack.imgur.com/kL2ce.png)
![enter image description here](https://i.stack.imgur.com/nyuJg.png)
FailedPreconditionError: Ошибка при чтении переменной ресурса _AnonymousVar46 из контейнера: localhost. Это могло означать, что переменная не инициализирована. Не найдено: ресурс localhost / _AnonymousVar46 / class tensorflow :: Var не существует. [[узел mul_46 / ReadVariableOp (определено в C: \ Anaconda3 \ envs \ tf \ lib \ site-packages \ keras \ backend \ tensorflow_backend.py: 3009)]] [Op: __ inference_keras_scratch_graph_2593]
Функция стек вызовов: keras_scratch_graph