
452
실무로 통하는
ML
문제 해결
with
파이썬
tensor([[-1.1254, 1.9643],
[-1.1533, -0.5007],
[ 0.2953, -0.2281],
[ 0.5739, -0.4234],
[ 1.4096, -0.8122]], grad_fn=<DivBackward0>)
21.3
신경망 구성하기
과제
신경망을 만들고 싶습니다.
해결
파이토치의
nn
.
Module
클래스를 사용해 간단한 신경망을 정의합니다.
# 라이브러리를 임포트합니다.
import torch
import torch.nn as nn
# 신경망을 정의합니다.
class SimpleNeuralNet(nn.Module):
def __init__(self):
super(SimpleNeuralNet, self).__init__()
self.fc1 = nn.Linear(10, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, 1)
def forward(self, x):
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = nn.functional.sigmoid(self.fc3(x))
return x
# 신경망을 초기화합니다.
network = SimpleNeuralNet ...