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
1vae.fit(x_train, x_train, 2 shuffle=True, 3 epochs=epochs, 4 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
1from __future__ import print_function 2 3import numpy as np 4import matplotlib.pyplot as plt 5from scipy.stats import norm 6import tensorflow as tf 7 8from keras.layers import Input, Dense, Lambda, Reshape 9from keras.models import Model 10from keras import backend as K 11from keras import objectives#追加 12from keras import metrics 13from keras.datasets import mnist 14 15# defining the key parameters 16batch_size = 100 17original_dim = 784 18latent_dim = 2 19intermediate_dim = 256 20epochs = 50 21epsilon_std = 1.0 22 23def sampling(args: tuple): 24 # we grab the variables from the tuple 25 z_mean, z_log_var = args 26 epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., 27 stddev=epsilon_std) 28 return z_mean + K.exp(z_log_var / 2) * epsilon 29 30# input to our encoder 31x = Input(shape=(original_dim,), name="input") 32# intermediate layer 33h = Dense(intermediate_dim, activation='relu', name="encoding")(x) 34# defining the mean of the latent space 35z_mean = Dense(latent_dim, name="mean")(h) 36# defining the log variance of the latent space 37z_log_var = Dense(latent_dim, name="log-variance")(h) 38# note that "output_shape" isn't necessary with the TensorFlow backend 39z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var]) 40# defining the encoder as a keras model 41encoder = Model(x, [z_mean, z_log_var, z], name="encoder") 42# print out summary of what we just did 43encoder.summary() 44 45# Input to the decoder 46input_decoder = Input(shape=(latent_dim,), name="decoder_input") 47# taking the latent space to intermediate dimension 48decoder_h = Dense(intermediate_dim, activation='relu', name="decoder_h")(input_decoder) 49# getting the mean from the original dimension 50x_decoded = Dense(original_dim, activation='sigmoid', name="flat_decoded")(decoder_h) 51# defining the decoder as a keras model 52decoder = Model(input_decoder, x_decoded, name="decoder") 53decoder.summary() 54 55# grab the output. Recall, that we need to grab the 3rd element our sampling z 56output_combined = decoder(encoder(x)[2]) 57# link the input and the overall output 58vae = Model(x, output_combined) 59# print out what the overall model looks like 60vae.summary() 61 62Model?? 63 64def vae_loss(x: tf.Tensor, x_decoded_mean: tf.Tensor, 65 z_log_var=z_log_var, z_mean=z_mean, 66 original_dim=original_dim): 67 xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean) 68 kl_loss = - 0.5 * K.sum( 69 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) 70 vae_loss = K.mean(xent_loss + kl_loss) 71 return vae_loss 72 73vae.compile(optimizer='rmsprop', loss=vae_loss) 74vae.summary() 75 76(x_train, y_train), (x_test, y_test) = mnist.load_data() 77 78x_train = x_train.astype('float32') / 255. 79x_test = x_test.astype('float32') / 255. 80x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) 81x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) 82 83vae.fit(x_train, x_train, 84 shuffle=True, 85 epochs=epochs, 86 batch_size=batch_size) 87 88# display a 2D plot of the digit classes in the latent space 89x_test_encoded = encoder.predict(x_test, batch_size=batch_size)[0] 90plt.figure(figsize=(6, 6)) 91plt.scatter(x_test_encoded[:,0], x_test_encoded[:,1], c=y_test, cmap='viridis') 92plt.colorbar() 93plt.show() 94 95# display a 2D manifold of the digits 96n = 15 # figure with 15x15 digits 97digit_size = 28 98figure = np.zeros((digit_size * n, digit_size * n)) 99grid_x = norm.ppf(np.linspace(0.05, 0.95, n)) 100grid_y = norm.ppf(np.linspace(0.05, 0.95, n)) 101 102for i, yi in enumerate(grid_x): 103 for j, xi in enumerate(grid_y): 104 z_sample = np.array([[xi, yi]]) 105 x_decoded = decoder.predict(z_sample) 106 digit = x_decoded[0].reshape(digit_size, digit_size) 107 figure[i * digit_size: (i + 1) * digit_size, 108 j * digit_size: (j + 1) * digit_size] = digit 109 110plt.figure(figsize=(10, 10)) 111plt.imshow(figure, cmap='Greys_r') 112plt.show()
3.補足情報(FW/ツールのバージョンなど)
開発環境:Google Colaboratory
プログラム言語:python3
OS:windows10 Home
CPU:Intel(R) Core(TM) i7-7500U CPU@2.70GHz 2.90GHz
回答2件
あなたの回答
tips
プレビュー