Add tensorflow implementation and Encoder experiment

This commit is contained in:
Corentin 2021-09-24 15:42:07 +09:00
commit db52231fa0
6 changed files with 471 additions and 72 deletions

View file

@ -9,6 +9,7 @@ import torch
from torch import nn
from torch.utils.tensorboard import SummaryWriter
from src.metrics import Metrics
from src.torch_networks import (
TorchLSTMModel, TorchLSTMCellModel, TorchGRUModel,
CustomRNNModel, ChainRNNLayer, BNLSTMCell, CustomLSTMCell)
@ -34,6 +35,35 @@ def score_sequences(sequences: np.ndarray) -> float:
return score, max_score
def get_network(model: str, input_dim: int, hidden_size: int, num_layer: int, device: str) -> nn.Module:
if model == 'stack':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
if model == 'stack-torchcell':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer, cell_class=nn.LSTMCell).to(device)
if model == 'stack-bn':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer, cell_class=BNLSTMCell).to(device)
if model == 'stack-custom':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer, cell_class=CustomLSTMCell).to(device)
if model == 'chain-lstm':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer,
layer_class=ChainRNNLayer).to(device)
if model == 'chain-bn':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer,
layer_class=ChainRNNLayer, cell_class=BNLSTMCell).to(device)
if model == 'chain-custom':
return CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer,
layer_class=ChainRNNLayer, cell_class=CustomLSTMCell).to(device)
if model == 'torch-cell':
return TorchLSTMCellModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
if model == 'torch-lstm':
return TorchLSTMModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
if model == 'torch-gru':
return TorchGRUModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
print('Error : Unkown model')
sys.exit(1)
def main():
parser = ArgumentParser()
parser.add_argument('--output', type=Path, default=Path('output', 'recorder'), help='Output dir')
@ -57,32 +87,8 @@ def main():
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
torch.backends.cudnn.benchmark = True
if model == 'stack':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
elif model == 'stack-torchcell':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer, cell_class=nn.LSTMCell).to(device)
elif model == 'stack-bn':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer, cell_class=BNLSTMCell).to(device)
elif model == 'stack-custom':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer, cell_class=CustomLSTMCell).to(device)
elif model == 'chain-lstm':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer,
layer_class=ChainRNNLayer).to(device)
elif model == 'chain-bn':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer,
layer_class=ChainRNNLayer, cell_class=BNLSTMCell).to(device)
elif model == 'chain-custom':
network = CustomRNNModel(input_dim + 1, hidden_size, num_layer=num_layer,
layer_class=ChainRNNLayer, cell_class=CustomLSTMCell).to(device)
elif model == 'torch-cell':
network = TorchLSTMCellModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
elif model == 'torch-lstm':
network = TorchLSTMModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
elif model == 'torch-gru':
network = TorchGRUModel(input_dim + 1, hidden_size, num_layer=num_layer).to(device)
else:
print('Error : Unkown model')
sys.exit(1)
network = get_network(model, input_dim, hidden_size, num_layer, device)
output_dir = output_dir.parent / f'recorder_{model}_b{batch_size}_s{sequence_size}_h{hidden_size}_l{num_layer}'
if not output_dir.exists():
@ -109,52 +115,6 @@ def main():
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.996)
criterion = nn.CrossEntropyLoss()
class Metrics:
class Bench:
def __init__(self):
self.data_gen = 0.0
self.data_process = 0.0
self.predict = 0.0
self.loss = 0.0
self.backprop = 0.0
self.optimizer = 0.0
self.metrics = 0.0
def reset(self):
self.data_gen = 0.0
self.data_process = 0.0
self.predict = 0.0
self.loss = 0.0
self.backprop = 0.0
self.optimizer = 0.0
self.metrics = 0.0
def get_proportions(self, train_time: float) -> str:
return (
f'data_gen: {self.data_gen / train_time:.02f}'
f', data_process: {self.data_process / train_time:.02f}'
f', predict: {self.predict / train_time:.02f}'
f', loss: {self.loss / train_time:.02f}'
f', backprop: {self.backprop / train_time:.02f}'
f', optimizer: {self.optimizer / train_time:.02f}'
f', metrics: {self.metrics / train_time:.02f}')
def __init__(self):
self.loss = 0.0
self.accuracy = 0.0
self.score = 0.0
self.max_score = 0.0
self.count = 0
self.bench = self.Bench()
def reset(self):
self.loss = 0.0
self.accuracy = 0.0
self.score = 0.0
self.max_score = 0.0
self.count = 0
self.bench.reset()
metrics = Metrics()
summary_period = max_step // 100
min_sequence_size = 4