python - Tensorflow Linear Regression - Exponential Model Not Fitting Exponent -
i'm trying fit exponentially decaying model (y=ax^b + c)to data have yet value other 0 for b. have 2 "working" sets of code right now, 1 steps through each x,y pair, , other attempts use entire [x,y] array, i'm not sure have implemented correctly. i'd correctly fit curve. linear model works fine i'm not sure going south.
data here - pastebin
#!/usr/bin/python import numpy np import tensorflow tf import sys import matplotlib.pyplot plt k=0 xdata= [] ydata = [] # open data , read in, ignore header. open('curvedata_full_formatted.csv') f: 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 fed model - 1 feature x = tf.placeholder(tf.float32, [none, 1]) # - training variable - 1 feature, 1 output = 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 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 in range(steps): # read in data log file , use x,y (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)
as test, try starting optimizer initial values close expected final parameters. test tell whether or not problem in selection of initial parameter values.
Comments
Post a Comment