我正在使用Keras在R中进行文本分类。下面是我的可复制代码。
library(keras)
train_labels=c(1700,1300,1500,600,200,300,1000,900,900,700,700,1500,1200,2600,1700,
200,600,1700,100,2900,1400,700,100,1400,700,1500,2500,2500,2500,
200,200,2500,2500,2500,2500,2500,2500,2500,2500,2500,2500,2500,
2500,100,2500,2000,100,1200,1600,900,1600,100,1200,1600,700,2000,1700,500,1400,
1700,2500,1500,2100,1500,800,100,1500,1200,200,600,1200,2700,2300,2000,2000,200,
100,2600,2000,1600,1700,2000,1500,500,1700,800,700,2000,1400,700,2500,2000,1500,
1500,900,2000,700,100,900,100,100,100,1700,100,2000,700,1600,900,900,900,900,900
,900,900,1400,1600,1700,1700,1100,2700,700,100,900,900,2900,2900,1700,700,2500,
2500,2500,2500,1600,900,900,600,900,700,900,900,1700,1700,2300,1600,2700,600,100,
1400,200,900,700,700,100,100,1400,700,2700,2500,2500,100,2500,900,700,1700,300,
1500,100,900,300,1200,100,600,900,1400,1500,100,1500,1400,100,1700,1200,1400,1400,
100,1400,1000,500,100,900,1400,1400,100,700,1300,1400,300,1400,900,600,700,1000,
1400,1400,1400,1400,1400,1400,1400,600,100,2900,1500,1400,1400,100,100,100,100,
1700,700,1400,1400,900,100,100,1400,100,100,1400,1400,1400,900,3300,2500,1500,1400,
600,2900,1400,900,1400,1400,1400,2000,1000,1400,1700,1700,1200,1500,100,600,900,1500,1500,1400,
100,1200,600,1400,1400,
1400,700,100,1500,900,1200,700,600,600,1500,100,900,1500,800,100,2900,100,900,700,2900,1400,
100,400,700,900,900,1200,1200,900,700,100,1600,1500,1500,1400,1500,1100,1100,900,1400,600,100,
700,900,900,900,1200,100,100,1400,3300,1500,100,1400,1400,100,2700,200,100,1300,700,800,2000,
1400,100,1500,1400,2000,900,900,1500,1400,1500,700,1500,1400,100,1400,700,1400,3300,100,1400,
1400,100,2700,1500,1500,1400,900,1500,1400,1400,300,100,100,1700,2900,100,900,100,1800,1000,1400,
1400,1400,1500,600,1400,1700,100,100,100,100,2700,1400,1500,700,700,100,1400,2600,100,100,900,
1000,1400,1000,100,1400,1000,1400,100,1400,100,600,1400,1200,600,1500,900,100,1400,2700,1400,
1000,1400,600,600,600,600,700,600,900,1200,100,900,1400,100,1500,1400,1400,600,100,100,1400,900,
900,100,1700,100,1000,1000,1100,1300,900,100,1500,100,900,1400,1400,1400,100,1500,600,1400,1400,
1400,600,1200,1200,900,1400,1400,100,1400,100,1400,900,1400,100,100,100,1700,2700,100,100,100,
1500,600,1500,100,100,100,1000,600,1200,1500,1400,1400,1400,700,1400,1000,1400,1400,700,1400,
1500,1400,900,700,100,100,1500,100,1400,1400,1400,1400,900,900,1000,1000,1500,1400,700,100,2000,
2700,100,900,600,1500,1700,700,100,1400,700,600,100,1400,1000,600,600,900,2000,1400,600,100,600,
100,100,200,900,1400,600,600,1600,1200,600,700,2500,1400,600,300,100,2700,100,1400,100,600,1200,
2000,1400,100,100,900,700,1500,1700,100,1200,1400,100,1600,1400,1400,1600,1400,1400,1400,600,700,
800,800,700,100,900,600,900,1400,700,100,900,1200,900,1500,1700,1700,1400,600,1600,600,1400,900,
1000,1700,1400,600,2000,2500,2500,1400,100,1700,100,1400,100,100,100,100,100,100,100,1500,2500,
100,100,100,600,2900,1400,100,300,2900,100,1500,2500,2500,900,1200,900,900,1400,100,1500,1700,
100,1400,1400,1600,100,1300,900,2500,900,100,600,100,1400,1700,900,900,700,1400,1400,1400,1400,
1400,700,1700,3000,600,600,900,1400,700,700,600,1700,1400,1400,900,1700,1500,1600,300,700,700,
700,600,700,1700,100,900,2500,700,700,1700,100,1400,1400,1200,600,1200,900,100,600,900,1200,1200,900,1400,900,700,900,700,1700,1000,700,1700,1400,1700,700,2500,1700,1700,300,2500,600,2500,1500,600,600,1600,2700,700,100,1700,700,600,100,1200,2000,2700,900,600,700,900,1700,1400,1400,1200,1200,900,700,600,100,900,300,900,1200,1200,700,1200,1200,900,700,600,100,1700,700,700,100,100,1400,900,2500,2500,1700,700,700,700,700,900,1200,1500,2000,1500,2000,1500,1600,1500,700,2500,1700,2500,900,700,1200,2500,700,700,700,1700,300,1200,600,1500,1600,600,900,700,700,600,1200,1400,900,600,600,1700,1200,600,900,700,1200,1200,1200,700,600,1200,100,100,100,100,100,100,1700,1600,700,600,900,100,600,700,700,700,2500,1200,900,1200,600,900,700,1700,600,900,1000,1200,1400,900,900,100,1500,1600,700,100,2700,1600,700,900,1600,1600,600,700,1700,700,700,1600,600,1400,1200,900,700,900,1200,1000,600,1200,1200,1200,600,900,1200,900,100,100,700,100,100,1500,1500,1500,600,600,100,1400,100,900,900,700,1400,1500,1400,1400,700,1400,700,1200,1700,1500,700,1200,600,1200,1400,1200,700,1500,1500,1700,1500,1500,200,1400,600,1500,2100,700,2100,1600,2500,1400,1500,1500,600,1400,1000,1400,2000,1200,100,1200,1700,700,1200,700,1400,600,700,1200,700,700,1200,1500,1500,700,100,1500,800,800,900,600,2100,1500,1500,1500,700,100,100,1400,1400,1400,1200,600,2100,2500,200,1300,900,1700,600,1700,700,1300,700,2100,700,1600,1400,600,200,800,1400,1400,900,
1400,1500,900,2600,1200,1200,2500,2100,1200,1700,2300,200,1400,1700,600,600,700,600,
2800,600,1500,600,2100,1500,100,900,1400,700,2500,1200,700,200,1400,1400,1400,1400,
600,700,1400,600,1800,900,1400,900,900,700,2100,2800,1600,700,900,2000,1400,1500,
1500,2700,1400,1400,100,2900,100,2800,700,100,700,600,900,1400,900,900,1200,1500,
700,1500,1700,700,1400,2100,2800,1500,1500,1600,1200,900,1200,900,1400,700,900,1700,
1400,900,900,600,1400,2000,200,2000,1500,1400,1200,1600,700,1400,100,900,2600,900,
900,100,1400,600,600,200,1700,1200,1400,600,1500,2300,2100,1500,1700,1400,1500,1400,
2800,1500,1700,2500,100,600,1400,1500,1700,900,700,1500,1500,100,1500,1400,700,100,
1400,2600,1500,1400,200,100,200,1400,2800,700,900,100,1600,900,1200,900,500,500,100,
2000,1700,200,100,1500,1700,1200,1400,1500,1800,2500,600,1400,200,700,200,700,200,
1400,900,1400,100,700,1200,600,1500,1400,600,2300,2300,700,2300,900,1400,700,1500,
700,100,1400,1000,1500,1400,1400,1400,1700,1400,1400,900,700,700,1200,100,900,900,
2300,700,1500,1500,3000,600,600,1400,1400,1200,100,200,1000,1400,2100,900,1300,100,
100,200,900,1700,1700,900,900,700,2500,2800,900,700,1700,1200,600,1500,100,700,1800,1000,1500,
1400,1700,2800,1700,1500,700,1400,200,1600,600,900,1400,600,900,1400,1400,900,1400,1500,2800,
2000,900,700,900,2100,1500,1200,1200,1400,1200,1600,1700,900,900,600,1500,600,3000,2500,900,700,
900,1400,200,1400,700,1200,2600,1500,600,1600,100,600,700,1200,1400,1400,1400,1500,600,600,700,
1500,700,1500,1700,700,1500,2000,700,1700,600,700,100,100,1500,1000,900,1400,700,900,1400,1500,
1700,1700,1400,1500,1700,1400,700,800,1600,2100,1700,700,700,900,700,1400,600,100,2300,900,1700,1400,1400,700,1500,100,100,2000,1500,1400,1200,700,1400,200,2100,600,1500,900,700,700,1500,1700,1400,1700,1400,700,700,1500,2500,100,1500,2100,900,2100,900,2800,900,900,200,1400,1400,600,1700,700,700,900,1600,1200,200,1500,2300,1400,1400,1400,1500,1200,1400,1500,1500,1500,1400,900,1500,1400,700,1500,1400,1400,1400,600,200,1600,1400,2500,2600,1500,700,2800,1400,700,700,1500,1400, 1400,1700,900,1400,1400,1400,700,2800,1400,700,100,700,700,600,600,1700,1400,800,1600,600,1400,300,1200,100,2300,600,1700,900,1200,900,100,1500,900,2700,2700,900,900,1200,1400,700,1400,1400,1400,1200,200,1400,1500,600,2000,1200,1400,1400,700,2100,900,900,200,1200,1400,1400,600,2100,1500, 700,1500,700,1200,1500,2300,1500,1500,800,200,2000,2700,1700,700,1500,600,2300,1500,700,1400,1400,800, 200,1400,900,1400,200,1400,1400,1200,600,1500,700,1500,2300,1400,900,700,1700,700,900,600,1400,1000,3000,1200,1700,1500,1500,900,2100,700,1400,600,1500,1600,700,1600,1400,1400,2800,700,1400,1400,1500,1400,700,1600,600,700,1500,1700,100,1500,600,2000,100,100,900,900,1400,2100,1500,700,2000,1200,2000,1400,600,1400,1100,1400,200,1500,1400,1400,1400,1500,1700,1500,2000,1500,1500,1700,100,1400,1400,200,1400,1400,1400,700,1200,1500,1700,1400,1500,600,100,200,2000,1400,900,1400,1500,600,2500,700,1500,900,700,1200,700,200,1400,1600,1400,1400,1400,100,1600,700,1400,700,1400,200,100,100,700,100,2100,700,700,2300,1200,2000,100,900,1400,1500,1400,1400,800,1500,1700,1500,700,100,700,900,1400,1400,1400,200,1400,200,700,1500,200,800,700,800,1500,700,900,900,600,900,900,1700,600,1400,600,900,900,600,900,200,2300,700,900,1500,1500,700,1500,1500,700,600,1200,1400,1400,1500,600,1400,2300,700,100,1500,1400,800,900,900,700,1700,1200,900,700,1200,100,1200,600,100,100,100,900,800,1500,1200,1500,1200,1200,2300,700,1200,900,1200,100,700,700,1500,1700,600,1700,600,700,700,100,600,1400,1200,100,2100,1500,1400,1500,1400,700,1400,1400,1500,1400,900,1400,1400,1200,1400,1500,900,1400,2700,1400,1400,700,1600,200,1000,1500,700,700,1200,1400,900,1200,1400,1400,900,1400,1400,1500,1500,1200,1200,600,1500,2300,1400,1400,1600,900,1400,1500,1500,1400,100,100,1400,1500,900,1500,2500,1700,1700,700,600,1400,700,1500,2300,1500,600,1400,900,900,2100,600,2300,1400,700,700,2500,700,700,1400,200,1400,1000,1700,900,100,1500,1700,1500,2000,700,800,1500,100,600,900,2000,1500,600,600,1500,600,900,1400,1600,1200,2500,700,700,700,600,1500,1500,1200,1500,900,900,800,100,1300,2300,1500,2300,1500,1300,600,1700,100,1500,2300,1400,700,900,1400,900,700,900,2300,200,1200,900,1500,700,1700,1500,1700,900,700,1400,1200,900,700,2600,1500,1500,600,700,1400,700,2900,1500,900,1200,600,1500,1400,1700,900,700,1700,1400,800,1400,900,1400,1400,1200,700,800,1700,100,1400,1400,200,2300,1400,1700,1500,700,700,2100,700,1500,1400,1500,700,900,900,900,900,700,1200,1400,800,700,1400,1400,1800,600,1500,1600,1400,1400,2300,700,2600,1500,100,1400,700,1200,900,600,100,1700,700,600,2300,2000,600,900)
length(unique(train_labels))
train_labels=to_categorical(train_labels)
dim(train_labels)
以上一种热编码产生一个尺寸为(2000,3310)的矩阵。但是类的唯一数目是28。我尝试在 to_categorical 函数中使用 num_classes = 28 参数,但是出现了
错误IndexError:索引1700超出了尺寸为28的轴1的边界
我的模型的定义
model <- keras_model_sequential() %>%
layer_dense(units = 64, activation = "relu", input_shape = c(20)) %>%
layer_dense(units = 64, activation = "relu") %>%
layer_dense(units = 28, activation = "softmax")
不确定如何执行此操作。
答案 0 :(得分:0)
根据R文档,根据 to_categorical 函数,需要将标签从0转换为27,要求将整数从0转换为类数。