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 ...
随机推荐
- 启动Jenkins
启动Jenkins? 进入到Jenkins的war包所在的目录 ->CMD 到 Jenkins的war包所在的目录 输入: Java -jar jenkins.war 启动Jenkins服务n ...
- lambda 表达式拼接
类库: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...
- JAVA编程思想学习笔记2-chap4-6-斗之气2段
1.foreach:只能用于数组与容器 2.this指针:内部有个指针指向自己 3.super指针:内部有个指针指向父类部分 4.方法存放于代码区:方法调用时,a.fun()可能会被转换为fun(a) ...
- 实验long raw 和 blob两种数据类型遇到dblink的表现
首先long raw从Oracle 10g开始就不再被建议使用,建议用blob代替.同理,long建议用clob代替. 本文从运维角度实验long raw 和 blob两种数据类型在遇到dblink时 ...
- c#之如何正确地实现IDisposable接口
见实例: public class TestClass : IDisposable { //供程序员显式调用的Dispose方法 public void Dispose() { //调用带参数的Dis ...
- 阿里云ECS服务器 常见问题(1)
无法在外网访问服务器的公网ip 解决方法: 在阿里云 云服务器ECS-安全组规则 添加端口 可更根据阿里的教程来 配置完成后即可访问!
- Vue系列之 => 模拟购物车添加小球动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- git时光机操作
A状态:代码版本A B状态:代码版本B(比A状态时增加了图片.代码) 这时,git add. git commit -m"" .push之前,意识到忘了让git忽略图片的添加,就: ...
- 【转】HTTP429
转载:http://codewa.com/question/45600.html Q:How to avoid HTTP error 429 (Too Many Requests) python Q: ...
- Linux基础命令---join
join 找出两个文件中相同的字段,根据相同字段合并两个文件,将合并结果显示到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora ...