tensorflow 網路構建和使用筆記

2021-10-16 06:21:58 字數 3724 閱讀 7905

比如:0-9如果不用杜熱編碼,在歐氏距離上,1和3比8和3更接近,但實際上並不是這樣,因此用獨熱編碼就能很好的打破這種聯絡。

如何讀取呢:

argmax
np.argmax(x_data[0]

)#返回最大數的索引

# 或在tensorflow中

tf,argmax(x_data,0)

# 按行取值,即同列的每一行取最大

tf.argmax(x_data,1)

# 按列取值,即同列的每一行取最大

例如:

arr1 = np.array([1

,3,2

,5,7

,0])

arr2 = np.array([[

1,2,

3][3

,2,1

][4,

7,2]

[8,3

,2]]

)tf.argmax(arr1)

#4tf,argmax(arr2 ,0)

# [3 2 0]

tf,argmax(arr2 ,1)

# [2 0 1 0]

tf,argmax(arr2 ,-1

)# [2 0 1 0] ,-1就是最後一維,也就是1

資料劃分為訓練集,驗證集和測試集:使用訓練集訓練模型,驗證集評估模型,根據在驗證集上獲得的效果調整模型,選擇在驗證集上獲得最佳效果的模型在測試集上確認模型的效果6
一次批量讀取多條資料
#next_batch()實現內部會對資料集先做shuffle

x_image,x_label = minist,train.next_batch(batch_size=

10)

tf.random_normal()
norm=tf.random_normal(

[784,10

])#生成784行10列的隨機數的節點

with tf.session(

)as sess:

norm_data = norm.

eval()

print

(norm_data[:3

])#列印前3行10列的數

回歸處理分類問題

sigmod函式s型,保證輸出在0-1之間

邏輯回歸:對數損失函式

均方差容易導致陷入區域性最優解,

因此採用對數損失函式

softmax(類別於sigmod)

本質上就是講邏輯回歸公式延伸到了多類別

交叉熵損失函式

刻畫兩個概率分布之間的距離,交叉熵越小,兩個概率的分布越接近

,模型越好。比如:

這樣可以很好的判別不同網路**的優劣。

構建模型

import tensorflow as tf

import nu,py as np

from time import time

starttime = time(

)with tf.variable_scope(

'model'):

x = tf.placeholder(tf.float32,

[none

,784

],name=

'x')

y = tf.placeholder(tf.float32,

[none,10

],name=

'y')

w = tf.varibale(tf.random_mordel(

[784,10

]),name=

'w')

b = tf.varibale(tf.zeros([10

]),name=

'b')

forward = tf.matmul(x,w)

+b

# softmax變成概率

pred = tf.nn.softmax(forward)

# 定義交叉熵損失函式

loss_function = tf.reduce_mean(

-tf.reduce_sum(y*tf.log(pred)

, reduction_indices=1)

)# 梯度下降優化器,根據損失進行優化

optimizer = tf.train.gradientdescentoptimizer(learning_rate)

.minimize(loss_function)

# 定義準確率,判斷1維的pred和y是否相等

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

,tf.argmax(y,1)

)# 把布林值轉化為浮點數,並計算平均值

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

)#訓練模型的引數

train_epochs =

50# 訓練輪數

batch_size =

100# 每批訓練樣本數

display_step =

1# 顯示粒度

learning_rate =

0.01

# 學習率

#執行with tf.session(

)as sess:

init = tf.global_variables_initializer(

) sess.run(init)

for epoch in

range

(train_epochs)

: xs,ys = mnist.train.next_batch(batch_size)

#mnist批量讀取資料

sess.run(optimizer,feed_dict=

)# 用驗證集驗證網路效果

loss,acc = sess.run(

[loss_function,accuracy]

,feed_dict=)if

(epoch+1)

%display_step ==0:

print

('train epoch:{}'

.format

(epoch+1)

,'loss:{}'

.format

(loss)

,'accuracy={}'

.format

(acc)

)# 應用模型:

prediction = sess.run(tf.argmax(pred,1)

, feed_dict=

)# 顯示時間

duration = time(

)- starttime

vue獨立構建和執行構建

概念 有兩種構建方式,獨立構建和執行構建。它們的區別在於前者包含模板編譯器而後者不包含。模板編譯器 模板編譯器的職責是將模板字串編譯為純 j ascript 的渲染函式。如果你想要在元件中使用template選項,你就需要編譯器。模板字串 template el 提供乙個在頁面上已存在的 dom 元...

vue element admin專案構建和發布

如果你的資料不是模擬的,則需要將 src main.js中的以下 注釋掉 import from mock if process.env.node env production 將以上 注釋掉 然後對根目錄中名為.env.production的配置檔案做以下修改 base api prod api ...

Tensorflow專案構建流程

參考部落格 整理 一。訓練階段 1.tensorflow打包資料 2 採用tfrecoder進行高效資料讀取 2.網路架構與訓練 經過上面的資料格式處理,接著我們只要寫一寫網路結構,網路優化方法,把資料搞進網路中就可以 3.視覺化顯示 二。測試階段 直接通重載入圖模型,讀取引數等,然後直接通過ten...