Tensorflow: 13 Linear Regression using Tensorflow

Published: 02 October 2020
on channel: BharatOnlineDS
99
1

import numpy as np
=== Create data and simulate results =====
x_data = np.random.randn(200,3)
w_real = [0.3,0.5,0.1]
b_real = -0.2
noise = np.random.randn(1,200)*0.1
y_data = np.matmul(w_real,x_data.T) + b_real + noise
................................................................................................
import tensorflow as tf
................................................................................................
NUM_STEPS = 10
g = tf.Graph()
wb_ = []
with g.as_default():
x = tf.compat.v1.placeholder(tf.float32,shape=[None,3])
y_true = tf.compat.v1.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')
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 = .4
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss)

Before starting, initialize the variables. We will 'run' this first.
init = tf.compat.v1.global_variables_initializer() #variables given memory

with tf.compat.v1.Session() as sess:
sess.run(init)
for step in range(NUM_STEPS):
sess.run(train,{x: x_data, y_true: y_data})
print(step, sess.run([w,b]))
wb_.append(sess.run([w,b]))
print(10, sess.run([w,b]))


On this page of the site you can watch the video online Tensorflow: 13 Linear Regression using Tensorflow with a duration of hours minute second in good quality, which was uploaded by the user BharatOnlineDS 02 October 2020, share the link with friends and acquaintances, this video has already been watched 99 times on youtube and it was liked by 1 viewers. Enjoy your viewing!