LoginSignup
0
0

More than 5 years have passed since last update.

scikit-learn、Spark.ml、TensorFlow で線形回帰〜(4)TensorFlow

Last updated at Posted at 2017-05-07

TensorFlow はニューラルネットの構築フレームワークというイメージが強いですが、tf.contrib.learnの下に KMeansClustering や SVMなど、一般的な機械学習ライブラリも入っており、LinearRegression のライブラリも存在します。
Tutorial にも2つ記事があります。

基本的な使い方は以下に書かれているので、これに沿ってやってみましょう。

4.1 学習データの準備

先のページの最後の部分にあるbasic usageのコードからコメントを入れ替えると以下になります。
入力データをファイルから読み込んで入れ替えれば良さそうです。

import tensorflow as tf
import numpy as np

# xは一次元
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]

# モデル作成
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)

# 学習データ
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4,
                                              num_epochs=1000)
# 学習
estimator.fit(input_fn=input_fn, steps=1000)

# 評価
print(estimator.evaluate(input_fn=input_fn))

input_fn で tf.contrib.learn.io.numpy_input_fn を使っていますが、input_fn の作り方が Building Input Functions with tf.contrib.learn に書かれているので、これを参考に作ってみましょう。

tfLR.py
import tensorflow as tf
import numpy as np

# scikit-learn と同じ
import csv
def readArrayWithCSV(dataFile):
    x = []
    y = []
    f = open(dataFile, 'r')
    reader = csv.reader(f)
    for row in reader:
        x.append([float(row[0])])  # リストにする
        y.append(float(row[1]))
    return x,y

# read data                                                                          
dataFile = 'sampleLR.csv'
x0,y0 = readArrayWithCSV(dataFile)

# input_fn
def input_data():
    # カラム名をキーとし、N次元の学習データを収めた tf.constantを値とする辞書を作成
    feature_cols = {'x': tf.constant(x0)}
    # ラベルの値を tf.contant に設定
    labels = tf.constant(y0)
    # feature_cols と labels を返す
    return feature_cols, labels

4.2 モデルの作成

元ソースとほぼ同じです。
結果の a1, a2 ... 及び b は、get_variable_value() で取得できます。

tfLR.py
# define features                                                                    
features = [tf.contrib.layers.real_valued_column('x', dimension=1)]

# build model                                                                        
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)

# training                                                                           
steps = 1000
estimator.fit(input_fn=input_data, steps=steps)

# model parameters
a = estimator.get_variable_value('linear/x/weight')
b = estimator.get_variable_value('linear/bias_weight')

4.3 モデルを用いた予測

モデルの予測には predict_scores() または predict() を使います。リターン値は predict値の generator になるので、list() でリストとして取得しておきます。

tfLR.py
# prediction                                                                         
def predict_data():
    feature_cols = {'x': tf.constant(x0)}
    labels = tf.zeros(len(y0))
    return feature_cols, labels

p0 = list(estimator.predict_scores(input_fn=predict_data))

# output                                                                             
x = np.array(x0)
y = np.array(y0)
p = np.array(p0)

from plotLR import plotLR
title = "predLR_with_tensorflow"
plotLR(title, x, y, p, 0.4, 0.8, a[0][0], b[0], steps)

plotLR は scikit-learn編参照。
steps=1000 だと、scikit-learn、spark.ml と同じ値でしたが、100だとちょっとずれた値になりました。
predLR_with_tensorflow_1000.png
predLR_with_tensorflow_100.png

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