深度学習2:IMDBデータセット学習(1)

3542 ワード

映画評論の二分類問題


今回のデータセットはプラスコメントとマイナスコメントに分けられ、いずれも半分を占め、訓練セットとテストセットはそれぞれ25000件あった.

コード実戦分析

import keras
keras.__version__

いつもkerasのバージョンを見ています.
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

ここではimdbデータセットをロードする操作であり、後ろの10000は、学習の妥当性を保証するために、最初の10000語のみを取り出すことを表す.
train_data[0]
train_labels[0]
max([max(sequence) for sequence in train_data])

ここで最初の文とラベル、そしてインデックスの最大値を見てみると、最初の出力は最初の文の単語インデックスで、1行の数字で、2番目は対応するラベルで、1は積極的で、0は消極的で、3番目の文は最大インデックスで、10000単語しかないので、最大インデックスは間違いなく9999です.
word_index = imdb.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])

この3行は、対応するコメント文を解析するために使用されます.1行目はインデックス辞書を取得し、2行目はキー値を逆さまにし、最後の行は復号化します.最初の3つは予め保持されているインデックスなのでスキップします.
decoded_review

この文を見てみると、コメントがわかります."? this film was just brilliant casting location scenery story direction everyone’s really suited the part they played and you could just imagine being there robert ? is an amazing actor and now the same being director ? father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for ? and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also ? to the two little boy’s that played the ? of norman and paul they were just brilliant children are often left out of the ? list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have don’t you think the whole story was so lovely because it was true and was someone’s life after all that was shared with us all「長すぎて正直に言うと人工分解に時間がかかる.
import numpy as np

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(train_data)
x_test = vectorize_sequences(test_data)

y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')

従来、ここではonehot符号化を用い、データをマトリクス化することを目的とし、0から9999まで0と1に数値を簡略化し、ラベルも量子化することを目的としている.
from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

ここでネットワーク構造を構築し始めました.最初の2層はrelu関数であり,非線形の関数であり,より豊富な仮定空間を得ることを目的としており,このような多層構造こそ意味があり,後にsigmoid関数で0から1の値を出力することが最終的な判断である.
from keras import optimizers

model.compile(optimizer=optimizers.RMSprop(lr=0.001),
              loss='binary_crossentropy',
              metrics=['accuracy'])

ここのコンパイルは前の編で述べたが,ここではこれ以上言わない.ここの関数指標もカスタマイズできますが、具体的には後述します.
これで,ネットワーク構築,前期準備が完了し,この例は最初の例と非常に近いが,データ量が大きくなり,評価方法がより最適化された.これらは次号でまた話します.