0509
https://blog.csdn.net/goldxwang/article/details/78790797
tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法
https://blog.csdn.net/ZHANGHUIHUIA/article/details/83784943
利用tf.argmax()按行求出真實值y_、預測值y最大值的下標,用tf.equal()求出真實值和預測值相等的數量,也就是預測結果正確的數量,tf.argmax()和tf.equal()一般是結合着用。
tf.cast
強制轉換類型
tf.cast(data_1,dtype)
將data_1(可以爲int,bool,float,python列表,python元組
輸出轉換後的數據,原數據不變 轉換成指定dtype類型
兩行結合 計算準確率 accuracy
corect_prediction = tf.equal(tf.argmax(ys, 1), tf.argmax(logits, 1))
acc = tf.reduce_mean(tf.cast(corect_prediction, tf.float32))
cnn_minist.py
可去pycharm 查看代碼 ./TF_Foundation/MINST_data/3_cnn_minst.py
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
"""
place_holder+conv2d+pool2d+circle+fcn(reshape)+out+get logits+ loss + optimizer+sess(train)+acc
"""
minst = input_data.read_data_sets('MINST_data', one_hot=True)
learning_rate = 0.001 #學習率
batch_size = 128 #每次取出128個batch
num_step = 10 #迭代訓練多少次
display_step = 100 #最後每100次輸出一下
input_size = 784 #28*28 總共有784個像素點
num_labels = 10 #輸出樣本的分類
drop_out = 0.9 #drop_out 保留率
xs = tf.placeholder(tf.float32, [batch_size, input_size]) # # batch_size 是每次訓練多少個 input_size 數據集中 h*w*c 計算的張量
ys = tf.placeholder(tf.float32, [batch_size, num_labels]) # 10個輸出
keep_prob = tf.placeholder(tf.float32) #保留率的佔位符
## 卷積層 wx+b
def conv2d(input, W, b, stride=1):
conv = tf.nn.conv2d(input, W, strides=[1, stride, stride, 1], padding='SAME')
return tf.nn.bias_add(conv, b)
## 池化層
def maxpool2d(input, stride=2):
return tf.nn.max_pool(input, ksize=[1, stride, stride, 1], strides=[1, stride, stride, 1], padding='SAME')
def inference(x, W, b, dropout):
#minist 讀取出來是 [128,784] 前面是設定每次訓練的batch_size 後面是讀取的張量 28*28*1
x = tf.reshape(x, shape=[-1, 28, 28, 1]) # 把784的向量轉換爲[28,28,1]的矩陣,通道數爲1 就是黑白圖像
conv1 = conv2d(x, W['wc1'], b['bc1']) #第一次卷積完 feature map 是 128*26*26*32 x[28*28*1] w[5*5*1] (28-5+2)/1+1=26 用了32個卷積核 batch=128 所以爲128*26*26*32
pool1 = maxpool2d(conv1) #第一次 池化層 降採樣 完 是 128*14*14*32 池化操作與w無關, feature map[26*26] ksize[2*2] stride=2 (26-2+2)/2+1=14 batch=128 所以降維爲128*14*14*32
conv2 = conv2d(pool1, W['wc2'], b['bc2']) #第一次卷積完 feature map 是 128*12*12*64
pool2 = maxpool2d(conv2) #128*7*7*64
print(pool2.get_shape().as_list())
pool2 = tf.reshape(pool2, shape=[batch_size, -1]) #128*3136 -1表示懶得計算由程序根據其他位置的數計算 本來是[128,7,7,64] 7*7*64=3136 所以爲[128,3136]
print(pool2.shape)
dim = pool2.shape.as_list()
Weight_fc = tf.Variable(tf.random_normal([int(dim[1]), 1024])) # [3136,1024] 後一個參數爲輸出全連接層的輸出,一般設爲2的n次方
bia_fc = tf.Variable(tf.random_normal([1024]))
fc = tf.nn.relu(tf.add(tf.matmul(pool2, Weight_fc), bia_fc)) # [128,1024]
fc = tf.nn.dropout(fc, dropout) #根據設置的保留率 進行drop out
out = tf.add(tf.matmul(fc, W['out']), b['out'])
return out
Weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), # 5*5*1 32個卷積核
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), # 5*5*32 64個卷積核
'out': tf.Variable(tf.random_normal([1024, 10])) #
}
bias= {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'out': tf.Variable(tf.random_normal([10]))
}
logits = inference(xs, Weights, bias, keep_prob)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=ys) # 最後全連接輸出層輸出後 是softmax進行歸一化變成概率 + 交叉熵損失函數
loss = tf.reduce_mean(cross_entropy) #求平均
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #反向傳播 梯度下降 根據學習率優化 adam優化方法
#兩行配合 計算準確率
corect_prediction = tf.equal(tf.argmax(ys, 1), tf.argmax(logits, 1)) # 利用tf.argmax()按行求出真實值ys、預測值logits最大值的下標,用tf.equal()求出真實值和預測值相等的數量,也就是預測結果正確的數量
acc = tf.reduce_mean(tf.cast(corect_prediction, tf.float32)) # tf.cast 強制類型轉換
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(num_step):
x_input, y_input = minst.train.next_batch(batch_size) #按照設定的batch 每次讀取
sess.run(train_op, feed_dict={xs: x_input, ys: y_input, keep_prob: drop_out})
if i % display_step == 0 or i == 1:
ac, los = sess.run([acc, loss], feed_dict={xs: x_input, ys: y_input, keep_prob: 1})
print("acc is "+"{:.3f}".format(ac)+" loss is "+"{:.3f}".format(los))
print('over')
test_ac = sess.run(acc, feed_dict={xs: minst.test.images[:128], ys: minst.test.labels[:128], keep_prob: 1})
print("test acc is ", test_ac)
這篇講的很詳細
https://blog.csdn.net/u010312436/article/details/78616918
tensorflow 保存模型時
只能保存參數,不能保存整個神經網絡
所以保存後,restore時,還需要定義整個網絡,然後再restore把參數放進來
完成mnist手寫體識別
參見 ComputerVision - - - > AI-woniu - - - > MINST_TheNeuralNetwork
訓練完後,保存參數
先對自己上傳的圖片 進行 灰度處理,像素裁剪到28*28 再進行二值化處理
再進行test測試
常用網絡模型
RNN
神經元與上下都有關係
使用w3 來保留中間這層的信息