Iris Classification on PyTorch
Breast Cancer on PyTorch
Code
# encoding:utf8
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import torch
import torch.nn as nn
import torch.optim as optim
from matplotlib import pyplot as plt
import numpy as np
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.l1 = nn.Linear(30, 60)
self.a1 = nn.Sigmoid()
self.l2 = nn.Linear(60, 2)
self.a2 = nn.ReLU()
self.l3 = nn.Softmax(dim=1)
def forward(self, x):
x = self.l1(x)
x = self.a1(x)
x = self.l2(x)
x = self.a2(x)
x = self.l3(x)
return x
if __name__ == '__main__':
breast_cancer = load_breast_cancer()
x_train, x_test, y_train, y_test = train_test_split(breast_cancer.data, breast_cancer.target, test_size=0.25)
x_train, x_test = torch.tensor(x_train, dtype=torch.float), torch.tensor(x_test, dtype=torch.float)
y_train, y_test = torch.tensor(y_train, dtype=torch.long), torch.tensor(y_test, dtype=torch.long)
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.005) # PyTorch suit to tiny learning rate
error = list()
for epoch in range(250):
optimizer.zero_grad()
y_pred = net(x_train)
loss = criterion(y_pred, y_train)
loss.backward()
optimizer.step()
error.append(loss.item())
y_pred = net(x_test)
y_pred = torch.argmax(y_pred, dim=1)
# it is necessary that drawing the loss plot when we fine tuning the model
plt.plot(np.arange(1, len(error)+1), error)
plt.show()
print(classification_report(y_test, y_pred, target_names=breast_cancer.target_names))
损失函数图像:

nn.Sequential
# encoding:utf8
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import torch
import torch.nn as nn
import torch.optim as optim
from matplotlib import pyplot as plt
import numpy as np
if __name__ == '__main__':
breast_cancer = load_breast_cancer()
x_train, x_test, y_train, y_test = train_test_split(breast_cancer.data, breast_cancer.target, test_size=0.25)
x_train, x_test = torch.tensor(x_train, dtype=torch.float), torch.tensor(x_test, dtype=torch.float)
y_train, y_test = torch.tensor(y_train, dtype=torch.long), torch.tensor(y_test, dtype=torch.long)
net = nn.Sequential(
nn.Linear(30, 60),
nn.Sigmoid(),
nn.Linear(60, 2),
nn.ReLU(),
nn.Softmax(dim=1)
)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.005) # PyTorch suit to tiny learning rate
error = list()
for epoch in range(250):
optimizer.zero_grad()
y_pred = net(x_train)
loss = criterion(y_pred, y_train)
loss.backward()
optimizer.step()
error.append(loss.item())
y_pred = net(x_test)
y_pred = torch.argmax(y_pred, dim=1)
# it is necessary that drawing the loss plot when we fine tuning the model
plt.plot(np.arange(1, len(error)+1), error)
plt.show()
print(classification_report(y_test, y_pred, target_names=breast_cancer.target_names))
模型性能:
precision recall f1-score support
setosa 1.00 1.00 1.00 14
versicolor 1.00 1.00 1.00 16
virginica 1.00 1.00 1.00 20
accuracy 1.00 50
macro avg 1.00 1.00 1.00 50
weighted avg 1.00 1.00 1.00 50
Iris Classification on PyTorch的更多相关文章
- Iris Classification on Tensorflow
Iris Classification on Tensorflow Neural Network formula derivation \[ \begin{align} a & = x \cd ...
- Iris Classification on Keras
Iris Classification on Keras Installation Python3 版本为 3.6.4 : : Anaconda conda install tensorflow==1 ...
- (转)Awesome PyTorch List
Awesome-Pytorch-list 2018-08-10 09:25:16 This blog is copied from: https://github.com/Epsilon-Lee/Aw ...
- Pytorch collate_fn用法
By default, Dataloader use collate_fn method to pack a series of images and target as tensors (first ...
- pytorch和tensorflow的爱恨情仇之定义可训练的参数
pytorch和tensorflow的爱恨情仇之基本数据类型 pytorch和tensorflow的爱恨情仇之张量 pytorch版本:1.6.0 tensorflow版本:1.15.0 之前我们就已 ...
- pytorch下对简单的数据进行分类(classification)
看了Movan大佬的文字教程让我对pytorch的基本使用有了一定的了解,下面简单介绍一下二分类用pytorch的基本实现! 希望详细的注释能够对像我一样刚入门的新手来说有点帮助! import to ...
- pytorch -- CNN 文本分类 -- 《 Convolutional Neural Networks for Sentence Classification》
论文 < Convolutional Neural Networks for Sentence Classification>通过CNN实现了文本分类. 论文地址: 666666 模型图 ...
- pytorch之 classification
import torch import torch.nn.functional as F import matplotlib.pyplot as plt # torch.manual_seed(1) ...
- pytorch 5 classification 分类
import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.p ...
随机推荐
- 笔记:yum和apt-get的区别
rpm包和deb包是两种Linux系统下最常见的安装包格式,在安装一些软件或服务的时候免不了要和它们打交道. rpm包主要应用在RedHat系列包括 Fedora等发行版的Linux系统上, deb包 ...
- 安装Cuda9.0+cudnn7.3.1+tensorflow-gpu1.13.1
我的安装版本: win10 x64 VS2015 conda python 3.7 显卡 GTX 940mx Cuda 9.0 cudnn v7.3.1 Tensorflow-gpu 1.13.1 1 ...
- java基础(四) -变量类型
在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ; 格式说明:ty ...
- @responsebody 返回json
添加jackson依赖 添加@ResponseBody 测试: 注意,如果输入中文,出现乱码现象,则需要@RequestMapping(value="/appinterface" ...
- cookie和session必须了解的东西
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- Java第一次考试作业
这次考试感觉自己充分的意识到自己的不足,对于Java没有系统的理解,敲程方面也有很大问题,本次程序题目为ATM机的账户记录Account有账户的唯一性标识,用户的姓名,操作日期(Date),操作类型, ...
- gitlab4.0->5.0->6.0->7.14->8.0->8.2升级
参考官方文档: https://gitlab.com/gitlab-org/gitlab-ce/tree/master/doc/update 本地服务器为4.0.1版本 1)4.0.1->4. ...
- 有关g++的Xlinker选项
事情的起因是,在使用Eclipse配置opencv的库时,发现使用-Wl 选项需要自己在Linker flags的输入框里指定.并不是像使用-l选项或-I选项那样可以直接添加选项的条目就可以了.但是在 ...
- BFS 搜索 蓝桥杯模拟赛
题目链接:https://nanti.jisuanke.com/t/36117 这个题目想不到用广搜来做,一直在想深搜. 广搜的思路呢,是把最外圈不是黑色(不是0)的数 的位置 i 和 j 进队,赋值 ...
- centos下jdk、jre安装
1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java [root@localhost ~]# cd /usr/java 2.下载jdk,然后解压 ...