生活随笔
收集整理的這篇文章主要介紹了
tessorflow实战
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- tessorflow實戰
- TensorFlow 實現Softmax Regression識別手寫數字
tessorflow實戰
TensorFlow 實現Softmax Regression識別手寫數字
數據集下載鏈接地址https://my.oschina.net/chkui/blog/888346
(擴展:tessorflow模型下載地址 https://github.com/tensorflow/models )
i 代表第幾類,
x 一張圖片
j一張圖片的第幾個像素
下面的代碼下載的數據集位置與執行文件在同一個文件夾中。
步驟:
首先在訓練集上訓練模型,
然后在驗證集上檢驗效果并決定何時完成訓練,
最后在驗證集測評模型的效果。
"""作者:FLY功能:實現softmax regression 識別手寫數字2.0:數據集位置與執行文件在同一個文件夾中版本:2.0日期:05/23/2019
"""import tensorflow
.examples
.tutorials
.mnist
.input_data
as input_data
mnist
= input_data
.read_data_sets
("D:/software/pycharm/shizhan/softmax/MNIST_data/", one_hot
=True) print(mnist
.train
.images
.shape
, mnist
.train
.labels
.shape
)
print(mnist
.test
.images
.shape
, mnist
.test
.labels
.shape
)
print(mnist
.validation
.images
.shape
, mnist
.validation
.labels
.shape
) import tensorflow
as tf
x
= tf
.placeholder
(tf
.float32
, [None, 784])
W
= tf
.Variable
(tf
.zeros
([784, 10]))
b
= tf
.Variable
(tf
.zeros
([10]))
y
= tf
.nn
.softmax
(tf
.matmul
(x
, W
) + b
)
y_
= tf
.placeholder
(tf
.float32
, [None, 10])
cross_entropy
= tf
.reduce_mean
(-tf
.reduce_sum
(y_
* tf
.log
(y
), reduction_indices
=[1]))
train_step
= tf
.train
.GradientDescentOptimizer
(0.5).minimize
(cross_entropy
) init
= tf
.initialize_all_variables
()
sess
= tf
.Session
()
sess
.run
(init
)
for i
in range(1000): batch_xs
, batch_ys
= mnist
.train
.next_batch
(100) sess
.run
(train_step
, feed_dict
={x
: batch_xs
, y_
: batch_ys
})
correct_prediction
= tf
.equal
(tf
.argmax
(y
,1), tf
.argmax
(y_
,1))
accuracy
= tf
.reduce_mean
(tf
.cast
(correct_prediction
, "float"))
print(sess
.run
(accuracy
, feed_dict
={x
: mnist
.test
.images
, y_
: mnist
.test
.labels
}))
輸出結果:0.9195
4個步驟:1、定義算法公式,也就是神經網絡forward時的計算
2、定義loss,選定優化器,并指定優化器優化loss。
3、迭代地對數據進行訓練
4、在測試集或驗證集上對準確率進行評測。
注意:只有調用run方法,并feed數據時計算才真正執行。
總結
以上是生活随笔為你收集整理的tessorflow实战的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。