0
0

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.

TensorFlowを使用した線形回帰

Posted at

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

TensorFlowを使用した線形回帰

実装

# 学習回数、後のfor文で使用される
iters_num = 300
# 何回学習するごとに表示するか
plot_interval = 10

# データを生成
# 100個のランダムなx
# randは0~1の一様分布
# randn(10)       # 標準正規分布を10個生成
n = 100
x = np.random.rand(n)
# 正解データの作成
# これでは直線データができるだけなので、下でノイズを加えている
d = 3 * x + 2

# ノイズを加える
# 0~0.3くらいのノイズ
# randnは平均0、分散1のガウス分布(標準正規分布)
# randn(10)       # 標準正規分布を10個生成
# randn(10,10)    # 標準正規分布による 10x10 の行列
# randn(100)で、1から-1の数を100個作り、これに0.3をかけてやることで、0.3から-0.3の数を100個作っている

noise = 0.3
d = d + noise * np.random.randn(n) 

# 入力値
# xを入れるプレースホルダ
xt = tf.placeholder(tf.float32)
# dを入れるプレースホルダ
dt = tf.placeholder(tf.float32)

# 最適化の対象の変数を初期化
# ここから、trainの行までがTensorFlowで学習を行うための準備
# 初期値0の重み
W = tf.Variable(tf.zeros([1]))
# 初期値0のバイアス
b = tf.Variable(tf.zeros([1]))

# 今回の学習では、この重みWとバイアスdを求めることが目的
y = W * xt + b

# 誤差関数 平均2乗誤差
# 回帰問題なので、誤差には平均2乗誤差を用いる
loss = tf.reduce_mean(tf.square(y - dt))
# https://qiita.com/mine820/items/747a876d0bce658ad9ba
# TensorFlowで用意してくれている関数
# 「GradientDescentOptimizer」は急速降下法
# 「AdamOptimizer」はAdamアルゴリズム
# この0.1は学習率
optimizer = tf.train.GradientDescentOptimizer(0.1)
# 誤差を最小化して学習していく
train = optimizer.minimize(loss)

# 初期化
# TensorFlowの初期化およびセッションの立ち上げ
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# 作成したデータをトレーニングデータとして準備
# 縦ベクトル化
x_train = x.reshape(-1,1)
d_train = d.reshape(-1,1)

# トレーニング
# 先ほど定義したiters_nomは300なので、300回実施
#
for i in range(iters_num):
    #sess.runを行うときに、先ほど定義したtrain(誤差を最小化する処理)
    #feed_dictで、dtには正解データを縦ベクトル化したd_train(ノイズ入り)を、
    #xtには、0~100の整数入力値を、それぞれ代入
    sess.run(train, feed_dict={xt:x_train,dt:d_train})
    if (i+1) % plot_interval == 0:
        loss_val = sess.run(loss, feed_dict={xt:x_train,dt:d_train}) 
        W_val = sess.run(W)
        b_val = sess.run(b)
        print('Generation: ' + str(i+1) + '. 誤差 = ' + str(loss_val))

print(W_val)
print(b_val)

#  予測関数
# 予測できた重みWとバイアスbを用いて、式(モデル)を算出
def predict(x):
    return W_val * x + b_val

noiseやd(正解モデル)の重みW/バイアスbを変えた実験と考察

noiseの変更

noise = 0.1
image.png
W = [2.9645967]
b = [2.017563]

noise = 0.5
image.png
W = [2.9131765]
b = [2.0111299]

上下に均等にnoiseが乗る場合には、それほど近似性に際は内容に思われる。

noise = 5
image.png
W = [2.0730288]
b = [5.7792187]

予測値が正解式と大きくかい離。

Wの変更

W=9で正解式を作成(noise = 0.3)
image.png
W = [8.721432]
b = [2.1758988]

b=6で正解式を作成(noise = 0.3)
image.png
W = [2.9766734]
b = [5.998174]

依然として、近似性に際は内容に思われる。

ノイズには弱く、線形性の傾きや切片の変化には追従性が高い

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?