码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow-非线性回归

时间:2018-11-21 16:10:11      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:eval   eva   oss   nop   stc   init   validate   imp   creat   

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Sep 15 10:54:53 2018 @author: myhaspl @email:myhaspl@myhaspl.com 非线性回归 单样本 """ import tensorflow as tf import numpy as np trainCount=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 result def 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 trainOp def 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+b2 return (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=sampleX inputY=sampleY print inputX.shape print inputY.shape for 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-非线性回归

标签:eval   eva   oss   nop   stc   init   validate   imp   creat   

原文地址:http://blog.51cto.com/13959448/2319840

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!