if __name__ == "__main__": from q71 import SingleLayerNN from q73 import load_dataloader from q75 import train_model batch_size_list = [2**i for i in range(20)] for batch_size in batch_size_list: dataloader_train = load_dataloader("train.data", batch_size=batch_size) dataloader_valid = load_dataloader("valid.data", batch_size=512) model = SingleLayerNN(300, 4) model, *_ = train_model(model, dataloader_train, dataloader_valid, lr=1e-2, epochs=100, take_time=True)
conc = self.dropout(conc) out = self.out(conc) return out if __name__ == "__main__": import torch from torch import nn from q73 import load_dataloader from q75 import train_model n_epochs = 6 batch_size = 512 load_corpus() train_loader = load_dataloader("train2.data", batch_size=batch_size) valid_loader = load_dataloader("valid2.data", batch_size=batch_size) model = BiLSTM() loss_fn = nn.CrossEntropyLoss(reduction="sum") optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001) model.cuda() train_loss = [] valid_loss = [] for epoch in range(n_epochs): # 訓練モード model.train() avg_loss = 0. for i, (x_batch, y_batch) in enumerate(train_loader): y_pred = model(x_batch)