首页 > 其他分享 >3-2中阶API示范

3-2中阶API示范

时间:2024-03-05 22:33:58浏览次数:29  
标签:loss 中阶 metric torch 示范 API plt import model

  • 下面的范例使用Pytorch的低阶API实现线性回归和DNN二分类
  • Pytorch的中阶API主要包括各种模型层,损失函数,优化器,数据管道等。
import os
import datetime

# 打印时间
def printbar():
    nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print('\n' + '========='*8 + '%s' % nowtime)

#mac系统上pytorch和matplotlib在jupyter中同时跑需要更改环境变量
# os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" 

import torch
print('torch.__version__=' + torch.__version__)

"""
torch.__version__=2.1.1+cu118
"""

1.线性回归模型

# 准备数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader, TensorDataset

#样本数量
n = 400

# 生成测试用数据集
X = 10*torch.rand([n,2])-5.0  #torch.rand是均匀分布 
w0 = torch.tensor([[2.0],[-3.0]])
b0 = torch.tensor([[10.0]])
Y = X@w0 + b0 + torch.normal( 0.0,2.0,size = [n,1])  # @表示矩阵乘法,增加正态扰动

# 数据可视化

%matplotlib inline
%config InlineBackend.figure_format = 'svg'

plt.figure(figsize = (12,5))
ax1 = plt.subplot(121)
ax1.scatter(X[:,0],Y[:,0], c = "b",label = "samples")
ax1.legend()
plt.xlabel("x1")
plt.ylabel("y",rotation = 0)

ax2 = plt.subplot(122)
ax2.scatter(X[:,1],Y[:,0], c = "g",label = "samples")
ax2.legend()
plt.xlabel("x2")
plt.ylabel("y",rotation = 0)
plt.show()

# 构建输入数据管道
ds = TensorDataset(X, Y)
dl = DataLoader(ds, batch_size=10, shuffle=True, num_workers=2)
# 定义模型
model = nn.Linear(2, 1)
model.loss_fn = nn.MSELoss()
model.optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# 训练模型
def train_step(model, features, labels):
    predictions = model(features)
    loss = model.loss_fn(predictions, labels)
    loss.backward()
    model.optimizer.step()
    model.optimizer.zero_grad()
    return loss.item()

# 测试train_step效果
features, labels = next(iter(dl))
train_step(model, features, labels)

def train_model(model, epochs):
    for epoch in range(1, epochs+1):
        for features, labels in dl:
            loss = train_step(model, features, labels)
        if epoch % 10 == 0:
            printbar()
            w = model.state_dict()['weight']
            b = model.state_dict()['bias']
            print("epoch =",epoch,"loss = ",loss)
            print("w =",w)
            print("b =",b)
train_model(model,epochs = 50)
# 结果可视化
%matplotlib inline
%config InlineBackend.figure_format='svg'


plt.figure(figsize = (12,5))
ax1 = plt.subplot(121)
ax1.scatter(X[:,0].numpy(),Y[:,0].numpy(), c = "b",label = "samples")
ax1.plot(X[:,0].numpy(),(model.w[0].data*X[:,0]+model.b[0].data).numpy(),"-r",linewidth = 5.0,label = "model")
ax1.legend()
plt.xlabel("x1")
plt.ylabel("y",rotation = 0)


ax2 = plt.subplot(122)
ax2.scatter(X[:,1].numpy(),Y[:,0].numpy(), c = "g",label = "samples")
ax2.plot(X[:,1].numpy(),(model.w[1].data*X[:,1]+model.b[0].data).numpy(),"-r",linewidth = 5.0,label = "model")
ax2.legend()
plt.xlabel("x2")
plt.ylabel("y",rotation = 0)

plt.show()

2.DNN二分类模型

# 准备数据
import numpy as np 
import pandas as pd 
from matplotlib import pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader,TensorDataset
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

#正负样本数量
n_positive,n_negative = 2000,2000

#生成正样本, 小圆环分布
r_p = 5.0 + torch.normal(0.0,1.0,size = [n_positive,1]) 
theta_p = 2*np.pi*torch.rand([n_positive,1])
Xp = torch.cat([r_p*torch.cos(theta_p),r_p*torch.sin(theta_p)],axis = 1)
Yp = torch.ones_like(r_p)

#生成负样本, 大圆环分布
r_n = 8.0 + torch.normal(0.0,1.0,size = [n_negative,1]) 
theta_n = 2*np.pi*torch.rand([n_negative,1])
Xn = torch.cat([r_n*torch.cos(theta_n),r_n*torch.sin(theta_n)],axis = 1)
Yn = torch.zeros_like(r_n)

#汇总样本
X = torch.cat([Xp,Xn],axis = 0)
Y = torch.cat([Yp,Yn],axis = 0)


#可视化
plt.figure(figsize = (6,6))
plt.scatter(Xp[:,0],Xp[:,1],c = "r")
plt.scatter(Xn[:,0],Xn[:,1],c = "g")
plt.legend(["positive","negative"]);

# 构建输入数据管道
ds = TensorDataset(X, Y)
dl = DataLoader(ds, batch_size=10, shuffle=True, num_workers=2)
# 定义模型
class DNNModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(2, 4)
        self.fc2 = nn.Linear(4, 8)
        self.fc3 = nn.Linear(8, 1)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        y = nn.Sigmoid()(self.fc3(x))
        return y

    def loss_fn(self, y_pred, y_true):
        return nn.BCELoss()(y_pred, y_true)

    def metric_fn(self, y_pred, y_true):
        y_pred = torch.where(y_pred > 0.5, torch.ones_like(y_pred, dtype=torch.float32),
                            torch.zeros_like(y_pred, dtype=torch.float32))
        acc = torch.mean(1 - torch.abs(y_true - y_pred))
        return acc

    @property
    def optimizer(self):
        return torch.optim.Adam(self.parameters(), lr=0.001)

model = DNNModel()

# 测试模型结构
features, labels = next(iter(dl))
predictions = model(features)
loss = model.loss_fn(predictions, labels)
metric = model.metric_fn(predictions, labels)

print('init loss:', loss.item())
print('init metric:', metric.item())
"""
init loss: 0.7185380458831787
init metric: 0.6000000238418579
"""
# 训练模型
def train_step(model, features, labels):
    
    # 正向传播求损失
    predictions = model(features)
    loss = model.loss_fn(predictions,labels)
    metric = model.metric_fn(predictions,labels)
    
    # 反向传播求梯度
    loss.backward()
    
    # 更新模型参数
    model.optimizer.step()
    model.optimizer.zero_grad()
    
    return loss.item(),metric.item()

def train_model(model,epochs):
    for epoch in range(1,epochs+1):
        loss_list,metric_list = [],[]
        for features, labels in dl:
            lossi,metrici = train_step(model,features,labels)
            loss_list.append(lossi)
            metric_list.append(metrici)
        loss = np.mean(loss_list)
        metric = np.mean(metric_list)

        if epoch % 10 == 0:
            printbar()
            print("epoch =",epoch,"loss = ",loss,"metric = ",metric)
        
train_model(model,epochs = 50)

# 结果可视化
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize = (12,5))
ax1.scatter(Xp[:,0],Xp[:,1], c="r")
ax1.scatter(Xn[:,0],Xn[:,1],c = "g")
ax1.legend(["positive","negative"]);
ax1.set_title("y_true");

Xp_pred = X[torch.squeeze(model.forward(X)>=0.5)]
Xn_pred = X[torch.squeeze(model.forward(X)<0.5)]

ax2.scatter(Xp_pred[:,0],Xp_pred[:,1],c = "r")
ax2.scatter(Xn_pred[:,0],Xn_pred[:,1],c = "g")
ax2.legend(["positive","negative"]);
ax2.set_title("y_pred");

标签:loss,中阶,metric,torch,示范,API,plt,import,model
From: https://www.cnblogs.com/lotuslaw/p/18055379

相关文章

  • Vue3.0里为什么要用 Proxy API 替代 defineProperty API ?
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一、Object.defineProperty定义:Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性,并返回此对象为什么能实现响应式通过defineProperty 两个属性,get及setget属性......
  • C# WinForm基于owin创建WebApi
    在实际的项目开发中,可能会有在WinForm程序中提供Web服务器的需求。通过owin可以很方便的实现,并且可提供Web静态文件访问服务。操作方法:1.在NuGet引用owinMicrosoft.AspNet.WebApi.OwinMicrosoft.AspNet.WebApi.OwinSelfHostMicrosoft.Owin.StaticFiles2.添加服务启动配置类 ......
  • 使用go写的一个api接口
    记录一下使用go写的一些脚本packagemainimport( "encoding/json" "fmt" "log" "net/http" "os" "os/exec" "strconv" "strings" "sync" "time")var( requestCo......
  • resurfaceio graylog 的api 安全方案
    resurfaceio是graylog的api安全方案,包含的特性特性简易的api调用捕捉立即攻击以及异常的rest以及graphqlapi处理基于webhook,sql查询,以及数据导出自动化处理快速部署本地或者基于k8s的云环境架构设计resurfaceio对于流量的处理基于了goreplay扩展参考网络流量......
  • 阿里巴巴/1688 api接口 获取商品详情 数据采集
    iDataRiver平台https://www.idatariver.com/zh-cn/提供开箱即用的阿里巴巴1688电商数据采集API,供用户按需调用。接口使用详情请参考阿里巴巴1688接口文档接口列表1.获取商品详情参数类型是否必填默认值示例值描述apikeystring是idr_***从控制台里复制api......
  • 3-1低阶API示范
    下面的范例使用Pytorch的低阶API实现线性回归和DNN二分类低阶API主要包括张量操作,计算图和自动微分importosimportdatetime#打印时间defprintbar():nowtime=datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S')print('\n'+'========='*8+'%s'......
  • 37vector容器与API
    vector容器与API#include<iostream>#include<vector>usingnamespacestd;/*vector容器:向量容器底层数据结构:动态开辟的数组,每次以原来空间2倍进行扩容vector<int>vec;增加:vec.push_back(20);末尾添加元素O(1)导致容器扩容vec.insert(it,20);it迭代器指向的位......
  • 38deque, list及其API
    deque,list及其APIdeque:双端队列容器。底层数据结构:动态开辟的二维数组,一维数组是指针数组,长度从2开始,以2倍的方式进行扩容,每次扩容后,原来第二维的数组,从新的第一维数组的下标oldsize/2开始存放,上下都预留相同的空行,方便支持deque的首尾元素添加。deque<int>deq;......
  • ChatGPT4.0_API
    importopenaiimportjsonimportos#openai.api_key=get_api_key()openai.api_key="yourkey"#q="用python实现:提示手动输入3个不同的3位数区间,输入结束后计算这3个区间的交集,并输出结果区间"#q="WhatisthvbScript?"#q="翻译成日语:市场非常有潜力"#q="北京、......
  • 微信小程序开发:接入阿里云人像动漫化api接口
    前面我已经把腾讯云的人像转动漫化接口接到了我的小程序里,但是和阿里云的对比后,发现阿里云的效果会更好一些,且支持更多特效,如下: 我比较喜欢这个3D特效风格,动画3D也可以,大家拭目以待。话说上次接了腾讯云的人像转动漫接口,小程序提审后居然没过,说什么我的小程序设计AI合成: ......