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による非線形回帰

正解値データ

y=-0.4x^3+1.6x^2-2.8x+1

# データを生成
n=100
x = np.random.rand(n).astype(np.float32) * 4 - 2
d =  - 0.4 * x ** 3 + 1.6 * x ** 2 - 2.8 * x + 1

非線形な解を生成

モデル

# xの3乗、2乗、1乗、0乗の4つ分のプレースホルダ
xt = tf.placeholder(tf.float32, [None, 4])
dt = tf.placeholder(tf.float32, [None, 1])
# -0.4,1.6,-2.8,1という4つの重みがあるので、4つ。0.01の標準偏差で初期化
W = tf.Variable(tf.random_normal([4, 1], stddev=0.01))
# ノードとしてxt×Wを定義
y = tf.matmul(xt,W)

1次元目はバッチサイズで上記のようにNoneにしておくと可変サイズに対応可能

誤差関数

optimizer = tf.train.AdamOptimizer(0.001)

Adamを使用、学習率は0.001

結果

image.png
W1 = [-0.40030134] ※正解-0.4
W2 = [ 1.5934367 ] ※正解1.6
W3 = [-2.7970264 ] ※正解2.8
W4 = [ 1.0092059 ] ※正解1

ノイズの変更

10倍のnoise = 0.5
image.png
W1 = [-0.47843903]
W2 = [ 1.5267874 ]
W3 = [-2.5911577 ]
W4 = [ 1.1071517 ]
保ててはいるが、精度は落ちている

100倍のnoise = 5
image.png
W1 = [-0.07317215]
W2 = [ 1.7690974 ]
W3 = [-3.3754215 ]
W4 = [-0.42890525]
ノイズに対しては弱いことが観測できる

dの正解値モデルの変更

y=1.4x^3+3.1x^2-5.5x^2+0.03

image.png
非常にきれいにモデルを予測できている

演習

y=30x^{2} +0.5x+0.2

の予測を行う
iters_numやlearning_rateを変更せずに実施
image.png
W1 =[ 4.544718 ]
W2 = [ 9.391723 ]
W3 = [-2.7000463]
W4 = [ 9.274796 ]

誤差が収束するようiters_numやlearning_rateを調整
学習率0.00001
image.png
むしろ悪化
⇒プレースホルダーの個数変更等、ウェイトが減ったことを加味していなかったことが原因

学習率0.01
image.png
非常に高精度に改善

学習回数100万回
image.png
W1 = [4.2578713e-03]
W2 = [3.0001066e+01]
W3 = [4.8582685e-01]
W4 = [2.0072795e-01]
変数対応をしないで、計算回数だけで力技で実行
ここまで学習を回すと、3乗の係数がウェイトを無視できるものまで落としていることが観測できる

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?