참가자 모집과제 수행 출처 및 참고
pytorch_lighting install

찾아보니 애초에 3.9까지 밖에 지원을 안한다... 가상환경을 새로 설치하자 다음부터는 공식 문서를 참조하자...
가상환경을 새로 생성하고(3.9) 커널도 생성하자 블로그 참조
conda create --name lightning python=3.9
conda install ipykernel -y
ipython kernel install --user --name=[lightning]
conda install jupyter -y
conda install jupyterlab -y
conda install -c anaconda numpy -y
conda install -c anaconda pandas -y
conda install -c conda-forge matplotlib -y
conda install -c anaconda scikit-learn -y
conda install -c conda-forge opencv -y
conda install -c conda-forge tqdm -y
conda install pytorch-lightning -c conda-forge -y
conda install -c conda-forge jupyterlab_widgets -y
conda install -c conda-forge ipywidgets-y
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
드디어 error없이 깔끔하게 된다!
from pytorch_lightning import LightningDataModule
가 안되는경우가 있다. 아직 python = 3.8.10으로 해야할 것 같다. 안됨...
import os
from collections import OrderedDict
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
from pytorch_lightning import LightningDataModule, LightningModule, Trainer
from torch.utils.data import DataLoader, random_split
from torchvision.datasets import MNIST필요한 파일을 import
class MNISTModel(LightningModule):
def __init__(self):
super().__init__()
self.l1 = torch.nn.Linear(28 * 28, 10) #linear를 생성
def forward(self, x):
return torch.relu(self.l1(x.view(x.size(0), -1)))
def training_step(self, batch, batch_nb): #정의 구간?
x, y = batch
loss = F.cross_entropy(self(x), y)
return loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=0.02) #optimizer를 설정하는 곳인가 보다. training부분 마치 keras나 sklearn같다.
mnist_model = MNISTModel()
# Init DataLoader from MNIST Dataset
train_ds = MNIST(PATH_DATASETS, train=True, download=True, transform=transforms.ToTensor())
train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE)
# Initialize a trainer
trainer = Trainer(
gpus=AVAIL_GPUS,
max_epochs=3,
progress_bar_refresh_rate=20,
)
# Train the model ⚡
trainer.fit(mnist_model, train_loader)


