训练和评估PyTorch模型

huangapple go评论54阅读模式
英文:

How Fiting and evaluate model in pytorch

问题

我是PyTorch新手,之前使用过Keras,所以我写了以下代码:

import torch
import torch.optim as optim
import torch.nn as nn

# Define your PyTorch model and loss function
model = YourModel()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# Training
for epoch in range(100):
    running_loss = 0.0
    for i, data in enumerate(training_set, 0):
        inputs, labels = data

        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
    
    # Print the average loss for this epoch
    print(f'Epoch {epoch + 1}, loss: {running_loss / len(training_set)}')

print('Finished Training')

# Evaluation
correct = 0
total = 0
with torch.no_grad():
    for data in test_set:
        images, labels = data
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy: {100 * correct / total}%')

请替换YourModel为你的PyTorch模型,以及根据你的数据和模型定义进行适当的更改。这段代码将帮助你在PyTorch中执行类似的训练和评估操作。

英文:

I 'm newer in Pytorch,
I worked with keras, so I write:


history = model.fit(training_set, steps_per_epoch=2020 // 16,
        epochs=100,
        validation_data=test_set,
        validation_steps=862 // 16,
        callbacks=[early_stop, reduce_lr])



###Evaluation
loss, accuracy = model.evaluate(test_set, verbose=0)
print('loss={}, accuracy={}'.format(loss, accuracy))

So how can I transform this bloc in Pytorch
Thanks

答案1

得分: 1

In pytorch, there is no fit method or evaluate method, normally you need to define the custom training loop and your evaluate function manually. So it's basically quite low level.
If you really need the fit method, you can use pytorch lightning, which is a high-level wrapper of pytorch.
There are plenty of video tutorials on youtube, I recommend you watch some to begin with.

英文:

In pytorch, there is no fit method or evaluate method, normally you need to define the custom training loop and your evaluate function manually. So it's basically quite low lever.
If you really need the fit method, you can use pytorch lightning, which is a high lever wrapper of pytorch.
There are plenty of video tutorials on youtube, I recommend you watch some to begin with.

答案2

得分: 0

你的代码尚未完成,所以我们无法提供确切的答案。不过,我可以为你提供一个大致的示例,展示如何继续。通常需要手动编写循环,而不是使用fit()。类似这样:

import torch
import torch.nn as nn
import torch.optim as optim

model = MyModel()

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

train_loader = DataLoader(training_set, batch_size=16, shuffle=True)
test_loader = DataLoader(test_set, batch_size=16, shuffle=False)

# 训练模型
for epoch in range(100):
    running_loss = 0.0
    for i, data in enumerate(train_loader):
        inputs, labels = data

        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if i % 100 == 99:
            print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 100))
            running_loss = 0.0

    correct = 0
    total = 0
    with torch.no_grad():
        for data in test_loader:
            inputs, labels = data
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    print('测试集准确率: %d %%' % (100 * correct / total))
英文:

Your code is not complete so we can't give a exact answer. However, I just give you a rough sample of how to proceed. Loop is typically written manually instead of using the fit(). something like this:

import torch
import torch.nn as nn
import torch.optim as optim

model = MyModel()

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

train_loader = DataLoader(training_set, batch_size=16, shuffle=True)
test_loader = DataLoader(test_set, batch_size=16, shuffle=False)

# Train the model
for epoch in range(100):
    running_loss = 0.0
    for i, data in enumerate(train_loader):
        inputs, labels = data

        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if i % 100 == 99:
            print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 100))
            running_loss = 0.0

    correct = 0
    total = 0
    with torch.no_grad():
        for data in test_loader:
            inputs, labels = data
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the test set: %d %%' % (100 * correct / total))

huangapple
  • 本文由 发表于 2023年4月11日 16:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定