写一个pytorch构建自定义数据集、模型初始化、推理,然后,计算loss和准确率
时间: 2024-01-23 07:04:23 浏览: 109
好的,这是一个完整的pytorch代码示例,包括构建自定义数据集、模型初始化、推理、计算loss和准确率。
首先,我们需要导入所需的库:
```python
import torch
from torch.utils.data import Dataset, DataLoader
from torch import nn
from torch.optim import Adam
```
接下来,我们定义一个自定义数据集类,此处我们使用一个简单的示例数据集:
```python
class CustomDataset(Dataset):
def __init__(self):
self.data = [(torch.randn(10), torch.randint(0, 2, (1,))) for _ in range(100)]
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]
```
接下来,我们定义一个简单的模型,包括一个全连接层和一个sigmoid激活函数:
```python
class CustomModel(nn.Module):
def __init__(self):
super(CustomModel, self).__init__()
self.fc = nn.Linear(10, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.fc(x)
x = self.sigmoid(x)
return x
```
然后,我们可以定义一个函数来计算loss和准确率:
```python
def calculate_loss_and_accuracy(model, dataloader, criterion):
loss = 0
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in dataloader:
outputs = model(inputs)
loss += criterion(outputs, labels.float())
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels).sum().item()
loss = loss / len(dataloader)
accuracy = correct / total
return loss, accuracy
```
接下来,我们可以定义训练函数:
```python
def train(model, train_dataloader, valid_dataloader, epochs):
criterion = nn.BCELoss()
optimizer = Adam(model.parameters())
for epoch in range(epochs):
model.train()
for inputs, labels in train_dataloader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels.float())
loss.backward()
optimizer.step()
model.eval()
train_loss, train_accuracy = calculate_loss_and_accuracy(model, train_dataloader, criterion)
valid_loss, valid_accuracy = calculate_loss_and_accuracy(model, valid_dataloader, criterion)
print(f"Epoch {epoch+1}: Train Loss={train_loss:.4f}, Train Accuracy={train_accuracy:.4f}, Valid Loss={valid_loss:.4f}, Valid Accuracy={valid_accuracy:.4f}")
```
最后,我们可以加载数据集、初始化模型并进行训练:
```python
train_dataset = CustomDataset()
valid_dataset = CustomDataset()
train_dataloader = DataLoader(train_dataset, batch_size=16, shuffle=True)
valid_dataloader = DataLoader(valid_dataset, batch_size=16, shuffle=False)
model = CustomModel()
train(model, train_dataloader, valid_dataloader, epochs=10)
```
这样,我们就完成了一个简单的pytorch代码示例,包括构建自定义数据集、模型初始化、推理、计算loss和准确率。
阅读全文