LoginSignup
0
2

More than 3 years have passed since last update.

線形回帰(Linear Regression)

Last updated at Posted at 2019-07-06

はじめに

これは筆者の勉強まとめページですので、指摘しまくってい頂けると幸いです

BackPropagationを用いた線形回帰

今回はBack Propagationを用いた線形回帰をまとめます

Back Propagationとは、確立的勾配降下法の原理を用いた、パラメータ学習アルゴリズムです
もっとざっくり言うと、関数の極小値求めるものです(また今度まとめよっと)

$$ y = x・w + b $$

今回はwとbを学習してデータにフィットしたパラメータを得る操作をします

損失関数は今回二乗和誤差を採用しています

$$ loss = \frac{1}{n}\ \sum^{n}_{k=1}\ (y_k - t)^{2} $$

以下コード


import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn import datasets

sess = tf.Session()

iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])

learning_rate = 0.05
batch_size = 25

x_data = tf.placeholder(shape = [None, 1], dtype = tf.float32)
y_target = tf.placeholder(shape = [None, 1], dtype = tf.float32)

A = tf.Variable(tf.random_normal(shape = [1, 1]))
b = tf.Variable(tf.random_normal(shape = [1, 1]))

model_output = tf.add(tf.matmul(x_data, A), b)

loss = tf.reduce_mean(tf.square(y_target - model_output))

init = tf.global_variables_initializer()
sess.run(init)

optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss)

loss_vec = []

for i in range(100):

    rand_index = np.random.choice(len(x_vals), size = batch_size)
    rand_x = np.transpose([x_vals[rand_index]])
    rand_y = np.transpose([y_vals[rand_index]])

    sess.run(train, feed_dict = {x_data: rand_x, y_target: rand_y})
    temp_loss = sess.run(loss, feed_dict = {x_data: rand_x, y_target: rand_y})

    loss_vec.append(temp_loss)

    if (i + 1) % 25 == 0:

        print("Step #" + str(i + 1) + " A = " + str(sess.run(A)) + " b = " + str(sess.run(b)))
        print("Loss = " + str(temp_loss))

[slope] = sess.run(A)
[y_intercept] = sess.run(b)

best_fit = []

for i in x_vals:

    best_fit.append(slope * i + y_intercept)

plt.plot(x_vals, y_vals, "o", label = "Data points")
plt.plot(x_vals, best_fit, "r-", label = "Best fit line", linewidth = 3)
plt.legend(loc = "upper left")
plt.title("Sepal Lentgth, Pedal Widh")
plt.xlabel("Pedal Width")
plt.ylabel("Sepal Length")
plt.show()

plt.plot(loss_vec, "k-")
plt.title("L2 Loss per Generation")
plt.xlabel("Generation")
plt.ylabel("L2 Loss")
plt.show()

Unknown.png

人によって得られる直線は違うと思われますが、大体こんな感じの直線が得られるかと思います

Loss値が減少方向に向かっていればうまく学習が進んでいる証拠です

Unknown.png

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