paranormal-or-skeptic-ISI-p.../model.py

57 lines
1.8 KiB
Python
Raw Normal View History

2021-05-26 01:54:33 +02:00
import torch
import torch.nn as nn
import numpy as np
class Model(nn.Module):
"""NN Model class"""
def __init__(self, input_dim=300, hidden_dim=600, output_dim=1):
"""Initializes new instance of Model class"""
super(Model, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.fc1 = nn.Linear(self.input_dim, self.hidden_dim)
2021-05-26 02:33:40 +02:00
self.fc2 = nn.Linear(self.hidden_dim, self.hidden_dim)
self.fc3 = nn.Linear(self.hidden_dim, self.output_dim)
self.relu = nn.ReLU()
2021-05-26 01:54:33 +02:00
self.criterion = nn.BCELoss()
self.optimizer = torch.optim.SGD(self.parameters(), lr=0.01)
def forward(self, x):
"""Step forward learning fn"""
x = self.fc1(x)
2021-05-26 02:33:40 +02:00
x = self.relu(x)
2021-05-26 01:54:33 +02:00
x = self.fc2(x)
2021-05-26 02:33:40 +02:00
x = self.relu(x)
x = self.fc3(x)
2021-05-26 01:54:33 +02:00
x = torch.sigmoid(x)
return x
def run_training(self, X_train, Y_train, batch_size, epochs_count):
for _ in range(epochs_count):
self.train()
2021-05-26 02:33:40 +02:00
print(f"{Y_train.shape[0]}, {Y_train.shape[0] == self.input_dim}")
print(f"{Y_train.shape[0]}, {Y_train.shape[0] == self.hidden_dim}")
print(f"{Y_train.shape[0]}, {Y_train.shape[0] == self.output_dim}")
2021-05-26 01:54:33 +02:00
for i in range(0, Y_train.shape[0], batch_size):
X = X_train[i: i + batch_size]
X = torch.tensor(X)
y = Y_train[i: i + batch_size]
y = torch.tensor(
y.astype(np.float32).to_numpy()).reshape(-1, 1)
outputs = self(X.float())
loss = self.criterion(outputs, y)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()