tensorflow随笔-非线性回归
生活随笔
收集整理的這篇文章主要介紹了
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
(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随笔-非线性回归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv在vs2012下重编译详细教
- 下一篇: tensorflow随笔-保存与读取使用