<jupyter_start><jupyter_text>Create dataset<jupyter_code>X, Y = utils.create_dataset(FOLDERS)
X = utils.normalize_data(X)
X = np.expand_dims(X, axis=1)

#################################
#### MAKE TEST TRAIN SPLIT   ####
#################################
def create_dataloaders(CONFIG, X, Y):
    from sklearn.model_selection import train_test_split

    X_train, X_test, Y_train, Y_test = train_test_split(
        X, Y, test_size=0.2, random_state=42)


    train_dataset = TensorDataset(torch.Tensor(X_train), torch.Tensor(Y_train))
    test_dataset = TensorDataset(torch.Tensor(X_test), torch.Tensor(Y_test))

    train_dataloader = DataLoader(train_dataset,
                                  shuffle=True,
                                  num_workers=CONFIG['cpu_workers'],
                                  batch_size=CONFIG['train_batch_size'],
                                  drop_last=True)

    test_dataloader = DataLoader(test_dataset,
                                 shuffle=False,
                                 num_workers=CONFIG['cpu_workers'],
                                 batch_size=CONFIG['test_batch_size'],
                                 drop_last=False)

    return train_dataloader, test_dataloader
CONFIG['ephs'] = 100
net = ConvNet7()
train_dataloader, test_dataloader = create_dataloaders(CONFIG, X, Y)
optimizer = torch.optim.Adam(net.parameters(), lr=CONFIG.get('lr', 1e-3))
run_training(net, optimizer, CONFIG, train_dataloader, test_dataloader)<jupyter_output>100%|███████████████████████████████████████| 1599/1599 [00:25<00:00, 61.75it/s]<jupyter_text>Try LSTM<jupyter_code>class RnnNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.drop_p = 0.1
        self.h_dim = 4
        self.activ = F.gelu
        self.rnn = nn.LSTM(16, 32, 4, bidirectional=True, batch_first=True) # need (batch, seq, feature)
        self.global_avg = nn.AdaptiveAvgPool1d(4)
        self.flat = nn.Flatten()
        self.drop = nn.Dropout(p=self.drop_p)
        self.lin = nn.Sequential(nn.Linear(self.h_dim, self.h_dim * 2),
                                 nn.BatchNorm1d(self.h_dim * 2),
                                 nn.GELU(),
                                 nn.Dropout(p=self.drop_p),
                                 nn.Linear(self.h_dim * 2, self.h_dim // 2),
                                 nn.BatchNorm1d(self.h_dim // 2),
                                 nn.GELU(),
                                 nn.Dropout(p=self.drop_p),
                                 nn.Linear(self.h_dim // 2, 4))

    def forward(self, x):
        x, _ = self.rnn(x)
        x = self.global_avg(x)
        x = self.flat(x)
        x = self.drop(x)
        x = self.lin(x)
        return x
model = RnnNet()
inp = torch.rand(32, 1, 16)
out = model(inp)
out.shape
CONFIG['train_batch_size'] = 128
CONFIG['ephs'] = 15
net = RnnNet()
train_dataloader, test_dataloader = create_dataloaders(CONFIG, X, Y)
optimizer = torch.optim.Adam(net.parameters(), lr=CONFIG.get('lr', 1e-3))
utils.run_training(net, optimizer, CONFIG, train_dataloader, test_dataloader)<jupyter_output>100%|█████████████████████████████████████████| 399/399 [00:04<00:00, 82.59it/s]
StarcoderdataJupyterStructuredCleanDedup