1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kerasによるirisの分類

Posted at

※以下、個人的な勉強のためのレポートです。
※間違い多々あると存じますが、現在の理解レベルのスナップショットのようなものです。
※勉強のためWebサイトや書籍からとても参考になったものを引用させていただいております。
http://ai999.careers/rabbit/

iris

http://home.a00.itscom.net/hatada/ml/data/iris01.html
セトナ(setosa[ラベル:0])、バーシクル(versicolor[ラベル:1)、 バージニカ(virginica[ラベル:2])という3種類のあやめの 4個の計測値:がく片長(Sepal Length),がく片幅(Sepal Width),花びら長(Petal Length),花びら幅(Petal Width)と種(Species)からなる。 判別分析,クラスター分析などの研究にもテストデータとして良く用いられる。
がく片/がく片幅/花びら/花びら幅/あやめの種類

実装

# from sklearn.cross_validation import train_test_splitは最新版では使えない
from sklearn.model_selection import  train_test_split
# 学習に使うデータと、性能検証用のデータの分離
# len(x_train),len(x_test)を実行すると、(120, 30)と0.2対0.8になっている
x_train, x_test, d_train, d_test = train_test_split(x, d, test_size=0.2)

# モデルの設定
model = Sequential()
# 入力4、中間層は12ノードで活性化関数はRelu、出力層は3ノードで出力用活性化関数はsoftmax
model.add(Dense(12, input_dim=4))
model.add(Activation('relu'))
# model.add(Activation('sigmoid'))
model.add(Dense(3, input_dim=12))
model.add(Activation('softmax'))
model.summary()

model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

目的関数sparse_categorical_crossentropy
https://keras.io/ja/objectives/
one-hot形なら、categorical_crossentropyを使う。今回は[0,1,2]なので、sparse_categorical_crossentropy
※sparse:まばらな

metrics=['accuracy']
学習の途中で精度を確認できる

結果

image.png
120/120 [==============================] - 0s 217us/step - loss: 0.2735 - acc: 0.9667 - val_loss: 0.2776 - val_acc: 0.9667
Relu関数からsigmoid関数へ変更

model.add(Activation('sigmoid'))

image.png
120/120 [==============================] - 0s 225us/step - loss: 0.8497 - acc: 0.8417 - val_loss: 0.8642 - val_acc: 0.6667
SDGのデフォルトの学習率は0.01、これを0.1に変更

from keras.optimizers import SGD

model.compile(optimizer=SGD(lr=0.1), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

image.png
120/120 [==============================] - 0s 217us/step - loss: 0.3040 - acc: 0.8917 - val_loss: 0.3655 - val_acc: 0.8000

Kerasでは、活性化関数やハイパーパラメータの変更が容易に実装、実行できる。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?