作业描述如下:
实现代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
x_data = [1.0, 2.0, 3.0]
y_data = [1.5, 4.0, 6.5]
W, B = np.arange(0.0, 4.1, 0.1), np.arange(-2.0, 2.1, 0.1)
w, b = np.meshgrid(W, B, indexing='ij')
def forward(x):
return x*w+b
def loss(y_pred, y):
return (y_pred-y)*(y_pred-y)
mse_lst = []
l_sum = 0.
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
loss_val = loss(y_pred_val, y_val)
l_sum += loss_val
mse_lst.append(l_sum/3)
fig = plt.figure(figsize=(10,10), dpi=300)
ax = Axes3D(fig)
surf = ax.plot_surface(w, b, np.array(mse_lst[0]), rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_zlim(0, 40)
ax.set_xlabel("w")
ax.set_ylabel("b")
ax.set_zlabel("loss")
ax.text(0.2, 2, 43, "Cost Value", color='black')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
运行结果(不能说像,只能说一模一样)