pytorch复习笔记--python类中getitem的用法

目录

1-- 类中__getitem__的作用

2-- 实例

3-- 结合pytorch封装并读取batch数据

4-- 参考


1-- 类中__getitem__的作用

当一个python类中定义了__getitem__函数,则其实例对象能够通过下标来进行索引数据

2-- 实例

代码:

import numpy as np

# 创建类
class Example():
    def __getitem__(self, index):
        data = np.array([[1,2,3], [4,5,6], [7,8,9]])
        return data[index]

# 使用Example类实例对象example1
example1 = Example()

# 索引访问数据
print('example1[0][0]:', example1[0][0])
print('example1[0]:', example1[0])

# 切片访问数据
print('example1[0:2]:\n', example1[0:2])

输出:

example1[0][0]: 1

example1[0]: [1 2 3]

example1[0:2]:
 [[1 2 3]
 [4 5 6]]

3-- 结合pytorch封装并读取batch数据

代码:

import torch
import numpy as np
from torch.utils.data import Dataset

# 创建MyDataset类
class MyDataset(Dataset):
    def __init__(self, x, y):
        self.data = torch.from_numpy(x).float()
        self.label = torch.LongTensor(y)

    def __getitem__(self, idx):
        return self.data[idx], self.label[idx], idx

    def __len__(self):
        return len(self.data)

Train_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
Train_label = np.array([10, 11, 12, 13])
TrainDataset = MyDataset(Train_data, Train_label) # 创建实例对象
print('len:', len(TrainDataset))

# 创建DataLoader
loader = torch.utils.data.DataLoader(
    dataset=TrainDataset,
    batch_size=2,
    shuffle=False,
    num_workers=0,
    drop_last=False)

# 按batchsize打印数据
for batch_idx, (data, label, index) in enumerate(loader):
    print('batch_idx:',batch_idx, '\ndata:',data, '\nlabel:',label, '\nindex:',index)
    print('---------')

输出:

len: 4

batch_idx: 0 
data: tensor([[1., 2., 3.],
        [4., 5., 6.]]) 
label: tensor([10, 11]) 
index: tensor([0, 1])
---------
batch_idx: 1 
data: tensor([[ 7.,  8.,  9.],
        [10., 11., 12.]]) 
label: tensor([12, 13]) 
index: tensor([2, 3])
---------

4-- 参考

参考链接1

  • 14
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值