Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow Type Error: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64

I am trying to create a DCGAN and I'm running into this error when I think I'm trying to use a linear() method:

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

I believe the problem resides in one of these areas:

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)

This is the generator method where I try to reshape a tensor. Before that a build method is called which sets up all the place holders and other variables:

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)

Finally, this is the linear() method being called:

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

I tried the suggestion of changing self.z to int32. I receive this error:

TypeError: Input 'b' of MatMul Op has type float32 that does not match type int32 of argument 'a'
like image 906
Volken Avatar asked Feb 18 '17 18:02

Volken


1 Answers

The error raises in the following line:

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

you probably only have to cast to int the parameters that may not be:

self.h0 = tf.reshape(self.z,[-1, sample_H16, sample_W16, int(self.gen_dimension * 8)])
like image 186
Cae Avatar answered Sep 22 '22 22:09

Cae