梯度下降算法
import torch
import numpy as np
import matplotlib.pyplot as plt
x_data = np.random.randint(1, 10, size=(3))
y_data = x_data * 2
w = 1.0
def forward(x):
return x * w
def cost(x_s, y_s):
loss = 0
for x, y in zip(x_s, y_s):
y_pred = forward(x)
loss += (y_pred - y) ** 2
return loss / x_data.shape
def gradient(x_s, y_s):
grad = 0
for x, y in zip(x_s, y_s):
grad = 2 * x * (w * x - y)
return grad / x_data.shape
print("Print w = %f (before training)" % w)
cost_list = []
w_list = []
for epoch in range(100):
w_list.append(w)
cost_val = cost(x_data, y_data)
grad = gradient(x_data, y_data)
w -= 0.01 * grad
print("Epoch: ", epoch, "\t", "w=: ", w, "\t", "cost=: ", cost_val, "\t")
cost_list.append(cost_val)
print("Predict w = %f (after training)" % w)
plt.plot(range(100), cost_list)
plt.xlabel("epoch")
plt.ylabel("cost")
plt.show()
plt.close()