gpt4 book ai didi

python - tensorflow 类型错误 : Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64

转载 作者:行者123 更新时间:2023-11-30 22:41:56 27 4
gpt4 key购买 nike

我正在尝试创建一个 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)

这是我尝试 reshape 张量的生成器方法。在此之前,调用构建方法来设置所有占位符和其他变量:

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'

最佳答案

以下行中出现错误:

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)])

关于python - tensorflow 类型错误 : Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com