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

tensorflow入门(二)

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt#使用numpy生成200个随机点
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise#定义两个placeholder
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])#定义神经网络中间层
Weights_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)#定义神经网络输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)#二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)with tf.Session() as sess:#变量初始化
    sess.run(tf.global_variables_initializer())for _ in range(2000):sess.run(train_step,feed_dict={x:x_data,y:y_data})#获得预测值prediction_value = sess.run(prediction,feed_dict={x:x_data})#画图
    plt.figure()plt.scatter(x_data,y_data)plt.plot(x_data,prediction_value,'r-',lw = 5)plt.show()

得到结果:

——

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784]) #图片
y = tf.placeholder(tf.float32,[None,10]) #标签#创建一个简单的神经网络
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,w)+b)#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#梯度下降算法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)#初始化变量
init = tf.global_variables_initializer()#结果存放在一个bool类型的列表中,argmax()返回一维张量中最大值所在的位置,equal函数判断两者是否相等
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率,cast:将bool型转换为0,1然后求平均正好算到是准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))with tf.Session() as sess:sess.run(init)for epoch in range(21):for batch in range(batch_size):#next_batch:不断获取下一组数据batch_xs,batch_ys = mnist.train.next_batch(batch_size)sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})print("Iter "+str(epoch)+",Testing Accuracy "+str(acc))

训练结果:

Iter 0,Testing Accuracy 0.657
Iter 1,Testing Accuracy 0.7063
Iter 2,Testing Accuracy 0.7458
Iter 3,Testing Accuracy 0.7876
Iter 4,Testing Accuracy 0.8203
Iter 5,Testing Accuracy 0.8406
Iter 6,Testing Accuracy 0.8492
Iter 7,Testing Accuracy 0.8586
Iter 8,Testing Accuracy 0.8642
Iter 9,Testing Accuracy 0.8678
Iter 10,Testing Accuracy 0.8709
Iter 11,Testing Accuracy 0.8736
Iter 12,Testing Accuracy 0.8751
Iter 13,Testing Accuracy 0.8784
Iter 14,Testing Accuracy 0.88
Iter 15,Testing Accuracy 0.8816
Iter 16,Testing Accuracy 0.883
Iter 17,Testing Accuracy 0.8841
Iter 18,Testing Accuracy 0.8847
Iter 19,Testing Accuracy 0.8855
Iter 20,Testing Accuracy 0.8878

可见,通过多次的训练,识别的准确值不断提高

——

转载于:https://www.cnblogs.com/cunyusup/p/8635309.html

相关文章:

DRF序列化和反序列化

一、自定义序列化组件 新建一个任意名的py文件,里面导入serlizerfrom rest_framework import serializers自定义一个类继承serializers,里面写需要序列化的字段方法一:继承serializers.Serializerclass BookSerlizer(serializers.Serializer)…

设计模式学习笔记-中介模式

概述: 用中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以对立地改变他们之间的…

【数据库】兴唐第二十七节课之jdbc的使用

使用jdbc修改数据库表中的信息 package java27practice;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement;public class JDBCDemo {public static void main(String[] args) {Connection conn null;Stat…

windows 2008 R2无法创建性能收集器

问题: 检查排除方法: 1.确保用户有权限 Http://technet.microsoft.com/zh-cn/library/cc749154(WS.10).aspx (参考: 2.确保 Distributed Transaction Coordinator服务 及Task Scheduler服务有启动。 3.检查 C:\Windows\System32\T…

Silverlight WCF RIA服务(二十三)Silverlight 客户端 4

DomainDataSource WCF RIA Services提供DomainDataSource控件来简化用户界面和域上下文中数据的交互。通过DomainDataSource,我们可以只是用声明性语法来检索、编辑数据。我们指定域上下文与DomainDataSource一起使用,然后通过这个上下文来调用操作。Dom…

Storm Trident示例function, filter, projection

以下代码演示function, filter, projection的使用,可结合注释 省略部分代码,省略部分可参考:https://blog.csdn.net/nickta/article/details/79666918 FixedBatchSpout spout new FixedBatchSpout(new Fields("user", "score…

解决 sh: java: command not found 问题

在执行脚本上加入如下配置即可#!/bin/bashJAVA_HOME/usr/java/jdk1.8.0_152export PATH$PATH:$JAVA_HOME/bin转载于:https://www.cnblogs.com/jimw/p/11126437.html

【数据库】 兴唐第二十七节课只sql注入

首先来一个用户登录程序 public static void login(String username, String password) {Connection conn null;Statement stat null;ResultSet rs null; try {Class.forName("com.mysql.jdbc.Driver");String url "jdbc:mysql://127.0.0.1:3306/tyrantfor…

SIEM部署的几条最佳实践

2010年11月12号,NetworkWorld发表了一篇文章——《SIEM部署的最佳实践》,业界同仁给出了他的一些建议。 这些建议主要是针对Verizon2010年的那个DBIR报告中提到的日志缺失造成的严重问题。 至于建议,主要有: 1)先要搞明…

ffmpeg解码视频存为BMP文件

ffmpeg解码视频存为BMP文件 分类&#xff1a; ffmpeg2011-07-28 12:13 8人阅读 评论(0) 收藏 举报view plain#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma once #ifdef __cplusplus extern …

(四)Asp.net web api中的坑-【api的返回值】

&#xff08;四&#xff09;Asp.net web api中的坑-【api的返回值】 原文:&#xff08;四&#xff09;Asp.net web api中的坑-【api的返回值】void无返回值IHttpActionResultHttpResponseMessage自定义类型我这里并不想赘述这些返回类型&#xff0c; 可以参考博文http://blog.c…

如何提高编程能力?

其实很多人学编程都会遇到困难&#xff0c;我觉得其中一个根本原因是他们没搞明白学编程到底是学什么。编程不是一种知识&#xff0c;而是一门手艺。我们从小到大的学习都是学习知识&#xff0c;流程一般是课前看书预习&#xff0c;上课听讲&#xff0c;下课做作业&#xff0c;…

【HTML】兴唐第二十八节课之初识HTML

1、HTML&#xff1a;hyper text markup language&#xff08;超级文本标记语言&#xff09;算编程&#xff0c;但HTML不是编程语言 2、注意&#xff1a; &#xff08;1&#xff09;所有的HTML文件都是以.html或者htm作为扩展名 &#xff08;2&#xff09;html文件需要被浏览…

Nagios插件NDOUtils安装

1.DBI的安装# wget http://www.cpan.org/modules/by-module/DBI/DBI-1.608.tar.gz # tar zxvf DBI-1.608.tar.gz # cd DBI-1.608# perl Makefile.PL# make# make test# make install2.DBD的安装# wget http://www.cpan.org/modules/by-module/DBD/DBD-mysql-4.011.tar.gz # tar…

maven 获取pom.xml的依赖---即仓库搜索服务

常用仓库地址&#xff1a; http://repository.sonatype.org/ (https://repository.sonatype.org/)如下图&#xff1a; http://www.mvnrepository.com 转载于:https://www.cnblogs.com/hblthink/p/8643137.html

XFile 关键帧动画的解析遇到的问题

一、mesh 数据储存方式的修改 由于在设计CXFileMesh类时考虑不够全面&#xff0c;原CXFileMesh 类内部储存mesh数据采用的是vector模板。这使后来试图为该类添加支持3dsmax关键帧动画功能时带来很大麻烦。最后还是对CXFileMesh 类做了整体修改&#xff1a;用二叉树储存mesh数据…

【HTML】兴唐二十八节课之常用标签(不定期更新)

部分属性的详细参数见菜鸟教程 &#xff08;1&#xff09;换行 <br/> (2)字体设置颜色和大小 <font size 6 color blue>小米巨能写</font> &#xff08;3&#xff09;添加图片 <img src "index.jpg" width "300px"> 注&…

Python中自定义类如果重写了__repr__方法为什么会影响到str的输出?

这是因为Python3中&#xff0c;str的输出是调用类的实例方法__str__来输出&#xff0c;如果__str__方法没有重写&#xff0c;则自动继承object类的__str__方法&#xff0c;而object类的__str__方法是调用__repr__方法&#xff0c;因此自定义类未重写__str__方法的情况下&#x…

IT人士的人际关系压力

感谢听心心理学网站的投递在造成IT从业者的众多压力之中&#xff0c;人际关系带来的压力或许是最明显并且循环效应最强的一种。IT行业的冷漠环境是出了名的&#xff0c;在这样的状态之下&#xff0c;如何调整我们的人际关系&#xff0c;将恶性循环改造成良性循环&#xff0c;对…

软件工程网络15结对编程作业

软件工程网络15结对编程作业 1.项目成员 学号&#xff1a;201521123014 博客地址&#xff1a;http://www.cnblogs.com/huangsh/学号&#xff1a; 201521123102 博客地址&#xff1a;http://home.cnblogs.com/u/hyy786030686/结对编程码云项目地址&#xff1a;https://gitee.com…

【THML】兴唐第二十八节课 几个小程序

1、第一个html文件 <HTML><Head><title>小米商城</title></head><body><font size 6 color blue>小米巨能写</font><hr /><img src "index.jpg" width "300px"><p>标签组成部分&a…

Android系统默认Home应用程序(Launcher)的启动过程源代码分析

在前面一篇文章中&#xff0c;我们分析了Android系统在启动时安装应用程序的过程&#xff0c;这些应用程序安装好之后&#xff0c;还需要有一个Home应用程序来负责把它们在桌面上展示出来&#xff0c;在Android系统中&#xff0c;这个默认的Home应用程序就是Launcher了&#xf…

Data - 数据思维 - 下篇

9 - 数据解读与表达 数据解读 数据解读需要选择一个基点、一个参照系&#xff0c;单独的一个数值往往不具备价值&#xff0c;它只是数字。 注意点&#xff1a; 关注异常值&#xff0c;并深究WHY?相互验证、大胆假设、多方验证。把握趋势或者规律。归纳总结、数清理明。数据表达…

cas server 配置

1.修改cas server的deployerConfigContext.xml <bean id"dataSource" class"org.apache.commons.dbcp.BasicDataSource"> <property name"driverClassName"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriv…

四 Vue学习 router学习

index.js: 按需加载组件&#xff1a; const login r > require.ensure([], () > r(require(/page/login)), login); 把JS文件分模块&#xff0c;安需加载&#xff0c;而不是&#xff0c;整个都加载。 routes &#xff1a; 定义路径和组件的mapping关系。c…

Oracle 10.2.0.5.4 Patch Set Update (PSU) – Patch No: p12419392

有关Oracle patch和PSU&#xff0c;PSR 说明参考我的blog&#xff1a;Oracle 补丁体系 及opatch 工具 介绍http://blog.csdn.net/tianlesoftware/article/details/5809526Oracle 10g 最新的版本是10.2.0.5.4. 其中的5是PSR 版本号&#xff0c;4是PSU版本号。MOS 上的2篇文档&am…

【数据库】兴唐第二十八节课零散知识点汇总

1、group by order by等都要放到语句的最后 2、表格标签&#xff1a; <table> <tr>表示行 <td>表示一个行里的单元格 </table> 3、表格调整 内容水平方向跳整&#xff1a; align"center" 表示水平居中 align 有三个值&#xff1a;left…

服务器端往手机端推送数据的问题(手机解决方案)

1.方案一&#xff1a; 思路&#xff1a;使用socket连接&#xff0c;在手机端开个socketserver&#xff0c;然后服务器端连接手机端&#xff0c;实现服务器端的不定时发送数据。 MIDlet关闭时, 你可以通过sms激活它. midlet运行时, 你可以通过socket来解决双向推数据的功能. 个人…

软件测试实验一

实验报告 a) The brief description that I install junit, hamcrest and eclemma. Junit&#xff0c;hamcrest 上网下载junit,hamrest包&#xff0c;然后在项目中新建文件夹lib,复制包到其中&#xff0c;然后单击项目->build path -> configer build path,然后在把包加入…

【java】兴唐第二十九节课作业

将用户在网页填写的信息输入数据库 数据库&#xff1a; create table user_infer(id int(2) not null auto_increment primary key,user_name varchar(12), password varchar(64) not null,real_name varchar(8) not null,age int(3) ); JAAVEE stuList <% page langu…