MNIST資料集手寫數字識別的一點小問題

2021-09-27 09:57:38 字數 3000 閱讀 1392

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

#載入資料集

mnist = input_data.

read_data_sets

("mnist_data"

,one_hot=true)

#批次大小並計算批次數

batch_size =

100n_batch = mnist.train.num_examples // batch_size

x = tf.

placeholder

(tf.float32,

[none,

784]

)y = tf.

placeholder

(tf.float32,

[none,10]

)#神經網路

#第一層

weights_l1 = tf.

variable

(tf.

zeros([

784,

300]))

biases_l1 = tf.

variable

(tf.

zeros([

300]))

wx_plus_b_l1 = tf.

matmul

(x,weights_l1)

+biases_l1

l1= tf.nn.

tanh

(wx_plus_b_l1)

#第二層

weights_l2 = tf.

variable

(tf.

zeros([

300,10]

))biases_l2 = tf.

variable

(tf.

zeros([

10]))

wx_plus_b_l2 = tf.

matmul(l1

,weights_l2)

+biases_l2

prediction = tf.nn.

tanh

(wx_plus_b_l2)

loss = tf.

reduce_mean

(tf.

square

(y-prediction)

)train_step = tf.train.

gradientdescentoptimizer

(0.2).

minimize

(loss)

init = tf.

global_variables_initializer()

correct_prediction = tf.

equal

(tf.

argmax

(y,1

),tf.

argmax

(prediction,1)

)#argmax返回一維張量中最大的值所在位置。

#準確率

accuracy = tf.

reduce_mean

(tf.

cast

(correct_prediction,tf.float32)

)with tf.

session()

as sess:

sess.

run(init)

for epoch in

range(21

):for batch in

range

(n_batch)

: batch_xs,batch_ys = mnist.train.

next_batch

(batch_size)

sess.

run(train_step,feed_dict=

) acc = sess.

run(accuracy,feed_dict=

)print

("iter :"

+str

(epoch)

+",testing accuracy :"

+str

(acc)

)

問題出在這段**上

weights_l1 = tf.

variable

(tf.

zeros([

784,

300]))

biases_l1 = tf.

variable

(tf.

zeros([

300]))

wx_plus_b_l1 = tf.

matmul

(x,weights_l1)

+biases_l1

l1= tf.nn.

tanh

(wx_plus_b_l1)

#第二層

weights_l2 = tf.

variable

(tf.

zeros([

300,10]

))biases_l2 = tf.

variable

(tf.

zeros([

10]))

wx_plus_b_l2 = tf.

matmul(l1

,weights_l2)

+biases_l2

prediction = tf.nn.

tanh

(wx_plus_b_l2)

如果權值是上面這樣的使用全零矩陣,輸出結果會卡在0.1135

如果改為

weights_l1 = tf.

variable

(tf.

truncated_normal([

784,

500]

, stddev=

0.1)

就可以正常進行訓練了

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...