1.質問内容
実践GAN 敵対性生成ネットワークによる深層学習(マイナビ)で、VAEの実装を勉強中です。
参考コードも
実践GAN 敵対性生成ネットワークによる深層学習の公開github中コードです。
githubは、以下です。
https://github.com/GANs-in-Action/gans-in-action
Chapter-2の「Chapter_2_Autoencoder.ipynb」が参考コードです。
参考コードは、vAE実装例です。google colab上で作動させると、以下の
エラーが、下記の場所で出ます。
python
vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size)
エラー文は、以下のもの
FailedPreconditionError: Could not find variable training_2/RMSprop/rho. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/training_2/RMSprop/rho/N10tensorflow3VarE does not exist.
対処方法をお願いいたします。
2.参考コード
python
from __future__ import print_function import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm import tensorflow as tf from keras.layers import Input, Dense, Lambda, Reshape from keras.models import Model from keras import backend as K from keras import objectives#追加 from keras import metrics from keras.datasets import mnist # defining the key parameters batch_size = 100 original_dim = 784 latent_dim = 2 intermediate_dim = 256 epochs = 50 epsilon_std = 1.0 def sampling(args: tuple): # we grab the variables from the tuple z_mean, z_log_var = args epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., stddev=epsilon_std) return z_mean + K.exp(z_log_var / 2) * epsilon # input to our encoder x = Input(shape=(original_dim,), name="input") # intermediate layer h = Dense(intermediate_dim, activation='relu', name="encoding")(x) # defining the mean of the latent space z_mean = Dense(latent_dim, name="mean")(h) # defining the log variance of the latent space z_log_var = Dense(latent_dim, name="log-variance")(h) # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var]) # defining the encoder as a keras model encoder = Model(x, [z_mean, z_log_var, z], name="encoder") # print out summary of what we just did encoder.summary() # Input to the decoder input_decoder = Input(shape=(latent_dim,), name="decoder_input") # taking the latent space to intermediate dimension decoder_h = Dense(intermediate_dim, activation='relu', name="decoder_h")(input_decoder) # getting the mean from the original dimension x_decoded = Dense(original_dim, activation='sigmoid', name="flat_decoded")(decoder_h) # defining the decoder as a keras model decoder = Model(input_decoder, x_decoded, name="decoder") decoder.summary() # grab the output. Recall, that we need to grab the 3rd element our sampling z output_combined = decoder(encoder(x)[2]) # link the input and the overall output vae = Model(x, output_combined) # print out what the overall model looks like vae.summary() Model?? def vae_loss(x: tf.Tensor, x_decoded_mean: tf.Tensor, z_log_var=z_log_var, z_mean=z_mean, original_dim=original_dim): xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean) kl_loss = - 0.5 * K.sum( 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) vae_loss = K.mean(xent_loss + kl_loss) return vae_loss vae.compile(optimizer='rmsprop', loss=vae_loss) vae.summary() (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size) # display a 2D plot of the digit classes in the latent space x_test_encoded = encoder.predict(x_test, batch_size=batch_size)[0] plt.figure(figsize=(6, 6)) plt.scatter(x_test_encoded[:,0], x_test_encoded[:,1], c=y_test, cmap='viridis') plt.colorbar() plt.show() # display a 2D manifold of the digits n = 15 # figure with 15x15 digits digit_size = 28 figure = np.zeros((digit_size * n, digit_size * n)) grid_x = norm.ppf(np.linspace(0.05, 0.95, n)) grid_y = norm.ppf(np.linspace(0.05, 0.95, n)) for i, yi in enumerate(grid_x): for j, xi in enumerate(grid_y): z_sample = np.array([[xi, yi]]) x_decoded = decoder.predict(z_sample) digit = x_decoded[0].reshape(digit_size, digit_size) figure[i * digit_size: (i + 1) * digit_size, j * digit_size: (j + 1) * digit_size] = digit plt.figure(figsize=(10, 10)) plt.imshow(figure, cmap='Greys_r') plt.show()
3.補足情報(FW/ツールのバージョンなど)
開発環境:Google Colaboratory
プログラム言語:python3
OS:windows10 Home
CPU:Intel(R) Core(TM) i7-7500U CPU@2.70GHz 2.90GHz
まだ回答がついていません
会員登録して回答してみよう