Tensorflow类型错误:传递给参数“shape”的值的数据类型为float32,不在允许的值列表中:int32,int64。

5

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

1
也许尝试将 self.z = tf.placeholder 更改为 int32 而不是 float32... - l'L'l
当我尝试这样做时,我收到了以下错误提示:“TypeError: 'MatMul'操作的输入'b'具有float32类型,与参数'a'的int32类型不匹配”。 - Volken
1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - l'L'l
1
似乎需要将conv_out方法更改为int类型,因为我认为它返回了一个浮点数。感谢您的帮助! - Volken
1个回答

5
错误出现在以下代码行:
self.h0 = tf.reshape(self.z,[-1, sample_H16, sample_W16, self.gen_dimension * 8])

你可能只需要将可能不是整数的参数转换为整数:

self.h0 = tf.reshape(self.z,[-1, sample_H16, sample_W16, int(self.gen_dimension * 8)])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接