<jupyter_start><jupyter_text>3.2.1 gen_dataset<jupyter_code>num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = tf.random.normal((num_examples, num_inputs),stddev = 1)
labels = true_w[0] * features[:,0] + true_w[1] * features[:,1] + true_b
labels += tf.random.normal(labels.shape,stddev=0.01)

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)<jupyter_output><empty_output><jupyter_text>3.2.2 read data<jupyter_code>import numpy as np
def data_iter(batch_size, features, labels):
    features = np.array(features)
    labels = np.array(labels)
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        j = np.array(indices[i:min(i + batch_size, num_examples)])
        yield features[j], labels[j]
batch_size = 10

for X, y in data_iter(batch_size, features, labels):
    print(X, y)
    break<jupyter_output>[[-1.8040096  -0.15478396]
 [ 0.30197513  0.7226112 ]
 [ 1.3643339   1.2256716 ]
 [ 0.64083093  0.00985407]
 [-0.9130684   1.2870392 ]
 [-0.73635167  0.8616958 ]
 [-1.3767787  -0.7688884 ]
 [ 0.26842278 -0.40469164]
 [ 1.1865063   0.6506257 ]
 [ 0.24220389  0.41220406]] [ 1.1166337   2.3442128   2.7511618   5.4408355  -2.003743   -0.19769916
  4.0604286   6.11232     4.3865256   3.3064191 ]<jupyter_text>3.2.3 initialize weight<jupyter_code>w = tf.Variable(tf.random.normal((num_inputs, 1), stddev=0.01))
b = tf.Variable(tf.zeros((1,)))<jupyter_output><empty_output><jupyter_text>3.2.4 define model<jupyter_code>def linreg(X, w, b):
    return tf.matmul(X, w) + b<jupyter_output><empty_output><jupyter_text>3.2.5 define loss<jupyter_code>def squared_loss(y_hat, y):
    return (y_hat - tf.reshape(y, y_hat.shape)) ** 2 /2<jupyter_output><empty_output><jupyter_text>3.2.6 define optimization<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>3.2.7 training<jupyter_code>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)))
true_w, w
true_b, b<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.153219837157661, 'fraction_numerical': 0.11435973353071799, 'mean_word_length': 4.148571428571429, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '385687', 'license': 'Apache-2.0', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1125, 'n_words': 205, 'path': 'code/chapter03_DL-basics/3.2_linear-regression-scratch.ipynb', 'repo_name': 'laubob/D2DL', 'size': '2702'}