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:通用入口点脚本. ...
随机推荐
- vsphere虚拟化之 NTP服务的创建(三)
1.先修改windows 2012的注册表. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\ 设置 Annou ...
- Phone-java标准类
//project-module-package //.代表包的目录层次 package cn.learn.day01.demo01; /* 1.类是一组相关属性(成员变量)与行为(方法)的集合,对象 ...
- 2019牛客暑期多校训练营(第二场) - F - Partition problem - 枚举
https://ac.nowcoder.com/acm/contest/882/F 潘哥的代码才卡过去了,自己写的都卡不过去,估计跟评测机有关. #include<bits/stdc++.h&g ...
- 问题 1436: 地宫取宝 (dp)
题目传送门 时间限制: 1Sec 内存限制: 128MB 提交: 423 解决: 94 题目描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标 ...
- P4390 [BOI2007]Mokia 摩基亚
传送门 对于一个询问 $(xa,ya),(xb,yb)$,拆成 $4$ 个询问并容斥一下 具体就是把询问变成求小于等于 $xb,yb$ 的点数,减去小于等于 $xa-1,yb$ 和小于等于 $xb,y ...
- JavaScript的变量作用域
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 我心中的ASP.NET Core 新核心对象WebHost(一)
以本系列文章向Fish 前辈的那篇我心中的ASP.NET 核心对象致敬.(虽然不知道前辈现在在干什么).一晃就6年过去了,那首 郝云 的<回到那一天>怎么唱来着? 时光一晃,你就三十了. ...
- macos Item2 添加 Shell Integration (ftp传输)
macos系统 的item2软件 的 Shell Integration (ftp传输) 功能强大,无需 安装其他ftp软件,也是为了保证 密码安全 在使用时报错如下(因为本地 ping不通): ...
- (解决某些疑难杂症)Ubuntu16.04 + NVIDIA显卡驱动 + cuda10 + cudnn 安装教程
一.NVIDIA显卡驱动 打开终端,输入: sudo nautilus 在新打开的文件夹中,进入以下路径(不要用命令行): 左下角点计算机,lib,modules 这时会有几个文件夹,对每个文件夹都进 ...
- weblogic下载
1.网址 https://edelivery.oracle.com/osdc/faces/SoftwareDelivery 2.信息