tensorflow-非线性回归(2)

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 10:54:53 2018

@author: myhaspl
@email:[email protected]
非线性回归y=a*x^3+b*x^3+c
单样本
"""
import tensorflow as tf
import numpy as np

trainCount=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 weights

    def getBias(shape,bname):
        biases=tf.Variable(tf.constant(0.1,shape=shape),name=bname)
        return biases

    def inference(x):
        result=tf.add(tf.matmul(tf.pow(x,3),w),b)
        return result

    def 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 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)
        sampleY=np.matmul(pow(sampleX,3),samplew)+sampleb1
        return (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=sampleX
    inputY=sampleY
    print inputX.shape
    print inputY.shape
    trainOp=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:
                break
    print "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

猜你喜欢

转载自blog.51cto.com/13959448/2321784