Keras模型不能推断Inpu的形状

2024-04-26 12:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的解码器实现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层。你知道吗


Tags: selflayerinputsizereturnnppiblock