PyTorch学习记录(一)线性模型

第一次接触pytorch,本贴仅记录学习过程,侵删
在B站上找到了一个很不错的PyTorch学习视频
附上链接:《PyTorch深度学习实践》完结合集
学习完Lecture2.线性模型后有个课后作业
Lecture2课程课件 可在链接中评论区下载
于是按自己的想法实现了一下,最后发现没有使用到Tips中的np.meshgrid()

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 设函数为y=3x+2


x_data = [1.0, 2.0, 3.0]
y_data = [5.0, 8.0, 11.0]


def forward(x):
    return x * w + b


def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)


w_list = []
b_list = []
mse_list = []
for w in np.arange(0.0, 4.1, 0.1):
    for b in np.arange(0.0, 4.1, 0.1):
        print('w=', w)
        print('b=', b)
        l_sum = 0
        for x_val, y_val in zip(x_data, y_data):
            y_pred_val = forward(x_val)
            loss_val = loss(x_val, y_val)
            l_sum += loss_val
            print('\t', x_val, y_val, y_pred_val, loss_val)
        print('MSE=', l_sum / 3)
        w_list.append(w)
        b_list.append(b)
        mse_list.append(l_sum / 3)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(w_list, b_list, mse_list)
plt.show()
plt.savefig('Linear_Model.jpg')

print('最小的MSE为:', min(mse_list))
num = mse_list.index(min(mse_list))
w_num = w_list[num]
b_num = b_list[num]
print('此时的w为', w_num)
print('此时的b为', b_num)
print('故为y=' + str(int(w_num)) + 'x+' + str(int(b_num)))

得到的结果是


w= 0.0
b= 0.0
	 1.0 5.0 0.0 25.0
	 2.0 8.0 0.0 64.0
	 3.0 11.0 0.0 121.0
MSE= 70.0
w= 0.0
b= 0.1
	 1.0 5.0 0.1 24.010000000000005
	 2.0 8.0 0.1 62.410000000000004
	 3.0 11.0 0.1 118.81
MSE= 68.41000000000001
w= 0.0
b= 0.2
	 1.0 5.0 0.2 23.04
	 2.0 8.0 0.2 60.839999999999996
	 3.0 11.0 0.2 116.64000000000001
MSE= 66.84


# 此处省略了一大段


w= 3.0
b= 1.8
	 1.0 5.0 4.8 0.04000000000000007
	 2.0 8.0 7.8 0.04000000000000007
	 3.0 11.0 10.8 0.039999999999999716
MSE= 0.03999999999999995
w= 3.0
b= 1.9000000000000001
	 1.0 5.0 4.9 0.009999999999999929
	 2.0 8.0 7.9 0.009999999999999929
	 3.0 11.0 10.9 0.009999999999999929
MSE= 0.009999999999999929
w= 3.0
b= 2.0
	 1.0 5.0 5.0 0.0
	 2.0 8.0 8.0 0.0
	 3.0 11.0 11.0 0.0
MSE= 0.0
w= 3.0
b= 2.1
	 1.0 5.0 5.1 0.009999999999999929
	 2.0 8.0 8.1 0.009999999999999929
	 3.0 11.0 11.1 0.009999999999999929
MSE= 0.009999999999999929
w= 3.0
b= 2.2
	 1.0 5.0 5.2 0.04000000000000007
	 2.0 8.0 8.2 0.039999999999999716
	 3.0 11.0 11.2 0.039999999999999716
MSE= 0.039999999999999834


# 此处省略了一大段


w= 4.0
b= 4.0
	 1.0 5.0 8.0 9.0
	 2.0 8.0 12.0 16.0
	 3.0 11.0 16.0 25.0
MSE= 16.666666666666668
最小的MSE为: 0.0
此时的w为 3.0
此时的b为 2.0
故为y=3x+2

得到的图3D图为
Linear Model
发现没有使用np.meshgrid()后,查了一下文档:https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html#numpy.meshgrid

numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')

Return coordinate matrices from coordinate vectors.

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.

在评论中找到了使用np.meshgrid()实现的代码,这里就不再贴了(为了方便查看,上面的代码采用了同一个公式y=3x+2)
附上链接:PyTorch学习(一)–线性模型

但是其中我运行的时候似乎是有一个警告:

MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6.  This is consistent with other Axes classes.
  ax = Axes3D(fig)

我就把

  ax = Axes3D(fig)

替换为

ax = plt.axes(projection='3d')

啥也不懂的小白欢迎指正,侵删

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值