読者です 読者をやめる 読者になる 読者になる

ゲームAI備忘録

ゲームAIに使えそうな知識を備忘録として書き留める

人助けと思って何卒インストールをば! 詰碁/ アルコネ/ 五目並べ

sklearn風にTensorFlowを使えるライブラリskflowを使ってみる.

TensorFlow AI 機械学習

TensorFlowをsklearn風に使えるライブラリskflowを発見.

GitHub - tensorflow/skflow: Simplified interface for TensorFlow (mimicking Scikit Learn) for Deep Learning

インストール方法

pip install git+git://github.com/tensorflow/skflow.git

サンプル

skflow/examples at master · tensorflow/skflow · GitHub

iris(アヤメ)の線形分類

ロジスティック回帰を用いてアヤメを分類

  • コード
import skflow
from sklearn import datasets, metrics

iris = datasets.load_iris()
classifier = skflow.TensorFlowLinearClassifier(n_classes=len(set(iris.target)))
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print("Accuracy: %f" % score)
  • 実行結果
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
Step #1, avg. loss: 5.27594
Step #21, epoch #4, avg. loss: 1.46109
Step #41, epoch #8, avg. loss: 0.80689
Step #61, epoch #12, avg. loss: 0.61384
Step #81, epoch #16, avg. loss: 0.52919
Step #101, epoch #20, avg. loss: 0.46205
Step #121, epoch #24, avg. loss: 0.63972
Step #141, epoch #28, avg. loss: 0.36715
Step #161, epoch #32, avg. loss: 0.48894
Step #181, epoch #36, avg. loss: 0.32878
Accuracy: 0.886667

real	0m3.050s
user	0m2.123s
sys	0m0.554s
bostonの線形回帰

最小二乗誤差を用いてボストンの物件価格を推定.

  • コード
import skflow
from sklearn import datasets, metrics, preprocessing

boston = datasets.load_boston()
X = preprocessing.StandardScaler().fit_transform(boston.data)
regressor = skflow.TensorFlowLinearRegressor()
regressor.fit(X, boston.target)
score = metrics.mean_squared_error(regressor.predict(X), boston.target)
print ("MSE: %f" % score)
  • 実行結果
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
Step #1, avg. loss: 477.46436
Step #21, epoch #1, avg. loss: 392.31329
Step #41, epoch #2, avg. loss: 150.05814
Step #61, epoch #3, avg. loss: 33.18234
Step #81, epoch #5, avg. loss: 25.03089
Step #101, epoch #6, avg. loss: 22.11081
Step #121, epoch #7, avg. loss: 26.25769
Step #141, epoch #8, avg. loss: 24.56068
Step #161, epoch #10, avg. loss: 21.69363
Step #181, epoch #11, avg. loss: 23.11506
MSE: 22.851618

real	0m3.734s
user	0m2.203s
sys	0m0.632s
iris(アヤメ)の分類

隠れ層が[10,20,10]となるDNNを用いてアヤメの分類

  • コード
import skflow
from sklearn import datasets, metrics

iris = datasets.load_iris()
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=len(set(iris.target)))
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print("Accuracy: %f" % score)
  • 実行結果
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
Step #1, avg. loss: 1.75397
Step #21, epoch #4, avg. loss: 0.88171
Step #41, epoch #8, avg. loss: 0.71925
Step #61, epoch #12, avg. loss: 0.56106
Step #81, epoch #16, avg. loss: 0.49119
Step #101, epoch #20, avg. loss: 0.42057
Step #121, epoch #24, avg. loss: 0.45917
Step #141, epoch #28, avg. loss: 0.36405
Step #161, epoch #32, avg. loss: 0.40900
Step #181, epoch #36, avg. loss: 0.30453
Accuracy: 0.920000

real	0m5.406s
user	0m2.905s
sys	0m1.039s