Tensorflow 線性回歸與非線性回歸

2021-10-02 15:40:16 字數 2830 閱讀 8538

#二次代價函式 均方差

#神經網路:1-20-1

w1 = tf.variable(tf.random_normal([1

,20])

)b1 = tf.variable(tf.zeros([20

]))wxplus1 = tf.matmul(x,w1)

+ b1

l1 = tf.nn.tanh(wxplus1)

w2 = tf.variable(tf.random_normal([20

,1])

)b2 = tf.variable(tf.zeros([1

]))wxplus2 = tf.matmul(l1,w2)

+ b2

prediction = tf.nn.tanh(wxplus2)

loss = tf.losses.mean_squared_error(y,prediction)

#使用梯度下降法最小化loss

train = tf.train.gradientdescentoptimizer(

0.1)

.minimize(loss)

with tf.session(

)as sess:

#變數初始化

tensorflow入門線性回歸

實際上編寫tensorflow可以總結為兩步.1 組裝乙個graph 2 使用session去執行graph中的operation。當使用tensorflow進行graph構建時,大體可以分為五部分 1 為輸入x與輸出y定義placeholder 2 定義權重w 3 定義模型結構 4 定義損失函式 ...

TensorFlow 線性回歸實現

usr bin env python3 coding utf 8 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt if name main 給引數賦值 learning rate 0.01 trai...

TensorFlow實踐 線性回歸

通過生 工資料集合,基於tensorflow實現線性回歸 利用隨機數在直線y 2.321 x 1.564的基礎上,加上一定的噪音,產生在 1,1 範圍的資料。x,y是兩個佔位符,用於儲存訓練過程中的資料,w,b是兩個變數,訓練就是不斷地改變這兩個值,以使模型達到最好。train epochs和lea...