当前位置:网站首页>pytorch-08. Load dataset
pytorch-08. Load dataset
2022-08-10 05:55:00 【Shengxin Research Ape】
import torchfrom torch.utils.data import Dataset # Dataset abstract class, cannot be instantiated, can only be inheritedfrom torch.utils.data import DataLoader # DataLoader can be instantiatedimport numpy as npclass DiabetesDataset(Dataset):def __init__(self, filepath):xy = np.loadtxt(filepath,delimiter=',',dtype=np.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 the length of the datasetreturn self.lendataset = DiabetesDataset('diabetes.csv.gz')train_loader = DataLoader(dataset=dataset,batch_size=32,shuffle=True,num_workers=2) #num_workers read data in parallelclass 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()self.activate = torch.nn.ReLU()def forward(self,x):x = self.activate(self.linear1(x))x = self.activate(self.linear2(x))x = self.sigmoid(self.linear3(x)) #RELU, the y value when x is less than 0 is 0, and ln0 may appear when calculating the lossreturn xmodel = Model()criterion = torch.nn.BCELoss(size_average=True)optimizer = torch.optim.SGD(model.parameters(),lr=0.1)'''Enumerate is mostly used to get the count in the for loop. It can be used to obtain the index and value at the same time, that is, when the index and value values are needed, enumerate can be used'''if __name__ == '__main__':for epoch in range(100):for i,data in enumerate(train_loader,0): #0 means start from the specified index 0print('train_loader:',train_loader)# 1 Prepare datainputs,labels = data# 2 Forwardy_pred = model(inputs)loss = criterion(y_pred, labels)print(epoch,i,loss.item())#3 Backwardoptimizer.zero_grad()loss.backward()#4 Updateoptimizer.step()
边栏推荐
- Four characteristics of ACID
- 基于MNIST数据集的简单FC复现
- Content related to ZigBee network devices
- LeetCode 94. Inorder Traversal of Binary Trees (Simple)
- [List Exercise] Traverse the collection and sort by price from low to high,
- Chained Picks: Starbucks looks at digital collectibles and better engages customers
- 链读|最新最全的数字藏品发售日历-08.02
- Collection set interface
- view【】【】【】【】
- Knowledge Distillation Thesis Learning
猜你喜欢
常用类 String概述
Chain Reading|The latest and most complete digital collection sales calendar-08.02
Index Notes【】【】
generic notes()()()
I use this recruit let the team to improve the development efficiency of 100%!
IO流【】【】【】
LeetCode 100.相同的树(简单)
Chain Reading Recommendation: From Tiles to Generative NFTs
21天挑战杯MySQL-Day05
分享一款恋爱星座男女配对微信小程序源码
随机推荐
21天挑战杯MySQL-Day05
shell脚本中利用sqlplus操作数据库
微信小程序--模板与设置WXML
常用类 String概述
我不喜欢我的代码
Chain Reading|The latest and most complete digital collection sales calendar-07.29
.Net Core imports tens of millions of data to Mysql
LeetCode 1720.解码异或后的数组(简单)
我不喜欢我的代码
Chain Reading|The latest and most complete digital collection sales calendar-08.02
链读|最新最全的数字藏品发售日历-07.29
Models corresponding to each architecture instruction set
Smart contracts and DAPP decentralized applications
LeetCode 162.寻找峰值(中等)
The latest and most complete digital collection sales calendar-07.26
Convolutional Neural Network (CNN) for Clothing Image Classification
pytorch-07.处理多维特征的输入
Linux数据库Oracle客户端安装,用于shell脚本用sqlplus连接数据库
2022李宏毅机器学习hw1--COVID-19 Cases Prediction
棋类游戏-五子棋小游戏