<gh_stars>0
import tensorflow as tf

print(tf.__version__)
from matplotlib import pyplot as plt
import random

num_feature = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = tf.random.normal((num_examples, num_feature), stddev=1)
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += tf.random.normal(labels.shape, stddev=0.01)
print(features[0], labels[0])


def set_figsize(figsize=(3.5, 2.5)):
    plt.rcParams['figure.figsize'] = figsize


set_figsize()
plt.scatter(features[:, 1], labels, 1)
plt.show()


def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        j = indices[i: min(i + batch_size, num_examples)]
        yield tf.gather(features, axis=0, indices=j), tf.gather(labels, axis=0, indices=j)


batch_size = 10

for X, y in data_iter(batch_size, features, labels):
    print(X, y)
    break

w = tf.Variable(tf.random.normal((num_feature, 1), stddev=0.01))
b = tf.Variable(tf.zeros((1,)))


def linreg(X, w, b):
    return tf.matmul(X, w) + b


def squared_loss(y_pre, y):
    return (y_pre - tf.reshape(y, y_pre.shape)) ** 2 / 2


def sgd(params, lr, batch_size, grads):
    """Mini-batch stochastic gradient descent."""
    for i, param in enumerate(params):
        param.assign_sub(lr * grads[i] / batch_size)


lr = 0.03
num_epochs = 3
net = linreg
loss = squared_loss

for epoch in range(num_epochs):
    for X, y in data_iter(batch_size, features, labels):
        with tf.GradientTape() as t:
            t.watch([w, b])
            l = loss(net(X, w, b), y)
        grads = t.gradient(l, [w, b])
        sgd([w, b], lr, batch_size, grads)
    train_l = loss(net(features, w, b), labels)
    print('epoch %d, loss %f' % (epoch + 1, tf.reduce_mean(train_l)))

print(true_w, w)
print(true_b, b)

import tensorflow as tf

print(tf.__version__)
from matplotlib import pyplot as plt
import random

num_feature = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = tf.random.normal((num_examples, num_feature), stddev=1)
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += tf.random.normal(labels.shape, stddev=0.01)
print(features[0], labels[0])


def set_figsize(figsize=(3.5, 2.5)):
    plt.rcParams['figure.figsize'] = figsize


set_figsize()
plt.scatter(features[:, 1], labels, 1)
plt.show()


def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        j = indices[i: min(i + batch_size, num_examples)]
        yield tf.gather(features, axis=0, indices=j), tf.gather(labels, axis=0, indices=j)


batch_size = 10

for X, y in data_iter(batch_size, features, labels):
    print(X, y)
    break

w = tf.Variable(tf.random.normal((num_feature, 1), stddev=0.01))
b = tf.Variable(tf.zeros((1,)))


def linreg(X, w, b):
    return tf.matmul(X, w) + b


def squared_loss(y_pre, y):
    return (y_pre - tf.reshape(y, y_pre.shape)) ** 2 / 2


def sgd(params, lr, batch_size, grads):
    """Mini-batch stochastic gradient descent."""
    for i, param in enumerate(params):
        param.assign_sub(lr * grads[i] / batch_size)


lr = 0.03
num_epochs = 3
net = linreg
loss = squared_loss

for epoch in range(num_epochs):
    for X, y in data_iter(batch_size, features, labels):
        with tf.GradientTape() as t:
            t.watch([w, b])
            l = loss(net(X, w, b), y)
        grads = t.gradient(l, [w, b])
        sgd([w, b], lr, batch_size, grads)
    train_l = loss(net(features, w, b), labels)
    print('epoch %d, loss %f' % (epoch + 1, tf.reduce_mean(train_l)))

print(true_w, w)
print(true_b, b)
Mini-batch stochastic gradient descent.