tensorflow使用神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)mnist分類
本文實(shí)例為大家分享了tensorflow神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)mnist分類的具體代碼,供大家參考,具體內(nèi)容如下
只有兩層的神經(jīng)網(wǎng)絡(luò),直接上代碼
#引入包 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #引入input_data文件 from tensorflow.examples.tutorials.mnist import input_data #讀取文件 mnist = input_data.read_data_sets('F:/mnist/data/',one_hot=True) #定義第一個隱藏層和第二個隱藏層,輸入層輸出層 n_hidden_1 = 256 n_hidden_2 = 128 n_input = 784 n_classes = 10 #由于不知道輸入圖片個數(shù),所以用placeholder x = tf.placeholder("float",[None,n_input]) y = tf.placeholder("float",[None,n_classes]) stddev = 0.1 #定義權(quán)重 weights = { 'w1':tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev = stddev)), 'w2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)), 'out':tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev)) } #定義偏置 biases = { 'b1':tf.Variable(tf.random_normal([n_hidden_1])), 'b2':tf.Variable(tf.random_normal([n_hidden_2])), 'out':tf.Variable(tf.random_normal([n_classes])), } print("Network is Ready") #前向傳播 def multilayer_perceptrin(_X,_weights,_biases): layer1 = tf.nn.sigmoid(tf.add(tf.matmul(_X,_weights['w1']),_biases['b1'])) layer2 = tf.nn.sigmoid(tf.add(tf.matmul(layer1,_weights['w2']),_biases['b2'])) return (tf.matmul(layer2,_weights['out'])+_biases['out']) #定義優(yōu)化函數(shù),精準(zhǔn)度等 pred = multilayer_perceptrin(x,weights,biases) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred,labels=y)) optm = tf.train.GradientDescentOptimizer(learning_rate = 0.001).minimize(cost) corr = tf.equal(tf.argmax(pred,1),tf.argmax(y,1)) accr = tf.reduce_mean(tf.cast(corr,"float")) print("Functions is ready") #定義超參數(shù) training_epochs = 80 batch_size = 200 display_step = 4 #會話開始 init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) #優(yōu)化 for epoch in range(training_epochs): avg_cost=0. total_batch = int(mnist.train.num_examples/batch_size) for i in range(total_batch): batch_xs,batch_ys = mnist.train.next_batch(batch_size) feeds = {x:batch_xs,y:batch_ys} sess.run(optm,feed_dict = feeds) avg_cost += sess.run(cost,feed_dict=feeds) avg_cost = avg_cost/total_batch if (epoch+1) % display_step ==0: print("Epoch:%03d/%03d cost:%.9f"%(epoch,training_epochs,avg_cost)) feeds = {x:batch_xs,y:batch_ys} train_acc = sess.run(accr,feed_dict = feeds) print("Train accuracy:%.3f"%(train_acc)) feeds = {x:mnist.test.images,y:mnist.test.labels} test_acc = sess.run(accr,feed_dict = feeds) print("Test accuracy:%.3f"%(test_acc)) print("Optimization Finished")
程序部分運(yùn)行結(jié)果如下:
Train accuracy:0.605 Test accuracy:0.633 Epoch:071/080 cost:1.810029302 Train accuracy:0.600 Test accuracy:0.645 Epoch:075/080 cost:1.761531130 Train accuracy:0.690 Test accuracy:0.649 Epoch:079/080 cost:1.711757494 Train accuracy:0.640 Test accuracy:0.660 Optimization Finished
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用TensorFlow搭建一個全連接神經(jīng)網(wǎng)絡(luò)教程
- Tensorflow實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)擬合線性回歸
- TensorFlow卷積神經(jīng)網(wǎng)絡(luò)之使用訓(xùn)練好的模型識別貓狗圖片
- Python通過TensorFlow卷積神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)貓狗識別
- Tensorflow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的詳細(xì)代碼
- Tensorflow實(shí)現(xiàn)AlexNet卷積神經(jīng)網(wǎng)絡(luò)及運(yùn)算時間評測
- Tensorflow卷積神經(jīng)網(wǎng)絡(luò)實(shí)例進(jìn)階
- Tensorflow卷積神經(jīng)網(wǎng)絡(luò)實(shí)例
- tensorflow之自定義神經(jīng)網(wǎng)絡(luò)層實(shí)例
相關(guān)文章
基于Opencv圖像識別實(shí)現(xiàn)答題卡識別示例詳解
這篇文章主要為大家詳細(xì)介紹了基于OpenCV如何實(shí)現(xiàn)答題卡識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12基于telepath庫實(shí)現(xiàn)Python和JavaScript之間交換數(shù)據(jù)
telepath是一個Django庫,用于在Python和JavaScript之間交換數(shù)據(jù),使您可以構(gòu)建具有豐富客戶端接口的應(yīng)用程序,同時將業(yè)務(wù)邏輯保留在服務(wù)器端代碼中。2021-05-05解決virtualenv -p python3 venv報錯的問題
這篇文章主要介紹了解決virtualenv -p python3 venv報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Python 實(shí)現(xiàn)子類獲取父類的類成員方法
今天小編就為大家分享一篇Python 實(shí)現(xiàn)子類獲取父類的類成員方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python Matplotlib條形圖之垂直條形圖和水平條形圖詳解
這篇文章主要為大家詳細(xì)介紹了Python Matplotlib條形圖之垂直條形圖和水平條形圖,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03對numpy中數(shù)組元素的統(tǒng)一賦值實(shí)例
下面小編就為大家分享一篇對numpy中數(shù)組元素的統(tǒng)一賦值實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Pycharm無法使用已經(jīng)安裝Selenium的解決方法
今天小編就為大家分享一篇Pycharm無法使用已經(jīng)安裝Selenium的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10