tensorflow随笔-非线性回归
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 10:54:53 2018非线性回归y=a*x^3+b*x^3+c
单样本
"""
import tensorflow as tf
import numpy as nptrainCount=350
g=tf.Graph()
with g.as_default():def getWeights(shape,wname):weights=tf.Variable(tf.truncated_normal(shape,stddev=0.1),name=wname)return weightsdef getBias(shape,bname):biases=tf.Variable(tf.constant(0.1,shape=shape),name=bname)return biasesdef inference(x):result=tf.add(tf.matmul(tf.pow(x,3),w),b)return resultdef loss(x,y):yp=inference(x)return tf.multiply(tf.reduce_sum(tf.squared_difference(y,yp)),0.5)def train(learningRate,trainLoss,trainStep):trainOp=tf.train.GradientDescentOptimizer(learningRate).minimize(trainLoss,global_step=trainStep)return trainOpdef evaluate(x):return inference(x)def accuracy(x,y):yp=inference(x)return tf.subtract(1.0,tf.reduce_mean(tf.divide(tf.abs(yp-y),y)))def inputs(n):sampleX=np.array(np.random.rand(n,2),dtype=np.float32)sampleb1=5.samplew=np.array([0.5,0.9],dtype=np.float32)sampleY=np.matmul(pow(sampleX,3),samplew)+sampleb1return (sampleX,sampleY)with tf.name_scope("variables"):w=getWeights([2,1],"w")b=getBias((),"b") trainStep=tf.Variable(0,dtype=tf.int32,name="step") with tf.name_scope("inputDatas"):x=tf.placeholder(dtype=tf.float32,shape=[None,2],name="input_x")y=tf.placeholder(dtype=tf.float32,shape=[None],name="input_y")init=tf.global_variables_initializer()
with tf.Session(graph=g) as sess:sess.run(init)sampleX,sampleY=inputs(100)sampleCount=sampleX.shape[0]testX,testY=inputs(5)testCount=testX.shape[0]trainLoss=loss(x,y)accuracyOp=accuracy(sampleX,sampleY)inputX=sampleXinputY=sampleYprint inputX.shapeprint inputY.shapetrainOp=train(0.25,trainLoss,trainStep)while trainStep.eval()<trainCount: for i in xrange(sampleCount):inputX=np.array([sampleX[i]],dtype=np.float32)inputY=np.array([sampleY[i]],dtype=np.float32)sess.run(trainOp,feed_dict={x:inputX,y:inputY})nowStep=sess.run(trainStep)if nowStep%50==0:validate_acc=sess.run(accuracyOp)print "%d次后=>正确率%g"%(nowStep,validate_acc)if nowStep>trainCount:breakprint "w:",sess.run(w) print "b:",sess.run(b) print "测试样本正确率%g"%sess.run(accuracy(testX,testY))
(100, 2)
(100,)
50次后=>正确率0.941076
100次后=>正确率0.942413
150次后=>正确率0.943086
200次后=>正确率0.943109
250次后=>正确率0.943165
300次后=>正确率0.943153
350次后=>正确率0.943156
w: [[0.5005716]
[0.8993188]]
b: 5.000005
测试样本正确率0.950526
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 10:54:53 2018非线性回归
单样本
"""
import tensorflow as tf
import numpy as nptrainCount=50
g=tf.Graph()
with g.as_default():with tf.name_scope("variables"):w=tf.Variable(tf.zeros([2,1]),name="w",dtype=tf.float32)b=tf.Variable(0.,dtype=tf.float32,name="b") with tf.name_scope("inputDatas"):x=tf.placeholder(dtype=tf.float32,shape=[None,2],name="input_x")y=tf.placeholder(dtype=tf.float32,shape=[None],name="input_y")def inference(x):result=tf.add(tf.matmul(tf.pow(x,2),w),b)return resultdef loss(x,y):yp=inference(x)return tf.reduce_sum(tf.squared_difference(y,yp))def train(learningRate,trainLoss):trainOp=tf.train.GradientDescentOptimizer(learningRate).minimize(trainLoss)return trainOpdef evaluate(x):return inference(x)def accuracy(x,y):yp=inference(x)return tf.subtract(1.0,tf.reduce_mean(tf.divide(tf.abs(yp-y),y)))def inputs(n):sampleX=np.array(np.random.rand(n,2),dtype=np.float32)sampleb1=5.samplew=np.array([0.5,0.9],dtype=np.float32)b2=np.array(np.random.rand(n),dtype=np.float32)sampleY=np.matmul(pow(sampleX,2),samplew)+sampleb1+b2return (sampleX,sampleY)init=tf.global_variables_initializer()
with tf.Session(graph=g) as sess:sess.run(init)sampleX,sampleY=inputs(100)sampleCount=sampleX.shape[0]testX,testY=inputs(5)testCount=testX.shape[0]trainLoss=loss(x,y)trainOp=train(0.25,trainLoss)accuracyOp=accuracy(sampleX,sampleY)inputX=sampleXinputY=sampleYprint inputX.shapeprint inputY.shapefor trainStep in xrange(trainCount): if trainStep%5==0:validate_acc=sess.run(accuracyOp)print "%d次后=>正确率%g"%(trainStep,validate_acc)for i in xrange(sampleCount):inputX=np.array([sampleX[i]],dtype=np.float32)inputY=np.array([sampleY[i]],dtype=np.float32)sess.run(trainOp,feed_dict={x:inputX,y:inputY})print "w:",sess.run(w) print "b:",sess.run(b) print "测试样本正确率%g"%sess.run(accuracy(testX,testY))
(100, 2)
(100,)
0次后=>正确率0
5次后=>正确率0.927204
10次后=>正确率0.927204
15次后=>正确率0.927204
20次后=>正确率0.927204
25次后=>正确率0.927204
30次后=>正确率0.927204
35次后=>正确率0.927204
40次后=>正确率0.927204
45次后=>正确率0.927204
w: [[0.4828106 ]
[0.82115054]]
b: 5.412575
测试样本正确率0.956847
单样本训练
tensorflow随笔-非线性回归相关推荐
- tensorflow随笔-队列管理器QueueRunner-生产者与消费者
# -*- coding: utf-8 -*- """ Spyder EditorThis is a temporary script file. "" ...
- tensorflow随笔-随机数
Aliases: tf.compat.v1.random.normal tf.compat.v1.random_normal tf.compat.v2.random.normal tf.random_ ...
- tensorflow随笔-读取图像文件数据(2)-TFRecord
写文件 代码如下 # -*- coding: utf-8 -*- """ Created on Tue May 7 18:29:30 2019@author: liuxi ...
- tensorflow随笔-读取图像文件数据(1)
# -*- coding: utf-8 -*- """ Created on Tue May 7 18:29:30 2019@author: liuxing @email ...
- tensorflow随笔-constant
# -*- coding: utf-8 -*-import tensorflow as tf import numpy as np a=tf.constant(np.array([[6,2]],dty ...
- tensorflow随笔-条件循环控制(4)
例如非严格语义的示例:在下面的示例中,计数器i的最终值不依赖于x.while_loop可并行地增加计数器,并更新x. #!/usr/bin/env python2 # -*- coding: utf- ...
- tensorflow随笔-条件循环控制(2)
tf.count_up_to tf.count_up_to( ref, limit, name=None ) 增加"ref"直到它达到"limit" 参数: r ...
- tensorflow随笔-队列(1)
tf.FIFOQueue Class FIFOQueue 先进先出的队列 属性 dtypes 队列元素的每个组件的dtypes列表 name 队列名字 基础队列的名称. names 队列元素的每个组件 ...
- TensorFlow随笔-多分类单层神经网络softmax
#!/usr/bin/env python2 # -*- coding: utf-8 -*-import tensorflow as tf from tensorflow.examples.tutor ...
最新文章
- BUZZER Driver
- QT的QDateTimeAxis类的使用
- es 精确查询不模糊_ES系列17:Terms聚合结果不精确,怎么破?
- php 判断美国zip code
- Java render用法_SpringMVC ModelAndView的用法使用详解
- 手动迁移KVM虚拟机
- Web项目下的WebRoot和WebContent
- 团队编程项目作业6-程序维护
- llinux 查看自己的公网ip
- 5 EDA技术实用教程【基本语句2】
- 快逸报表整合至MyEclipse项目中
- Python系列9——类和对象(3)
- VOC2007数据集 VOC2012数据集下载 百度云
- 有没有英语语音测试软件,推荐我用过的几款真正可以找外国人练口语的软件app...
- 联想小新22H2版跳过联网方法最新方法
- fMRI数据分析处理原理及方法fMRI数据分析处理原理及方法
- 这10款数据可视化软件工具免费好用
- 数据分析师需要具备哪些技能?如何拿到月薪20K+数据分析师OFFER?
- 2023年上半年 软件设计师答案解析
- web端 刷卡器_web开发与IC卡读卡器