张量线性回归 - 指数模型不拟合指数

时间:2016-11-06 01:51:02

标签: python machine-learning tensorflow regression

我正在尝试将指数衰减模型(y = Ax ^ b + C)拟合到某些数据,但是还没有为b获得0以外的值。我现在有两个“工作”代码集,每个X,Y对一步,另一个尝试使用整个[X,Y]数组,但我不确定我是否已正确实现。现在我希望它能正确拟合曲线。线性模型运行良好,所以我不确定它向南的位置。

数据在此处 - PASTEBIN

#!/usr/bin/python

import numpy as np
import tensorflow as tf
import sys
import matplotlib.pyplot as plt
k=0
xdata= []
ydata = []
# Open the data and read it in, ignore the header.
with open('curvedata_full_formatted.csv') as f:
    for line in f:
        k+=1
        if k==1:continue
        items = line.split(',')
        xdata.append(float(items[0]))
        ydata.append(float(items[1]))


# Model linear regression y = A*x^B+C
# x - data to be fed into the model - 1 feature
x = tf.placeholder(tf.float32, [None, 1])

# A - training variable - 1 feature, 1 output
A = tf.Variable(tf.zeros([1,1]))

# B - training variable - 1 output
B = tf.Variable(tf.zeros([1,1]))

# C - training variable - 1 output
C = tf.Variable(tf.zeros([1]))

# x^B
xb = tf.exp(B)
# A*x^b
product = tf.mul(A,xb)


# Prediction
y = tf.add(product,C)
# Actual value ybar

y_ = tf.placeholder(tf.float32)
# Cost function sum((y_-y)**2)
cost = tf.reduce_mean(tf.square(y_-y))

# Training using Gradient Descent to minimize cost
train_step = tf.train.GradientDescentOptimizer(1*10**-9).minimize(cost)

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
steps = 150

for i in range(steps):
    # Read in data from log file and use as x,y
    for (X,Y) in zip(xdata,ydata):
        #xs = np.array([[xdata]])
        #ys = np.array([[ydata]])
        # Train
        # Feed dict x placeholder xs, y_ placeholder ys
        X = np.array([[X]])
        Y = np.array([[Y]])
        feed = { x: X, y_: Y }
        sess.run(train_step, feed_dict=feed)
    sys.stdout.write("\rIteration %i " %i +"cost %.15f" % sess.run(cost,     feed_dict=feed))
    sys.stdout.flush()
print ''
print 'A: %f'%sess.run(A)
print 'B: %f'%sess.run(B)
print 'C: %f'%sess.run(C)   

1 个答案:

答案 0 :(得分:0)

作为测试,尝试使用接近预期最终参数的初始值启动优化器。此测试将告诉您问题是否在选择初始参数值。