#%%
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
MNIST手写数字识别:分类应用入门
最新推荐文章于 2024-01-25 09:38:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)