MNIST手寫數字識別 tensorflow

2021-09-25 18:24:31 字數 1252 閱讀 5691

神經網路一半包含三層,輸入層、隱含層、輸出層。如下圖所示:

現以手寫數字識別為例:

輸入為784個變數,輸出為10個節點,10個節點再通過softmax啟用函式轉化為**值。

**如下,準確率可達0.9226

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("mnist_data/", one_hot=true)

sess = tf.interactivesession()

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

w = tf.variable(tf.zeros([784, 10]))

b = tf.variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,w) + b)

y_ = tf.placeholder(tf.float32, [none, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.gradientdescentoptimizer(0.1).minimize(cross_entropy)

tf.global_variables_initializer().run()

for i in range(10000):

batch_xs, batch_ys = mnist.train.next_batch(128)

train_step.run()

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

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

print(accuracy.eval())

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

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

基於MNIST的手寫數字識別

1 mnist 資料資料集獲取 方式一 使用 tf.contrib,learn 模組載入 mnist 資料集 棄用 如下 使用 tf.contrib.learn 模組載入 mnist 資料集 deprecated 棄用 import tensorflow as tf from tensorflow....