Python CNN with different conv pool activation layers

Nahla_Chekired: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/f/dfaa6ca1ac58df97b6a2f8874b4ff14ac4c8c73d.png" data-download-href="https://discuss.pytorch.org/uploads/default/dfaa6ca1ac58df97b6a2f8874b4ff14ac4c8c73d" title="Capture d’écran 2021-06-10 134911"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/dfaa6ca1ac58df97b6a2f8874b4ff14ac4c8c73d_2_690x388.png" alt="Capture d’écran 2021-06-10 134911" data-base62-sha1="vUDBIBH5cpzdmHgTO9b1TU0tN8N" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/dfaa6ca1ac58df97b6a2f8874b4ff14ac4c8c73d_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/dfaa6ca1ac58df97b6a2f8874b4ff14ac4c8c73d_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/dfaa6ca1ac58df97b6a2f8874b4ff14ac4c8c73d_2_1380x776.png 2x" data-dominant-color="F4F4F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture d’écran 2021-06-10 134911</span><span class="informations">1920×1080 203 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
hello i am trying to do CNN with different layers conv and pool but i got this error (on the image) i have to put to the second conv layer result of of first conv pool activation that helps me to do ResNet<br>
here is my code<br>
import torch</p>
<p>from torchtext.legacy import data</p>
<p>from torchtext.legacy import datasets</p>
<p>import random</p>
<p>import numpy as np</p>
<p>SEED = 1234</p>
<p>random.seed(SEED)</p>
<p>np.random.seed(SEED)</p>
<p>torch.manual_seed(SEED)</p>
<p>torch.backends.cudnn.deterministic = True</p>
<p>TEXT = data.Field(tokenize = ‘spacy’,</p>
<pre><code>              tokenizer_language = 'en_core_web_sm',

              batch_first = True)
</code></pre>
<p>LABEL = data.LabelField(dtype = torch.float)</p>
<p>train_data, test_data = datasets.IMDB.splits(TEXT, LABEL)</p>
<p>train_data, valid_data = train_data.split(random_state = random.seed(SEED))</p>
<p>MAX_VOCAB_SIZE = 25_000</p>
<p>TEXT.build_vocab(train_data,</p>
<pre><code>             max_size = MAX_VOCAB_SIZE, 

             vectors = "glove.6B.100d", 

             unk_init = torch.Tensor.normal_)
</code></pre>
<p>LABEL.build_vocab(train_data)</p>
<p>BATCH_SIZE = 64</p>
<p>device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<p>train_iterator, valid_iterator, test_iterator = data.BucketIterator.splits(</p>
<pre><code>(train_data, valid_data, test_data), 

batch_size = BATCH_SIZE, 

device = device)
</code></pre>
<p>import torch.nn as nn</p>
<p>import torch.nn.functional as F</p>
<p>class CNN1d(nn.Module):</p>
<pre><code>def __init__(self, vocab_size, embedding_dim, n_filters, filter_sizes, output_dim, 

             dropout, pad_idx):

          

    super().__init__()

    

    self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx = pad_idx)

    

    self.convs = nn.ModuleList([

                                nn.Conv1d(in_channels = embedding_dim, 

                                          out_channels = n_filters, 

                                          kernel_size = fs)

                                for fs in filter_sizes

                                ])

    

    self.fc = nn.Linear(len(filter_sizes) * n_filters, output_dim)

    

    self.dropout = nn.Dropout(dropout)

    

def forward(self, text):

    

    #text = [batch size, sent len]

    

    embedded = self.embedding(text)

            

    #embedded = [batch size, sent len, emb dim]

    

    embedded = embedded.permute(0, 2, 1)

    print("nahla")

    

    #embedded = [batch size, emb dim, sent len]

    

    conved1 = [F.relu(conv(embedded)) for conv in self.convs]

    print("hla")

        

    #conved_n = [batch size, n_filters, sent len - filter_sizes[n] + 1]

    

    pooled1 = [F.max_pool1d(conv, conv.shape[2]).squeeze(2) for conv in conved1]

    print("ahla")

    

    #pooled_n = [batch size, n_filters]

    

    conved2 = [F.relu(conv(pooled1)) for conv in self.convs]

    print("nnnnnnahla")

        

    #conved_n = [batch size, n_filters, sent len - filter_sizes[n] + 1]

    

    pooled2 = [F.max_pool1d(conv, conv.shape[2]).squeeze(2) for conv in conved2]

    print("nnnahla")

    

    #pooled_n = [batch size, n_filters]

    

    cat = self.dropout(torch.cat((pooled1,pooled2)), dim = 1)

    print("nnahla")

    

    #cat = [batch size, n_filters * len(filter_sizes)]

        

    return self.fc(cat)
</code></pre>
<p>INPUT_DIM = len(TEXT.vocab)</p>
<p>EMBEDDING_DIM = 100</p>
<p>N_FILTERS = 100</p>
<p>FILTER_SIZES = [3,4,5]</p>
<p>OUTPUT_DIM = 1</p>
<p>DROPOUT = 0.5</p>
<p>PAD_IDX = TEXT.vocab.stoi[TEXT.pad_token]</p>
<p>model = CNN1d(INPUT_DIM, EMBEDDING_DIM, N_FILTERS, FILTER_SIZES, OUTPUT_DIM, DROPOUT, PAD_IDX)</p>
<p>def count_parameters(model):</p>
<pre><code>return sum(p.numel() for p in model.parameters() if p.requires_grad)
</code></pre>
<p>print(f’The model has {count_parameters(model):,} trainable parameters’)</p>
<p>pretrained_embeddings = TEXT.vocab.vectors</p>
<p>model.embedding.weight.data.copy_(pretrained_embeddings)</p>
<p>UNK_IDX = TEXT.vocab.stoi[TEXT.unk_token]</p>
<p>model.embedding.weight.data[UNK_IDX] = torch.zeros(EMBEDDING_DIM)</p>
<p>model.embedding.weight.data[PAD_IDX] = torch.zeros(EMBEDDING_DIM)</p>
<p>import torch.optim as optim</p>
<p>optimizer = optim.Adam(model.parameters())</p>
<p>criterion = nn.BCEWithLogitsLoss()</p>
<p>model = model.to(device)</p>
<p>criterion = criterion.to(device)</p>
<p>def binary_accuracy(preds, y):</p>
<pre><code>"""

Returns accuracy per batch, i.e. if you get 8/10 right, this returns 0.8, NOT 8

"""

#round predictions to the closest integer

rounded_preds = torch.round(torch.sigmoid(preds))

correct = (rounded_preds == y).float() #convert into float for division 

acc = correct.sum() / len(correct)

return acc
</code></pre>
<p>def train(model, iterator, optimizer, criterion):</p>
<pre><code>epoch_loss = 0

epoch_acc = 0



model.train()



for batch in iterator:

    

    optimizer.zero_grad()

    

    predictions = model(batch.text).squeeze(1)

    

    loss = criterion(predictions, batch.label)

    

    acc = binary_accuracy(predictions, batch.label)

    

    loss.backward()

    

    optimizer.step()

    

    epoch_loss += loss.item()

    epoch_acc += acc.item()

    

return epoch_loss / len(iterator), epoch_acc / len(iterator)
</code></pre>
<p>import time</p>
<p>def epoch_time(start_time, end_time):</p>
<pre><code>elapsed_time = end_time - start_time

elapsed_mins = int(elapsed_time / 60)

elapsed_secs = int(elapsed_time - (elapsed_mins * 60))

return elapsed_mins, elapsed_secs
</code></pre>
<p>def evaluate(model, iterator, criterion):</p>
<pre><code>epoch_loss = 0

epoch_acc = 0



model.eval()



with torch.no_grad():



    for batch in iterator:

        predictions = model(batch.text).squeeze(1)

        

        loss = criterion(predictions, batch.label)

        

        acc = binary_accuracy(predictions, batch.label)

        epoch_loss += loss.item()

        epoch_acc += acc.item()

    

return epoch_loss / len(iterator), epoch_acc / len(iterator)
</code></pre>
<p>N_EPOCHS = 5</p>
<p>best_valid_loss = float(‘inf’)</p>
<p>for epoch in range(N_EPOCHS):</p>
<pre><code>start_time = time.time()



train_loss, train_acc = train(model, train_iterator, optimizer, criterion)

valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)



end_time = time.time()

epoch_mins, epoch_secs = epoch_time(start_time, end_time)



if valid_loss &lt; best_valid_loss:

    best_valid_loss = valid_loss

    torch.save(model.state_dict(), 'tut4-model.pt')



print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')

print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')

print(f'\t Val. Loss: {valid_loss:.3f} |  Val. Acc: {valid_acc*100:.2f}%')
</code></pre>

eqy: <p>Is <code>batch.text</code> a list or a tensor? It looks like the issue is that some input somewhere in the model is a Python list when it is expected to be a tensor.</p>

Nahla_Chekired: <p>yes this the issue can you help me fix it</p>