LoginSignup
0
0

More than 3 years have passed since last update.

学習記録 その27(31日目)

Posted at

学習記録(31日目)

勉強開始:12/7(土)〜

教材等:
・大重美幸『詳細! Python3 入門ノート』(ソーテック社、2017年):12/7(土)〜12/19(木)読了
・Progate Python講座(全5コース):12/19(木)〜12/21(土)終了
・Andreas C. Müller、Sarah Guido『(邦題)Pythonではじめる機械学習』(オライリージャパン、2017年):12/21(土)〜12月23日(土)読了
Kaggle : Real or Not? NLP with Disaster Tweets :12月28日(土)投稿〜1月3日(金)まで調整
・Wes Mckinney『(邦題)Pythonによるデータ分析入門』(オライリージャパン、2018年):1/4(水)〜1/13(月)読了
・斎藤康毅『ゼロから作るDeep Learning』(オライリージャパン、2016年):1/15(水)〜1/20(月)
François Chollet『PythonとKerasによるディープラーニング』(クイープ、2018年):1/21(火)〜

『PythonとKerasによるディープラーニング』

p.186 第5章 コンピュータービジョンのためのディープラーニングまで読み終わり。

ニューラルネットワークによるアヤメの分類

ニューラルネットワークについて、書籍ではkerasデータセットの手書き文字などを使って解説してあったものの、あえてsklearnのアヤメデータセットを使って分類を実施してみました。

各種モジュールのインポート
from keras import models, layers
from keras.utils.np_utils import to_categorical
from sklearn import datasets
from sklearn.model_selection import train_test_split

下準備
#トイデータの読み込み
iris = datasets.load_iris()

#ディレクトリ中身の確認、'data'と'target'についてそれぞれ定義
dir(iris)
x = iris.data
y = iris.target

#データ形状を確認
print([x.shape, y.shape])
print(iris.fearture_names)
print(x[0])

x.shape = (150, 4), y.shape = (150, )

iris.feature_namesでxの要素について確認。

xの4列がそれぞれ ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] に対応していることを確認。

yは0~2で、3種類のアヤメに対応。


正規化
def normalization(x):
    mean = x.mean(axis = 0)
    std = x.std(axis = 0)
    x -= mean
    x /= std
    return x

x = normalization(x)
y = to_categorical(y)

xから平均を引き標準偏差で割ることで、「中心が0、標準偏差が1」になるよう各要素ごと正規化する。
yもto_categoricalで0か1かのベクトル化を実施する。


訓練用とテスト用に分割
x_train, x_test, y_train, y_test = 
train_test_split(x, y, train_size = 0.7, random_state = 3)

print([x_train.shape, x_test.shape, y_train.shape, y_test.shape])

訓練用とテスト用を7:3で分割。
ShuffleはDefaultでTrueなのでrandom_stateのみ設定する。

要素はそれぞれ、[(105, 4), (45, 4), (105, 3), (45, 3)]となっており、ちゃんと分割できたことを確認。


モデル構築:2層
model = models.Sequential()

model.add(layers.Dense(64, activation = 'relu', input_shape = (x.shape[1], )))
model.add(layers.Dense(64, activation = 'relu'))
model.add(layers.Dense(3, activation = 'softmax'))

model.summary()

model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'])

各層の活性化関数はReLU、最後の層の活性化関数はsoftmaxを使用。(p.119 表4-1を参照)

model.summary()で各層について可視化
last image.png

オプティマイザは、ほぼrmspropで足りるとの記載よりこちらを選択した。


学習及び評価
model.fit(x_train, y_train, epochs = 100, batch_size = 1, verbose=0)

result = model.evaluate(x_test, y_test)

print(result)
#[0.15816927485995821, 0.9555555582046509]

epochsはとりあえず100、batch_sizeは当然ながら1。

以上、単純な構造であったものの、結果として96%の精度が得られました。

処置事項1:モデル最終層の出力

value error.png
アヤメは3種類あるので、layers.Dense(3)とすべきところをlayers.Dense(1) にしていたため発生したエラー。
必要とする出力の数に合わせて変える必要がある。(手書き文字なら0〜9の10個)

処置事項2:学習の記録

log.png
特にエラーではないが、記録を必要としない場合は引数verbose=0とすることで対処できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0