资源简介
MOOC上北大老师讲的《Tensorflow笔记》里的手写体识别代码,初学者食用,无误版。
代码片段和文件信息
#建“mnist_forward.py”
# mnist_forward.py
import tensorflow as tf
INPUT_NODE = 784
OUTPUT_NODE = 10
layer1_NODE = 500
def get_weight(shape regularizer):
w = tf.Variable(tf.truncated_normal(shape stddev=0.1))
if regularizer != None:
tf.add_to_collection(‘losses‘ tf.contrib.layers.l2_regularizer(regularizer)(w))#把正则化加入到losses里面
return w
def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b
def forward(x regularizer):
w1 = get_weight([INPUT_NODE layer1_NODE] regularizer)
b1 = get_bias([layer1_NODE])
y1 = tf.nn.relu(tf.matmul(x w1) + b1)
w2 = get_weight([layer1_NODE OUTPUT_NODE] regularizer)
b2 = get_bias([OUTPUT_NODE])
y = tf.matmul(y1 w2) + b2
return y
#从此处重新建一个“mnist_backward.py“
# mnist_backward.py
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os
BATCH_SIZE = 200
LEARNING_RATE_base = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 50000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = “./model/“
MODEL_NAME = “mnist_model“
def backward(mnist):
x = tf.placeholder(tf.float32 [None mnist_forward.INPUT_NODE])
y_ = tf.placeholder(tf.float32 [None mnist_forward.OUTPUT_NODE])
y = mnist_forward.forward(x REGULARIZER)
global_step = tf.Variable(0 trainable=False)
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y labels=tf.argmax(y_ 1))
cem = tf.reduce_mean(ce)
loss = cem + tf.add_n(tf.get_collection(“losses“))
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_base
global_step
mnist.train.num_examples / BATCH_SIZE
LEARNING_RATE_DECAY
staircase=True)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss global_step=global_step)
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step ema_op]):
train_op = tf.no_op(name=“train“)
saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
for i in range(STEPS):
xs
- 上一篇:某网Python3.6+电商实战+Vue+Django
- 下一篇:文本查重系统
相关资源
- TensorFlow实现股票预测的Python代码
- Tensorflow实现GAN生成mnist手写数字图片
- Anaconda3-5.2.0-Windows-x86_64 .exe
- python+tensorflow的yolo实现代码
- word2vec.py
- 基于selective_search对手写数字串进行分
- pb模型文件进行前向预测亲测可用
- tensorflow样例 BP神经网络
- TensorFlow usb摄像头视频目标检测代码
- tensorflow_gpu-2.3.1-cp37-cp37m-win_amd64.whl
- python3使用tensorflow构建CNN卷积神经网络
- tensorflow糖尿病数据二分类python代码
- 神经网络-二分类问题(IMDB) Keras
- win7 32位系统下tensorflow的安装,以及在
- Tensorflow练习1对电影评论进行分类
- tensorflow手写数字识别python源码案例
- Tensorflow之CNN实现CIFAR-10图像的分类p
- cython_bbox.so
- TensorFlow实战中实现word2vec代码含中文
- opencv_tensorflow
- tensorflow2.0实现mnist手写数字识别代码
- Python-TensorFlow语义分割组件
- tensorflow_random_forest_demo.py
- Tensorflow-BiLSTM分类
- TensorFlow实现人脸识别(3)--------对人
- Python-手势识别使用在TensorFlow中卷积神
- Python+Tensorflow+CNN实现车牌识别的
- 基于TensorFlow实现的闲聊机器人
- CBAM_MNIST.py
- TensorFlow 实现 Yolo
评论
共有 条评论