感谢大佬分享!
本人跑代码中的短文本时,结果和你发的一样,然后我就用模型跑长文本,还是THUCNews数据集,用的数据是:https://github.com/gaussic/text-classification-cnn-rnn中用到的数据,下载链接: https://pan.baidu.com/s/1hugrfRu 密码: qfud
pad_size设置成600或1000都不行,日志如下:
Loading data...
50000it [00:27, 1849.42it/s]
5000it [00:02, 1879.22it/s]
10000it [00:05, 1667.15it/s]
Time usage: 0:00:36
<bound method Module.parameters of Model(
(embedding): Embedding(6206, 300)
(convs): ModuleList(
(0): Conv2d(1, 256, kernel_size=(2, 300), stride=(1, 1))
(1): Conv2d(1, 256, kernel_size=(3, 300), stride=(1, 1))
(2): Conv2d(1, 256, kernel_size=(4, 300), stride=(1, 1))
)
(dropout): Dropout(p=0.5)
(fc): Linear(in_features=768, out_features=10, bias=True)
)>
Epoch [1/20]
Iter: 0, Train Loss: 2.2, Train Acc: 17.97%, Val Loss: 3.5, Val Acc: 10.00%, Time: 0:00:03 *
Iter: 100, Train Loss: 1.4e-06, Train Acc: 100.00%, Val Loss: 2.3e+01, Val Acc: 10.00%, Time: 0:00:41
Iter: 200, Train Loss: 1.2e+01, Train Acc: 0.00%, Val Loss: 2.1e+01, Val Acc: 10.00%, Time: 0:01:19
Iter: 300, Train Loss: 1.3e-06, Train Acc: 100.00%, Val Loss: 3.3e+01, Val Acc: 10.00%, Time: 0:01:58
Epoch [2/20]
Iter: 400, Train Loss: 1.8, Train Acc: 45.31%, Val Loss: 1.2e+01, Val Acc: 14.48%, Time: 0:02:35
Iter: 500, Train Loss: 2.4e-05, Train Acc: 100.00%, Val Loss: 2.2e+01, Val Acc: 10.00%, Time: 0:03:13
Iter: 600, Train Loss: 1.0, Train Acc: 68.75%, Val Loss: 3.2, Val Acc: 15.80%, Time: 0:03:51 *
Iter: 700, Train Loss: 0.06, Train Acc: 98.44%, Val Loss: 8.1, Val Acc: 10.00%, Time: 0:04:29
Epoch [3/20]
Iter: 800, Train Loss: 0.0094, Train Acc: 100.00%, Val Loss: 1.6e+01, Val Acc: 10.00%, Time: 0:05:06
Iter: 900, Train Loss: 2.1e+01, Train Acc: 0.00%, Val Loss: 2.3e+01, Val Acc: 10.00%, Time: 0:05:45
Iter: 1000, Train Loss: 0.25, Train Acc: 99.22%, Val Loss: 3.5, Val Acc: 11.32%, Time: 0:06:23
Iter: 1100, Train Loss: 3.9, Train Acc: 0.00%, Val Loss: 3.9, Val Acc: 13.44%, Time: 0:07:01
Epoch [4/20]
Iter: 1200, Train Loss: 0.0073, Train Acc: 100.00%, Val Loss: 1.1e+01, Val Acc: 11.70%, Time: 0:07:38
Iter: 1300, Train Loss: 0.47, Train Acc: 85.94%, Val Loss: 7.9, Val Acc: 18.34%, Time: 0:08:17
Iter: 1400, Train Loss: 0.063, Train Acc: 100.00%, Val Loss: 4.9, Val Acc: 12.82%, Time: 0:08:55
Iter: 1500, Train Loss: 0.47, Train Acc: 89.84%, Val Loss: 3.1, Val Acc: 23.12%, Time: 0:09:33 *
Epoch [5/20]
Iter: 1600, Train Loss: 0.0037, Train Acc: 100.00%, Val Loss: 7.4, Val Acc: 15.36%, Time: 0:10:10
Iter: 1700, Train Loss: 0.027, Train Acc: 100.00%, Val Loss: 1.3e+01, Val Acc: 10.34%, Time: 0:10:49
Iter: 1800, Train Loss: 3.7, Train Acc: 3.91%, Val Loss: 5.2, Val Acc: 11.98%, Time: 0:11:27
Iter: 1900, Train Loss: 0.1, Train Acc: 98.44%, Val Loss: 4.0, Val Acc: 24.80%, Time: 0:12:05
Epoch [6/20]
Iter: 2000, Train Loss: 0.72, Train Acc: 76.56%, Val Loss: 4.2, Val Acc: 28.34%, Time: 0:12:42
Iter: 2100, Train Loss: 0.034, Train Acc: 99.22%, Val Loss: 8.1, Val Acc: 13.46%, Time: 0:13:20
Iter: 2200, Train Loss: 0.4, Train Acc: 92.97%, Val Loss: 4.2, Val Acc: 33.14%, Time: 0:13:59
Iter: 2300, Train Loss: 0.2, Train Acc: 96.09%, Val Loss: 4.6, Val Acc: 22.86%, Time: 0:14:36
Epoch [7/20]
Iter: 2400, Train Loss: 0.16, Train Acc: 96.88%, Val Loss: 3.7, Val Acc: 35.28%, Time: 0:15:13
Iter: 2500, Train Loss: 0.014, Train Acc: 99.22%, Val Loss: 8.2, Val Acc: 11.10%, Time: 0:15:52
No optimization for a long time, auto-stopping...
/usr/local/lib/python3.5/dist-packages/sklearn/metrics/classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
Test Loss: 3.3, Test Acc: 23.61%
Precision, Recall and F1-Score...
precision recall f1-score support
a 0.8361 0.0510 0.0961 1000
b 1.0000 0.0140 0.0276 1000
c 1.0000 0.0020 0.0040 1000
d 0.0000 0.0000 0.0000 1000
e 0.0000 0.0000 0.0000 1000
f 0.1711 0.7450 0.2783 1000
g 0.8728 0.5490 0.6740 1000
h 0.0791 0.0510 0.0620 1000
i 0.2523 0.9490 0.3986 1000
j 0.0000 0.0000 0.0000 1000
avg / total 0.4211 0.2361 0.1541 10000
Confusion Matrix...
[[ 51 0 0 0 0 325 0 0 91 533]
[ 3 14 0 0 0 627 1 159 196 0]
[ 1 0 2 0 0 182 3 415 397 0]
[ 2 0 0 0 0 458 56 16 468 0]
[ 3 0 0 0 0 866 4 4 123 0]
[ 0 0 0 0 0 745 12 0 243 0]
[ 1 0 0 0 0 55 549 0 395 0]
[ 0 0 0 0 0 891 0 51 58 0]
[ 0 0 0 0 0 50 1 0 949 0]
[ 0 0 0 0 0 155 3 0 842 0]]
Time usage: 0:00:06
模型跑长文本数据时loss波动太大,训练不出来,这是为什么呢?应该如何修改呢?谢谢!