
21
장
신경망
459
21.5
다중 분류기 훈련하기
과제
다중 분류 신경망을 훈련하고 싶습니다.
해결
파이토치를 사용하여 출력층에 소프트맥스 활성화 함수를 사용하는 피드포워드 신경망
을 만듭니다.
# 라이브러리를 임포트합니다.
import torch
import torch.nn as nn
import numpy as np
from torch.utils.data import DataLoader, TensorDataset
from torch.optim import RMSprop
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
N_CLASSES=3
EPOCHS=3
# 훈련 세트와 테스트 세트를 만듭니다.
features, target = make_classification(n_classes=N_CLASSES, n_informative=9,
n_redundant=0, n_features=10, n_samples=1000)
features_train, features_test, target_train, target_test = train_test_split(
features, target, test_size=0.1, random_state=1)
# ...