序文
前のブログ:tensorflow学習(4) -線形関数のtensorflow訓練傾きと切片を持つ
この章のリターンの問題を解決します。
それぞれのパケットの紹介
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
ニューラルネットワークレベル
ここで、図は最も左側との本は、私たちの使用は、Wは重量をnumpyの生成されたランダムサンプル(x_data、y_data)を表している我々の場合には、ネットワークの入力層で選びます。
試験サンプルを準備します
#使用numpy来生成200个随机点,等差数列
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
チップ:
- [:、Np.newaxis]コンマリットル寸法前のパラメータの範囲を表す寸法Lの行列、などである[1:5、newaxis]表すアレイメンバー1〜5リットル寸法。
- linspace(-0.5,0.5,200)この関数は、第三の総数であり、第二の端を開始、最初のパラメータです。演算シーケンスを生成します。
中間層の設計
#定义神经网络中间层
Weights_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
#激活函数
L1 = tf.nn.tanh(Wx_plus_b_L1)
チップ:
- Weights_L1:これには、第一層の重量であり、この層x_data入力の隠れ層重量唯一のノード、最初のパラメータのtf.random_normal([1,10])が1にセットされ、その後、我々は、10のニューロンを設計します二番目のパラメータは10に設定されています。
- biases_L1:これは入力だけ一つのノードとして、我々は中間層に10ニューロンを設計するオフセットパラメータであり、Weights_L1、tf.Variableにパラメータセット(tf.zeros([1,10]))
- Wx_plus_b_L1:これは、A11上の図、A12、A13の結果、重み行列プラスオフセット行列によって行列を入力しています。
- L1 = tf.nn.tanh(Wx_plus_b_L1):これは、ニューラルネットワークのコア部分であり、我々は、上記の重み行列プラスオフセット行列の行列を入力します。この文は、明らかに、入力と出力の関係が線形である知ることができますが、私たちの例では、サンプルは、明らかに、Y = KX + Bなどのラインにより、二次関数である私たちはうまく適合することができないということです曲線、ここで使用される活性化関数は、可能な非線形線形に結合TANHは、です。
デザイン出力層
#定义输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
#激活函数
prediction = tf.nn.tanh(Wx_plus_b_L2)
チップ:
- Weights_L2:我々は、最初の引数は唯一つの出力がある以来、10に設定されている中間層、右重量tf.Variable(tf.random_normal([10,1]))は、出力層の以前の10ニューロンを設計しているためYは、これ第二パラメータが1に設定されています。
- biases_L2:ディメンションバイアス行列は同じ重み行列が設けられています。
- Wx_plus_b_L2:重み行列プラスオフセットマトリクスによって入力行列。
- 予測= tf.nn.tanh(Wx_plus_b_L2):活性化関数と同じ意味を、本明細書中。
トレーニング
#二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
#用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
with tf.Session() as sess:
#变量初始化
sess.run(tf.global_variables_initializer())
for i in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获得预测值
prediction_value = sess.run(prediction,feed_dict = {x:x_data})
#画图
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()
トレーニング結果
アクティベーション機能検査
その上で、オンラインの活性化関数の引数、一般的に使用されるrelu、双曲線正接、S状結腸との多くをお読みください。
これらの関数を使用する理由は、以下の考慮事項以下のとおりです。
- 非線形
- 限られた出力範囲、出力層として好適です
- 出力は0を中心とします
- 計算するのが容易
- Hengdaの値はゼロまたは一定しないゼロ未満であります
- ...
初心者ニューラルネットワークは、ここでいくつかのパラメータを変更し、活性化関数の違いを見てみましょうしようとすると、説明の原理は本当に理解していない参照してください。
Reluは、活性化関数として使用しました
アクティベーション機能:relu
#使用numpy来生成200个随机点,等差数列
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
#定义两个palceholder
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
#定义神经网络中间层
Weights_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
#激活函数
L1 = tf.nn.relu(Wx_plus_b_L1)
#定义输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
#激活函数
prediction = tf.nn.relu(Wx_plus_b_L2)
#二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
#用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)#修改学习率!!!
with tf.Session() as sess:
#变量初始化
sess.run(tf.global_variables_initializer())
for i in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获得预测值
prediction_value = sess.run(prediction,feed_dict = {x:x_data})
#画图
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()
出力
学習率:0.04
学習率:0.1
学習率:0.2
学習率:0.3
学習率:0.4
学習率:0.5
学習率:0.6
reluの概要
学習率が高いほど、より簡単に起動に失敗しました。
これは、サンプルがランダムに生成され、各世代が異なっている、変数をコントロールしていない...しかし、おそらく学習率relu活性化機能の影響を確認することができ、非常に厳格ではありません。
活性化関数として使用TANH
#使用numpy来生成200个随机点,等差数列
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
#定义两个palceholder
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
#定义神经网络中间层
Weights_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
#激活函数
L1 = tf.nn.tanh(Wx_plus_b_L1)
#定义输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
#激活函数
prediction = tf.nn.tanh(Wx_plus_b_L2)
#二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
#用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.04).minimize(loss)
with tf.Session() as sess:
#变量初始化
sess.run(tf.global_variables_initializer())
for i in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获得预测值
prediction_value = sess.run(prediction,feed_dict = {x:x_data})
#画图
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()
出力
学習率:0.01
学習率:0.04
学習率:0.1
学習率:0.2
学習率:0.3
学習率:0.4
学習率:0.5
学習率:0.6
概要
率が低すぎる学習、結果は正確ではありません
シグモイド活性化関数として使用します
#使用numpy来生成200个随机点,等差数列
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
#定义两个palceholder
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
#定义神经网络中间层
Weights_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
#激活函数
L1 = tf.nn.sigmoid(Wx_plus_b_L1)
#定义输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
#激活函数
prediction = tf.nn.sigmoid(Wx_plus_b_L2)
#二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
#用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.04).minimize(loss)
with tf.Session() as sess:
#变量初始化
sess.run(tf.global_variables_initializer())
for i in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获得预测值
prediction_value = sess.run(prediction,feed_dict = {x:x_data})
#画图
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()
出力
学習率:0.04
学習率:0.1
学習率:0.2
学習率:0.3
学習率:0.4
学習率:0.5
学習率:0.6
概要
これは明らかに我々のモデル不適切な活性化関数です。