코드 예시 모음
Created
August 4, 2021 06:12
-
-
Save sunrise2575/e5dd9c5c10c3deb42087c185842bcdb5 to your computer and use it in GitHub Desktop.
Code snippet collection
sequential data 1-d convolution으로 학습하는 예제
sequential data가 scalar가 들어오는게 아니고 vector값이 들어오면 MyModel
에서 in_channel` 을 vector dimension 만큼 늘려주면 끝.
입력 데이터 넣기 전에 -1~1 사이로 값을 정규화 하면 성능이 훨씬 향상될 것.
import torch
class MyModel(torch.nn.Module):
def __init__(self, time_length, kernel_size):
super(MyModel, self).__init__()
self.time_length = time_length
self.conv = torch.nn.Conv1d(
in_channels=1, out_channels=32, kernel_size=kernel_size)
self.fc = torch.nn.Conv1d(
in_channels=32, out_channels=1, kernel_size=1)
self.relu = torch.nn.ReLU()
def forward(self, x):
h = self.relu(self.conv(x[:, :, :self.time_length - 1]))
y_hat = self.fc(h)
return y_hat
def main_():
batch_size = 100
time_length = 32
kernel_size = 16
total_epoch = 350
x = torch.Tensor([[range(x, x + time_length)] for x in range(batch_size)])
y = x[:, :, kernel_size:]
cut = int(0.8 * batch_size)
x_train, y_train = x[:cut], y[:cut]
x_test, y_test = x[cut:], y[cut:]
model = MyModel(time_length=time_length, kernel_size=kernel_size)
loss_fn = torch.nn.L1Loss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)
for epoch in range(350):
# train
y_train_hat = model(x_train)
loss_train = loss_fn(y_train_hat, y_train)
optimizer.zero_grad()
loss_train.backward()
optimizer.step()
# test
y_test_hat = model(x_test)
loss_test = loss_fn(y_test_hat, y_test)
if epoch % 25 == 0 or total_epoch == 25 - 1:
print("epoch {:4d}, train loss: {:.3f}, test loss: {:.3f}".format(
epoch, loss_train.item(), loss_test.item()))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tensorflow single-GPU & multi-GPU matrix multiplication
Python 3.7
TensorFlow 1.13
Numpy 1.16
TensorFlow 1.13버전 쓰려면 위 3가지 세팅 (파이썬, 넘파이 버전) 이 정확히 맞아야 함