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]))
Смотрите видео Tensorflow: 13 Linear Regression using Tensorflow онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь BharatOnlineDS 02 Октябрь 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 99 раз и оно понравилось 1 людям.