4-1-6-13. Fashion-MNIST Solution

Again.

So, in the last video,

I hand you try out building

your own neural network to classify this fashion in this dataset.

Here is my solution like how I decided to build this.

So first, building our network.

So, here, I’m going to import our normal modules from PyTorch.

So, nn and optim, so,

nn is going to allow us to build our network,

and optim is going to give us our optimizers.

I must going to import this functional modules, so,

we can use functions like ReLU and log softmax.

I decided to define my network architectures using the class.

So, in nn.modules subclassing from this,

and it’s called a classifier.

Then I created four different linear transformations.

So, in this case, it’s three hidden layers and then one output layer.

Our first hidden layer has 256 units.

The second hidden layer has a 128,

one after that has 64.

Then our output has 10 units.

So, in the forward pass, I did something a little different.

So, I made sure here that the input tensor is actually flattened.

So now, you don’t have to flatten your input tensors in the training loop,

it’ll just do it in the forward pass itself.

So, to do this is do x.view,

which is going to change our shape.

So, x.shape zero is going to give us our batch size.

Then the negative one here is going to basically fill out

the the second dimension with as many elements as it

needs to keep the same total number of elements.

So, what this does is it basically gives us another tensor,

that is the flattened version of our input tensor.

It doing a pass these through our linear transformations,

and then ReLU activation functions.

Then finally, we use a log softmax with a dimension set to one,

as our output, and return that from our forward function.

With the model defined,

I can do model equals classifiers.

So, this actually creates our model.

Then we define our criterion with the negative log likelihood loss.

So, I’m using log softmax as the output my model.

So, I want to use the NLLLoss as the criterion.

Then, here I’m using the Adam optimizer.

So, this is basically the same as stochastic gradient descent,

but it has some nice properties where it uses

momentum which speeds up the actual fitting process.

It also adjust the learning rate for each of the individual parameters in your model.

Here, I wrote my training loop.

So, again I’m using five epochs.

So, for e in range epoch, so,

this is going to basically loop through our dataset five times,

I’m tracking the loss with running loss,

and just kind of instantiated it here.

Then, getting our images.

So, from images labels in train loader,

so I get our log probabilities by passing in the images to a model.

So, one thing to note, you can kind of do a little shortcut.

If you just pass these in to model as if it was a function,

then it will run the forward method.

So, this is just a kind of a shorter way to run the forward pass through your model.

Then with the log probabilities and the labels,

I can calculate the loss.

Then here, I am zeroing the gradients.

Now I’m doing the lost up backwards to calculating our gradients,

and then with the gradients, I can do our optimizer step.

If we tried it, we can see at least for

these first five epochs that are loss actually drops.

Now the network is trained,

we can actually test it out.

So, we pass in data to our model, calculate the probabilities.

So, here, doing the forward pass through the model to

get our actual log probabilities and with the log probabilities,

you can take the exponential to get the actual probabilities.

Then with that, we can pass it into this nice little view classify function that I wrote,

and it shows us,

if we pass an image of a shirt,

it tells us that it’s a shirt.

So, our network seems to have learned fairly well,

what this dataset is showing us.

다시.

그래서 지난 영상에서

나는 당신에게 건물을 시험해 봅니다.

이 데이터 세트에서 이 패션을 분류하기 위한 자신의 신경망.

다음은 내가 이것을 구축하기로 결정한 방법과 같은 솔루션입니다.

따라서 먼저 네트워크를 구축합니다.

여기에서는 PyTorch에서 일반 모듈을 가져올 것입니다.

그래서, nn과 optim, 그래서,

nn을 사용하면 네트워크를 구축할 수 있습니다.

optim은 우리에게 최적화 프로그램을 제공할 것입니다.

이 기능적 모듈을 가져와야 하므로,

ReLU 및 log softmax와 같은 기능을 사용할 수 있습니다.

클래스를 사용하여 네트워크 아키텍처를 정의하기로 결정했습니다.

그래서, nn.modules에서 이것으로부터 서브클래싱,

분류기라고 합니다.

그런 다음 네 가지 선형 변환을 만들었습니다.

따라서 이 경우 3개의 은닉층과 1개의 출력층입니다.

첫 번째 은닉층에는 256개의 단위가 있습니다.

두 번째 은닉층은 128,

그 다음은 64개입니다.

그런 다음 출력에는 10개의 단위가 있습니다.

그래서 포워드 패스에서는 조금 다른 작업을 했습니다.

그래서 여기에서 입력 텐서가 실제로 평평해졌는지 확인했습니다.

이제 훈련 루프에서 입력 텐서를 평면화할 필요가 없습니다.

그냥 순방향 패스 자체에서 할 것입니다.

그래서, 이것을 하기 위해 x.view를 하고,

우리의 모양을 바꿀 것입니다.

따라서 x.shape 0은 배치 크기를 알려줍니다.

그러면 여기에 있는 부정적인 것이 기본적으로 채워질 것입니다.

요소가 많은 두 번째 차원

동일한 총 요소 수를 유지해야 합니다.

그래서 이것이 하는 일은 기본적으로 우리에게 또 다른 텐서를 제공한다는 것입니다.

그것은 우리의 입력 텐서의 평평한 버전입니다.

선형 변환을 통해 이를 전달합니다.

그런 다음 ReLU 활성화 기능.

그런 다음 마지막으로 차원이 1로 설정된 log softmax를 사용합니다.

우리의 출력으로, 우리의 앞으로 함수에서 그것을 반환합니다.

정의된 모델로,

모델 같음 분류기를 할 수 있습니다.

이것은 실제로 우리의 모델을 생성합니다.

그런 다음 음의 로그 가능성 손실로 기준을 정의합니다.

그래서 내 모델의 출력으로 log softmax를 사용하고 있습니다.

따라서 NLLLoss를 기준으로 사용하고 싶습니다.

그런 다음 여기에서는 Adam 최적화 프로그램을 사용하고 있습니다.

이것은 기본적으로 확률적 경사하강법과 동일합니다.

그러나 그것은 그것을 사용하는 몇 가지 좋은 속성을 가지고 있습니다

실제 피팅 프로세스를 가속화하는 모멘텀.

또한 모델의 개별 매개변수 각각에 대한 학습률을 조정합니다.

여기에서 훈련 루프를 작성했습니다.

그래서 다시 저는 5개의 에포크를 사용하고 있습니다.

따라서 범위 신기원의 e에 대해

이것은 기본적으로 데이터세트를 5번 반복할 것입니다.

나는 달리기 손실로 손실을 추적하고 있습니다.

여기에서 일종의 인스턴스화했습니다.

그런 다음 이미지를 가져옵니다.

따라서 기차 로더의 이미지 레이블에서

그래서 이미지를 모델에 전달하여 로그 확률을 얻습니다.

따라서 한 가지 주의할 점은 약간의 지름길을 만들 수 있다는 것입니다.

이것을 마치 함수인 것처럼 모델에 전달하면,

그러면 forward 메서드가 실행됩니다.

따라서 이것은 모델을 통해 정방향 패스를 실행하는 일종의 짧은 방법일 뿐입니다.

그런 다음 로그 확률과 레이블을 사용하여

손실을 계산할 수 있습니다.

그런 다음 여기에서 그라디언트를 0으로 만듭니다.

이제 저는 기울기를 계산하기 위해 역방향 손실을 하고 있습니다.

그런 다음 그라디언트를 사용하여 최적화 단계를 수행할 수 있습니다.

우리가 그것을 시도한다면, 우리는 적어도

손실이 발생한 처음 5개의 에포크는 실제로 떨어집니다.

이제 네트워크가 훈련되고,

우리는 실제로 그것을 테스트할 수 있습니다.

그래서 우리는 데이터를 모델에 전달하고 확률을 계산합니다.

따라서 여기에서 모델을 통해 순방향 전달을 수행하여

실제 로그 확률을 구하고 로그 확률을 사용하여

지수를 취하여 실제 확률을 얻을 수 있습니다.

그런 다음 이를 통해 내가 작성한 이 멋진 보기 분류 함수에 전달할 수 있습니다.

그리고 그것은 우리에게 보여줍니다.

셔츠 이미지를 전달하면

그것은 우리에게 그것이 셔츠라고 말합니다.

따라서 우리 네트워크는 상당히 잘 학습한 것 같습니다.

이 데이터 세트가 우리에게 보여주는 것.

import torch
from torchvision import datasets, transforms
import helper

# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5,), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)

image, label = next(iter(trainloader))
helper.imshow(image[0,:]);



# TODO: Define your network architecture here
class Classifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 256)
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 64)
        self.fc4 = nn.Linear(64, 10)
        
    def forward(self, x):
        # make sure input tensor is flattened
        x = x.view(x.shape[0], -1)
        
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = F.log_softmax(self.fc4(x), dim=1)
        
        return x


# TODO: Create the network, define the criterion and optimizer
model = Classifier()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=0.003)      
      
      
# TODO: Train the network here
epochs = 5

for e in range(epochs):
    running_loss = 0
    for images, labels in trainloader:
        log_ps = model(images)
        loss = criterion(log_ps, labels)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        running_loss += loss.item()
    else:
        print(f"Training loss: {running_loss/len(trainloader)}")



import helper

# Test out your network!

dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[0]
# Convert 2D image to 1D vector
img = img.resize_(1, 784)



# TODO: Calculate the class probabilities (softmax) for img
ps = torch.exp(model(img))



# Plot the image and probabilities
helper.view_classify(img.resize_(1, 28, 28), ps, version='Fashion')

Dr. Serendipity에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

Continue reading