3.tensorflow——NN
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data numClasses=10
inputsize=784
numHiddenUnits=50
trainningIterations=50000#total steps
batchSize=64# #1.dataset
mnist=input_data.read_data_sets('data/',one_hot=True)
############################################################
#2.tarin
X=tf.placeholder(tf.float32,shape=[None,inputsize])
y=tf.placeholder(tf.float32,shape=[None,numClasses])
#2.1 initial paras
#y1=X*W1+B1
W1=tf.Variable(tf.truncated_normal([inputsize,numHiddenUnits],stddev=0.1))
B1=tf.Variable(tf.constant(0.1),[numHiddenUnits])
#y=y1*W2+B2
W2=tf.Variable(tf.truncated_normal([numHiddenUnits,numClasses],stddev=0.1))
B2=tf.Variable(tf.constant(0.1),[numClasses])
#layers
hiddenLayerOutput=tf.nn.relu(tf.matmul(X,W1)+B1)
finalOutput=tf.nn.relu(tf.matmul(hiddenLayerOutput,W2)+B2) #2.2 tarin set up
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=finalOutput))
opt=tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)
correct_prediction=tf.equal(tf.argmax(finalOutput,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #2.3 run tarin
sess=tf.Session()
init=tf.global_variables_initializer()
sess.run(init)
for i in range(trainningIterations):
batch=mnist.train.next_batch(batchSize)
batchInput=batch[0]
batchLabels=batch[1]
sess.run(opt,feed_dict={X:batchInput,y:batchLabels})
if i%1000 == 0:
train_accuracy=sess.run(accuracy,feed_dict={X:batchInput,y:batchLabels})
print("step %d, tarinning accuracy %g" % (i,train_accuracy)) #2.4 run test to accuracy
batch=mnist.test.next_batch(batchSize)
testAccuracy=sess.run(accuracy,feed_dict={X:batch[0],y:batch[1]})
print("test accuracy %g" % (testAccuracy))
输出结果:
step 0, tarinning accuracy 0.171875
step 1000, tarinning accuracy 0.84375
step 2000, tarinning accuracy 0.953125
step 3000, tarinning accuracy 0.84375
step 4000, tarinning accuracy 0.953125
step 5000, tarinning accuracy 1
step 6000, tarinning accuracy 0.984375
step 7000, tarinning accuracy 1
step 8000, tarinning accuracy 0.984375
step 9000, tarinning accuracy 1
step 10000, tarinning accuracy 1
step 11000, tarinning accuracy 0.96875
step 12000, tarinning accuracy 1
step 13000, tarinning accuracy 0.96875
step 14000, tarinning accuracy 1
step 15000, tarinning accuracy 0.984375
step 16000, tarinning accuracy 0.953125
step 17000, tarinning accuracy 1
step 18000, tarinning accuracy 1
step 19000, tarinning accuracy 1
step 20000, tarinning accuracy 1
step 21000, tarinning accuracy 1
step 22000, tarinning accuracy 1
step 23000, tarinning accuracy 1
step 24000, tarinning accuracy 1
step 25000, tarinning accuracy 1
step 26000, tarinning accuracy 1
step 27000, tarinning accuracy 1
step 28000, tarinning accuracy 1
step 29000, tarinning accuracy 1
step 30000, tarinning accuracy 1
step 31000, tarinning accuracy 1
step 32000, tarinning accuracy 1
step 33000, tarinning accuracy 1
step 34000, tarinning accuracy 1
step 35000, tarinning accuracy 1
step 36000, tarinning accuracy 1
step 37000, tarinning accuracy 1
step 38000, tarinning accuracy 1
step 39000, tarinning accuracy 1
step 40000, tarinning accuracy 0.984375
step 41000, tarinning accuracy 1
step 42000, tarinning accuracy 1
step 43000, tarinning accuracy 1
step 44000, tarinning accuracy 1
step 45000, tarinning accuracy 1
step 46000, tarinning accuracy 1
step 47000, tarinning accuracy 1
step 48000, tarinning accuracy 1
step 49000, tarinning accuracy 1
test accuracy 0.984375
3.tensorflow——NN的更多相关文章
- tensorflow.nn.bidirectional_dynamic_rnn()函数的用法
在分析Attention-over-attention源码过程中,对于tensorflow.nn.bidirectional_dynamic_rnn()函数的总结: 首先来看一下,函数: def bi ...
- Tensorflow.nn 核心模块详解
看过前面的例子,会发现实现深度神经网络需要使用 tensorflow.nn 这个核心模块.我们通过源码来一探究竟. # Copyright 2015 Google Inc. All Rights Re ...
- Tensorflow学习笔记(2):tf.nn.dropout 与 tf.layers.dropout
A quick glance through tensorflow/python/layers/core.py and tensorflow/python/ops/nn_ops.pyreveals t ...
- 『PyTorch x TensorFlow』第八弹_基本nn.Module层函数
『TensorFlow』网络操作API_上 『TensorFlow』网络操作API_中 『TensorFlow』网络操作API_下 之前也说过,tf 和 t 的层本质区别就是 tf 的是层函数,调用即 ...
- tensorflow 手写数字识别
https://www.kaggle.com/kakauandme/tensorflow-deep-nn 本人只是负责将这个kernels的代码整理了一遍,具体还是请看原链接 import numpy ...
- tensorflow项目构建流程
https://blog.csdn.net/hjimce/article/details/51899683 一.构建路线 个人感觉对于任何一个深度学习库,如mxnet.tensorflow.thean ...
- tensorflow代码中的一个bug
tensorflow-gpu版本号 pip show tensorflow-gpu Name: tensorflow-gpu Version: 1.11.0 Summary: TensorFlow i ...
- tensorflow中的sequence_loss_by_example
在编写RNN程序时,一个很常见的函数就是sequence_loss_by_example loss = tf.contrib.legacy_seq2seq.sequence_loss_by_examp ...
- TensorFlow API 汉化
TensorFlow API 汉化 模块:tf 定义于tensorflow/__init__.py. 将所有公共TensorFlow接口引入此模块. 模块 app module:通用入口点脚本. ...
随机推荐
- 《JAVA设计模式》之责任链模式(Chain of Responsibility)
在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...
- Linux系统的镜像文件iso下载地址
CentOS-6.1-x86_64-bin-DVD1.iso 官方网址:http://archive.kernel.org/centos-vault/6.1/isos/x86_64/ 下载链接地址:h ...
- final-finally-finalize有什么区别
一.final 1.final用于声明属性.方法和类,分别表示属性不可变,方法不可覆盖类和类不可能被继承(不可能再派生出新的子类). final属性:被final修饰的变量不可变. 1).引用不可变 ...
- mt-picker 样式修改
// html : <div class="applyInformations" @click="chooseSex"> <p>性别&l ...
- Android手机、电视(盒子) 打开ADB调试 一览表
手机.电视(盒子) 打开ADB调试 一览表 一.手机打开ADB调试方法 序号 名称 描述 方式 1 华为手机 EMUI 1.设置 ->关于手机-> 版本号 点击(4~5次)2.返回设置 - ...
- centos7配置sudo免密
1.chmod +w /etc/sudoers 2.vim /etc/sudoers 在已经有了的root下面加 username ALL=NOPASSWD:ALL (这是所有的命 ...
- c# UDP分包发送
考虑到UDP的高速和其他协议的复杂性,做了一个依靠时间发送的分包组包重发的UDP库. https://github.com/jinyuttt/UDPTTL.git
- MapReduce-WordCountDemo
/** * @Author: dreamer Q * @Date: 2019/11/4 22:26 * @Version 1.0 * @Discription 使用MapReduce 开发 WordC ...
- 2018-5-20-C#-BBcode-转-Markdown
title author date CreateTime categories C# BBcode 转 Markdown lindexi 2018-05-20 14:58:57 +0800 2018- ...
- docker运行模式图
docker运行模式图: