iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎃

Trying Automated Machine Learning Training and Inference with GPT-Code UI

に公開

As a follow-up to Karaage-san's article "Trying out GPT-Code UI, an OSS implementation of Code Interpreter", I will try to automatically execute training and inference using a machine learning model, using the OSS implementation of Code Interpreter, GPT-Code UI.

What I'll be trying

I will attempt to automatically run a model that performs MNIST image classification after generating the code with PyTorch.

ChatGPT + Code Interpreter

First, I tried asking ChatGPT with Code Interpreter enabled as follows:

Demonstrate an ML model using the MNIST dataset with PyTorch.
Show some image from the dataset and print the answer.

While it generates the code, it does not actually execute it, stating that it doesn't support running ML models or loading large datasets.

GPT-Code UI

I'll try the same thing with GPT-Code UI.

First, as a preparation, I'll have it install the necessary libraries.

Install fundamental ML libraries (including PyTorch) for data scientists.

When I ask as above, it executes the pip command.

!pip install numpy pandas matplotlib seaborn scikit-learn pytorch torchvision

Then, just like in the case of ChatGPT, I'll try asking the following:

Demonstrate an ML model using the MNIST dataset with PyTorch.
Show some image from the dataset and print the answer.

Then, the following code was generated:

import torch
import torchvision
import torchvision.transforms as transforms
from torch import nn, optim
import matplotlib.pyplot as plt

# Load MNIST dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Define a feedforward neural network
model = nn.Sequential(nn.Linear(784, 128),
                      nn.ReLU(),
                      nn.Linear(128, 64),
                      nn.ReLU(),
                      nn.Linear(64, 10),
                      nn.LogSoftmax(dim=1))

criterion = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.003)

# Train the model
epochs = 5
for e in range(epochs):
    running_loss = 0
    for images, labels in trainloader:
        images = images.view(images.shape[0], -1)
        optimizer.zero_grad()
        output = model(images)
        loss = criterion(output, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

# Test the model with a single image
images, labels = next(iter(trainloader))
img = images[0].view(1, 784)

# Turn off gradients to speed up this part
with torch.no_grad():
    logps = model(img)

# Output of the network are log-probabilities, need to take exponential for probabilities
ps = torch.exp(logps)
predicted_label = ps.argmax(dim=1)

# Display the image
plt.imshow(images[0].numpy().squeeze(), cmap='gray_r')
print(f'Predicted Label: {predicted_label.item()}')

Of course, it executed the generated code properly and produced the following output. It's amazing that it even handles the automatic execution of training and inference using a machine learning model!

Conclusion

In reality, things didn't go perfectly smoothly; I had to explicitly instruct it to install PyTorch and retry whenever the generated code resulted in errors. However, even so, these results are quite interesting. I have high expectations for the future.

GitHubで編集を提案

Discussion