tensorflow教程筆記

import tensorflow as tf
import numpy as np
import pylab as pl
from PIL import Image
from numpy import *
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
mnist=read_data_sets("MNIST/",one_hot=True)
# y=wx+b
x=tf.placeholder(tf.float32, [None,784],name="x")
w=tf.Variable(tf.zeros([784,10], name="w"))
b=tf.Variable(tf.zeros([10], tf.float32, name="b"))
y=tf.nn.softmax(tf.matmul(x,w) + b)     #結果矩陣
y_=tf.placeholder(tf.float32 , [None,10], name="y_")    #定義labels矩陣

cross_entropy = -tf.reduce_sum(y_*tf.log(y))    #交叉熵函數
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init=tf.initialize_all_variables()
sess=tf.Session()
sess.run(init)
#Train
for i in range(10000):
    batch_xs,batch_ys=mnist.train.next_batch(10)
    g=sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys})
    if i%1000==0:
        print('訓練了',i/1000+1,'')
    pass

correct_prediction=tf.equal(tf.argmax(y,1), tf.argmax(y_,1), name="validate")
print("正確率預測",correct_prediction)
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print("正確率Tensor對象",accuracy)
set_printoptions(threhold=10000)
print(sess.run(correct_prediction,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))
print("w",w,"b",b)

各種函數:python

(1)tf.nn.softmax()使用softmax函數將[0,0,0,1,0,0,0,0,0,0]平均分配《使用多項分佈》使得預測爲其餘數據的機率不等於0
算法

(2)tf.reduce_sum(arg1,arg2)使用函數求的數組的和,arg2爲None,表示在數組全部元素上面求和數組

(3)tf.train.GradientDescentOptimizer(arg1)使用梯度降低算法《須要深刻學習》函數

(4)mnist.train.next_batch(arg1) arg1指定隨機多少個圖片數組進入訓練,返回batch_xs的shape爲[arg1,784]的數組,batch_ys的shape爲[arg1,10]的數組學習

(5)tf.argmax(arg1,arg2) 求取arg1數組上面的最大值,arg2可取0《列上面最大值》、1《行上面最大值》,因爲arg1爲[0.6,0.03,···,0.02]共10列1行,因此應該取行上面最大值可獲得當前預測的結果是數字幾。spa

(6)tf.equal(arg1,arg2)比較兩個參數的值是否相同,相同返回True,不一樣返回Falsecode

(7)tf.cast(arg1,dtype)將bool類型轉化爲float32類型《可逆》對象

(8)ft.reduce_mean(arg1)求取arg1的平均值,即[1,0,1,0,0,0,0,0,0,0],20%正確率blog

<!--   以上就是對tensorflow初級mnist的理解 。 -->圖片

相關文章
相關標籤/搜索