1
votes

I am getting the below error while executing my program ...

def conv2d(x, output_dim, k_size=5, stride=2, stddev=0.02, name="conv2d"):
    #conv = tf.keras.layers.Conv2D(x, output_dim, kernel_size=k_size, 
                                   strides=[stride, stride], padding="SAME", 
                                   kernel_initializer=init(stddev=0.02), name=name)
    conv = tf.compat.v1.layers.Conv2D(x, output_dim, kernel_size=k_size, 
                                      strides=[stride, stride], padding='SAME', 
                                      kernel_initializer=init(stddev=0.02), name=name)

Error

File "/nfs/s-iibi54/users/skuanar/Downloads/VAE-GAN-Autoencoding-Beyond-Pixels-Using-a-Similarity-Metric-master/vaegan.py", line 20, in conv2d conv = tf.compat.v1.layers.Conv2D(x, output_dim, kernel_size=k_size, strides=[stride, stride], padding='SAME', kernel_initializer=init(stddev=0.02), name=name) TypeError: init() got multiple values for argument 'kernel_size'

3

3 Answers

1
votes

You are passing x to the layer's __init__ method. That's not how Keras layers work.

You should pass x by calling a layer that already exists:

def conv2d(x, output_dim, k_size=5, stride=2, stddev=0.02, name="conv2d"):
    #conv = tf.keras.layers.Conv2D(output_dim, kernel_size=k_size, 
                                   strides=[stride, stride], padding="SAME", 
                                   kernel_initializer=init(stddev=0.02), name=name)(x)
    conv_output = tf.compat.v1.layers.Conv2D(output_dim, kernel_size=k_size, 
                                      strides=[stride, stride], padding='SAME', 
                                      kernel_initializer=init(stddev=0.02), name=name)(x)

Assuming x is your input tensor.


This is the same as:

conv_layer = Conv2D(output_dim, kernel_size=k_size, 
                    strides=[stride, stride], padding="SAME", 
                    kernel_initializer=init(stddev=0.02), name=name)
conv_layer_output_tensor = conv_layer(x)
0
votes

As stated in Tensorflow 2.0 Conv2D documentation, the second argument is kernel_size, so your output_dim is conflicting with it. The right way to use Conv2D is to initialize it first and then pass to it its input tensor like this:

def conv2d(x, output_dim, k_size=5, stride=2, stddev=0.02, name="conv2d"):
    conv = tf.compat.v1.layers.Conv2D(output_dim, kernel_size=k_size, strides=[stride, stride], padding='SAME', kernel_initializer=init(stddev=0.02), name=name)
    y = conv(x)

You could also get the output tensor in one line as done in the tutorial The Keras functional API in TensorFlow:

y = tf.compat.v1.layers.Conv2D(output_dim, kernel_size=k_size, strides=[stride, stride], padding='SAME', kernel_initializer=init(stddev=0.02), name=name)(x)
0
votes

As you can see in the keras docs, the Conv2D second argument is kernel_size. You are calling this method with the second argument and the kernel_size named argument as well