当前位置: 首页 > 编程日记 > 正文

tensorflow mnist 1

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import keras.backend.tensorflow_backend as KTFdef add_layer(inputs,in_size,out_size,activation_function=None):#Weights是一个矩阵,[行,列]为[in_size,out_size]Weights=tf.Variable(tf.random_normal([in_size,out_size]))#正态分布#初始值推荐不为0,所以加上0.1,一行,out_size列biases=tf.Variable(tf.zeros([1,out_size])+0.1)#Weights*x+b的初始化的值,也就是未激活的值Wx_plus_b=tf.matmul(inputs,Weights)+biases#激活if activation_function is None:#激活函数为None,也就是线性函数outputs=Wx_plus_belse:outputs=activation_function(Wx_plus_b)return outputsdef compute_accuracy(prediction, xs, ys, sess, v_xs,v_ys):y_pre=sess.run(prediction,feed_dict={xs:v_xs})correct_prediction=tf.equal(tf.arg_max(y_pre,1),tf.arg_max(v_ys,1))accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))result=sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})return resultdef train_test_mnist():mnist=input_data.read_data_sets('MNIST_data',one_hot=True)# define placeholder for inputs to networks# 不规定有多少个sample,但是每个sample大小为784(28*28)xs=tf.placeholder(tf.float32,[None,784])ys=tf.placeholder(tf.float32,[None,10])#add output layerprediction=add_layer(xs,784,10,activation_function=tf.nn.softmax)#the error between prediction and real datacross_entropy=tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))train_strp=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)config = tf.ConfigProto()config.gpu_options.allow_growth = True  # 不全部占满显存, 按需分配config.gpu_options.per_process_gpu_memory_fraction = 0.6  #限制GPU内存占用率init=tf.global_variables_initializer()sess = tf.Session(config=config)KTF.set_session(sess)  # 设置sessionif True:#with tf.Session() as sess:sess.run(init)for i in range(2000):batch_xs,batch_ys=mnist.train.next_batch(100)sess.run(train_strp,feed_dict={xs:batch_xs,ys:batch_ys})if i%20==0:print("accuracy:", compute_accuracy(prediction, xs, ys, sess, mnist.test.images, mnist.test.labels))def train_test_mnist_visual():#define placeholder for inputs to networkxs=tf.placeholder(tf.float32,[None,64])ys=tf.placeholder(tf.float32,[None,10])#add output layer# l1为隐藏层,为了更加看出overfitting,所以输出给了100l1=add_layer(xs,64,100,'l1',activation_function=tf.nn.tanh)prediction=add_layer(l1,100,10,'l2',activation_function=tf.nn.softmax)def main():train_test_mnist()if __name__ == '__main__':main()

相关文章:

framework7使用笔记

2019独角兽企业重金招聘Python工程师标准>>> myApp.addView(.view-main, {}); 以上这句代码一定要添加 ,否则链接的页面不能正常加载 --------------------------------------------- 如果初始化时定义了preprocess,则页面上链接的自动加载将…

Linux:检查当前运行级别的五种方法

2019独角兽企业重金招聘Python工程师标准>>> 运行级就是Linux操作系统当前正在运行的功能级别。存在七个运行级别,编号从0到6。系统可以引导到任何给定的运行级别。运行级别由数字标识。每个运行级别指定不同的系统配置,并允许访问不同的进程…

概率机器人资料整理

机器人算法仿真 https://atsushisakai.github.io/PythonRobotics/ 最大熵对应的概率分布及其优化推导 https://www.cnblogs.com/yychi/p/9401807.html 矩阵计算网址 http://www.yunsuan.info/matrixcomputations/index.html

LLVM官方文档翻译---- LLVM原子指令与并发指引

英文原文地址:http://llvm.org/docs/Atomics.html 译文原文地址:http://blog.csdn.net/wuhui_gdnt/article/details/52485591 注:该文章转载已经得到译者授权。 --------------------------------------------------------------------------…

matplotlib画图

import matplotlib.pyplot as plt import numpy as npdef test1():# 从[-1,1]中等距去50个数作为x的取值x np.linspace(-1, 1, 50)print(x)y 2*x 1y1 2**x 1# 第一个是横坐标的值,第二个是纵坐标的值plt.plot(x, y)plt.plot(x, y1)# 必要方法,用于将…

学习新对象字面量语法

目标 使用简写属性名称使用简写方法名称使用计算属性名称问题 哪些部分是冗余的? const person{name:name,address:address,sayHello:function(){/*......*/},sayName:function(){/*......*/}sayAddress:function(){/*......*/}} 复制代码简写属性名称 //ES5 const message{te…

ORB-SLAM2代码/流程详解

ORB-SLAM2代码详解 文章目录ORB-SLAM2代码详解1. ORB-SLAM2代码详解01_ORB-SLAM2代码运行流程1 运行官方Demo1.2. 阅读代码之前你应该知道的事情1.2.1 变量命名规则1.3 理解多线程1.3.1 为什么要使用多线程?1.3.2 多线程中的锁1.4 SLAM主类System1.4.1 System类是ORB-SLAM2系统…

VS2010 CUDA 5.5 Win7 64位配置以及项目创建配置

一.安装CUDA5.5以及配置VS助手 1、安装之前必须确认自己电脑的GPU支持CUDA。在设备管理器中找到显示适配器(Display adapters),找到自己电脑的显卡型号,如果包含在http://www.nvidia.com/object/cuda_gpus.html的列表中,说明支持…

HTC VIVE SDK 中的例子 hellovr_opengl 程序流程分析

最近Vive的VR头盔设备很火,恰逢项目需求,所以对 SDK 中的例子 hellovr_opengl 做了比较细致的代码分析,先将流程图绘制如下,便于大家理解。 在ViVe头盔中实现立体效果的技术核心: 如果要外挂Vive的VR设备实现立体效果&…

Proximal Algorithms 4 Algorithms

目录 Proximal minimization解释\(f(x) g(x)\)解释1 最大最小算法不动点解释Forward-backward 迭代解释加速 proximal gradient method交替方向方法 ADMM解释1 自动控制解释2 Augmented Largranians解释3 Flow interpretation解释4 不动点特别的情况 \(f(x) g(Ax)\)Proximal …

C# TripleDES NoPadding 时对待加密内容进行补字节(8个字节为一个Block)

补一个空格(半角): private static byte[] FormatData(String str) {var yu str.Length % 8;if (yu 0) return Encoding.GetEncoding(Consts.Charset).GetBytes(str);var size 8 - yu;var arr new byte[str.Length size];var data Enco…

keras Regressor 回归

import numpy as np np.random.seed(1337) # for reproducibility from keras.models import Sequential from keras.layers import Dense import matplotlib.pyplot as plt # 可视化模块import tensorflow as tf import keras.backend.tensorflow_backend as KTF# create som…

13、JsonResponse响应介绍

转载于:https://blog.51cto.com/yht1990/2406566

keras Classifier 分类

import numpy as np np.random.seed(1337) # for reproducibility from keras.models import Sequential from keras.layers import Dense, Activation from keras.optimizers import RMSprop import matplotlib.pyplot as plt # 可视化模块import tensorflow as tf import ke…

如何管理好自己的性格?

往往因为我们太感性,而获得与男人不一样的灵动的感受。而当过分的感性不合时宜地在职业生涯中表现出来时,我们该怎么调整自己呢?  由于女人与生俱来的特点,我们善良、有耐心,所以我们更易得到别人的支持和帮助&#…

Axis2 webservice入门--Webservice的发布与调用

一、Webservice发布 参考 http://www.cnblogs.com/demingblog/p/3263576.html 二、webservice 调用 部分参考:http://www.cnblogs.com/demingblog/p/3264688.html 使用myeclipse中的axis2插件生成客户端代码 new -->others到如下界面: 点next 到如下界…

Java断点续传(基于socket与RandomAccessFile的实现)

这是一个简单的C/S架构,基本实现思路是将服务器注册至某个空闲端口用来监视并处理每个客户端的传输请求。 客户端先获得用户给予的需传输文件与目标路径,之后根据该文件实例化RandomAccessFile为只读,之后客户端向服务器发送需传输的文件名文…

EJB调用原理分析

EJB调用原理分析 作者:robbin (MSN:robbin_fan AT hotmail DOT com) 版权声明:本文严禁转载,如有转载请求,请和作者联系 一个远程对象至少要包括4个class文件:远程对象;远程对象的接口;实现远程…

Jfinal Generator 不需要生成带某个前缀的表名数组的方法

2019独角兽企业重金招聘Python工程师标准>>> package com.demo.common.model; import javax.sql.DataSource; import com.jfinal.kit.PathKit; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.generato…

tensorflow 2

import tensorflow as tf import numpy as npdef test1():#create datax_datanp.random.rand(100).astype(np.float32)y_datax_data*0.10.3#create tensorflow structureWeightstf.Variable(tf.random_uniform([1],-1.0,1.0)) #一维,范围[-1,1]biasestf.Variable(tf…

PCB多层线路板打样难点

PCB多层板无论从设计上还是制造上来说,都比单双层板要复杂,一不小心就会遇到一些问题,那在PCB多层线路板打样中我们要规避哪些难点呢?  1、层间对准的难点  由于多层电路板中层数众多,用户对PCB层的校准要求越来越…

GARFIELD@11-07-2004

Vanity Fair转载于:https://www.cnblogs.com/rexhost/archive/2004/11/07/61286.html

python文件读写1

# -*- coding: utf-8 -*-# read txt file def readTextFile(file):f open(file, r)# 尽可能多的读取文件的内容,一般会将整个文件内容都会读取context f.read() print(context)f.close()def readTextFileByLines(file):f open(file, "r")lines f.read…

jfinal框架下使用c3P0连接池连接sql server 2008

2019独角兽企业重金招聘Python工程师标准>>> 闲话少说 进入正题 首先是工程需要的jar包 然后是c3p0的配置文件。我是这样配置的 仅供参考 jdbcDriver com.microsoft.sqlserver.jdbc.SQLServerDriver jdbcUrl jdbc:sqlserver://localhost:7777;databaseNametest us…

mongodb插入文档时不传ObjectId

type BookExt struct {ID bson.ObjectId bson:"_id"Title string bson:"title"SubTitle string bson:"subTitle"Author string bson:"author" } 以上结构体,在通过此结构体对象作为参数传入Insert插入…

[问题]DotNet 项目如何实现在构建时 Build 号自动增加?

[问题]DotNet 项目如何实现在构建时 Build 号自动增加? 继续昨天的问题,今天在Google上找了一下,没有找到很好的方案。目前找到的解决方案有以下几种:1.使用一个地三方的 VS.Net 插件,实现在编译时 Build 号自动增加&a…

编写程序记录文件位置

当我们编写程序是会注意到,首先是配置一些函数的结构体。 所以我们就要找到下面的界面,然后打开FWLB中.c文件下面所对应的.h文件,这样就能查找到相应的结构体。下图为我所找到的中断的结构体、 然后就是查找相对应的中断向量。具体就是打开 还…

mnist数据集保存为图片

#coding: utf-8 from tensorflow.examples.tutorials.mnist import input_data import scipy.misc import os import numpy as np# 读取MNIST数据集。如果不存在会事先下载。 mnist input_data.read_data_sets("MNIST_data/", one_hotTrue)# 我们把原始图片保存在MN…

Python3数据分析与挖掘建模实战

<div>课程地址&#xff1a;http://icourse8.com/Python3_shujufenxi.html</div>复制代码第1章 课程介绍【赠送相关电子书随堂代码】 第2章 数据获取 第3章 单因子探索分析与数据可视化 第4章 多因子探索分析 第5章 预处理理论 第6章 挖掘建模 第7章 模型评估 第8章…

tensorflow生成对抗网络

import tensorflow as tf import numpy as np import os from tensorflow.examples.tutorials.mnist import input_data from matplotlib import pyplot as pltBATCH_SIZE 64 UNITS_SIZE 128 LEARNING_RATE 0.001 EPOCH 300 SMOOTH 0.1print("mnist手写体生成对抗网络…