Totally New
Totally New

Reputation: 147

Tensorflow with gradient decent results in wrong coefficients

Currently, i am trying to construct a linear regression that uses birth rate (x) as predictor to predict life expectancy (y). y=w*x+b The dataset could be found here: Dataset

Here is an online link for my code: Code

The idea is simple: i run 300 epochs, inside each epoch, i fed one-by-one paired sample (x value,y value) to the gradient decent optimizer to minimize loss function.

However, the result that i obtained is quite wrong. Image of my result: my result

Instead of having negative slope, it always result in positive slope, while the sample answer provided here results in a better model with negative slope.

What were wrong in my coding?

Upvotes: 0

Views: 54

Answers (1)

tomkot
tomkot

Reputation: 956

The problem is the location of the line

sess.run(tf.global_variables_initializer())

Since it is inside the while loop, w and b are reinitialized every iteration to 0. What you are seeing therefore is the result of one while loop iteration of training (the last one). You should move the line before the while loop.

Upvotes: 1

Related Questions