我正在尝试在Rstudio Keras中构建用于文本分类的CNN。我是神经网络世界的新手,所以我只是看一些例子,看看我如何从别人的行为中学到东西。我在下面的代码中使用的模型规范是从我发现on the package GitHub.的示例中复制的。我将进一步参考此代码/示例作为“示例”。
当我按“原样”运行示例时(即,使用包作者提供的数据),它运行正常。但是,当我使用自己的数据(提供here in rdata format)时,模型不会编译。它卡在以下错误消息上:
py_call_impl中的错误(callable,dots $ args,dots $ keywords): ValueError:检查目标时出错:期望dense_22有形状(无,10)但是有形状的数组(28686,11)
对我而言,这意味着y_train
(即类别标签)的形状错误。但是,当我将标签的尺寸和数据类型与示例的尺寸和数据类型进行比较时,它们具有相似的格式。两者都是矩阵,其中行代表训练案例,而cols是表示标签的单热矢量。我的嵌入矩阵(embedding_matrix
)和我的单词序列(x_test, x_train
)
有没有人对这里可能出现的问题有任何想法?
library(keras)
library(stringi)
MAX_SEQUENCE_LENGTH <- 1000
MAX_NB_WORDS <- 20000
EMBEDDING_DIM <- 100
#Load my data
load("testdata.rdata")
#MARK start code copied from example
#without any changes
# load pre-trained word embeddings into an Embedding layer
# note that we set trainable = False so as to keep the embeddings fixed
num_words <- min(MAX_NB_WORDS, length(word_index))
embedding_layer <- layer_embedding(
input_dim = num_words,
output_dim = EMBEDDING_DIM,
weights = list(embedding_matrix),
input_length = MAX_SEQUENCE_LENGTH,
trainable = FALSE
)
cat('Training model\n')
# train a 1D convnet with global maxpooling
sequence_input <- layer_input(shape = list(MAX_SEQUENCE_LENGTH), dtype='int32')
preds <- sequence_input %>%
embedding_layer %>%
layer_conv_1d(filters = 128, kernel_size = 5, activation = 'relu') %>%
layer_max_pooling_1d(pool_size = 5) %>%
layer_conv_1d(filters = 128, kernel_size = 5, activation = 'relu') %>%
layer_max_pooling_1d(pool_size = 5) %>%
layer_conv_1d(filters = 128, kernel_size = 5, activation = 'relu') %>%
layer_max_pooling_1d(pool_size = 35) %>%
layer_flatten() %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = length(labels_index), activation = 'softmax')
model <- keras_model(sequence_input, preds)
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = c('acc')
)
model %>% fit(
x_train, y_train,
batch_size = 128,
epochs = 10,
validation_data = list(x_val, y_val)
)
________________________________________________________________________________
Layer (type) Output Shape Param #
================================================================================
input_2 (InputLayer) (None, 1000) 0
________________________________________________________________________________
embedding_2 (Embedding) (None, 1000, 100) 2000000
________________________________________________________________________________
conv1d_4 (Conv1D) (None, 996, 128) 64128
________________________________________________________________________________
max_pooling1d_4 (MaxPooling1D) (None, 199, 128) 0
________________________________________________________________________________
conv1d_5 (Conv1D) (None, 195, 128) 82048
________________________________________________________________________________
max_pooling1d_5 (MaxPooling1D) (None, 39, 128) 0
________________________________________________________________________________
conv1d_6 (Conv1D) (None, 35, 128) 82048
________________________________________________________________________________
max_pooling1d_6 (MaxPooling1D) (None, 1, 128) 0
________________________________________________________________________________
flatten_2 (Flatten) (None, 128) 0
________________________________________________________________________________
dense_3 (Dense) (None, 128) 16512
________________________________________________________________________________
dense_4 (Dense) (None, 20) 2580
================================================================================
Total params: 2,247,316
Trainable params: 247,316
Non-trainable params: 2,000,000
________________________________________________________________________________
答案 0 :(得分:1)
不确定Glove数据维度,但您自己的y_train
确实有11列而不是10列:
load("testdata.rdata")
dim(y_train)
# [1] 28686 11
此外,您的第一列V1
似乎是虚假的,因为它只包含零:
> summary(y_train) # shown only V1-V8
V1 V2 V3 V4 V5 V6 V7 V8
Min. :0 Min. :0.0000 Min. :0.00000 Min. :0.0000 Min. :0.00000 Min. :0.0000 Min. :0.00000 Min. :0.00000
1st Qu.:0 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
Median :0 Median :0.0000 Median :0.00000 Median :0.0000 Median :0.00000 Median :0.0000 Median :0.00000 Median :0.00000
Mean :0 Mean :0.4336 Mean :0.01928 Mean :0.1167 Mean :0.08143 Mean :0.1171 Mean :0.06041 Mean :0.07227
3rd Qu.:0 3rd Qu.:1.0000 3rd Qu.:0.00000 3rd Qu.:0.0000 3rd Qu.:0.00000 3rd Qu.:0.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
Max. :0 Max. :1.0000 Max. :1.00000 Max. :1.0000 Max. :1.00000 Max. :1.0000 Max. :1.00000 Max. :1.00000
因此,如果10确实是目标维度(您的labels_index
确实长度为10),则从y_train
中删除(无用的)第一列应解决错误:
y_train_new <- y_train[,2:11]
dim(y_train_new)
# [1] 28686 10