tensorflow mnist 1

news/2024/7/3 0:52:44
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()

 


http://lihuaxi.xjx100.cn/news/250393.html

相关文章

数据库的概念以及MYSQL的安装和卸载

一、数据库的基本概念: 1、什么是数据库? DataBase,简称DB。是用来存储和管理数据的仓库。 2、数据库的特点: 持久化存储数据的。其实数据库就是一个文件系统。方便存储和管理数据使用了统一的方式操作数据库——SQL  3、最热门…

玉山银行的一名新员工“玉山小i随身金融顾问”

市场竞争、监管变化、客户体验一直在对金融行业发起挑战,所以无论监管、竞争、客户都会影响金融行业在成本和服务上的创新,金融行业越来越多的开始利用人工智能去满足现有发展提出的要求。 台湾玉山银行的数字化转型就是一个很好的例子。台湾有一句顺口溜…

电子学会青少年编程等级考试Python二级题目解析01

Python二级题目解析 1、题目 原有列表s [5, 2, 9, 1],下列哪个操作不能使得列表s变为[9, 5, 2, 1]?( )【2021.06】 A. s.sort() s.reverse() print(s)B. s.sort(reverseTrue) print(s)C. print(sorted(s, reverseTrue))D. …

Use MVS Dsbame convensions. windows下ftp.exe客户端上传错误

环境:主机 220-FTPD1 IBM FTP CS V1R11 at BT11 windows下的ftp.exe客户端 现象:windows下ftp.exe命令上传文件,报"Use MVS Dsbame convensions." 错误 原因: 主要是IBM FTP Server 文件系统的命名规范造成的: 8个基本字符 解决: 通过 cd 根目录.目录1.目录2----…

node.js(一)

2019独角兽企业重金招聘Python工程师标准>>> 1.简介 Node.js is a platform built on Chromes JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it light…

电子学会青少年编程等级考试Python二级题目解析02

电子学会青少年编程等级考试Python二级题目解析 1、题目 执行下列代码后,运行结果是?( )【2021.06】 seq [hello, good, morning] s *.join(seq) print(s)A. hello*good*morning*B. *hello*good*morningC. hello*good*mornin…

荣之联“云桥OneBridge”让IT运维事半功倍

近日,荣之联发布“云桥OneBridge”新一代的轻量级企业用户账号管理平台,为企业提供账号全生命周期管理解决方案。“云桥OneBridge”让IT运维工作效率大幅提升,并降低人为差错率,IT运维从此事半功倍。 据介绍,北京荣之联…

用herl工具解决微信内链接或二维码可直接用外部浏览器打开

很多朋友问我怎么解决微信内点击链接或扫描二维码可以直接跳出微信在外部浏览器打开网页链接,其实这并不难,只要我们实现微信跳转功能即可。 下面给大家推荐 herl工具(http://www.nicejump.cn/) 使用步骤 1. 用浏览器打开我们的工…