上帝他老人家手里有一条神奇的恋爱公式!特别神奇,只要你告诉他怎么追妹子的,他就能告诉你将在多少天以后追上妹子!
接下来我们将用代码把整个过程描述出来:准备好了吗?
准备好开发环境
!pip install torch
#安装torch模块
输出如下:
Requirement already satisfied: torch in /Users/mac/anaconda3/envs/pytorch_learn/lib/python3.7/site-packages (1.0.1.post2)
!pip install sklearn
!安装sklearn模块
输出如下:
Collecting sklearn
Collecting scikit-learn (from sklearn)
[?25l Downloading https://files.pythonhosted.org/packages/bd/1a/5dc99dfa7e7bf2e1f22a07f440b61299148a42d13dee1d5153360313ed67/scikit_learn-0.20.3-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (8.0MB)
[K 100% |████████████████████████████████| 8.0MB 361kB/s ta 0:00:011
[?25hRequirement already satisfied: numpy>=1.8.2 in /Users/mac/anaconda3/envs/pytorch_learn/lib/python3.7/site-packages (from scikit-learn->sklearn) (1.16.2)
Collecting scipy>=0.13.3 (from scikit-learn->sklearn)
[?25l Downloading https://files.pythonhosted.org/packages/dd/6c/ccf7403d14f0ab0f20ce611696921f204f4ffce99a4fd383c892a6a7e9eb/scipy-1.2.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (27.3MB)
[K 100% |████████████████████████████████| 27.3MB 104kB/s ta 0:00:011 55% |█████████████████▉ | 15.3MB 360kB/s eta 0:00:34 99% |████████████████████████████████| 27.2MB 134kB/s eta 0:00:01 99% |████████████████████████████████| 27.3MB 123kB/s eta 0:00:01
[?25hInstalling collected packages: scipy, scikit-learn, sklearn
Successfully installed scikit-learn-0.20.3 scipy-1.2.1 sklearn-0.0
import torch
from torch.utils.data import Dataset,DataLoader
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as I
切换成上帝的身份
#颜值分数
appearance=torch.randint(low=60,high=90,size=(1000,1),)
#让女朋友不开心的次数
unhappy=torch.randint(low=1,high=10,size=(1000,1),)
#上自习学习的次数
study=torch.randint(low=1,high=30,size=(1000,1),)
#一起游戏的次数
game=torch.randint(low=1,high=50,size=(1000,1),)
features = torch.cat((appearance, unhappy, study, game), dim=1).type(torch.FloatTensor)
#前三条数据
features[:3]
输出如下:
tensor([[81., 7., 27., 40.],
[69., 6., 14., 37.],
[87., 7., 15., 8.]])
上帝手中的真实公式是这样的
执行中的偏差项b是1
true_w = [0.5, 3.8, 0.1, 0.1]
true_b = 1
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_w[2] * features[:, 2]+ true_w[3] * features[:, 3] + true_b
labels = labels.unsqueeze(dim=1).type(torch.FloatTensor)
#前三条的lable
labels[:3]
输出如下:
tensor([[74.8000],
[63.4000],
[73.4000]])
features.shape, labels.shape
输出如下:
(torch.Size([1000, 4]), torch.Size([1000, 1]))
以上操作相当于建立了如下数据:
切换成数据科学家的视角
好了,你现在是一名凡人了,所以你是不知道上帝手里的公式是什么样的,你只能靠观察,然后去猜。
这里我提出一个问题,你观察的数据会不会有噪音呢?
一定会有偏差啊,假设科比真的用了19天追求到了他的女朋友,可是你在做调查问卷的时候,他记错了,他记成了20,科比的颜值在上帝那里是71,在你采集数据的时候可能采集成70,这都是有可能的。这个没有办法,作为一名凡人平静接受就好了。
so,你真是拿到的数据集还需要加一下噪音:
noise = torch.randn(size=(1000,1))
noise[:3]
输出如下:
tensor([[-0.7166],
[ 0.4368],
[-0.0055]])
labels = labels + noise
labels[:3]
输出如下:
tensor([[75.4070],
[63.9435],
[72.6591]])
只是为了演示,所以我并未给features中添加噪音。那最后作为数据科学家你手里获得的数据是这个样子的:
加载数据
class myDataset(Dataset):
def __init__(self, features, lables):
self.features = features
self.labels = labels
def __getitem__(self, index):
sample = (self.features[index], labels[index])
return sample
def __len__(self):
return len(self.features)
batch_size = 5
train_features = features[:500]
train_labels = labels[:500]
test_features = features[500:]
test_labels = labels[500:]
train_dataset = myDataset(train_features, train_labels)
train_data_iter = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
test_dataset = myDataset(test_features, test_labels)
test_data_iter = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
#看到了吗?这个batch_size和刚才讲mse中的点相关。
for f, l in train_data_iter:
print("features: {} labels: {}".format(f,l))
break
输出如下:
features: tensor([[71., 6., 22., 24.],
[79., 6., 26., 45.],
[88., 7., 11., 14.],
[79., 3., 13., 34.],
[69., 5., 5., 41.]]) labels: tensor([[65.3587],
[68.8864],
[72.7446],
[54.5244],
[58.2802]])
好了,到此数据就准备好了,接下来我们将用线性回归破解上帝手中的公式了。
用上帝的恋爱公式讲线性回归-下
转载:https://blog.csdn.net/weixin_43936997/article/details/106444829
查看评论