tensorflow基本操作小白快速構建線性回歸和分類模型_第1頁
tensorflow基本操作小白快速構建線性回歸和分類模型_第2頁
tensorflow基本操作小白快速構建線性回歸和分類模型_第3頁
tensorflow基本操作小白快速構建線性回歸和分類模型_第4頁
tensorflow基本操作小白快速構建線性回歸和分類模型_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第tensorflow基本操作小白快速構建線性回歸和分類模型目錄tensorflow是非常強的工具,生態(tài)龐大tensorflow提供了Keras的分支Definetensorconstants.LinearRegression分類模型本例使用MNIST手寫數字Modelprediction:7Modelprediction:2Modelprediction:1Modelprediction:0Modelprediction:4TF目前發(fā)布2.5版本,之前閱讀1.X官方文檔,最近查看2.X的文檔。

tensorflow是非常強的工具,生態(tài)龐大

tensorflow提供了Keras的分支

這里不再提供Keras相關順序模型教程。

關于環(huán)境:ubuntu的GPU,需要cuda和nvcc

不會安裝:查看

完整的Ubuntu18.04深度學習GPU環(huán)境配置,英偉達顯卡驅動安裝、cuda9.0安裝、cudnn的安裝、anaconda安裝

不安裝,直接翻墻用colab

測試GPU

fromtensorflow.python.clientimportdevice_lib

device_lib.list_local_devices()

這是意思是掛了一個顯卡

具體查看官方文檔:/install

服務器跑Jupyter

Definetensorconstants.

importtensorflowastf

#CreateaTensor.

hello=tf.constant("helloworld")

hello

#Definetensorconstants.

a=tf.constant(1)

b=tf.constant(6)

c=tf.constant(9)

#tensor變量的操作

#(+,*,...)

add=tf.add(a,b)

sub=tf.subtract(a,b)

mul=tf.multiply(a,b)

div=tf.divide(a,b)

#通過numpy返回數值和torch一樣

print("add=",add.numpy())

print("sub=",sub.numpy())

print("mul=",mul.numpy())

print("div=",div.numpy())

add=7

sub=-5

mul=6

div=0.16666666666666666

mean=tf.reduce_mean([a,b,c])

sum_=tf.reduce_sum([a,b,c])

#Accesstensorsvalue.

print("mean=",mean.numpy())

print("sum=",sum_.numpy())

mean=5

sum=16

#Matrixmultiplications.

matrix1=tf.constant([[1.,2.],[3.,4.]])

matrix2=tf.constant([[5.,6.],[7.,8.]])

product=tf.matmul(matrix1,matrix2)

product

tf.Tensor:shape=(2,2),dtype=float32,numpy=

array([[19.,22.],

[43.,50.]],dtype=float32)

#TensortoNumpy.

np_product=product.numpy()

print(type(np_product),np_product)

(numpy.ndarray,

array([[19.,22.],

[43.,50.]],dtype=float32))

LinearRegression

下面使用tensorflow快速構建線性回歸模型,這里不使用kears的順序模型,而是采用torch的模型定義的寫法。

importnumpyasnp

importtensorflowastf

#Parameters:

learning_rate=0.01

training_steps=1000

display_step=50

#TrainingData.

X=np.array([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1])

Y=np.array([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3])

random=np.random

#權重和偏差,隨機初始化。

W=tf.Variable(random.randn(),name="weight")

b=tf.Variable(random.randn(),name="bias")

#Linearregression(Wx+b).

deflinear_regression(x):

returnW*x+b

#Meansquareerror.

defmean_square(y_pred,y_true):

returntf.reduce_mean(tf.square(y_pred-y_true))

#隨機梯度下降優(yōu)化器。

optimizer=tf.optimizers.SGD(learning_rate)

#優(yōu)化過程。

defrun_optimization():

#將計算包在GradientTape中,以便自動區(qū)分。

withtf.GradientTape()asg:

pred=linear_regression(X)

loss=mean_square(pred,Y)

#計算梯度。

gradients=g.gradient(loss,[W,b])

#按照梯度更新W和b。

optimizer.apply_gradients(zip(gradients,[W,b]))

#按給定的步數進行訓練。

forstepinrange(1,training_steps+1):

#運行優(yōu)化以更新W和b值。

run_optimization()

ifstep%display_

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論