import numpy as np
x_train = np.random.rand(100000).astype(np.float32)
y_train = 0.5 * x_train + 2 import tensorflow as tf
W = tf.Variable(tf.random.normal([1]))
b = tf.Variable(tf.zeros([1]))
y = W * x_train + b
loss = tf.reduce_mean(tf.square(y - y_train))
optimizer = tf.optimizers.SGD(0.5)
for i in range(100):with tf.GradientTape() as tape:y = W * x_train + bloss = tf.reduce_mean(tf.square(y - y_train))gradients = tape.gradient(loss, [W, b])optimizer.apply_gradients(zip(gradients, [W, b]))if (i+1) % 50 == 0:print("Epoch [{}/{}], loss: {:.3f}, W: {:.3f}, b: {:.3f}".format(i+1, 1000, loss.numpy(), W.numpy()[0], b.numpy()[0]))
x_test = np.array([0.1, 0.2, 0.3], dtype=np.float32)
y_pred = W * x_test + b
print("Predictions:", y_pred.numpy())
import matplotlib.pyplot as plt
plt.scatter(x_train, y_train)
plt.plot(x_train, W * x_train + b, c='r')
plt.show()
Pytorch
import torch
import numpy as np
import matplotlib.pyplot as plt
x_train = torch.from_numpy(np.random.rand(100000).astype(np.float32))
y_train = 0.5 * x_train + 2
W = torch.randn(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD([W, b], lr=0.5)
for i in range(100):y = W * x_train + bloss = loss_fn(y, y_train)optimizer.zero_grad()loss.backward()optimizer.step()if (i + 1) % 50 == 0:print(f"Epoch [{i + 1}/{100}], loss: {loss.item():.3f}, W: {W.item():.3f}, b: {b.item():.3f}")
x_test = torch.tensor([0.1, 0.2, 0.3], dtype=torch.float32)
y_pred = W * x_test + b
print("Predictions:", y_pred.detach().numpy())
plt.scatter(x_train.numpy(), y_train.numpy())
plt.plot(x_train.numpy(), (W * x_train + b).detach().numpy(), c='r')
plt.show()