本文實(shí)例為大家分享了tensorflow神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)mnist分類的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)是一家專業(yè)提供通化縣企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、H5建站、小程序制作等業(yè)務(wù)。10年已為通化縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。只有兩層的神經(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) #定義第一個(gè)隱藏層和第二個(gè)隱藏層,輸入層輸出層 n_hidden_1 = 256 n_hidden_2 = 128 n_input = 784 n_classes = 10 #由于不知道輸入圖片個(gè)數(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 #會(huì)話開始 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")