<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]
{'fraction_non_alphanumeric': 0.16243336469112574, 'fraction_numerical': 0.026967701473816243, 'mean_word_length': 2.218970736629667, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '251650', 'license': 'MIT', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1082, 'n_words': 165, 'path': 'notebooks/dl/deep_methods_L=6.6_RNN.ipynb', 'repo_name': 'alexeyhorkin/ml_dl_with_topology_data', 'size': '3189'}