Keras:层序列 34 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但收到的输入具有形状

时间:2021-02-03 11:52:02

标签: python tensorflow keras

我正在尝试训练一个原型网络,该网络应该用于少量学习。为此,我正在读取我的文件,以便在重塑后获得以下数据形状:Xtrain = (13,4,100,100,3)Xtest = (19,4,100,100,3)。对于 Xtrain,13 个是类别的数量,4 个是每个类别的样本数量(k-shot 学习中的 k),其余的包括图像的维度。

以下代码适用于我的原型网络:

class PrototypicalNetwork(Coreset):
    
    def __init__(self, X, w=None, random_state=None):
        super(PrototypicalNetwork, self).__init__(X, w, random_state)
        
    def conv_net(self):
        convnet = Sequential()
        for i in range(4):
            convnet.add(Conv2D(64,(3,3),padding='same',input_shape=(28, 28, 1)))
            convnet.add(BatchNormalization())
            convnet.add(Activation('relu'))
            convnet.add(MaxPooling2D())
        convnet.add(Flatten())
        return convnet
    
    def get_proto_network(self):
        input_shape = (None,100, 100, 3)
        
        conv = self.conv_net()
        conv_5d = TimeDistributed(conv)
        
        # Input samples
        sample = Input(input_shape)
        sample_feature = conv_5d(sample)
        
        # Input Queries
        query = Input(input_shape)
        query_feature = conv_5d(query)      
        
        pred = Lambda(self.prior_dist)([sample_feature, query_feature])
        combine = Model([sample, query], pred)
        optimizer = 'Adam'#Adam()
        combine.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['categorical_accuracy'])
        return combine

当我开始运行以下代码时:

    ld = LoadData(train_folder, val_folder, save_path, k)
    (y,c,Xtrain), (yval, cval, Xval) = ld.get_image_one_shot()

    #(X_train, y_train), (X_test, y_test) = mnist.load_data()
    #x = np.random.random(100)
    c = PrototypicalNetwork(Xtrain.reshape(52,100,100,3))
    c.get_proto_network()

我得到以下回溯:


  File "C:\Users\AW\Desktop\few shot learning\proto_network.py", line 109, in <module>
    c.get_proto_network()

  File "C:\Users\AW\Desktop\few shot learning\proto_network.py", line 83, in get_proto_network
    sample_feature = conv_5d(sample)

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 957, in __call__
    return self._functional_construction_call(inputs, args, kwargs,

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1096, in _functional_construction_call
    outputs = self._keras_tensor_symbolic_call(

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 828, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 869, in _infer_output_signature
    outputs = call_fn(inputs, *args, **kwargs)

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\layers\wrappers.py", line 276, in call
    y = self.layer(inputs, **kwargs)

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1004, in __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)

  File "C:\Users\AW\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\input_spec.py", line 255, in assert_input_compatibility
    raise ValueError(

ValueError: Input 0 of layer sequential_36 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape (None, 100, 100, 3)

有人知道我的维度有什么问题吗?我该如何解决这个问题?

0 个答案:

没有答案