Search code examples
tensorflowmachine-learningdata-sciencelinear-regressiontensorflow2.0

Linear regression in python (tensorflow)


I am running a code i saw on buitin website on linear regression with tensorflow and it keeps giving me an error, I don't know what is wrong with the code. first i thought it was my ide , then when i switched to jupyter lab it showed me error at this point

from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
rng = np.random

X= np.array([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
            7.042,10.791,5.313,7.997,5.654, 9.27,3.1])

Y=np.array([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.22, 
            1, 2.827,3.465,1.65,2.904,2.42,2.94,1.3])

n_samples = X.shape[0]

# Setting up the hyperparameters
learning_rate = 0.01
epochs = 1000
display_step = 50

# Weigth and bias initialized randomly
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

# Linear regression (Wx + b)
def linear_regression(x):
    return W*x + b

# Mean swuare error.
def mean_square(y_pred, y_true):
    return tf.reduce_sum(tf.pow(y_pred-y_true,  2) / (2*n_samples))

# Stochastic Gradient Descent Optimizer
optimizer = tf.optimizers.SGD(learning_rate)

# Optimization process
def run_optimization():
# Wrap computation inside a GradientTape for automatic differentiation
    with tf.GradientTape() as g:
        pred = linear_regression(X)
        loss = mean_square(pred, Y)

    # compute gradient
    gradients = g.gradient(loss, [W,b])

    # update W and b following gradients
    optimizer.apply_gradients(zip(gradients, [W,b]))

# Run training for the given number of steps
for step in range(1, epochs + 1):

# Run the optimization to update W and b values
    run_optimization()
    if step % display_step == 0:
        pred = linear_regression(X)
        loss = mean_square(pred, Y)
        print("step: %i, loss: %f, W:%f, b:%f"% (step, loss, W.numpy(), b.numpy()))

import matplotlib.pyplot as plt 

# Graphic display

plt.plot(X, Y, 'ro', label="Original data")
plt.plot(X, np.array(W * X +b), label="Fitted line")
plt.legend()
plt.show()

first i thought it was my ide , then when i switched to jupyter lab it showed me error at this point

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-26-794baf7bf908> in <module>
      5     # Run the optimization to update W and b values.
      6 
----> 7     run_optimization()
      8 
      9     if step % display_step == 0:

<ipython-input-25-53e6e421f909> in run_optimization()
     13         pred = linear_regression(X)
     14 
---> 15         loss = mean_square(pred, Y)
     16 
     17     # Compute gradients.

<ipython-input-24-184c15846bf2> in mean_square(y_pred, y_true)
      9 def mean_square(y_pred, y_true):
     10 
---> 11     return tf.reduce_sum(tf.pow(y_pred-y_true, 2)) / (2 * n_samples)

~\miniconda3\lib\site-packages\tensorflow\python\ops\math_ops.py in binary_op_wrapper(x, y)
   1232         #   r_binary_op_wrapper use different force_same_dtype values.
   1233         x, y = maybe_promote_tensors(x, y, force_same_dtype=False)
-> 1234         return func(x, y, name=name)
   1235       except (TypeError, ValueError) as e:
   1236         # Even if dispatching the op failed, the RHS may be a tensor aware

~\miniconda3\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~\miniconda3\lib\site-packages\tensorflow\python\ops\math_ops.py in subtract(x, y, name)
    546 @dispatch.add_dispatch_support
    547 def subtract(x, y, name=None):
--> 548   return gen_math_ops.sub(x, y, name)
    549 
    550 

~\miniconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in sub(x, y, name)
  10549       return _result
  10550     except _core._NotOkStatusException as e:
> 10551       _ops.raise_from_not_ok_status(e, name)
  10552     except _core._FallbackException:
  10553       pass

~\miniconda3\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name)
   6895   message = e.message + (" name: " + name if name is not None else "")
   6896   # pylint: disable=protected-access
-> 6897   six.raise_from(core._status_to_exception(e.code, message), None)
   6898   # pylint: enable=protected-access
   6899 

~\miniconda3\lib\site-packages\six.py in raise_from(value, from_value)

InvalidArgumentError: Incompatible shapes: [17] vs. [18] [Op:Sub]

Solution

  • Well, just as the interpreter complains - you have a mismatch in shapes between the inputs to mean_square - your X / samples vector is 17 elements and your Y / targets vector is 18 elements, as you can verify.