我正在尝试为我的解码器实现Keras模型。为此,我有以下代码:
def Encoder(nc_in=3, input_size=64):
inp = Input(shape=(input_size, input_size, nc_in))
x = Conv2D(64, kernel_size=5, kernel_initializer=conv_init, use_bias=False, padding="same")(inp)
x = conv_block(x,128)
x = conv_block(x,256)
x = conv_block(x,512)
x = conv_block(x,1024)
//RELEVANT CODE IS HERE
dense_layer = Dense(1024)
x = dense_layer(Flatten()(x))
dense_layer_weights = dense_layer.get_weights()
x = AdaptiveDropout(0.5, dense_layer_weights) (x)
//END OF RELEVANT CODE
x = Dense(4*4*1024)(x)
x = Reshape((4, 4, 1024))(x)
out = upscale_ps(512)(x)
return Model(inputs=inp, outputs=out)
我正在创建一个AdaptiveDropout
图层,该图层需要前一图层的权重才能运行。
权重在AdaptiveDropout
图层中使用如下:
def call(self, inputs, training=None):
beta_matrix = np.ones_like(self.weights)
pi = self.alpha * self.weights + beta_matrix
p = sigmoid(np.tensordot(inputs[1],pi,[[None], [np.maximum(0,pi.ndim - 2)]]))
uniform = np.uniform(p.shape)
mask = uniform < p
if np.mean(p) == 0:
return p * inputs[0]
else:
return inputs[0] * mask
这里的问题是权重和输入都有未知的形状,例如输入的形状是:(?,1024)
。形状仅在运行时已知。因此,此代码失败,索引超出范围。我不确定如何解决这个问题,因为我必须在运行时之前定义模型,以后不能添加AdaptiveDropout
图层。