Skip to content

Instantly share code, notes, and snippets.

@l1m2p3
Created January 9, 2018 08:00
Show Gist options
  • Save l1m2p3/4d31a42c980c4737371e563c03df74da to your computer and use it in GitHub Desktop.
Save l1m2p3/4d31a42c980c4737371e563c03df74da to your computer and use it in GitHub Desktop.
SimpleModel definition
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
words_dim = 300
input_channel = 1
output_channel = 100
dropout_rate = 0.5
Ks = 3
target_class = 5
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.conv1 = nn.Conv2d(input_channel, output_channel, (3, words_dim), padding=(2,0))
self.conv2 = nn.Conv2d(input_channel, output_channel, (4, words_dim), padding=(3,0))
self.conv3 = nn.Conv2d(input_channel, output_channel, (5, words_dim), padding=(4,0))
self.dropout = nn.Dropout(dropout_rate)
self.fc1 = nn.Linear(Ks * output_channel, target_class)
def forward(self, x):
x1 = F.relu(self.conv1(x)).squeeze(3)
x2 = F.relu(self.conv2(x)).squeeze(3)
x3 = F.relu(self.conv3(x)).squeeze(3)
x1 = F.max_pool1d(x1, x1.shape[2]).view(-1, x1.shape[1])
x2 = F.max_pool1d(x2, x2.shape[2]).view(-1, x1.shape[1])
x3 = F.max_pool1d(x3, x3.shape[2]).view(-1, x1.shape[1])
x = torch.cat((x1,x2,x3), 1)
x = self.dropout(x)
x = self.fc1(x)
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment