原视频地址
导入基本库
1 2 3 4 5 import torchimport torch.nn as nnfrom sklearn import datasetsfrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import train_test_split
导入乳腺癌数据集
打印出数据集的样本值和特征值 1 2 3 4 bc=datasets.load_breast_cancer() X,y=bc.data,bc.target n_samples,n_features=X.shape print (n_samples,n_features)
输出结果
569个样本和30个特征 ### 对导入的数据进行处理
1 2 3 4 5 6 7 8 9 10 11 12 13 X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2 ,random_state=1234 ) sc=StandardScaler() X_train=sc.fit_transform(X_train) X_test=sc.transform(X_test) X_train=torch.from_numpy(X_train.astype(np.float32)) X_test=torch.from_numpy(X_test.astype(np.float32)) y_train=torch.from_numpy(y_train.astype(np.float32)) y_test=torch.from_numpy(y_test.astype(np.float32)) y_train=y_train.view(y_train.shape[0 ],1 ) y_test=y_test.view(y_test.shape[0 ],1 )
搭建模型
这里只需要一个线性层
1 2 3 4 5 6 7 8 class LogisticRegression (nn.Module): def __init__ (self,n_input_features ): super (LogisticRegression, self).__init__() self.linear=nn.Linear(n_input_features,1 ) def forward (self,x ): y_pred=torch.sigmoid(self.linear(x)) return y_pred
搭建优化器和损失函数
1 2 3 model=LogisticRegression(n_features) criterian=nn.BCELoss() optimizer=torch.optim.SGD(model.parameters(),lr=0.01 )
对每一轮计算Loss
1 2 3 4 5 6 7 8 9 10 11 12 13 epochs=100 for epoch in range (epochs): y_pred=model(X_train) loss=criterian(y_pred,y_train) loss.backward() optimizer.step() optimizer.zero_grad() if (epoch+1 )%10 ==0 : print (f'epoch:{epoch+1 } ,loss={loss.item():.4 f} ' )
评估模型准确度
1 2 3 4 5 with torch.no_grad(): y_pred = model(X_test) y_pred_cls=y_pred.round () acc=y_pred_cls.eq(y_test).sum ()/float (y_test.shape[0 ]) print (f'accuracy={acc:.4 f} ' )
输出结果
1 2 3 4 5 6 7 8 9 10 11 epoch:10 ,loss=0.5711 epoch:20 ,loss=0.4670 epoch:30 ,loss=0.4019 epoch:40 ,loss=0.3575 epoch:50 ,loss=0.3251 epoch:60 ,loss=0.3003 epoch:70 ,loss=0.2806 epoch:80 ,loss=0.2645 epoch:90 ,loss=0.2511 epoch:100 ,loss=0.2396 accuracy=0.9123
可以更改训练次数和学习率来提高模型准确度