我在Mnist数据集上运行GAN。 下面的脚本取自一本书。 我得到了上述警告,这也将我的损失转为了nans 输出空白图像。
我的损失转向NANs如下:
Iter: 52000
D loss: nan
G_loss: nan
Iter: 53000
D loss: nan
G_loss: nan
Iter: 54000
D loss: nan
G_loss: nan
首先,脚本运行正常,但经过一定的迭代后,我收到警告和“输出”。
我收到以下用户警告:
Warning (from warnings module):
File "C:\Users\Moondra\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\colors.py", line 821
dtype = np.min_scalar_type(value)
UserWarning: Warning: converting a masked element to nan.
Warning (from warnings module):
File "C:\Users\Moondra\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\ma\core.py", line 2809
order=order, subok=True, ndmin=ndmin)
UserWarning: Warning: converting a masked element to nan.
我已经看过这些问题了:
Warning: converting a masked element to nan
UserWarning: converting a masked element to nan
我在运行运行GAN的脚本时收到此警告。 该剧本直接来自一本书。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os
from tensorflow.examples.tutorials.mnist import input_data
tf.set_random_seed = 2017
def xavier_init(size):
input_dim = size[0]
xavier_variance = 1. / tf.sqrt(input_dim/2.)
return tf.random_normal(shape=size, stddev=xavier_variance)
def plot(samples):
fig = plt.figure(figsize=(4, 4))
gs = gridspec.GridSpec(4, 4)
gs.update(wspace=0.05, hspace=0.05)
for i, sample in enumerate(samples):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
plt.imshow(sample.reshape(28, 28), cmap='Greys_r')
return fig
# Random noise setting for Generator
Z = tf.placeholder(tf.float32, shape=[None, 100], name='Z')
#Generator parameter settings
G_W1 = tf.Variable(xavier_init([100, 128]), name='G_W1')
G_b1 = tf.Variable(tf.zeros(shape=[128]), name='G_b1')
G_W2 = tf.Variable(xavier_init([128, 784]), name='G_W2')
G_b2 = tf.Variable(tf.zeros(shape=[784]), name='G_b2')
theta_G = [G_W1, G_W2, G_b1, G_b2]
#Input Image MNIST setting for Discriminator [28x28=784]
X = tf.placeholder(tf.float32, shape=[None, 784], name='X')
#Discriminator parameter settings
D_W1 = tf.Variable(xavier_init([784, 128]), name='D_W1')
D_b1 = tf.Variable(tf.zeros(shape=[128]), name='D_b1')
D_W2 = tf.Variable(xavier_init([128, 1]), name='D_W2')
D_b2 = tf.Variable(tf.zeros(shape=[1]), name='D_b2')
theta_D = [D_W1, D_W2, D_b1, D_b2]
# Generator Network
def generator(z):
G_h1 = tf.nn.relu(tf.matmul(z, G_W1) + G_b1)
G_log_prob = tf.matmul(G_h1, G_W2) + G_b2
G_prob = tf.nn.sigmoid(G_log_prob)
return G_prob
# Discriminator Network
def discriminator(x):
D_h1 = tf.nn.relu(tf.matmul(x, D_W1) + D_b1)
D_logit = tf.matmul(D_h1, D_W2) + D_b2
D_prob = tf.nn.sigmoid(D_logit)
return D_prob, D_logit
G_sample = generator(Z)
D_real, D_logit_real = discriminator(X)
D_fake, D_logit_fake = discriminator(G_sample)
# Loss functions from the paper
D_loss = -tf.reduce_mean(tf.log(D_real) + tf.log(1. - D_fake))
G_loss = -tf.reduce_mean(tf.log(D_fake))
# Update D(X)'s parameters
D_solver = tf.train.AdamOptimizer().minimize(D_loss, var_list=theta_D)
# Update G(Z)'s parameters
G_solver = tf.train.AdamOptimizer().minimize(G_loss, var_list=theta_G)
def sample_Z(m, n):
return np.random.uniform(-1., 1., size=[m, n])
batch_size = 128
Z_dim = 100
sess = tf.Session()
sess.run(tf.global_variables_initializer())
mnist = input_data.read_data_sets('MNIST/', one_hot=True)
if not os.path.exists('output/'):
os.makedirs('output/')
i = 0
for itr in range(1000000):
if itr % 1000 == 0:
samples = sess.run(G_sample, feed_dict={Z: sample_Z(16, Z_dim)})
fig = plot(samples)
plt.savefig('output/{}.png'.format(str(i).zfill(3)), bbox_inches='tight')
i += 1
plt.close(fig)
X_mb, _ = mnist.train.next_batch(batch_size)
_, D_loss_curr = sess.run([D_solver, D_loss], feed_dict={X: X_mb, Z: sample_Z(batch_size, Z_dim)})
_, G_loss_curr = sess.run([G_solver, G_loss], feed_dict={Z: sample_Z(batch_size, Z_dim)})
if itr % 1000 == 0:
print('Iter: {}'.format(itr))
print('D loss: {:.4}'. format(D_loss_curr))
print('G_loss: {:.4}'.format(G_loss_curr))
print()
每1000次迭代,新生成的图像都会被保存并且看起来很好,但是一旦出现此错误,新生成的图像将全部变为白色。
答案 0 :(得分:0)
主要原因是CPU计算出的问题。 参考文档给出了解决方案的方向:
https://www.zhihu.com/question/62441748
调试用TensorFlow和Keras编写的机器学习模型 https://towardsdatascience.com/debugging-a-machine-learning-model-written-in-tensorflow-and-keras-f514008ce736
Put
xavier_variance = 1. / tf.sqrt(input_dim/2.)
Change to
xavier_variance = 1. / tf.sqrt(input_dim/2.+1e-6)
Also modify
D_loss = -tf.reduce_mean(tf.log(D_real) + tf.log(1. - D_fake))
G_loss = -tf.reduce_mean(tf.log(D_fake))
to
D_loss = -tf.reduce_mean(tf.log(D_real+1e-6) + tf.log(1. - D_fake+1e-6))
G_loss = -tf.reduce_mean(tf.log(D_fake+1e-6))