Pytorch利用dataloader定义batch数目为16,建立一个全连接神经网络,并以f1评估模型
时间: 2024-03-10 21:47:01 浏览: 66
Pytorch实现神经网络的分类方式
以下是利用dataloader定义batch数目为16,建立一个全连接神经网络,并以f1评估模型的示例代码:
```python
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset, BatchSampler
from sklearn.metrics import f1_score
# 定义数据集
tensor_data = torch.randn(14807, 24)
tensor_label = torch.randint(0, 2, (14807,))
# 定义BatchSampler
batch_size = 16
batch_sampler = BatchSampler(range(len(tensor_data)), batch_size=batch_size, drop_last=False)
# 定义数据集和dataloader
dataset = TensorDataset(tensor_data, tensor_label)
dataloader = DataLoader(dataset, batch_sampler=batch_sampler)
# 定义全连接神经网络
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(24, 32)
self.fc2 = nn.Linear(32, 2)
def forward(self, x):
x = self.fc1(x)
x = nn.functional.relu(x)
x = self.fc2(x)
return x
net = Net()
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# 训练神经网络
for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(dataloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 1000 == 999:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 1000))
running_loss = 0.0
print('Finished Training')
# 测试神经网络并计算f1-score
net.eval()
y_true = []
y_pred = []
for data in dataloader:
inputs, labels = data
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
y_true += labels.tolist()
y_pred += predicted.tolist()
f1 = f1_score(y_true, y_pred)
print('f1-score: %.3f' % f1)
```
以上代码中,在训练神经网络后,需要将神经网络设置为评估模式,即使用`net.eval()`。然后,遍历测试数据集,对于每个batch的数据,将输出的预测值和真实标签保存在列表中,最后计算f1-score。
阅读全文