pytorch深度学习实践 --线性模型
原创
©著作权归作者所有:来自51CTO博客作者tizzi的原创作品,请联系作者获取转载授权,否则将追究法律责任
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits import mplot3d
x_train = np.array([1.0, 2.0, 3.0])
t_train = np.array([5.0, 7.0, 9.0])
def forward(x):
return x * w + b
def loss(x, t):
y = forward(x)
return np.sum((y - t) ** 2)
w_list = np.arange(0, 4.1, 0.1)
b_list = np.arange(0, 4.1, 0.1)
train_size = len(x_train)
Z = np.zeros((len(w_list), len(b_list)))
for i in range(len(w_list)):
for j in range(len(b_list)):
w = w_list[i]
b = b_list[j]
Z[i][j] = loss(x_train, t_train) / train_size
plt.figure(figsize=(20, 8), dpi=80)
ax = plt.axes(projection='3d')
W, B = np.meshgrid(w_list, b_list)
ax.plot_surface(W, B, Z)
plt.show()