在执行程序时出现以下错误...
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”获得了多个值
答案 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)