不寫點東西學了都忘了
這是一個簡單的擬合線性迴歸的例子,若有錯誤和疑問歡迎指出!
深度學習的步驟大概分爲以下四個:
- 準備數據
- 搭建模型
- 迭代訓練
- 使用模型
分享連接
numpy模塊的中文學習鏈接
matplotlib官網學習鏈接(可藉助翻譯軟件學習)
Anaconda3.5.1 tensorflow-gpu==1.13.1 CUDA10 Cudnn7.5 安裝教程
numpy、matplotlib模塊pip安裝教程
一,準備數據
這個步驟實現兩個功能:
- 引入頭文件
- 生成帶噪音的數據點
import tensorflow as tf #導入tensorflow模塊
import numpy as np #導入numpy模塊
import matplotlib.pyplot as plt #導入matplotlib模塊中的pyplot模塊並重命名爲plt
train_X = np.linspace(-1,1,100) #在-1 ~ 1中生成100個數據點train_X
train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.3 #y = 2x,但是加入了本身乘上0.3的早生
#提示:下面的ro是plot函數中的format_string參數,r-紅色,o-實心圈標記
plt.plot(train_X,train_Y,'ro',label = 'Original data')#顯示模擬的數據點
plt.legend(title = 'y = 2x data') #數據點適應大小,圖例標題:y = 2x data
plt.show()
代碼效果:
二,搭建模型
1,正向搭建模型
這個步驟是爲了實現模型的正向傳輸,類似於,輸入x,然後得出y,相當於模擬使用
#create the model
#------------------------------
#set the placeholders
X = tf.placeholder('float')
Y = tf.placeholder('float')
#set model's parameter
W = tf.Variable(tf.random_normal([1]),name = 'weight') #weight:權重
b = tf.Variable(tf.zeros([1]),name = 'bias') #bias:偏向
#forward structure
z = tf.multiply(X,W) + b
#reverse optimization
cost = tf.reduce_mean(tf.square(Y - z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#----------------------------------------------------------------------------
2,反向搭建播型
這個步驟是爲了通過由正向模型計算出的值與標籤(我們希望生成的值)進行參數(W:權重與b:偏
差)調整,然後使模型的輸出越來越符合我們期望生成的值
#reverse optimization
cost = tf.reduce_mean(tf.square(Y - z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
三,迭代訓練
1,訓練模型
tensorflow中的任務通過session進行,全局初始化變量,設定迭代次數,啓動session進行訓練
#lterative training model
#---------------------------
#Initialize all variables
init = tf.global_variables_initializer()
#set the parameter
training_epochs = 20
display_step = 2
#start session
with tf.Session() as sess:
sess.run(init)
plotdata = {"batchsize":[],"loss":[]} #Storage batch value and loss value
#input data to the model
for epoch in range(training_epochs):
for (x,y) in zip(train_X,train_Y):
sess.run(optimizer,feed_dict = {X:x,Y:y})
if epoch % display_step == 0:
loss = sess.run(cost,feed_dict = {X:train_X,Y:train_Y})
print ("Epoch:", epoch+1, "cost=", loss,"W=", sess.run(W), "b=", sess.run(b))
if not (loss =="NA"):
plotdata["batchsize"].append(epoch)
plotdata["loss"].append(loss)
print("Finished!")
print ("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}), "w=", sess.run(W), "b=", sess.run(b))
運行效果:
2,模型可視化
#show in picture
plt.plot(train_X,train_Y,'ro',label='Original data')
plt.plot(train_X,sess.run(W) * train_X + sess.run(b),label = 'Fittedline')
plt.legend()
plt.show()
plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)
plt.plot(plotdata['batchsize'],plotdata['avgloss'],'b--')
plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs. Training loss')
plt.show()
運行效果:
四,使用模型
#use the model
print ("x=0.4,z=", sess.run(z, feed_dict={X: 0.4}))
運行效果:
五,完整代碼
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 8 16:41:37 2019
@author: HPN
CSDN博客:https://blog.csdn.net/RObot_123
"""
import tensorflow as tf #導入tensorflow模塊
import numpy as np #導入numpy模塊
import matplotlib.pyplot as plt #導入matplotlib模塊中的pyplot模塊並重命名爲plt
plotdata = { "batchsize":[], "loss":[] }
def moving_average(a, w=10):
if len(a) < w:
return a[:]
return [val if idx < w else sum(a[(idx-w):idx])/w for idx, val in enumerate(a)]
train_X = np.linspace(-1,1,100) #在-1 ~ 1中生成100個數據點train_X
train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.3 #y = 2x,但是加入了本身乘上0.3的早生
#提示:下面的ro是plot函數中的format_string參數,r-紅色,o-實心圈標記
plt.plot(train_X,train_Y,'ro',label = 'Original data')#顯示模擬的數據點
plt.legend(title = 'y = 2x data') #數據點適應大小,圖例標題:y = 2x data
plt.show() #顯示圖片
#create the model
#------------------------------
#set the placeholders
X = tf.placeholder('float')
Y = tf.placeholder('float')
#set model's parameter
W = tf.Variable(tf.random_normal([1]),name = 'weight') #weight:權重
b = tf.Variable(tf.zeros([1]),name = 'bias') #bias:偏向
#forward structure
z = tf.multiply(X,W) + b
#reverse optimization
cost = tf.reduce_mean(tf.square(Y - z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#----------------------------------------------------------------------------
#lterative training model
#---------------------------
#Initialize all variables
init = tf.global_variables_initializer()
#set the parameter
training_epochs = 20
display_step = 2
#start session
with tf.Session() as sess:
sess.run(init)
plotdata = {"batchsize":[],"loss":[]} #Storage batch value and loss value
#input data to the model
for epoch in range(training_epochs):
for (x,y) in zip(train_X,train_Y):
sess.run(optimizer,feed_dict = {X:x,Y:y})
if epoch % display_step == 0:
loss = sess.run(cost,feed_dict = {X:train_X,Y:train_Y})
print ("Epoch:", epoch+1, "cost=", loss,"W=", sess.run(W), "b=", sess.run(b))
if not (loss =="NA"):
plotdata["batchsize"].append(epoch)
plotdata["loss"].append(loss)
print("Finished!")
print ("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}), "w=", sess.run(W), "b=", sess.run(b))
#show in picture
plt.plot(train_X,train_Y,'ro',label='Original data')
plt.plot(train_X,sess.run(W) * train_X + sess.run(b),label = 'Fittedline')
plt.legend()
plt.show()
plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)
plt.plot(plotdata['batchsize'],plotdata['avgloss'],'b--')
plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs. Training loss')
plt.show()
#use the model
print ("x=0.4,z=", sess.run(z, feed_dict={X: 0.4}))
總結:這個擬合模型用到了numpy、matplotlib模塊,其中的細節需要翻看網上的資料,具體學習鏈接在文章開頭