這篇部落格是我在學習用tensorflow搭建神經網路時,所作的一些筆記。搭建的神經網路有兩層隱藏層,和輸入輸出層。採用全連線的方式進行傳輸,優化演算法採用自適用矩估計演算法。
1.首先,匯入tensorflow官方提供的庫
2.設定輸入:import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist_data", one_hot=true)
mnist資料集中的為2828個畫素點,並且每次訓練都是小批量同時輸入,所以輸入的維度設定為[none,2828],同時可以用batch_size 代替none。x = tf.placeholder(tf.float32,[none,784],name='x_input')
y_ = tf.placeholder(tf.float32,[none,10],name='y_input')
3.定義引數
為了提高**的可讀性和簡潔性,定義了兩個方法,分別來定義引數w和b。def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.variable(initial)
4.運算
對於啟用函式採用relu,去線性。#第一層
net = tf.nn.relu(tf.matmul(x,w1)+b1)
#net = tf.nn.dropout(net,0.25)
#第二層
net = tf.matmul(net,w2)+b2
y = tf.nn.softmax(net)
5.損失函式和優化演算法
6.進行批量訓練loss = -tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
optim = tf.train.adamoptimizer(0.001).minimize(loss)
總結with tf.session() as sess:
tf.global_variables_initializer().run()
for i in range(10000):
xs,ys = mnist.train.next_batch(batch_size)
_,l,accu=sess.run([optim,loss,accuracy],feed_dict=)
if(i%100==0):
print('after %d times,the loss is %g'%(i,l))
print('the modle accuracy is %g '%(accu))
這個**量和理解並不難,主要用於練手,提高對tensorflow的掌握,熟悉tensorflow的函式。
附上**
# -*- coding: utf-8 -*-
"""created on tue sep 3 16:09:17 2019
@author: asus
"""import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
batch_size =100
mnist = input_data.read_data_sets("mnist_data", one_hot=true)
x = tf.placeholder(tf.float32,[none,784],name='x_input')
y_ = tf.placeholder(tf.float32,[none,10],name='y_input')
w1 = tf.get_variable('w1',shape=[784,300],initializer=tf.truncated_normal_initializer(stddev=0.1))
b1 = tf.variable(tf.zeros([300]))
w2 = tf.get_variable('w2',shape=[300,10],initializer=tf.truncated_normal_initializer(stddev=0.1))
b2 = tf.variable(tf.zeros([10]))
net = tf.nn.relu(tf.matmul(x,w1)+b1)
#net = tf.nn.dropout(net,0.25)
net = tf.matmul(net,w2)+b2
y = tf.nn.softmax(net)
loss = -tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
optim = tf.train.adamoptimizer(0.001).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
def train():
with tf.session() as sess:
tf.global_variables_initializer().run()
for i in range(10000):
xs,ys = mnist.train.next_batch(batch_size)
_,l,accu=sess.run([optim,loss,accuracy],feed_dict=)
if(i%100==0):
print('after %d times,the loss is %g'%(i,l))
print('the modle accuracy is %g '%(accu))
def main(ar**=none):
train()
if __name__ == '__main__':
用Kersa搭建神經網路 MNIST手寫資料集
nist手寫資料集的識別算得上是深度學習的 hello world 了,所以想要入門必須得掌握。新手入門可以考慮使用keras框架達到快速實現的目的。完整 如下 1.導入庫和模組 from keras.models import sequential from keras.layers import...
TensorFlow 神經網路MNIST手寫識別
專案稍有停歇,終於有時間來玩一下tensorflow和dl了。看了官網入門教程就先來說說神經網路吧。神經網路其實是一種演算法,能夠提供給我們一種複雜的非線性模型hw,b x 並以此來擬合我們的資料。又因為它的構造和處理資訊的模式有點像人類,這讓神經網路更顯得神奇。神經網路中的引數weight和bia...
神經網路訓練
學習了bp神經網路演算法,剛開始最終要的一點不明白的就是前一層和後一層 這裡指的只有三層,輸入層,隱藏層,輸出層 的權重,其實神經網路演算法中,前一層的每乙個節點和後一層的每乙個節點都有連線權重,初始權重是隨機的。而更新權重是通過輸出層的值來反向更新的。基本過程如下 1.輸入第乙個訓練樣本 對應的就...