层“模型”的输入0与层不兼容:预期shape=(None,250,3),发现shape=(None,3)在训练的变压器模型中

原文标题Input of layer “model” is incompatible with the layer: expected shape=(None, 250, 3), found shape=(None, 3) in trained transformer model

我有一个用tensorflow2.7.0和python3.7训练的kerastransformer模型,输入形状:(None, 250, 3)和一个二维数组输入,形状:(250, 3)(不是图像)

进行预测时:

prediction = model.predict(state)

我明白了ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 250, 3), found shape=(None, 3)

项目代码:https://github.com/MikeSifanele/TT

这是state的样子:

state = np.array([[-0.07714844,-0.06640625,-0.140625],[-0.140625,-0.1650391,-0.2265625]...[0.6376953,0.6005859,0.6083984],[0.7714844,0.7441406,0.7578125]], np.float32)

原文链接:https://stackoverflow.com//questions/71503858/input-0-of-layer-model-is-incompatible-with-the-layer-expected-shape-none-2

回复

我来回复
  • paul-shuvo的头像
    paul-shuvo 评论

    一些解释:

    对于模型的输入形状ie(None, 250, 3),第一个轴(由None表示)是“样本”轴,而其余的ie250,3表示输入维度。因此,当输入形状为(250, 3)时,它假设第一个轴为“ sample”轴,其余作为输入维度,即。just3。因此,为了使其一致,我们需要在开头添加一个维度,如下所述:

    state = np.expand_dims(state, axis=0)
    

    state的形状就变成了(1, 250, 3)~(None, 250, 3)

    2年前 0条评论