简体   繁体   English

ValueError:logits 和标签必须具有相同的形状 ((1, 7, 7, 2) vs (1, 2))

[英]ValueError: logits and labels must have the same shape ((1, 7, 7, 2) vs (1, 2))

I'm quite new to CNN.我对CNN很陌生。 I'm trying to create a the following model.我正在尝试创建以下 model。 but I get the following error: "ValueError: logits and labels must have the same shape ((1, 7, 7, 2) vs (1, 2))"但我收到以下错误:“ValueError: logits and labels must have the same shape ((1, 7, 7, 2) vs (1, 2))

Below the code I'm trying to implement在我尝试实现的代码下方

#create the training data set
train_data=scaled_data[0:training_data_len,:]
#define the number of periods
n_periods=28
#split the data into x_train and y_train data set
x_train=[]
y_train=[]

for i in range(n_periods,len(train_data)):
    x_train.append(train_data[i-n_periods:i,:28])
    y_train.append(train_data[i,29])

x_train=np.array(x_train)
y_train=np.array(y_train)

#Reshape the train data
x_train=x_train.reshape(x_train.shape[0],x_train.shape[1],x_train.shape[2],1)
x_train.shape
y_train = keras.utils.to_categorical(y_train,2)

# x_train as the folllowing shape (3561, 28, 28, 1)
# y_train as the following shape (3561, 2, 2)

#Build the 2 D CNN model for regression
model= Sequential()
model.add(Conv2D(32,kernel_size=(3,3),padding='same',activation='relu',input_shape=(x_train.shape[1],x_train.shape[2],1)))
model.add(Conv2D(64,kernel_size=(3,3),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(4,4)))
model.add(Dropout(0.25))
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='sigmoid'))
model.add(Dense(2, activation='sigmoid'))
model.summary()

#compile the model
model.compile(optimizer='ADADELTA', loss='binary_crossentropy', metrics=['accuracy'])

#train the model

model.fit(x_train, y_train, batch_size=1, epochs=1, verbose=2)

There are two problems in your approach:您的方法有两个问题:

  1. You're using Convolutional/MaxPooling layers in which the inputs/outputs are as matrices, ie, with the shape of (Batch_Size, Height, Width, Depth).您正在使用 Convolutional/MaxPooling 层,其中输入/输出为矩阵,即形状为 (Batch_Size, Height, Width, Depth)。 You then add some Dense layers which usually expect vectors, not matrices as inputs.然后,您添加一些通常需要向量而不是矩阵作为输入的密集层。 Therefore, you have to first flatten the outputs of MaxPooling before giving it to Dense layer, ie, add a model.add(Flatten()) after model.add(Dropout(0.25)) and before model.add(Dense(128,activation='relu')) . Therefore, you have to first flatten the outputs of MaxPooling before giving it to Dense layer, ie, add a model.add(Flatten()) after model.add(Dropout(0.25)) and before model.add(Dense(128,activation='relu'))
  2. You are doing binary classification, ie, you have two classes.你正在做二进制分类,即你有两个类。 You are using binary_crossentropy as the loss function, for this to work, you should keep your targets as they are ( 0 and 1 ) and not use y_train = keras.utils.to_categorical(y_train,2) .您使用binary_crossentropy作为损失 function,为此,您应该保持目标原样( 01 ),而不是使用y_train = keras.utils.to_categorical(y_train,2) Your final layer should have 1 neuron and not 2 (Change model.add(Dense(2, activation='sigmoid')) into model.add(Dense(1, activation='sigmoid')) )您的最后一层应该有1神经元而不是2model.add(Dense(2, activation='sigmoid'))更改为model.add(Dense(1, activation='sigmoid'))

暂无
暂无

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

相关问题 ValueError:logits 和标签必须具有相同的形状 ((None, 5) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 5) vs (None, 1)) ValueError:logits 和标签必须具有相同的形状 ((1, 21) vs (21, 1)) - ValueError: logits and labels must have the same shape ((1, 21) vs (21, 1)) ValueError:logits 和标签必须具有相同的形状 ((None, 6, 8, 1) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 6, 8, 1) vs (None, 1)) ValueError:logits 和标签必须具有相同的形状 ((32, 1) vs (32, 2)) - ValueError: logits and labels must have the same shape ((32, 1) vs (32, 2)) ValueError:logits 和标签必须具有相同的形状 ((None, 1) vs (None, 2)) - ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2)) ValueError:logits 和标签必须具有相同的形状 ((None, 2) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1)) ValueError:logits 和标签必须具有相同的形状 ((None, 4) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 4) vs (None, 1)) Tensorflow 估计器 ValueError:logits 和标签必须具有相同的形状 ((?, 1) vs (?,)) - Tensorflow estimator ValueError: logits and labels must have the same shape ((?, 1) vs (?,)) ValueError:`logits` 和 `labels` 必须具有相同的形状 - ValueError: `logits` and `labels` must have the same shape Keras 深度学习 ValueError: logits 和 label must have the same shape ((None, 2) vs (None, 1)) - Keras Deep Learning ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM