shape '[119, 1, 187]' is invalid for input of size 22372

username: dew
cooked: <h1>
<a name="coding-utf-8-1" class="anchor" href="#coding-utf-8-1"></a>-<em>- coding: utf-8 -</em>-</h1>
<p>“”"<br>
Created on Tue Sep 17 11:16:34 2019</p>
<p><span class="mention">@author</span>: anne marie delaney<br>
eoin brophy</p>
<p>Module of the GAN model for time series synthesis.</p>
<p>“”"</p>
<p>import torch<br>
import torch.nn as nn</p>
<p>device = torch.device(‘cuda:0’ if torch.cuda.is_available() else ‘cpu’)</p>
<h2>
<a name="nn-definitions-2" class="anchor" href="#nn-definitions-2"></a>“”"<br>
NN Definitions</h2>
<p>Defining the Neural Network Classes to be evaluated in this Notebook</p>
<h2>
<a name="minibatch-discrimination-3" class="anchor" href="#minibatch-discrimination-3"></a>Minibatch Discrimination</h2>
<p>Creating a module for Minibatch Discrimination to avoid mode collapse as described:</p><aside class="onebox pdf" data-onebox-src="https://arxiv.org/pdf/1606.03498.pdf">
  <header class="source">

      <a href="https://arxiv.org/pdf/1606.03498.pdf" target="_blank" rel="noopener nofollow ugc">arxiv.org</a>
  </header>

  <article class="onebox-body">
    <a href="https://arxiv.org/pdf/1606.03498.pdf" target="_blank" rel="noopener nofollow ugc"><span class="pdf-onebox-logo"></span></a>

<h3><a href="https://arxiv.org/pdf/1606.03498.pdf" target="_blank" rel="noopener nofollow ugc">1606.03498.pdf</a></h3>

  <p class="filesize">2.24 MB</p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p><a href="https://torchgan.readthedocs.io/en/latest/modules/layers.html#minibatch-discrimination" class="onebox" target="_blank" rel="noopener nofollow ugc">https://torchgan.readthedocs.io/en/latest/modules/layers.html#minibatch-discrimination</a></p>
<p>“”"</p>
<p>class MinibatchDiscrimination(nn.Module):<br>
def <strong>init</strong>(self,input_features,output_features,minibatch_normal_init, hidden_features=16):<br>
super(MinibatchDiscrimination,self).<strong>init</strong>()</p>
<pre><code>  self.input_features = input_features
  self.output_features = output_features
  self.hidden_features = hidden_features
  self.T = nn.Parameter(torch.randn(self.input_features,self.output_features, self.hidden_features))
  if minibatch_normal_init == True:
    nn.init.normal(self.T, 0,1)
  #torch.randn 은 평균이 0이고 표준편차가 1인 정규분포의 난수로 채워진 tensor값을 돌려줌.
</code></pre>
<p>def forward(self,x):<br>
M = torch.mm(x,self.T.view(self.input_features,-1))<br>
<span class="hashtag">#matrix</span> multiplication<br>
M = M.view(-1, self.output_features, self.hidden_features).unsqueeze(0)<br>
# the size -1 is inferred from other dimensions<br>
M_t = M.permute(1, 0, 2, 3)<br>
# Broadcasting reduces the matrix subtraction to the form desired in the paper<br>
<span class="hashtag">#permute</span> : tensor 모양 변경<br>
out = torch.sum(torch.exp(-(torch.abs(M - M_t).sum(3))), dim=0) - 1<br>
return torch.cat([x, out], 1)</p>
<h2>
<a name="discriminator-class-4" class="anchor" href="#discriminator-class-4"></a>“”"<br>
Discriminator Class</h2>
<p>This discriminator has a parameter num_cv which allows the user to specify if<br>
they want to have 1 or 2 Convolution Neural Network Layers.</p>
<p>“”"</p>
<p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self,seq_length,batch_size,minibatch_normal_init, n_features = 1, num_cv = 1, minibatch = 0, cv1_out= 10, cv1_k = 3, cv1_s = 4, p1_k = 3, p1_s = 3, cv2_out = 10, cv2_k = 3, cv2_s = 3 ,p2_k = 3, p2_s = 3):<br>
super(Discriminator,self).<strong>init</strong>()<br>
self.n_features = n_features<br>
<span class="hashtag">#임의</span> 수정<br>
# self.seq_length = seq_length<br>
self.seq_length = seq_length -1<br>
self.batch_size = batch_size<br>
self.num_cv = num_cv<br>
self.minibatch = minibatch<br>
self.cv1_dims = int((((((seq_length - cv1_k)/cv1_s) + 1)-p1_k)/p1_s)+1)<br>
self.cv2_dims = int((((((self.cv1_dims - cv2_k)/cv2_s) + 1)-p2_k)/p2_s)+1)<br>
self.cv1_out = cv1_out<br>
self.cv2_out = cv2_out</p>
<pre><code>  #input should be size (batch_size,num_features,seq_length) for the convolution layer
  self.CV1 = nn.Sequential(
              nn.Conv1d(in_channels = self.n_features, out_channels = int(cv1_out),kernel_size = int(cv1_k), stride = int(cv1_s))
              ,nn.ReLU()        
              ,nn.MaxPool1d(kernel_size = int(p1_k), stride = int(p1_s))   
             )
  
  # 2 convolutional layers
  if self.num_cv &gt; 1:
    self.CV2 = nn.Sequential(
                  nn.Conv1d(in_channels = int(cv1_out), out_channels = int(cv2_out) ,kernel_size =int(cv2_k), stride = int(cv2_s))
                  ,nn.ReLU()
                  ,nn.MaxPool1d(kernel_size = int(p2_k), stride = int(p2_s))
              )
    
    #Adding a minibatch discriminator layer to add a cripple affect to the discriminator so that it needs to generate sequences that are different from each other.
    
    if   self.minibatch &gt; 0:
      self.mb1 = MinibatchDiscrimination(self.cv2_dims*cv2_out,self.minibatch, minibatch_normal_init)
      self.out = nn.Sequential(nn.Linear(int(self.cv2_dims*cv2_out)+self.minibatch,1),nn.Sigmoid()) # to make sure the output is between 0 and 1
    else:
      self.out = nn.Sequential(nn.Linear(int(self.cv2_dims*cv2_out),1),nn.Sigmoid()) # to make sure the output is between 0 and 1 
  
  # 1 convolutional layer
  else:
    
    #Adding a minibatch discriminator layer to add a cripple affect to the discriminator so that it needs to generate sequences that are different from each other.
    if self.minibatch &gt; 0 :
      
      self.mb1 = MinibatchDiscrimination(int(self.cv1_dims*cv1_out),self.minibatch, minibatch_normal_init)
      self.out = nn.Sequential(nn.Linear(int(self.cv1_dims*cv1_out)+self.minibatch,1),nn.Dropout(0.2),nn.Sigmoid()) # to make sure the output is between 0 and 1
    else:
      self.out = nn.Sequential(nn.Linear(int(self.cv1_dims*cv1_out),1),nn.Sigmoid())  
</code></pre>
<p>def forward(self,x):<br>
# print("Calculated Output dims after CV1: "+str(self.cv1_dims))<br>
# print("input: "+str(x.size()))<br>
<span class="hashtag">#x</span> = self.CV1(x.view(self.batch_size,1,self.seq_length))<br>
x = self.CV1(x.view(self.batch_size , 1, self.seq_length))<br>
# print("CV1 Output: "+str(x.size()))</p>
<pre><code>  #2 Convolutional Layers
  if self.num_cv &gt; 1:   
    
    x = self.CV2(x)
    x = x.view(self.batch_size,-1)
    
  #  print("CV2 Output: "+str(x.size()))
    if self.minibatch &gt; 0:
         x = self.mb1(x.squeeze())
   #      print("minibatch output: "+str(x.size()))
         x = self.out(x.squeeze())
    else:
        
         x = self.out(x.squeeze())
    
  # 1 convolutional layers
  else:
    
    x = x.view(self.batch_size,-1)
   
    #1 convolutional Layer and minibatch discrimination
    if self.minibatch &gt; 0:
         x = self.mb1(x)
         x = self.out(x)
    #1 convolutional Layer and no minibatch discrimination
    else:
         x = self.out(x)
  
  
  
  return x
</code></pre>
<h2>
<a name="generator-class-5" class="anchor" href="#generator-class-5"></a>“”"<br>
Generator Class</h2>
<p>This defines the Generator for evaluation. The Generator consists of two LSTM<br>
layers with a final fully connected layer.</p>
<p>“”"</p>
<p>class Generator(nn.Module):<br>
def <strong>init</strong>(self,seq_length,batch_size,n_features = 1, hidden_dim = 50,<br>
num_layers = 2, tanh_output = False):<br>
super(Generator,self).<strong>init</strong>()<br>
self.n_features = n_features<br>
self.hidden_dim = hidden_dim<br>
self.num_layers = num_layers<br>
<span class="hashtag">#임의</span> 수정<br>
<span class="hashtag">#self</span>.seq_length = seq_length<br>
self.seq_length = seq_length - 1<br>
self.batch_size = batch_size<br>
self.tanh_output = tanh_output</p>
<pre><code>  self.layer1 = nn.LSTM(input_size = self.n_features, hidden_size = self.hidden_dim, 
                              num_layers = self.num_layers,batch_first = True#,dropout = 0.2,
                             )
  if self.tanh_output == True:
    self.out = nn.Sequential(nn.Linear(self.hidden_dim,1),nn.Tanh()) # to make sure the output is between 0 and 1 - removed ,nn.Sigmoid()
  else:
    self.out = nn.Linear(self.hidden_dim,1) 
</code></pre>
<p>def init_hidden(self):<br>
weight = next(self.parameters()).data<br>
hidden = (weight.new(self.num_layers, self.batch_size, self.hidden_dim).zero_().to(device), weight.new(self.num_layers, self.batch_size, self.hidden_dim).zero_().to(device))<br>
return hidden</p>
<p>def forward(self,x,hidden):</p>
<pre><code>  x,hidden = self.layer1(x.view(self.batch_size,self.seq_length,1),hidden)
  
  x = self.out(x)
  
  return x #,hidden</code></pre>

username: ptrblck
cooked: <p>The error is raised as the desired view shape of <code>[119, 1, 187]</code> is invalid for the used tensor in the shape of <code>[22372]</code> as the former has 22253 elements.<br>
Your current code is a bit hard to read and you can format it by wrapping it into three backticks ```.</p>