Code_maniac
Code_maniac

Reputation: 49

Why is my training model not predicting correct result?

I have taken a stock price and a day number as the input data.

There are about 1365 input data, but my model is not able to predict the correct value of m ( slope ) and b of my regression problem, using a gradient descent optimizer in TensorFlow.

I have also tried to take different values for the learning rate ( 0.0000000001, .., 0.1 ), but none of them worked.

import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
import numpy as np

batch_size=8
ds=pd.read_csv("FB.csv",sep=",",header=None)
x_data=np.array(ds[0].values)
y_true=ds[1].values
x_data=np.array(x_data)
m=tf.Variable(2.2)
b=tf.Variable(0.5)
x_act=tf.placeholder(tf.float32,[batch_size])
y_act=tf.placeholder(tf.float32,[batch_size])
y_model=m*x_act + b
error=tf.reduce_sum(tf.square(y_act-y_model))
optim=tf.train.GradientDescentOptimizer(learning_rate=0.0000001)
train=optim.minimize(error)
init=tf.global_variables_initializer()
with tf.Session() as sess:
    batches=1700
    for i in range(batches):
        rand_ind = np.random.randint(len(x_data),size=batch_size)
        sess.run(init)
        feed = {x_act:x_data[rand_ind],y_act:y_true[rand_ind]}
        sess.run(train,feed_dict=feed)
    model_m,model_b=sess.run([m,b])
model_m
model_b
y_ans=(model_m*len(x_data)+1) + model_b
y_ans

Upvotes: 0

Views: 719

Answers (1)

user3666197
user3666197

Reputation: 1

Having spent more than +20 years in trading, quant-modeling and Machine Learning augmented decision support for FX-trading, there are few things I can help you understand before you start investing your time and efforts in a completely wrong direction. Linear regression model, reported above, has flaws, detailed below, which will not be salvaged if moving to any of the few more complex auto-regressive models ( ARMA / ARIMA ) and similarly even the LSTM-tools will not save a naive, skipped or underestimated system identification ( as it is common to call it in technical cybernetics ). Simply put, any-Model setups, that try to indoctrinate some Model-behaviour and abstract from non-TA behaviour-mode-switching, are principally blind and singlehanded for handling a complex ( almost hyperchaotic, in extended Lyapunov sense ) multi-agent ecosystem.


Why my training model not predicting correct result?

Because your assumption is straight wrong.

There are no such stocks, that behave as a linear model, whereas your instructions are strictly opposite,
you ask your linear model yPREDICTED = m.X + b
to find such m and b
so that the overall sum of penalty-errors is minimal.

Having found such m and b, for which the sum of penalty-errors is minimal, the learner, that you have pre-selected to use, has finished it's role.

Right, that means, you can be mathematically sure, there is no such other m and b, that would yield lesser sum of penalty-errors, computed as per your selected method, on the available ( and the same used part thereof ) of the observed examples.


While all was done according to an agreed plan,
that still does NOT make The Market
to start to "obey" the m.X + b linear behaviour...

If you forget to realise this iron-cast irony, you just started to blindly believe, that linear model rules the real-world ( which we second by second witness it indeed does not ).

So YGWYT -- You Get What You Train

If you train a linear model m.X + b, you cannot be surprised to get nothing else but a least-wrong linear model m.X + b.


Predictions simply have to systematically follow the Model

which means, all your predictions have to systematically be wrong, just by sticking to the least-wrong linear model m.X + b

Q.E.D.

Upvotes: 1

Related Questions