首页 > 其他分享 >3-1低阶API示范

3-1低阶API示范

时间:2024-03-04 22:58:06浏览次数:35  
标签:loss plt self torch 示范 labels API model 低阶

  • 下面的范例使用Pytorch的低阶API实现线性回归和DNN二分类
  • 低阶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

# 样本数量
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].numpy(), Y[:, 0].numpy(), c='b', label='samples')
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.legend()
plt.xlabel("x2")
plt.ylabel("y",rotation = 0)
plt.show()

# 构建数据管道迭代器
def data_iter(features, labels, batch_size=8):
    num_examples = len(features)
    indices = list(range(num_examples))
    np.random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        indexs = torch.LongTensor(indices[i: min(i+batch_size, num_examples)])
        yield features.index_select(0, indexs), labels.index_select(0, indexs)
        
# 测试数据管道效果
batch_size = 8
features, labels = next(data_iter(X, Y, batch_size))
print(features)
print(labels)

"""
tensor([[ 0.5934,  0.2840],
        [ 4.5548, -4.5195],
        [ 0.1557,  1.7895],
        [-3.6943, -3.1508],
        [-4.4599,  3.5827],
        [ 4.3526,  3.9085],
        [ 4.6064,  4.2197],
        [-3.9680,  3.2592]])
tensor([[  7.5161],
        [ 29.0174],
        [  5.4269],
        [ 14.0158],
        [-10.6806],
        [  8.7862],
        [  7.8535],
        [ -2.4455]])
"""
class LinearRegression:
    def __init__(self):
        self.w = torch.randn_like(w0, requires_grad=True)
        self.b = torch.zeros_like(b0, requires_grad=True)

    # 正向传播
    def forward(self, x):
        return [email protected] + self.b

    # 损失函数'
    def loss_fn(self, y_pred, y_true):
        return torch.mean((y_pred - y_true) ** 2 / 2)
    
model = LinearRegression()

def train_step(model, features, labels):
    predictions = model.forward(features)
    loss = model.loss_fn(predictions, labels)

    # 反向传播求梯度
    loss.backward()

    # 使用torch.no_grad()避免梯度记录,也可以通过操作model.w.data实现避免梯度记录
    with torch.no_grad():
        model.w -= 0.001 * model.w.grad
        model.b -= 0.001 * model.b.grad

        # 梯度清零
        model.w.grad.zero_()
        model.b.grad.zero_()
    return loss

# 测试train_step效果
batch_size = 10
features, labels = next(data_iter(X, Y, batch_size))
train_step(model, features, labels)
"""
tensor(128.2067, grad_fn=<MeanBackward0>)
"""

def train_model(model, epochs):
    for epoch in range(1, epochs+1):
        for features, labels in data_iter(X, Y, 10):
            loss = train_step(model, features, labels)

        if epoch % 20 == 0:
            printbar()
            print('epoch =', epoch, 'loss = ', loss.item())
            print('model.w =', model.w.data)
            print('model.b = ', model.b.data)
train_model(model, epochs=200)
# 结果可视化
%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
import matplotlib.pyplot as plt
import torch
%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)

#生成负样本, 大圆环分布
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)

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

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

# 构建数据管道迭代器
def data_iter(features, labels, batch_size=8):
    num_examples = len(features)
    indices = list(range(num_examples))
    np.random.shuffle(indices)  #样本的读取顺序是随机的
    for i in range(0, num_examples, batch_size):
        indexs = torch.LongTensor(indices[i: min(i + batch_size, num_examples)])
        yield  features.index_select(0, indexs), labels.index_select(0, indexs)
        
# 测试数据管道效果   
batch_size = 8
(features,labels) = next(data_iter(X,Y,batch_size))
print(features)
print(labels)

"""
tensor([[ 2.0455, -8.4665],
        [-4.2541, -2.2007],
        [ 1.3186, -4.0994],
        [ 1.1789, -6.0809],
        [ 3.2550, -2.1135],
        [-7.4584, -2.3193],
        [-2.2631,  7.4502],
        [-6.7228,  5.1542]])
tensor([[0.],
        [1.],
        [1.],
        [0.],
        [1.],
        [0.],
        [0.],
        [0.]])
"""
class DNNModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.w1 = torch.nn.Parameter(torch.randn(2, 4))
        self.b1 = torch.nn.Parameter(torch.zeros(1, 4))
        self.w2 = torch.nn.Parameter(torch.randn(4, 8))
        self.b2 = torch.nn.Parameter(torch.zeros(1, 8))
        self.w3 = torch.nn.Parameter(torch.randn(8, 1))
        self.b3 = torch.nn.Parameter(torch.zeros(1, 1))

    def forward(self, x):
        x = torch.relu([email protected] + self.b1)
        x = torch.relu([email protected] + self.b2)
        y = torch.sigmoid([email protected] + self.b3)
        return y

    def loss_fn(self, y_pred, y_true):
        eps = 1e-7
        y_pred = torch.clamp(y_pred, eps, 1.0-eps)
        bce = -y_true*torch.log(y_pred) - (1-y_true)*torch.log(1-y_pred)
        return torch.mean(bce)

    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
    
model = DNNModel()

# 测试模型结构
batch_size = 10
(features,labels) = next(data_iter(X,Y,batch_size))

predictions = model(features)

loss = model.loss_fn(labels,predictions)
metric = model.metric_fn(labels,predictions)

print("init loss:", loss.item())
print("init metric:", metric.item())
"""
init loss: 8.984026908874512
init metric: 0.4419798254966736
"""
def train_step(model, features, labels):   
    
    # 正向传播求损失
    predictions = model.forward(features)
    loss = model.loss_fn(predictions,labels)
    metric = model.metric_fn(predictions,labels)
        
    # 反向传播求梯度
    loss.backward()
    
    # 梯度下降法更新参数
    for param in model.parameters():
        #注意是对param.data进行重新赋值,避免此处操作引起梯度记录
        param.data = (param.data - 0.01*param.grad.data) 
        
    # 梯度清零
    model.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 data_iter(X,Y,20):
            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 = 100)

# 结果可视化
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,plt,self,torch,示范,labels,API,model,低阶
From: https://www.cnblogs.com/lotuslaw/p/18052914

相关文章

  • 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合成: ......
  • 七款顶级API测试工具
    在现代软件开发中,API接口已成为不可或缺的核心组件。鉴于API缺乏直观的图形用户界面(GUI),使得直接对接口进行测试变得困难,对于前后端开发者而言,选择一套高效且实用的工具来测试API接口,从而验证开发的程序是否满足预期,显得尤为重要。在处理大量API时,手动测试不仅效率低下,而且容易出错......
  • 气象信息API
    用WPF技术搭建简单的气象免费小控件,注册取得key 前端代码:<Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="611*"/><ColumnDefinitionWidth="189*"/></Grid.ColumnDefinitions><......
  • C# 调用Web Api post提交json格式
    转载:https://blog.csdn.net/q_17600689511/article/details/82735172?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-82735172-blog-86551903.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=di......
  • 百度地图API自定义地图
    <!DOCTYPEHTMLPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content......
  • 淘宝电商api接口 获取商品详情 搜索商品
    iDataRiver平台https://www.idatariver.com/zh-cn/提供开箱即用的taobao淘宝电商数据采集API,供用户按需调用。接口使用详情请参考淘宝接口文档接口列表1.获取商品详情参数类型是否必填默认值示例值描述apikeystring是idr_***从控制台里复制apikeyitem......
  • Asp.Net Core Web Api 对于ControllerBase的一些个人习惯扩展
    ApiResponse类是我定义的返回基类,Code我定的是0成功不等于0失败,对于Data我习惯于写object类型,不习惯写泛型,我习惯于给ControllerBase写扩展来定义我的返回方法,在写一个类继承于ControllerBase让后控制器再去继承这个类,也是可以的对于GetUserCode和GetRoleCode是方便获取to......