简体   繁体   English

一个形状为 (11203, 25) 的目标数组被传递给形状为 (None, 3) 的输出,同时用作损失`categorical_crossentropy`

[英]A target array with shape (11203, 25) was passed for an output of shape (None, 3) while using as loss `categorical_crossentropy`

I am a beginner in text processing techniques and I am trying to execute the below code.我是文本处理技术的初学者,我正在尝试执行以下代码。

from keras.layers import Dense, Input, GlobalMaxPooling1D
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Model
from keras.layers import Input, Dense, Embedding, Conv2D, MaxPooling2D, Dropout,concatenate
from keras.layers.core import Reshape, Flatten
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.models import Model
from keras import regularizers
sequence_length = trn_abs.shape[1]
filter_sizes = [3,4,5]
num_filters = 100
drop = 0.5



inputs = Input(shape=(sequence_length,))
embedding = embedding_layer(inputs)
reshape = Reshape((sequence_length,embedding_dim,1))(embedding)

conv_0 = Conv2D(num_filters, (filter_sizes[0], embedding_dim),activation='relu',kernel_regularizer=regularizers.l2(0.01))(reshape)
conv_1 = Conv2D(num_filters, (filter_sizes[1], embedding_dim),activation='relu',kernel_regularizer=regularizers.l2(0.01))(reshape)
conv_2 = Conv2D(num_filters, (filter_sizes[2], embedding_dim),activation='relu',kernel_regularizer=regularizers.l2(0.01))(reshape)

maxpool_0 = MaxPooling2D((sequence_length - filter_sizes[0] + 1, 1), strides=(1,1))(conv_0)
maxpool_1 = MaxPooling2D((sequence_length - filter_sizes[1] + 1, 1), strides=(1,1))(conv_1)
maxpool_2 = MaxPooling2D((sequence_length - filter_sizes[2] + 1, 1), strides=(1,1))(conv_2)

merged_tensor = concatenate([maxpool_0, maxpool_1, maxpool_2], axis=1)
flatten = Flatten()(merged_tensor)
reshape = Reshape((3*num_filters,))(flatten)
dropout = Dropout(drop)(flatten)
output = Dense(units=3, activation='softmax',kernel_regularizer=regularizers.l2(0.01))(dropout)

# this creates a model that includes
model = Model(inputs, output)
adam = Adam(lr=1e-3)

model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['acc'])
callbacks = [EarlyStopping(monitor='val_loss')]
model.fit(X_trn, trn[target_cols], epochs=100) 

and I am getting the following error:我收到以下错误:

ValueError: A target array with shape (11203, 25) was passed for output of shape (None, 3) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.

Could anyone help me with this, I am new to stackoverflow too,so please accept my apologies for ill-formating of question.任何人都可以帮我解决这个问题,我也是 stackoverflow 的新手,所以请接受我对问题格式错误的道歉。

It's really important that the number of neurons at the end of your neural network is the number of categories you have.神经网络末端的神经元数量是您拥有的类别数量,这一点非常重要。 So try this:所以试试这个:

output = Dense(units=25, activation='softmax'...

暂无
暂无

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

相关问题 形状为 (32, 3) 的目标数组被传递给形状为 (None, 15, 15, 3) 的 output,同时用作损失`categorical_crossentropy` - A target array with shape (32, 3) was passed for an output of shape (None, 15, 15, 3) while using as loss `categorical_crossentropy` ValueError: 形状为 (72148, 23) 的目标数组被传递给形状为 (None, 826, 23) 的 output,同时用作损失`categorical_crossentropy` - ValueError: A target array with shape (72148, 23) was passed for an output of shape (None, 826, 23) while using as loss `categorical_crossentropy` 一个形状为 (687, 809) 的目标数组被传递给形状 (None, 25) 的输出,同时使用作为损失`binary_crossentropy - A target array with shape (687, 809) was passed for an output of shape (None, 25) while using as loss `binary_crossentropy 形状为 (6400, 1) 的目标数组被传递给形状为 (None, 2) 的 output,同时用作损失`binary_crossentropy` - A target array with shape (6400, 1) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy` 形状为 (64, 4) 的目标数组被传递给形状为 (None, 3) 的 output,同时用作损失`binary_crossentropy` - A target array with shape (64, 4) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy` 将形状为 (15000, 250) 的目标数组传递给形状为 (None, 1) 的输出,同时使用“binary_crossentropy”作为损失。 我该怎么办? - A target array with shape (15000, 250) was passed for an output of shape (None, 1) while using as loss `binary_crossentropy`. What do I do? ValueError:形状为 (160, 299, 299, 3) 的目标数组被传递给形状为 (None, 1) 的 output,同时用作损失 `binary_crossentropy` - ValueError: A target array with shape (160, 299, 299, 3) was passed for an output of shape (None, 1) while using as loss `binary_crossentropy` CNN 值错误输入形状,同时用作损失“categorical_crossentropy”。 此损失期望目标与输出具有相同的形状 - CNN value error input shape while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output `categorical_crossentropy` 损失中的 ValueError function:形状问题 - ValueError in `categorical_crossentropy` loss function: shape issue keras自定义生成器categorical_crossentropy修复输出形状问题 - keras custom generator categorical_crossentropy fix output shape issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM