import torch
from torch.utils.data import Dataset #Dataset是抽象类,不能实例化,只能继承
from torch.utils.data import DataLoader
import numpy as np
class Set_Dataset(Dataset):
def __init__(self, filepath):
xy = np.loadtxt(filepath, delimiter=',', dtype='float32')
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[:, :-1])
self.y_data = torch.from_numpy(xy[:, [-1]])
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
pytorch学习3之加载数据集
最新推荐文章于 2024-01-14 10:41:17 发布
本文介绍了如何在PyTorch中加载数据集,包括DataLoader的使用,如设置`shuffle`参数用于每轮训练时的数据洗牌,`batch_size`指定每个批次样本数量,以及`num_workers`设定多进程加载数据的线程数。通过`enumerate(train_loader, 0)`进行遍历,详细解析了其内部的可迭代序列和起始位置。"
92264596,8257132,嵌入式定时器/计数器实验解析,"['嵌入式开发', '定时器实验', '微控制器']
摘要由CSDN通过智能技术生成