TypeError:__init __()为参数'kernel_size'获得了多个值

时间:2019-12-13 21:17:51

标签: tensorflow keras keras-layer tensorflow2.0 tf.keras

在执行程序时出现以下错误...

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)

错误

  

文件“ /nfs/s-iibi54/users/skuanar/Downloads/VAE-GAN-Autoencoding-Beyond-Pixels-Using-a-Similarity-Metric-master/vaegan.py”,第20行,在conv2d中   conv = tf.compat.v1.layers.Conv2D(x,output_dim,kernel_size = k_size,步幅= [步幅,步幅],填充='SAME',kernel_initializer = init(stddev = 0.02),name = name)   TypeError:init()为参数“ kernel_size”获得了多个值

3 个答案:

答案 0 :(得分:1)

您正在将x传递给图层的__init__方法。这不是Keras层的工作方式。

您应通过调用已存在的层来传递x

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)

假设x是您的输入张量。


这与:

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)

答案 1 :(得分:0)

正如您在keras docs中看到的那样,Conv2D的第二个参数是kernel_size。您正在使用第二个参数和kernel_size命名参数调用该方法

答案 2 :(得分:0)

Tensorflow 2.0 Conv2D documentation中所述,第二个参数为kernel_size,因此您的output_dim与之冲突。使用Conv2D的正确方法是先对其进行初始化,然后将其输入张量传递给它,如下所示:

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)

您也可以按照教程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)