Classifiying newswires: Reuters, a multiclass classification example

  • 这个例子整理自《Deep Learning with Python》 3.5节
  • 完整代码看这里

0.探索数据-Reuters

  • Reuters数据集,本数据库包含来自路透社的11,228条新闻,分为了46个主题。与IMDB库一样,每条新闻被编码为一个词下标的序列。
  • 我们希望输入新闻的内容,得到该新闻的分类,这是一个多类分类问题
from keras.datasets import reuters
from keras.preprocessing import sequenceimport numpy as np
import matplotlib.pyplot as plt
from collections import Counterfrom keras import layers
from keras import models
from keras import callbacks
from keras import regularizers%matplotlib inline
Using TensorFlow backend.
# 导入数据
max_featuers = 10000
(x_train, y_train),(x_test, y_test) = reuters.load_data(num_words=max_featuers)
print('Training data shape:{}, training labels shape:{}'.format(x_train.shape, y_train.shape))
print('Test data shape:{}, test labels shape:{}'.format(x_test.shape, y_test.shape))
Training data shape:(8982,), training labels shape:(8982,)
Test data shape:(2246,), test labels shape:(2246,)
# 显示部分数据
#print('No.\t\tLength\t\tContent(the first 10 words)\t\t\t\tTarget')
print('{:<6}{:<10}{:<60}{:<10}'.format('No.', 'Length', 'Content(first 10 words)', 'Targets'))
for i, (x,y) in enumerate(zip(x_train[:20], y_train[:20])):#target = 'Positive'if y==1 else 'Negative'print('{:<6}{:<10}{:<60}{:<10}'.format(i, len(x), str(x[:10]), y))
No.   Length    Content(first 10 words)                                     Targets
0     87        [1, 2, 2, 8, 43, 10, 447, 5, 25, 207]                       3
1     56        [1, 3267, 699, 3434, 2295, 56, 2, 7511, 9, 56]              4
2     139       [1, 53, 12, 284, 15, 14, 272, 26, 53, 959]                  3
3     224       [1, 4, 686, 867, 558, 4, 37, 38, 309, 2276]                 4
4     101       [1, 8295, 111, 8, 25, 166, 40, 638, 10, 436]                4
5     116       [1, 4, 37, 38, 309, 213, 349, 1632, 48, 193]                4
6     100       [1, 56, 5539, 925, 149, 8, 16, 23, 931, 3875]               4
7     100       [1, 53, 648, 26, 14, 749, 26, 39, 6207, 5466]               3
8     82        [1, 178, 53, 321, 26, 14, 948, 26, 178, 39]                 3
9     106       [1, 56, 7224, 81, 40, 1175, 174, 2, 6, 1793]                16
10    31        [1, 245, 273, 207, 156, 53, 74, 160, 26, 14]                3
11    59        [1, 56, 141, 5618, 1607, 149, 8, 16, 33, 223]               3
12    65        [1, 2, 81, 8, 16, 625, 42, 120, 7, 1679]                    4
13    316       [1, 248, 409, 166, 1461, 1284, 3906, 8, 4, 495]             4
14    527       [1, 4, 113, 23, 133, 6, 433, 226, 7, 1182]                  19
15    76        [1, 577, 9, 355, 430, 21, 4, 2222, 5, 4]                    8
16    114       [1, 945, 65, 111, 8, 10, 498, 40, 85, 2120]                 16
17    17        [1, 486, 341, 785, 26, 14, 482, 26, 255, 606]               3
18    91        [1, 53, 19, 296, 15, 14, 258, 26, 53, 959]                  3
19    77        [1, 7567, 851, 260, 542, 159, 13, 52, 29, 23]               21
# 对训练标签做统计
n_bins = max(y_train) + 1plt.figure(figsize=(9,6))
plt.subplot(121)
train_num,train_bins, _ = plt.hist(y_train, bins=n_bins, histtype='bar', facecolor='r')
train_pdf = train_num / np.sum(train_num)plt.subplot(122)
test_num, test_bins, _  = plt.hist(y_test, bins=n_bins, histtype='bar', facecolor='g')
test_pdf = test_num / np.sum(test_num)# 输出每种类别所对应的数量
print('{:^20}{:^20}{:^20}'.format('Class', 'Training Count', 'Test Count'))
for i, train_n, train_p, test_n, test_p in zip(range(n_bins), train_num, train_pdf, test_num, test_pdf):print('{:^20}{:>10}({:.2f}){:>10}({:.2f})'.format(int(i), int(train_n),train_p, int(test_n), test_p))
       Class           Training Count        Test Count     0                  55(0.01)        12(0.01)1                 432(0.05)       105(0.05)2                  74(0.01)        20(0.01)3                3159(0.35)       813(0.36)4                1949(0.22)       474(0.21)5                  17(0.00)         5(0.00)6                  48(0.01)        14(0.01)7                  16(0.00)         3(0.00)8                 139(0.02)        38(0.02)9                 101(0.01)        25(0.01)10                124(0.01)        30(0.01)11                390(0.04)        83(0.04)12                 49(0.01)        13(0.01)13                172(0.02)        37(0.02)14                 26(0.00)         2(0.00)15                 20(0.00)         9(0.00)16                444(0.05)        99(0.04)17                 39(0.00)        12(0.01)18                 66(0.01)        20(0.01)19                549(0.06)       133(0.06)20                269(0.03)        70(0.03)21                100(0.01)        27(0.01)22                 15(0.00)         7(0.00)23                 41(0.00)        12(0.01)24                 62(0.01)        19(0.01)25                 92(0.01)        31(0.01)26                 24(0.00)         8(0.00)27                 15(0.00)         4(0.00)28                 48(0.01)        10(0.00)29                 19(0.00)         4(0.00)30                 45(0.01)        12(0.01)31                 39(0.00)        13(0.01)32                 32(0.00)        10(0.00)33                 11(0.00)         5(0.00)34                 50(0.01)         7(0.00)35                 10(0.00)         6(0.00)36                 49(0.01)        11(0.00)37                 19(0.00)         2(0.00)38                 19(0.00)         3(0.00)39                 24(0.00)         5(0.00)40                 36(0.00)        10(0.00)41                 30(0.00)         8(0.00)42                 13(0.00)         3(0.00)43                 21(0.00)         6(0.00)44                 12(0.00)         5(0.00)45                 18(0.00)         1(0.00)

## 将数字转换为单词
word_index = reuters.get_word_index()
num_to_word = { value:key for (key, value) in word_index.items()}
def decode_review(num_to_word, review):# i - 3是因为0, 1, 2代表着'padding','start', 'unknow', 因此单词的下标真正是从3开始的decoded = ' '.join( [num_to_word.get(i-3, '?') for i in review])return decoded
decode_review(num_to_word, x_train[0])
'? ? ? said as a result of its december acquisition of space co it expects earnings per share in 1987 of 1 15 to 1 30 dlrs per share up from 70 cts in 1986 the company said pretax net should rise to nine to 10 mln dlrs from six mln dlrs in 1986 and rental operation revenues to 19 to 22 mln dlrs from 12 5 mln dlrs it said cash flow per share this year should be 2 50 to three dlrs reuter 3'

0.结论

  • 这是一个多类分类问题,每种类别的数量分布很不平衡,有的类别有几千个,但是有的类别只有十几个
  • 每条数据长度不一致,需要被统一长度

1. 定位问题

  • 输入数据:xRn
    , n
    表示长度,xi
    是一个数字,表示一个单词,不同的x
    ,n的值不同
  • 输出:y{0,,45}
    , 每个数字表示一种类别
  • 问题归类:多类别分类问题

2. 衡量指标

  • 样本类型分布不均匀,是imbalanced data,可能用Precision对数据进行衡量比较OK

3. 验证策略

  • hold-out

4. 准备数据

  • 截断太长的数据,填充过短的数据
max_len = 500x_train = sequence.pad_sequences(x_train, maxlen=max_len)
x_test = sequence.pad_sequences(x_test, maxlen=max_len)print('After Padding\nTraining data shape:{}, test data shape:{}'.format(x_train.shape, x_test.shape))
After Padding
Training data shape:(8982, 500), training labels shape:(8982,)

5. 基本模型

  • 首先确定一个基线,在这个问题中,我们利用随机猜测的力量来估计猜中的概率是多少,以这个概率作为基线。结果显示,基线大概为18%
  • 接下来,建立一个模型,能够打败基线即可。这里用上Embedding层。
# 确定基线,即靠猜
rand_results = y_test.copy()
np.random.shuffle(rand_results)hits_array = rand_results == y_test
random_acc = np.sum(hits_array) / len(y_test)
print('Accuracy based on random is :{}'.format(random_acc))
Accuracy based on random is :0.18210151380231523
def build_based_model():model = models.Sequential()model.add(layers.Embedding(max_featuers, 32, input_length=max_len))model.add(layers.Flatten())model.add(layers.Dense(46, activation='softmax'))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])return model
model = build_based_model()callback_list = [callbacks.EarlyStopping(patience=8),callbacks.ModelCheckpoint('best_based_model.h5', save_best_only=True)]model.fit(x_train, y_train, epochs=20, callbacks=callback_list, batch_size=16, validation_split=0.15)
Train on 7634 samples, validate on 1348 samples
Epoch 1/20
7634/7634 [==============================] - 10s 1ms/step - loss: 1.8541 - acc: 0.5312 - val_loss: 1.4829 - val_acc: 0.6521
Epoch 2/20
7634/7634 [==============================] - 3s 414us/step - loss: 0.8690 - acc: 0.7996 - val_loss: 1.2585 - val_acc: 0.7085
Epoch 3/20
7634/7634 [==============================] - 3s 399us/step - loss: 0.3951 - acc: 0.9230 - val_loss: 1.2860 - val_acc: 0.7122
Epoch 4/20
7634/7634 [==============================] - 3s 396us/step - loss: 0.2686 - acc: 0.9518 - val_loss: 1.3520 - val_acc: 0.7099
Epoch 5/20
7634/7634 [==============================] - 6s 784us/step - loss: 0.2179 - acc: 0.9572 - val_loss: 1.4452 - val_acc: 0.7010
Epoch 6/20
7634/7634 [==============================] - 3s 425us/step - loss: 0.2061 - acc: 0.9583 - val_loss: 1.3599 - val_acc: 0.7092
Epoch 7/20
7634/7634 [==============================] - 3s 406us/step - loss: 0.1906 - acc: 0.9580 - val_loss: 1.4439 - val_acc: 0.7062
Epoch 8/20
7634/7634 [==============================] - 3s 395us/step - loss: 0.1834 - acc: 0.9590 - val_loss: 1.3952 - val_acc: 0.7136
Epoch 9/20
7634/7634 [==============================] - 3s 398us/step - loss: 0.1844 - acc: 0.9589 - val_loss: 1.4438 - val_acc: 0.7070
Epoch 10/20
7634/7634 [==============================] - 3s 388us/step - loss: 0.1762 - acc: 0.9577 - val_loss: 1.4320 - val_acc: 0.7129<keras.callbacks.History at 0x7fa84b0c6550>

6.调整参数

def build_model(dropout=0.5):model = models.Sequential()model.add(layers.Embedding(max_featuers, 512, input_length=max_len))model.add(layers.Flatten())model.add(layers.Dense(64, activation='relu'))model.add(layers.Dropout(dropout))model.add(layers.BatchNormalization())model.add(layers.Dense(64, activation='relu'))model.add(layers.Dropout(dropout))model.add(layers.BatchNormalization())model.add(layers.Dense(46, activation='softmax'))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])return model
model = build_model()callback_list = [callbacks.EarlyStopping(patience=8),callbacks.ModelCheckpoint('best_model.h5', save_best_only=True)]model.fit(x_train, y_train, epochs=20, callbacks=callback_list, batch_size=32, validation_split=0.15)
model.evaluate(x_test, y_test)
Train on 7634 samples, validate on 1348 samples
Epoch 1/20
7634/7634 [==============================] - 18s 2ms/step - loss: 3.4759 - sparse_categorical_accuracy: 0.1969 - val_loss: 2.8408 - val_sparse_categorical_accuracy: 0.3984
Epoch 2/20
7634/7634 [==============================] - 17s 2ms/step - loss: 2.2078 - sparse_categorical_accuracy: 0.4700 - val_loss: 1.8516 - val_sparse_categorical_accuracy: 0.5364
Epoch 3/20
7634/7634 [==============================] - 17s 2ms/step - loss: 1.8520 - sparse_categorical_accuracy: 0.5400 - val_loss: 1.6885 - val_sparse_categorical_accuracy: 0.5645
Epoch 4/20
7634/7634 [==============================] - 16s 2ms/step - loss: 1.6485 - sparse_categorical_accuracy: 0.5849 - val_loss: 1.6602 - val_sparse_categorical_accuracy: 0.5809
Epoch 5/20
7634/7634 [==============================] - 18s 2ms/step - loss: 1.5075 - sparse_categorical_accuracy: 0.6180 - val_loss: 1.6275 - val_sparse_categorical_accuracy: 0.6098
Epoch 6/20
7634/7634 [==============================] - 18s 2ms/step - loss: 1.4243 - sparse_categorical_accuracy: 0.6447 - val_loss: 1.5509 - val_sparse_categorical_accuracy: 0.6261
Epoch 7/20
7634/7634 [==============================] - 17s 2ms/step - loss: 1.2934 - sparse_categorical_accuracy: 0.6768 - val_loss: 1.5114 - val_sparse_categorical_accuracy: 0.6424
Epoch 8/20
7634/7634 [==============================] - 17s 2ms/step - loss: 1.1879 - sparse_categorical_accuracy: 0.7025 - val_loss: 1.4534 - val_sparse_categorical_accuracy: 0.6706
Epoch 9/20
7634/7634 [==============================] - 16s 2ms/step - loss: 1.1050 - sparse_categorical_accuracy: 0.7222 - val_loss: 1.4207 - val_sparse_categorical_accuracy: 0.6721
Epoch 10/20
7634/7634 [==============================] - 17s 2ms/step - loss: 1.0093 - sparse_categorical_accuracy: 0.7435 - val_loss: 1.3601 - val_sparse_categorical_accuracy: 0.6840
Epoch 11/20
7634/7634 [==============================] - 16s 2ms/step - loss: 0.9710 - sparse_categorical_accuracy: 0.7562 - val_loss: 1.3266 - val_sparse_categorical_accuracy: 0.6899
Epoch 12/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.9320 - sparse_categorical_accuracy: 0.7671 - val_loss: 1.3298 - val_sparse_categorical_accuracy: 0.6921
Epoch 13/20
7634/7634 [==============================] - 17s 2ms/step - loss: 0.8661 - sparse_categorical_accuracy: 0.7798 - val_loss: 1.3053 - val_sparse_categorical_accuracy: 0.6973
Epoch 14/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.8259 - sparse_categorical_accuracy: 0.7883 - val_loss: 1.3658 - val_sparse_categorical_accuracy: 0.6818
Epoch 15/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.8249 - sparse_categorical_accuracy: 0.7864 - val_loss: 1.3313 - val_sparse_categorical_accuracy: 0.6958
Epoch 16/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.7886 - sparse_categorical_accuracy: 0.7960 - val_loss: 1.3467 - val_sparse_categorical_accuracy: 0.7003
Epoch 17/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.7431 - sparse_categorical_accuracy: 0.8040 - val_loss: 1.3598 - val_sparse_categorical_accuracy: 0.6958
Epoch 18/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.7337 - sparse_categorical_accuracy: 0.8139 - val_loss: 1.3726 - val_sparse_categorical_accuracy: 0.7010
Epoch 19/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.6931 - sparse_categorical_accuracy: 0.8140 - val_loss: 1.3841 - val_sparse_categorical_accuracy: 0.6929
Epoch 20/20
7634/7634 [==============================] - 15s 2ms/step - loss: 0.7050 - sparse_categorical_accuracy: 0.8133 - val_loss: 1.3131 - val_sparse_categorical_accuracy: 0.7055
2246/2246 [==============================] - 2s 765us/step[1.3704577141963046, 0.70124666073018704]

加了Embedding层效果反而不如书上的例子,我们再试试书本上的例子吧


def vectorize_sequences(sequences, dimension=10000):results = np.zeros((len(sequences), dimension))for i, sequence in enumerate(sequences):results[i, sequence] = 1.return results
x_train = vectorize_sequences(x_train)
x_test = vectorize_sequences(x_test)print('After vectorize\nTraining data shape:{}, test data shape:{}'.format(x_train.shape, x_test.shape))
After vectorize
Training data shape:(8982, 10000), test data shape:(2246, 10000)
def build_naive_model():model = models.Sequential()model.add(layers.Dense(64, activation='relu', input_shape=(10000,)))model.add(layers.Dense(64, activation='relu'))model.add(layers.Dense(46, activation='softmax'))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])return model
model = build_naive_model()callback_list = [callbacks.EarlyStopping(patience=8),callbacks.ModelCheckpoint('best_naive_model.h5', save_best_only=True)]model.fit(x_train, y_train, epochs=20, callbacks=callback_list, batch_size=512, validation_split=0.15)
model.evaluate(x_test, y_test)
Train on 7634 samples, validate on 1348 samples
Epoch 1/20
7634/7634 [==============================] - 6s 791us/step - loss: 3.2955 - sparse_categorical_accuracy: 0.4396 - val_loss: 2.5913 - val_sparse_categorical_accuracy: 0.5364
Epoch 2/20
7634/7634 [==============================] - 3s 393us/step - loss: 2.0406 - sparse_categorical_accuracy: 0.5713 - val_loss: 1.7177 - val_sparse_categorical_accuracy: 0.6387
Epoch 3/20
7634/7634 [==============================] - 9s 1ms/step - loss: 1.4246 - sparse_categorical_accuracy: 0.6981 - val_loss: 1.4003 - val_sparse_categorical_accuracy: 0.7047
Epoch 4/20
7634/7634 [==============================] - 7s 945us/step - loss: 1.1209 - sparse_categorical_accuracy: 0.7588 - val_loss: 1.2393 - val_sparse_categorical_accuracy: 0.7300
Epoch 5/20
7634/7634 [==============================] - 7s 963us/step - loss: 0.9021 - sparse_categorical_accuracy: 0.8070 - val_loss: 1.1404 - val_sparse_categorical_accuracy: 0.7493
Epoch 6/20
7634/7634 [==============================] - 13s 2ms/step - loss: 0.7233 - sparse_categorical_accuracy: 0.8463 - val_loss: 1.0657 - val_sparse_categorical_accuracy: 0.7663
Epoch 7/20
7634/7634 [==============================] - 14s 2ms/step - loss: 0.5755 - sparse_categorical_accuracy: 0.8787 - val_loss: 1.0067 - val_sparse_categorical_accuracy: 0.7767
Epoch 8/20
7634/7634 [==============================] - 6s 833us/step - loss: 0.4553 - sparse_categorical_accuracy: 0.9027 - val_loss: 0.9725 - val_sparse_categorical_accuracy: 0.7878
Epoch 9/20
7634/7634 [==============================] - 7s 935us/step - loss: 0.3600 - sparse_categorical_accuracy: 0.9225 - val_loss: 0.9450 - val_sparse_categorical_accuracy: 0.8004
Epoch 10/20
7634/7634 [==============================] - 6s 762us/step - loss: 0.2868 - sparse_categorical_accuracy: 0.9370 - val_loss: 0.9463 - val_sparse_categorical_accuracy: 0.8019
Epoch 11/20
7634/7634 [==============================] - 4s 546us/step - loss: 0.2316 - sparse_categorical_accuracy: 0.9462 - val_loss: 0.9426 - val_sparse_categorical_accuracy: 0.8064
Epoch 12/20
7634/7634 [==============================] - 3s 360us/step - loss: 0.1936 - sparse_categorical_accuracy: 0.9535 - val_loss: 0.9399 - val_sparse_categorical_accuracy: 0.8049
Epoch 13/20
7634/7634 [==============================] - 3s 370us/step - loss: 0.1634 - sparse_categorical_accuracy: 0.9566 - val_loss: 0.9745 - val_sparse_categorical_accuracy: 0.7990
Epoch 14/20
7634/7634 [==============================] - 3s 439us/step - loss: 0.1439 - sparse_categorical_accuracy: 0.9576 - val_loss: 0.9818 - val_sparse_categorical_accuracy: 0.7990
Epoch 15/20
7634/7634 [==============================] - 3s 330us/step - loss: 0.1283 - sparse_categorical_accuracy: 0.9604 - val_loss: 0.9858 - val_sparse_categorical_accuracy: 0.8012
Epoch 16/20
7634/7634 [==============================] - 3s 345us/step - loss: 0.1188 - sparse_categorical_accuracy: 0.9602 - val_loss: 0.9884 - val_sparse_categorical_accuracy: 0.8034
Epoch 17/20
7634/7634 [==============================] - 3s 339us/step - loss: 0.1131 - sparse_categorical_accuracy: 0.9621 - val_loss: 1.0551 - val_sparse_categorical_accuracy: 0.7953
Epoch 18/20
7634/7634 [==============================] - 3s 417us/step - loss: 0.1078 - sparse_categorical_accuracy: 0.9627 - val_loss: 1.0449 - val_sparse_categorical_accuracy: 0.7960
Epoch 19/20
7634/7634 [==============================] - 3s 425us/step - loss: 0.0976 - sparse_categorical_accuracy: 0.9620 - val_loss: 1.0495 - val_sparse_categorical_accuracy: 0.7990
Epoch 20/20
7634/7634 [==============================] - 3s 353us/step - loss: 0.0949 - sparse_categorical_accuracy: 0.9633 - val_loss: 1.0555 - val_sparse_categorical_accuracy: 0.7953
2246/2246 [==============================] - 0s 150us/step[1.0788633611089717, 0.79341050756901155]

尝试对书本的例子进行调参

def build_model():model = models.Sequential()reg = regularizers.l2(0.01)model.add(layers.Dense(64, activation='relu',kernel_initializer='he_normal',input_shape=(10000,)))model.add(layers.Dense(64, activation='relu',kernel_initializer='he_normal'))model.add(layers.Dropout(0.5))model.add(layers.BatchNormalization(center=False, scale=False))model.add(layers.Dense(46, activation='softmax',kernel_initializer='he_normal'))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])return model
model = build_model()callback_list = [callbacks.EarlyStopping(patience=50),callbacks.ModelCheckpoint('best_model.h5', save_best_only=True)]model.fit(x_train, y_train, epochs=200, callbacks=callback_list, batch_size=64, validation_split=0.15)
model.evaluate(x_test, y_test)
Train on 7634 samples, validate on 1348 samples
Epoch 1/200
7634/7634 [==============================] - 8s 1ms/step - loss: 2.3248 - sparse_categorical_accuracy: 0.5317 - val_loss: 2.2482 - val_sparse_categorical_accuracy: 0.7255
Epoch 2/200
7634/7634 [==============================] - 1s 175us/step - loss: 1.3301 - sparse_categorical_accuracy: 0.7419 - val_loss: 1.4470 - val_sparse_categorical_accuracy: 0.7522
Epoch 3/200
7634/7634 [==============================] - 1s 176us/step - loss: 0.9740 - sparse_categorical_accuracy: 0.8027 - val_loss: 1.2019 - val_sparse_categorical_accuracy: 0.7737
Epoch 4/200
7634/7634 [==============================] - 1s 174us/step - loss: 0.7744 - sparse_categorical_accuracy: 0.8385 - val_loss: 1.1152 - val_sparse_categorical_accuracy: 0.7834
Epoch 5/200
7634/7634 [==============================] - 1s 174us/step - loss: 0.6295 - sparse_categorical_accuracy: 0.8638 - val_loss: 1.0941 - val_sparse_categorical_accuracy: 0.7841
Epoch 6/200
7634/7634 [==============================] - 2s 203us/step - loss: 0.5230 - sparse_categorical_accuracy: 0.8864 - val_loss: 1.0889 - val_sparse_categorical_accuracy: 0.7841
Epoch 7/200
7634/7634 [==============================] - 1s 176us/step - loss: 0.4603 - sparse_categorical_accuracy: 0.8999 - val_loss: 1.0619 - val_sparse_categorical_accuracy: 0.7871
Epoch 8/200
7634/7634 [==============================] - 1s 182us/step - loss: 0.4077 - sparse_categorical_accuracy: 0.9099 - val_loss: 1.0504 - val_sparse_categorical_accuracy: 0.7893
Epoch 9/200
7634/7634 [==============================] - 1s 170us/step - loss: 0.3513 - sparse_categorical_accuracy: 0.9190 - val_loss: 1.0665 - val_sparse_categorical_accuracy: 0.7938
Epoch 10/200
7634/7634 [==============================] - 1s 170us/step - loss: 0.3118 - sparse_categorical_accuracy: 0.9289 - val_loss: 1.0712 - val_sparse_categorical_accuracy: 0.7864
Epoch 11/200
7634/7634 [==============================] - 1s 167us/step - loss: 0.2863 - sparse_categorical_accuracy: 0.9312 - val_loss: 1.0567 - val_sparse_categorical_accuracy: 0.7990
Epoch 12/200
7634/7634 [==============================] - 1s 168us/step - loss: 0.2736 - sparse_categorical_accuracy: 0.9403 - val_loss: 1.0731 - val_sparse_categorical_accuracy: 0.8034
Epoch 13/200
7634/7634 [==============================] - 1s 175us/step - loss: 0.2401 - sparse_categorical_accuracy: 0.9437 - val_loss: 1.0843 - val_sparse_categorical_accuracy: 0.7982
Epoch 14/200
7634/7634 [==============================] - 1s 178us/step - loss: 0.2287 - sparse_categorical_accuracy: 0.9450 - val_loss: 1.0765 - val_sparse_categorical_accuracy: 0.7975
Epoch 15/200
7634/7634 [==============================] - 1s 172us/step - loss: 0.2236 - sparse_categorical_accuracy: 0.9454 - val_loss: 1.0768 - val_sparse_categorical_accuracy: 0.7975
Epoch 16/200
7634/7634 [==============================] - 1s 172us/step - loss: 0.2033 - sparse_categorical_accuracy: 0.9502 - val_loss: 1.0835 - val_sparse_categorical_accuracy: 0.8056
Epoch 17/200
7634/7634 [==============================] - 1s 164us/step - loss: 0.1911 - sparse_categorical_accuracy: 0.9496 - val_loss: 1.0652 - val_sparse_categorical_accuracy: 0.7923
Epoch 18/200
7634/7634 [==============================] - 1s 165us/step - loss: 0.1987 - sparse_categorical_accuracy: 0.9492 - val_loss: 1.1018 - val_sparse_categorical_accuracy: 0.7938
Epoch 19/200
7634/7634 [==============================] - 1s 170us/step - loss: 0.1808 - sparse_categorical_accuracy: 0.9518 - val_loss: 1.0997 - val_sparse_categorical_accuracy: 0.7953
Epoch 20/200
7634/7634 [==============================] - 1s 173us/step - loss: 0.1754 - sparse_categorical_accuracy: 0.9538 - val_loss: 1.1229 - val_sparse_categorical_accuracy: 0.8019
Epoch 21/200
7634/7634 [==============================] - 1s 171us/step - loss: 0.1691 - sparse_categorical_accuracy: 0.9557 - val_loss: 1.1319 - val_sparse_categorical_accuracy: 0.7945
Epoch 22/200
7634/7634 [==============================] - 1s 164us/step - loss: 0.1764 - sparse_categorical_accuracy: 0.9531 - val_loss: 1.1406 - val_sparse_categorical_accuracy: 0.8034
Epoch 23/200
7634/7634 [==============================] - 1s 181us/step - loss: 0.1659 - sparse_categorical_accuracy: 0.9534 - val_loss: 1.1298 - val_sparse_categorical_accuracy: 0.8012
Epoch 24/200
7634/7634 [==============================] - 1s 185us/step - loss: 0.1612 - sparse_categorical_accuracy: 0.9536 - val_loss: 1.1073 - val_sparse_categorical_accuracy: 0.7967
Epoch 25/200
7634/7634 [==============================] - 1s 173us/step - loss: 0.1551 - sparse_categorical_accuracy: 0.9553 - val_loss: 1.1580 - val_sparse_categorical_accuracy: 0.8019
Epoch 26/200
7634/7634 [==============================] - 1s 179us/step - loss: 0.1477 - sparse_categorical_accuracy: 0.9566 - val_loss: 1.1572 - val_sparse_categorical_accuracy: 0.8071
Epoch 27/200
7634/7634 [==============================] - 1s 172us/step - loss: 0.1478 - sparse_categorical_accuracy: 0.9552 - val_loss: 1.1986 - val_sparse_categorical_accuracy: 0.8042
Epoch 28/200
7634/7634 [==============================] - 1s 168us/step - loss: 0.1419 - sparse_categorical_accuracy: 0.9583 - val_loss: 1.1755 - val_sparse_categorical_accuracy: 0.8064
Epoch 29/200
7634/7634 [==============================] - 1s 172us/step - loss: 0.1438 - sparse_categorical_accuracy: 0.9591 - val_loss: 1.1607 - val_sparse_categorical_accuracy: 0.7997
Epoch 30/200
7634/7634 [==============================] - 1s 176us/step - loss: 0.1453 - sparse_categorical_accuracy: 0.9576 - val_loss: 1.1859 - val_sparse_categorical_accuracy: 0.8019
Epoch 31/200
7634/7634 [==============================] - 1s 168us/step - loss: 0.1330 - sparse_categorical_accuracy: 0.9591 - val_loss: 1.1863 - val_sparse_categorical_accuracy: 0.8019
Epoch 32/200
7634/7634 [==============================] - 1s 166us/step - loss: 0.1331 - sparse_categorical_accuracy: 0.9570 - val_loss: 1.1786 - val_sparse_categorical_accuracy: 0.8027
Epoch 33/200
7634/7634 [==============================] - 1s 188us/step - loss: 0.1336 - sparse_categorical_accuracy: 0.9589 - val_loss: 1.1931 - val_sparse_categorical_accuracy: 0.7953
Epoch 34/200
7634/7634 [==============================] - 1s 195us/step - loss: 0.1291 - sparse_categorical_accuracy: 0.9603 - val_loss: 1.2248 - val_sparse_categorical_accuracy: 0.8056
Epoch 35/200
7634/7634 [==============================] - 1s 164us/step - loss: 0.1296 - sparse_categorical_accuracy: 0.9593 - val_loss: 1.1877 - val_sparse_categorical_accuracy: 0.8056
Epoch 36/200
7634/7634 [==============================] - 2s 296us/step - loss: 0.1285 - sparse_categorical_accuracy: 0.9597 - val_loss: 1.2065 - val_sparse_categorical_accuracy: 0.8034
Epoch 37/200
7634/7634 [==============================] - 1s 173us/step - loss: 0.1290 - sparse_categorical_accuracy: 0.9590 - val_loss: 1.2306 - val_sparse_categorical_accuracy: 0.8027
Epoch 38/200
7634/7634 [==============================] - 1s 170us/step - loss: 0.1239 - sparse_categorical_accuracy: 0.9597 - val_loss: 1.2083 - val_sparse_categorical_accuracy: 0.8049
Epoch 39/200
7634/7634 [==============================] - 1s 167us/step - loss: 0.1234 - sparse_categorical_accuracy: 0.9606 - val_loss: 1.2333 - val_sparse_categorical_accuracy: 0.7990
Epoch 40/200
7634/7634 [==============================] - 1s 173us/step - loss: 0.1209 - sparse_categorical_accuracy: 0.9616 - val_loss: 1.2485 - val_sparse_categorical_accuracy: 0.8012
Epoch 41/200
7634/7634 [==============================] - 1s 177us/step - loss: 0.1147 - sparse_categorical_accuracy: 0.9631 - val_loss: 1.2593 - val_sparse_categorical_accuracy: 0.8049
Epoch 42/200
7634/7634 [==============================] - 1s 168us/step - loss: 0.1110 - sparse_categorical_accuracy: 0.9624 - val_loss: 1.2866 - val_sparse_categorical_accuracy: 0.8042
Epoch 43/200
7634/7634 [==============================] - 1s 180us/step - loss: 0.1222 - sparse_categorical_accuracy: 0.9587 - val_loss: 1.2487 - val_sparse_categorical_accuracy: 0.8093
Epoch 44/200
7634/7634 [==============================] - 1s 174us/step - loss: 0.1210 - sparse_categorical_accuracy: 0.9586 - val_loss: 1.2545 - val_sparse_categorical_accuracy: 0.8042
Epoch 45/200
7634/7634 [==============================] - 1s 166us/step - loss: 0.1105 - sparse_categorical_accuracy: 0.9632 - val_loss: 1.2840 - val_sparse_categorical_accuracy: 0.8042
Epoch 46/200
7634/7634 [==============================] - 1s 169us/step - loss: 0.1172 - sparse_categorical_accuracy: 0.9598 - val_loss: 1.2637 - val_sparse_categorical_accuracy: 0.8056
Epoch 47/200
7634/7634 [==============================] - 1s 169us/step - loss: 0.1112 - sparse_categorical_accuracy: 0.9631 - val_loss: 1.2755 - val_sparse_categorical_accuracy: 0.8027
Epoch 48/200
7634/7634 [==============================] - 1s 172us/step - loss: 0.1132 - sparse_categorical_accuracy: 0.9611 - val_loss: 1.3092 - val_sparse_categorical_accuracy: 0.7997
Epoch 49/200
7634/7634 [==============================] - 1s 170us/step - loss: 0.1086 - sparse_categorical_accuracy: 0.9615 - val_loss: 1.2921 - val_sparse_categorical_accuracy: 0.8027
Epoch 50/200
7634/7634 [==============================] - 1s 173us/step - loss: 0.1095 - sparse_categorical_accuracy: 0.9608 - val_loss: 1.2780 - val_sparse_categorical_accuracy: 0.8027
Epoch 51/200
7634/7634 [==============================] - 1s 169us/step - loss: 0.1081 - sparse_categorical_accuracy: 0.9636 - val_loss: 1.3012 - val_sparse_categorical_accuracy: 0.8027
Epoch 52/200
7634/7634 [==============================] - 1s 169us/step - loss: 0.1047 - sparse_categorical_accuracy: 0.9629 - val_loss: 1.3114 - val_sparse_categorical_accuracy: 0.7967
Epoch 53/200
7634/7634 [==============================] - 1s 164us/step - loss: 0.1130 - sparse_categorical_accuracy: 0.9599 - val_loss: 1.3684 - val_sparse_categorical_accuracy: 0.8064
Epoch 54/200
7634/7634 [==============================] - 1s 167us/step - loss: 0.1103 - sparse_categorical_accuracy: 0.9640 - val_loss: 1.3155 - val_sparse_categorical_accuracy: 0.8056
Epoch 55/200
7634/7634 [==============================] - 1s 171us/step - loss: 0.1005 - sparse_categorical_accuracy: 0.9635 - val_loss: 1.2855 - val_sparse_categorical_accuracy: 0.8012
Epoch 56/200
7634/7634 [==============================] - 1s 159us/step - loss: 0.1080 - sparse_categorical_accuracy: 0.9624 - val_loss: 1.3084 - val_sparse_categorical_accuracy: 0.8019
Epoch 57/200
7634/7634 [==============================] - 1s 162us/step - loss: 0.1025 - sparse_categorical_accuracy: 0.9641 - val_loss: 1.3183 - val_sparse_categorical_accuracy: 0.8004
Epoch 58/200
7634/7634 [==============================] - 1s 163us/step - loss: 0.1048 - sparse_categorical_accuracy: 0.9637 - val_loss: 1.3418 - val_sparse_categorical_accuracy: 0.8004
2246/2246 [==============================] - 0s 160us/step[1.3954193265121746, 0.79608192347248852]

总结

  • 调参好累,调了半天也没啥效果,看来如果没有处理方式的改变或者新的网络结构,很难有突破
  • 找到一篇文本分类的博客,以后有空看看 http://blog.csdn.net/liuchonge/article/details/77140719?locationNum=6&fps=1

Keras-7 Reuters, a multiclass classification example相关推荐

  1. Micro Average vs Macro average Performance in a Multiclass classification setting

    整理摘自 https://datascience.stackexchange.com/questions/15989/micro-average-vs-macro-average-performanc ...

  2. 吴恩达机器学习练习3:Logistic regression(Multi-class Classification)

    ex3的第一小节是扩充之前的logistic regression算法,并将其应用到多分类器中. 1.数据集 文件ex3data1.mat中包含了5000个手写数字训练样本,每个数字图像含有2020个 ...

  3. Andrew Ng 的 Machine Learning 课程学习 (week4) Multi-class Classification and Neural Networks

    这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛.这门课程对想要了解 ...

  4. 利用Pytorch中深度学习网络进行多分类预测(multi-class classification)

    从下面的例子可以看出,在 Pytorch 中应用深度学习结构非常容易 执行多类分类任务. 在 iris 数据集的训练表现几乎是完美的. import torch.nn as nn import tor ...

  5. 文章解读:Feature selection based on FDA and F-score for multi-class classification

    原文出处 F-score是一种简单的特征选择技术,但它只适用于两个类. 文中提出了一种基于Fisher判别分析(FDA)和F-score的新颖特征排序方法,表示为FDAF评分,考虑了多维特征空间中类的 ...

  6. Keras学习笔记:序列式模型

    目录: 目录: 快速开始序列(Sequential)模型 指定输入数据的shape 编译 训练 例子 用于序列分类的栈式LSTM 采用stateful LSTM的相同模型 本系列参考官方文档官方文档 ...

  7. 从零开始学keras之多分类问题

    本节你会构建一个网络,将路透社新闻划分为 46 个互斥的主题.因为有多个类别,所以这是多分类(multiclass classification)问题的一个例子.因为每个数据点只能划分到一个类别,所以 ...

  8. keras bi-lstm_LSTM用于文本生成的应用介绍-使用Keras和启用GPU的Kaggle Kernels

    keras bi-lstm by Megan Risdal 梅根·里斯达尔(Megan Risdal) LSTM用于文本生成的应用介绍-使用Keras和启用GPU的Kaggle Kernels (An ...

  9. 主流机器学习[xgb, lgb, Keras, LR]

    Preprocess # 通用的预处理框架import pandas as pd import numpy as np import scipy as sp # 文件读取 def read_csv_f ...

最新文章

  1. 如何支持亿级用户分流实验?AB实验平台在爱奇艺的实践
  2. R语言使用ggplot2包使用geom_boxplot函数绘制基础分组箱图(分组箱体框颜色调色板配置)实战
  3. leetcode算法题--字符串相乘
  4. 基于规则的应用程序开发实战(转载与MSDN)
  5. win10诊断启动后联网_小技巧:win10网络共享文件夹出现错误无法访问如何解决?...
  6. spyder中绘图无法显示负号_[转载]Matlab常用函数
  7. MATLAB离散傅里叶变换实验结果分析,Matlab离散傅里叶变换实验报告
  8. 使用“ Row_Number”功能和WHILE循环创建滚动平均值报告
  9. win10专业版开机画面模糊_教你Win10系统电脑显示器显示模糊不清晰如何解决
  10. 计算机基础知识试题和答案
  11. 《遥感基础导论》知识图——第五章 微波遥感数据
  12. 家庭局域网_第二篇如何让电信IPTV与路由器同时上网(组建自己家庭局域网专题)...
  13. 【资源】DNW驱动,Win7 64位可用
  14. 贴片电阻的封装、功率
  15. 电子邮箱是什么,电子邮箱注册值得入手的邮箱都在这!
  16. 在PS里要怎么样才能把一个图片的数字改成另一个数字,还要看不出来?
  17. java 常用英语单词
  18. java丐帮_java多线程学习笔记(二)
  19. qt 3d迷宫游戏_玩游戏找不到路是如何成为常态的?游戏视角选择的得与失
  20. 非常实用,Android引用ttf图标字体库

热门文章

  1. java 资深_Java架构师之路:从Java码农到资深架构师
  2. mysql数据库事务隔离级别是_数据库事务隔离级别-MySQL为例 · Sean
  3. java可以连接php吗_java - 需要PHP或Java代码才能使用多个Internet连接
  4. W10系统matlab无法保存对该路径的更改 pathdef_MATLAB的运行与窗口介绍
  5. html图片倒角,CSS实例:纯CSS打造斜角
  6. python捷联惯导的姿态解算_自动驾驶中高精地图的大规模生产:视觉惯导技术在高德的应用...
  7. python+办公自动化_Python办公自动化之操作excel
  8. python123第6周答案_python123 测验6: 组合数据类型 (第6周)
  9. oracle下的inventory文件夹,oracle INVENTORY 详解
  10. 12星座的出生年月日性格_12星座男人最爱的女人性格