title: How can I connect between layers in two network by python and pytorch?
body: <p>For speaker verification from speech, I want to impellent bellow figure. I have some question please guide me.
  <img src="https://i.stack.imgur.com/tmzb2.jpg" alt="enter image description here" title="speaker verification Model" /></p>
  <ol>
  <li>First I am defining a class for first 4 layers (Code model #1). Then I am using ECAPA-TDNN code from github (code model #2 – link: <a href="https://github.com/TaoRuijie/ECAPA-TDNN" rel="nofollow noreferrer">https://github.com/TaoRuijie/ECAPA-TDNN</a> ). I don’t know how can I connect between two models 1 and 2 or in the other hand, how can I impelement bellow figure?</li>
  </ol>
  <p>Model #1 : The first 4 layer code:</p>
  <pre><code>import torch.nn as nn
  
  class NeuralNetwork(nn.Module):
      def __init__(self, num_class):
          super(NeuralNetwork, self).__init__()
          self.conv1 = nn.Sequential(nn.Conv2d(1, 80, T),
                BatchNorm2d(4),
                ReLU(inplace=True),
                MaxPool2d(kernel_size=2, stride=2))
          
         self.conv2 = nn.Sequential(nn.Conv2d(128, 40, T),
               BatchNorm2d(4),
               ReLU(inplace=True),
               MaxPool2d(kernel_size=2, stride=1))
  
         self.conv3 = nn.Sequential(nn.Conv2d(128, 40, T),
               BatchNorm2d(4),
               ReLU(inplace=True),
               MaxPool2d(kernel_size=2, stride=1))
  
          self.conv4 = nn.Sequential(nn.Conv2d(128, 40, T),
                BatchNorm2d(4),
                ReLU(inplace=True),
                MaxPool2d(kernel_size=2, stride=1))           
  
         self.conv5 = nn.Sequential(nn.Conv2d(128, 20, T = flatten),
               BatchNorm2d(4),
               ReLU(inplace=True),
               MaxPool2d(kernel_size=2, stride=2))
  </code></pre>
  <p>Model #2 : ECAPA-TDNN form git-hub:</p>
  <pre><code>import math, torch, torchaudio
  import torch.nn as nn
  import torch.nn.functional as F
  
  
  class ECAPA_TDNN(nn.Module):
  
      def __init__(self, C):
  
          super(ECAPA_TDNN, self).__init__()
  
          self.torchfbank = torch.nn.Sequential(
              PreEmphasis(),            
              torchaudio.transforms.MelSpectrogram(sample_rate=16000, n_fft=512, win_length=400, hop_length=160, \
                                                   f_min = 20, f_max = 7600, window_fn=torch.hamming_window, n_mels=80),
              )
  
          self.specaug = FbankAug() # Spec augmentation
  
          self.conv1  = nn.Conv1d(80, C, kernel_size=5, stride=1, padding=2)
          self.relu   = nn.ReLU()
          self.bn1    = nn.BatchNorm1d(C)
          self.layer1 = Bottle2neck(C, C, kernel_size=3, dilation=2, scale=8)
          self.layer2 = Bottle2neck(C, C, kernel_size=3, dilation=3, scale=8)
          self.layer3 = Bottle2neck(C, C, kernel_size=3, dilation=4, scale=8)
          # I fixed the shape of the output from MFA layer, that is close to the setting from ECAPA paper.
          self.layer4 = nn.Conv1d(3*C, 1536, kernel_size=1)
          self.attention = nn.Sequential(
              nn.Conv1d(4608, 256, kernel_size=1),
              nn.ReLU(),
              nn.BatchNorm1d(256),
              nn.Tanh(), # I add this layer
              nn.Conv1d(256, 1536, kernel_size=1),
              nn.Softmax(dim=2),
              )
          self.bn5 = nn.BatchNorm1d(3072)
          self.fc6 = nn.Linear(3072, 192)
          self.bn6 = nn.BatchNorm1d(192)
  
  
      def forward(self, x, aug):
          with torch.no_grad():
              x = self.torchfbank(x)+1e-6
              x = x.log()   
              x = x - torch.mean(x, dim=-1, keepdim=True)
              if aug == True:
                  x = self.specaug(x)
  
          x = self.conv1(x)
          x = self.relu(x)
          x = self.bn1(x)
  
          x1 = self.layer1(x)
          x2 = self.layer2(x+x1)
          x3 = self.layer3(x+x1+x2)
  
          x = self.layer4(torch.cat((x1,x2,x3),dim=1))
          x = self.relu(x)
  
          t = x.size()[-1]
  
          global_x = torch.cat((x,torch.mean(x,dim=2,keepdim=True).repeat(1,1,t), torch.sqrt(torch.var(x,dim=2,keepdim=True).clamp(min=1e-4)).repeat(1,1,t)), dim=1)
          
          w = self.attention(global_x)
  
          mu = torch.sum(x * w, dim=2)
          sg = torch.sqrt( ( torch.sum((x**2) * w, dim=2) - mu**2 ).clamp(min=1e-4) )
  
          x = torch.cat((mu,sg),1)
          x = self.bn5(x)
          x = self.fc6(x)
          x = self.bn6(x)
  
          return x
  </code></pre>
  <p>2. I am using log Mel filterbank energy for feature extraction. My dimension isn’t 80. How can I define 80 for dimension?(by batch-size? How?)</p>
  <p>3. C is constant (128) but how can I calculate T? for one speech signal I used bellow code But for all speech in my dataset, second code didn’t work. Or T is another thing?</p>
  <pre><code># Number of Channel
  ch = file_info.channels('/home/narges/Project-SV-1400.11.20/S.V-Code/dataset/train/id10292/FXxcN18rX7c/00001.wav')
  print (ch)
  
  # Number of Frame in One Audio File
  frame_one_wav = audiosegment.from_file('/home/narges/Project-SV-1400.11.20/S.V-Code/dataset/train/id10292/FXxcN18rX7c/00001.wav')
  print (frame_one_wav)
  
  # All Frame
  all_frame = []
   for i in range (number_of_files_train):
       wavs_info = rate_train, sig_train = wavfile.read(wavs_train[i])
       frame_wav = audiosegment.from_file(wavs_info)
       all_frame.append(frame_wav())
  </code></pre>