简体   繁体   English

不兼容的形状:[128,1] 与 [128,3,3]

[英]Incompatible shapes: [128,1] vs. [128,3,3]

I am trying to create a CNN to classify the SVHN dataset but run into an incompatible shape error when creating my model: Incompatible shapes: [128,3,3,10] vs. [128,1].我正在尝试创建一个 CNN 来对 SVHN 数据集进行分类,但在创建 model 时遇到了不兼容的形状错误:不兼容的形状:[128,3,3,10] 与 [128,1]。 How do I fix it?我如何解决它?

         model = Sequential([
                          Conv2D(filters=8, kernel_size=(3, 3), 
                           activation='relu', input_shape=(32, 32,3 
                           name='conv_1'),
                            
                           Conv2D(filters=8, kernel_size=(3, 3), 
                          activation='relu', padding= 'SAME',  
                                   `name='conv_2'),
                           MaxPooling2D(pool_size=(8, 8), name='pool_1'),
                            Dense(64,  kernel_regularizer = 
                            regularizers.l2(0.5),bias_initializer='ones',
                            activation='relu' , name='dense_1'),
                            Dropout(0.3),  
                            Dense(64,kernel_regularizer = 
                            regularizers.l2(0.5) , activation='relu' 
                           ,name='dense_2'),
                            BatchNormalization(),  
                            Dense(64,  kernel_regularizer = 
                            regularizers.l2(0.5) ,  activation='relu' 
                             ,name='dense_3'),
                             Dense(10,  activation='softmax' 
                             ,name='dense_4')
             ])


           model.compile(
           optimizer = 'adam',
           loss = 'sparse_categorical_crossentropy',
           metrics= ['accuracy' ])
   

           history = model.fit(train_images,train_labels , epochs = 30 
           ,validation_split = 0.15,
                batch_size= 128, verbose = False )

Put a Flatten layer before the last Dense layer.在最后一个Dense层之前放置一个Flatten层。 Because you are not doing that, that is why the tensor is not reduced to a single dimension tensor before the layer that gives you the class.因为您没有这样做,所以在为您提供 class 的层之前,张量没有减少为单维张量。

It is a general pattern in TensorFlow to use a Flatten before the layer that outputs that class. TensorFlow 中的一般模式是在输出 class 的层之前使用Flatten

Also, I have removed the BatchNormalization in the random dense layer you had put, BatchNormalization layer is put generally after a Conv layer, you can put them after a Dense layer, though.另外,我已经删除了您放置的随机密集层中的BatchNormalizationBatchNormalization层通常放在 Conv 层之后,但是您可以将它们放在Dense层之后。 If you are BatchNormalization, make sure the whole network or the relevant has it.如果你是 BatchNormalization,确保整个网络或相关的都有它。 Don't just put one random BatchNormalization layer.不要只放一个随机BatchNormalization层。

Here's how you change your code to do that.这是您更改代码以执行此操作的方法。

 model = Sequential([Conv2D(filters=8, kernel_size=(3, 3), 
                           activation='relu', input_shape=(32, 32,3), 
                           name='conv_1'),
                     BatchNormalization(),
                     Conv2D(filters=8, kernel_size=(3, 3), 
                          activation='relu', padding= 'SAME',  
                          name='conv_2'),
                     BatchNormalization(),
                     MaxPooling2D(pool_size=(8, 8), name='pool_1'),
                     Flatten(),
                     Dense(64,  kernel_regularizer = 
                           regularizers.l2(0.5), bias_initializer='ones',
                           activation='relu' , name='dense_1'),
                     Dropout(0.3),  
                     Dense(64,kernel_regularizer = 
                           regularizers.l2(0.5) , activation='relu', 
                           name='dense_2'),
                     Dense(64,  kernel_regularizer = 
                           regularizers.l2(0.5) ,  activation='relu', 
                           name='dense_3'),
                     Dense(10,  activation='softmax', 
                           name='dense_4')
             ])


           model.compile(
           optimizer = 'adam',
           loss = 'sparse_categorical_crossentropy',
           metrics= ['accuracy' ])
   

           history = model.fit(train_images,train_labels , epochs = 30)

I think there are two problems in your code.我认为您的代码中有两个问题。

At first, please check the shape of train_labels .首先,请检查train_labels的形状。 Incompatible shapes error would appear if the shapes of tensor are wrong.如果张量的Incompatible shapes的错误。 I think that shape of [128, 1] means that the train_label is not kind of one-hot vector.我认为[128, 1]的形状意味着train_label不是一种单热向量。 If train_label shapes are such as [1, 3, 8, ..], then you should change the shapes into [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], ...]如果 train_label 形状如 [1, 3, 8, ..],则应将形状更改为[[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], ...]

Second, you should add Flatten layer before Dense layer as mentioned above.其次,如上所述,您应该在Dense层之前添加Flatten层。

 model = Sequential([
       Conv2D(filters=8, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3), name='conv_1'),
       ...
       MaxPooling2D(pool_size=(8, 8), name='pool_1'),
       
       Flatten(),

       Dense(64,  kernel_regularizer ...
             ])

The shape of (32, 32, 1) means that the last dim of input shape should be one. (32, 32, 1)的形状意味着输入形状的最后一个暗淡应该是一个。 so you should change the input_shape of Conv2D into (32, 32, 1)所以你应该将 Conv2D 的Conv2D更改为(32, 32, 1)

Conv2D(filters=8, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 1) ...

Also, the train_images should be also changed into (32, 32, 1) because the channel of images is one.此外,train_images 也应更改为(32, 32, 1)因为图像的通道是 1。

train_images = tf.expand_dims(train_images, -1)

Additionally, you can get one-hot vector of train_labels like that:此外,您可以像这样获得train_labels的 one-hot 向量:

train_labels = tf.squeeze(train_labels)
train_labels = tf.one_hot(train_labels, depth=10)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgumentError:不兼容的形状:[32,128] 与 [128,128] [Op:BroadcastTo] - InvalidArgumentError: Incompatible shapes: [32,128] vs. [128,128] [Op:BroadcastTo] TensorFlow:不兼容的形状:[100,155]与[128,155]结合使用CNN和LSTM - TensorFlow: Incompatible shapes: [100,155] vs. [128,155] when combining CNN and LSTM 不兼容的形状:[84,6] 与 [128,6]。 第一个纪元结束时出错 - Incompatible shapes: [84,6] vs. [128,6]. Error at end of first epoch InvalidArgumentError: 矩阵大小不兼容: In[0]: [32,21], In[1]: [128,1] - InvalidArgumentError: Matrix size-incompatible: In[0]: [32,21], In[1]: [128,1] Tensorflow + Keras训练:InvalidArgumentError:不兼容的形状:[7,128,2,2] vs [7,128,3,3] - Tensorflow + Keras training: InvalidArgumentError: Incompatible shapes: [7,128,2,2] vs [7,128,3,3] 尺寸必须相等,但对于输入形状为 [128,1]、[64,128] 的 'sampled_softmax_loss/MatMul'(操作:'MatMul')为 1 和 128 - Dimensions must be equal, but are 1 and 128 for 'sampled_softmax_loss/MatMul' (op: 'MatMul') with input shapes: [128,1], [64,128] CNN ValueError:形状 (10, 4) 和 (10, 128, 128, 4) 不兼容 - CNN ValueError: Shapes (10, 4) and (10, 128, 128, 4) are incompatible InvalidArgumentError:不兼容的形状:[3] 与 [4] - InvalidArgumentError: Incompatible shapes: [3] vs. [4] TF2.0:变压器 model ValueError:形状 (40759, 128) 和 (40765, 128) 不兼容 - TF2.0: Transformer model ValueError: Shapes (40759, 128) and (40765, 128) are incompatible TensorFlow 不兼容的形状:CNN 的 [870] 与 [2] - TensorFlow Incompatible shapes: [870] vs. [2] for CNN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM