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:通用入口点脚本. ...
随机推荐
- LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...
- 关于JSON.stringify()与JSON.parse()
一.JSON.stringify()与JSON.parse()的区别 JSON.stringify()的作用是将js值转换成JSON字符串,而JSON.parse()是将JSON字符串转换成一个对象. ...
- ls命令输出文件的绝对路径
find $PWD | xargs ls -ld 再结合 grep 筛选
- eclipse安装weblogic Server服务器
1.首先打开eclipse,第一次进入欢迎画面点击上方标签X,关闭欢迎标签 2.关闭欢迎标签后,进入eclipse操作界面,在上方的菜单栏,选择windows下拉菜单,选择子菜单Preference ...
- maven私服nexus3.9安装配置
maven私服nexus3.9安装配置 私服介绍 私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构建.有了私服之后,当 Maven 需要下载构件时,直接请求私服 ...
- rpmbuild - 构建 RPM 打包
SYNOPSIS 构建打包: rpmbuild {-ba|-bb|-bp|-bc|-bi|-bl|-bs} [rpmbuild-options] SPECFILE ... rpmbuild {-ta| ...
- CentOS7.6系统安装zabbix3.4.8客户端
一. 准备安装包 将本地的zabbix-3.4.8软件包上传至服务器, 二. 安装依赖包 安装依赖包:yum install gcc* pcre* psmisc -y 三. 安 ...
- 390-基于Zynq UltraScale+ MPSoC的单板嵌入式计算机
基于Zynq UltraScale+ MPSoC的单板嵌入式计算机 概述:Aldec TySOM-3-ZU7EV,将Xilinx Zynq UltraScale+ ZU7EV MPSoC以及DDR4 ...
- 无法用另一台电脑上的navicat链接主机数据库lost connection toMYSQl server at "handshake":reading inital communication packet,system error:34
同事要用navicat登陆我的数据库,主机地址和密码都没错,但是报错,lost connection toMYSQl server at "handshake":reading i ...
- 【抓包工具之Fiddler】导出jmeter脚本
一.下载完成后,解压压缩包,将插件中的2个文件放入到Fiddler安装目录中 插件目录 二.打开fiddler,设置测试时过滤的条件(抓包可以指定域名的请求) 设置过滤条件 三.这里我 ...