Guest User

Untitled

a guest
Feb 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import torch
  2. import torch.nn as nn
  3. from torch.autograd import Variable
  4. from torch.nn import functional as F
  5.  
  6. class TextCNN(nn.Module):
  7. def __init__(self, batch_size, output_size, in_channels, out_channels, kernel_heights,
  8. stride, padding, keep_probab, vocab_size, embedding_dim, weights):
  9. super(TextCNN, self).__init__()
  10.  
  11. """
  12. Arguments
  13. ---------
  14. batch_size : Size of each batch which is same as the batch_size of the data returned by the TorchText BucketIterator
  15. output_size : Number of labels
  16. in_channels : Number of input channels. Here it is 1 as the input data has dimension = (batch_size, num_seq, embedding_length)
  17. out_channels : Number of output channels after convolution operation performed on the input matrix
  18. kernel_heights : A list consisting of 3 different kernel_heights. Convolution will be performed 3 times and finally results from each kernel_height will be concatenated.
  19. stride: The number of tokens that the slide conv window moves over for next input
  20. padding:
  21. keep_probab : Probability of retaining an activation node during dropout operation
  22. vocab_size : Size of the vocabulary containing unique words
  23. embedding_dim : Embedding dimension of GloVe word embeddings
  24. weights : Pre-trained GloVe word_embeddings which we will use to create our word_embedding look-up table
  25.  
  26. """
  27. self.batch_size = batch_size
  28. self.output_size = output_size
  29. self.out_channels = out_channels
  30. self.kernel_heights = kernel_heights
  31. self.stride = stride
  32. self.padding = padding
  33. self.vocab_size = vocab_size
  34. self.embedding_dim = embedding_dim
  35.  
  36. self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
  37. self.word_embeddings.weight = nn.Parameter(weights, requires_grad=False)
  38. # Define convlutions
  39. self.conv1 = nn.Conv2d(in_channels, out_channels, (kernel_heights[0], embedding_dim), stride, padding)
  40. self.conv2 = nn.Conv2d(in_channels, out_channels, (kernel_heights[1], embedding_dim), stride, padding)
  41. self.conv3 = nn.Conv2d(in_channels, out_channels, (kernel_heights[2], embedding_dim), stride, padding)
  42. self.dropout = nn.Dropout(keep_probab)
  43. self.label = nn.Linear(len(kernel_heights)*out_channels, output_size)
  44.  
  45. def conv_block(self, input, conv_layer):
  46.  
  47. """
  48. Parameters
  49. ----------
  50. input: Batch of tokens
  51. conv_layer: convolution layer to be applied
  52.  
  53. Returns
  54. -------
  55. Outputs fully connected max pooling on a layer and filter maximum activation.
  56. """
  57. conv_out = conv_layer(input) # (batch_size, out_channels, dim, 1)
  58.  
  59. activation = F.relu(conv_out.squeeze(3)) # (batch_size, out_channels, dim1)
  60. max_out = F.max_pool1d(activation, activation.size()[2]).squeeze(2) # (batch_size, out_channels)
  61.  
  62. return max_out
  63.  
  64. def forward(self, input_text, batch_size=None):
  65.  
  66. """
  67. Define how model is going to be run from input to output.
  68.  
  69. Parameters
  70. ----------
  71. input_text: input_sentences of shape = (batch_size, num_sequences)
  72. batch_size : default = None. Used only for prediction on a single sentence after training (batch_size = 1)
  73.  
  74. Returns
  75. -------
  76. Output of the linear layer containing logits for pos & neg class.
  77. logits.size() = (batch_size, output_size)
  78.  
  79. """
  80.  
  81. data_in = self.word_embeddings(input_text) # (batch_size, num_seq, embedding_length)
  82.  
  83. data_in = data_in.unsqueeze(1) # (batch_size, 1, num_seq, embedding_length)
  84.  
  85. max_out1 = self.conv_block(data_in, self.conv1)
  86. max_out2 = self.conv_block(data_in, self.conv2)
  87. max_out3 = self.conv_block(data_in, self.conv3)
  88.  
  89. out = torch.cat((max_out1, max_out2, max_out3), 1) # (batch_size, num_kernels*out_channels)
  90.  
  91. out = self.dropout(out) # (batch_size, num_kernels*out_channels)
  92.  
  93. logits = self.label(out)
  94.  
  95. return logits # (len(kernel_heights)*out_channels, output_size)
Add Comment
Please, Sign In to add comment