Changing to Traditional Equation for Linear Regression

by streams17   Last Updated June 30, 2020 04:26 AM

I am trying to change the (w * x^t + b) in this code to the more traditional (w^t * x + b) form.

import tensorflow as tf
import numpy as np

x_data = np.random.rand(2000,3) 
w_real = [0.3,0.5,0.1]
b_real = -0.2

noise = np.random.randn(1,2000)*0.1
y_data = np.matmul(w_real,x_data.T) + b_real + noise

NUM_STEPS = 10

g = tf.Graph()
wb_ = []
with g.as_default():
    x = tf.placeholder(tf.float32,shape=[None,3])
    y_true = tf.placeholder(tf.float32,shape=None)

    with tf.name_scope('inference') as scope:
        w = tf.Variable([[0,0,0]],dtype=tf.float32,name='weights')
        b = tf.Variable(0,dtype=tf.float32,name='bias')
        # Here w is initialized as a row vector; It works in this case because
        # transposing x will yield the same result as in the traditional equation (wt * x + b)

        y_pred = tf.matmul(w,tf.transpose(x)) + b

    with tf.name_scope('loss') as scope:
        loss = tf.reduce_mean(tf.square(y_true-y_pred))

    with tf.name_scope('train') as scope:
        learning_rate = 0.05
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train = optimizer.minimize(loss)

    #before starting, initialize the variables. We will 'run' this first.
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for step in range(NUM_STEPS):
            sess.run(train,{x: x_data, y_true: y_data})
            if (step % 5 == 0):
                print(step, sess.run([w,b]))
                wb_.append(sess.run([w,b]))

        print(10, sess.run([w,b]))

I was given the hint that changes need to be made somewhere within these lines of code, as well as a rule that we COULD NOT change line 5.

x_data = np.random.rand(2000,3) 

x = tf.placeholder(tf.float32,shape=[None,3])
y_true = tf.placeholder(tf.float32,shape=None)

w = tf.Variable([[0,0,0]],dtype=tf.float32,name='weights')
b = tf.Variable(0,dtype=tf.float32,name='bias')

I attempted to change the code to the (w^t * x + b) form and then work with the error codes until I got it working, however I kept running into circular problems where changing the shapes would only bring up different issues.

Note: This is ran using Tensorflow 1.15



Related Questions


Linear Regression - Tensorflow

Updated January 08, 2019 16:26 PM


Tensorflow model does not minimize error

Updated April 09, 2018 00:26 AM