MNIST手寫數字識別 分類應用入門

2021-09-28 18:36:14 字數 3384 閱讀 1656

#%%

import tensorflow as tf

import numpy as np

#%%from tensorflow import placeholder

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("f:\python\data\mnist_data/", one_hot=true)

#%%#mnist 中每張共有28*28=784個畫素點

x = tf.placeholder(tf.float32, [none, 784], name="x")

#0-9 一共10個數字=>10個類別

y = tf.placeholder(tf.float32, [none, 10], name="y")

#%%#定義變數

w = tf.variable(tf.random_normal([784, 10]), name="w")

b = tf.variable(tf.zeros([10]), name="b")

#%%forword=tf.matmul(x, w) + b #前向計算

#%%train_epochs = 50 #訓練輪數

batch_size = 100 #單詞訓練樣本數(批次大小)

total_batch = int(mnist.train.num_examples/batch_size) #一輪訓練有多少批次

display_step = 1 #顯示粒度

learning_rate = 0.05 #學習率

#%%#結果分類

pred = tf.nn.softmax(forword) # softmax分類

#%%loss_function = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),

reduction_indices=1)) #交叉熵

#%%optimizer = tf.train.gradientdescentoptimizer(learning_rate).minimize(loss_function) #梯度下降

#%%correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

#%%accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#%%arr1 = np.array([1,3,2,5,7,0])

arr2 = np.array([[1,2,3],[3,2,1],[4,7,2],[8,3,2]])

print("arr1=",arr1)

print("arr2=\n",arr2)

#%%sess = tf.session()

init = tf.global_variables_initializer()

sess.run(init)

#%%for epoch in range(train_epochs):

for batch in range(total_batch):

xs, ys = mnist.train.next_batch(batch_size)

sess.run(optimizer,feed_dict=)

loss,acc = sess.run([loss_function, accuracy],

feed_dict=)

if (epoch+1) % display_step == 0:

print("train epoch:",'%02d' % (epoch+1), "loss=","".format(loss),\

"accuracy=","".format(acc))

print("train finished")

#%%accu_test = sess.run(accuracy,

feed_dict=)

print("test accuracy:",accu_test)

#%%accu_validation = sess.run(accuracy,

feed_dict=)

print("test accuracy:",accu_validation)

#%%accu_train = sess.run(accuracy,

feed_dict=)

print("test accuracy:",accu_train)

#%%#進行**

prediction_result = sess.run(tf.argmax(pred,1),

feed_dict=)

#檢視**結果中的前10項

prediction_result[0:10]

#%%#定義視覺化函式

import matplotlib.pyplot as plt

def plot_images_labels_prediction(images,

labels,

prediction,

index,

num=10):

fig = plt.gcf() #獲取當前圖示,get current figure

fig.set_size_inches(10, 12) #1英吋等於2.54cm

if num > 25:

num = 25 #最多顯示25個子圖

for i in range(0, num):

ax = plt.subplot(5,5, i+1)

ax.imshow(np.reshape(images[index],(28,28)),

cmap='binary')

title = "label=" + str(np.argmax(labels[index]))

if len(prediction)>0:

title = "label=" +str(np.argmax(labels[index]))

ax.set_title(title,fontsize=10)

ax.set_xticks()

ax.set_yticks()

index += 1

plt.show()

#%%plot_images_labels_prediction(mnist.test.images,

mnist.test.labels,

prediction_result,10,10)

#%%plot_images_labels_prediction(mnist.test.images,

mnist.test.labels,

prediction_result,10,25)

#%%

mnist手寫數字識別

import tensorflow as tf import numpy as np from tensorflow.contrib.learn.python.learn.datasets.mnist import read data sets mnist read data sets f pyth...

MNIST手寫數字識別 tensorflow

神經網路一半包含三層,輸入層 隱含層 輸出層。如下圖所示 現以手寫數字識別為例 輸入為784個變數,輸出為10個節點,10個節點再通過softmax啟用函式轉化為 值。如下,準確率可達0.9226 import tensorflow as tf from tensorflow.examples.tu...

DNN識別mnist手寫數字

提取碼 sg3f 導庫import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers...