TensorFlow深度學習1

2021-10-17 04:40:49 字數 3537 閱讀 9540

tensorflow基本概念總結:使用圖(graphs)來表示計算任務;在被稱之為會話(session)的上下文(context)中執行圖; 使用tensor表示資料;通過變數(variable)維護狀態;使用feed和fetch可以為任意的操作賦值或者從其中獲取資料;

tensorflow是乙個程式設計系統,使用圖(graphs)來表示計算任務,圖(graphs)中的節點稱之為op(operation),乙個op獲得0個或多個tensor,執行計算,產生0個或多個tensor。tensor 看作是乙個 n 維的陣列或列表。圖必須在會話(session)裡被啟動。

tensorflow 圖結構說明:

非線性回歸python樣例:

# coding: utf-8

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

#使用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

#定義兩個placeholder

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.1)

.minimize(loss)

with tf.session(

)as sess:

#變數初始化

sess.run(tf.global_variables_initializer())

for _ in

range

(2000):

sess.run(train_step,feed_dict=

)#獲得**值

prediction_value = sess.run(prediction,feed_dict=

)#畫圖

plt.figure(

) plt.scatter(x_data,y_data)

plt.plot(x_data,prediction_value,

'r-'

,lw=5)

plt.show(

)

mnist資料集分類簡單樣例:

# coding: utf-8

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

#載入資料集

mnist = input_data.read_data_sets(

"mnist_data"

,one_hot=

true

)#每個批次的大小

batch_size =

100#計算一共有多少個批次

n_batch = mnist.train.num_examples // batch_size

#定義兩個placeholder

x = tf.placeholder(tf.float32,

[none

,784])

y = tf.placeholder(tf.float32,

[none,10

])#建立乙個簡單的神經網路

w = tf.variable(tf.zeros(

[784,10

]))b = tf.variable(tf.zeros([10

]))prediction = tf.nn.softmax(tf.matmul(x,w)

+b)#二次代價函式

loss = tf.reduce_mean(tf.square(y-prediction)

)#使用梯度下降法

train_step = tf.train.gradientdescentoptimizer(

0.2)

.minimize(loss)

#初始化變數

init = tf.global_variables_initializer(

)#結果存放在乙個布林型列表中

correct_prediction = tf.equal(tf.argmax(y,1)

,tf.argmax(prediction,1)

)#argmax返回一維張量中最大的值所在的位置

#求準確率

accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)

)with tf.session(

)as sess:

sess.run(init)

for epoch in

range(21

):for batch in

range

(n_batch)

: batch_xs,batch_ys = mnist.train.next_batch(batch_size)

sess.run(train_step,feed_dict=

)

acc = sess.run(accuracy,feed_dict=

)print

("iter "

+str

(epoch)

+",testing accuracy "

+str

(acc)

)

深度學習筆記 1 安裝tensorflow

tensorflow是google開發的一款神經網路的python外部的結構包,簡單說就是乙個搭建神經網路的工具,運用這個工具我們就能搭建神經網路了。參考教程 進行配置 替換為命令 conda create prefix f conda安裝的位置 conda envs tensorflow pyth...

Tensorflow深度學習筆記1 基礎知識

變數 將tensorflow1.x的 import tensorflow as tf 替換為 import tensorflow.compat.v1 as tf tf.disable eager execution 強制使用cpu模式 import os os.environ cuda visibl...

TensorFlow 深度學習筆記

google 深度學習筆記 經常總結是個好習慣,筆記目錄奉上。歡迎star,有問題可以到issue區討論 官方教程位址 最近tensorflow團隊出了乙個model專案,和這個課程無關,但是可以參考 框架 tensorflow 谷歌出品的基於python的深度學習工具集 工具 ipython,py...