我正在尝试创建一个DCGAN,当我想我尝试使用linear()方法时,我遇到了这个错误:
Traceback (most recent call last):
File "spritegen.py", line 71, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "spritegen.py", line 51, in main
cp_directory=FLAGS.checkpoint_dir)
File "/home/lewis/Documents/Sprite Generator/Sprite-Generator/dcgan.py", line 99, in __init__
self.build()
File "/home/lewis/Documents/Sprite Generator/Sprite-Generator/dcgan.py", line 113, in build
self.G = self.generator(self.z)
File "/home/lewis/Documents/Sprite Generator/Sprite-Generator/dcgan.py", line 281, in generator
self.h0 = tf.reshape(self.z,[-1, sample_H16, sample_W16, self.gen_dimension * 8])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 2630, in reshape
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 585, in apply_op
param_name=input_name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 61, in _SatisfiesTypeConstraint
", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64
我认为问题在于以下其中一个方面:
def generator(self, z):
with tf.variable_scope('generator') as scope:
sample_H = self.output_H
sample_W = self.output_W
sample_H2 = conv_out(sample_H,2)
sample_W2 = conv_out(sample_W,2)
sample_H4 = conv_out(sample_H2,2)
sample_W4 = conv_out(sample_W2,2)
sample_H8 = conv_out(sample_H4,2)
sample_W8 = conv_out(sample_W4,2)
sample_H16 = conv_out(sample_H8,2)
sample_W16 = conv_out(sample_W8,2)
# reshape
self.z_ = linear(self.z,self.gen_dimension * 8 * sample_H16 * sample_W16, 'gen_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_,[-1, sample_H16, sample_W16, self.gen_dimension * 8])
h0 = tf.nn.relu(self.gen_batchnorm1(self.h0))
self.h1 = deconv2d(h0, [self.batch_size, sample_H8, sample_W8, self.gen_dimension * 4], name='gen_h1', with_w=True)
h1 = tf.nn.relu(self.gen_batchnorm2(self.h1))
h2 = deconv2d(h1, [self.batch_size, sample_H4, sample_W4, self.gen_dimension * 2], name='gen_h2', with_w= True)
h2 = tf.nn.relu(self.gen_batchnorm3(h2))
h3 = deconv2d(h2, [self.batch_size, sample_H2, sample_W2, self.gen_dimension * 1], name='gen_h3', with_w= True)
h3 = tf.nn.relu(self.gen_batchnorm4(h3))
h4 = deconv2d(h3, [self.batch_size, sample_H, sample_W, 3], name='gen_h4', with_w= True)
return tf.nn.tanh(h4)
这是我尝试重塑张量的生成器方法。在此之前调用构建方法,它设置所有占位符和其他变量:
def build(self):
image_dimension = [self.input_H,self.input_H, 3]
self.inputs = tf.placeholder(tf.float32, shape=[self.batch_size] + image_dimension, name='real_images')
self.gen_inputs = tf.placeholder(tf.float32, shape=[self.sample_size] + image_dimension, name='sample_inputs')
inputs = self.inputs
sample_inputs = self.gen_inputs
self.z = tf.placeholder(tf.float32, shape=[None,self.z_dimension], name='z')
self.z_sum = tf.summary.histogram("z", self.z)
self.G = self.generator(self.z)
self.D = self.discriminator(inputs)
self.sampler = self.sampler(self.z)
self.dis_= self.discriminator(self.G, reuse=True)
最后,这是被调用的linear()方法:
def linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False):
shape = input_.get_shape().as_list()
with tf.variable_scope(scope or "Linear"):
matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32, tf.random_normal_initializer(stddev=stddev))
bias_term = tf.get_variable("bias", [output_size], initializer=tf.constant_initializer(bias_start))
if with_w:
return tf.matmul(input_, matrix) + bias_term, matrix
else:
return tf.matmul(input_, matrix) + bias_term
我尝试了将self.z更改为int32的建议。我收到此错误:
TypeError: Input 'b' of MatMul Op has type float32 that does not match type int32 of argument 'a'
答案 0 :(得分:3)
错误在以下行中出现:
self.h0 = tf.reshape(self.z,[-1, sample_H16, sample_W16, self.gen_dimension * 8])
你可能只需要将可能不是的参数转换为int:
self.h0 = tf.reshape(self.z,[-1, sample_H16, sample_W16, int(self.gen_dimension * 8)])