-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
446 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import torch | ||
from torch.autograd import Variable | ||
|
||
|
||
class LSTMClassifier(nn.Module): | ||
|
||
def __init__(self, embedding_dim, hidden_dim, vocab_size, label_size, batch_size): | ||
super(LSTMClassifier, self).__init__() | ||
self.hidden_dim = hidden_dim | ||
self.batch_size = batch_size | ||
self.word_embeddings = nn.Embedding(vocab_size, embedding_dim) | ||
self.lstm = nn.LSTM(embedding_dim, hidden_dim) | ||
self.hidden2hidden1 = nn.Linear(hidden_dim, hidden_dim) | ||
self.relu1 = nn.ReLU() | ||
self.hidden2label = nn.Linear(hidden_dim, label_size) | ||
self.dropout = nn.Dropout(0.5) | ||
|
||
|
||
def last_timestep(self, unpacked, lengths): | ||
# Index of the last output for each sequence. | ||
idx = (lengths - 1).view(-1, 1).expand(unpacked.size(0), | ||
unpacked.size(2)).unsqueeze(1) | ||
if torch.cuda.is_available(): | ||
idx = idx.cuda() | ||
|
||
return unpacked.gather(1, idx).squeeze() | ||
|
||
def init_hidden(self): | ||
if torch.cuda.is_available(): | ||
h0 = Variable(torch.zeros(1, self.batch_size, self.hidden_dim)).cuda() | ||
c0 = Variable(torch.zeros(1, self.batch_size, self.hidden_dim)).cuda() | ||
else: | ||
h0 = Variable(torch.zeros(1, self.batch_size, self.hidden_dim)) | ||
c0 = Variable(torch.zeros(1, self.batch_size, self.hidden_dim)) | ||
return (h0, c0) | ||
|
||
def forward(self, sentence,lengths): | ||
|
||
packed = torch.nn.utils.rnn.pack_padded_sequence(sentence, lengths,batch_first=True) | ||
lstm_out, self.hidden = self.lstm(packed, self.hidden) | ||
unpacked, unpacked_len = torch.nn.utils.rnn.pad_packed_sequence(lstm_out,batch_first=True) | ||
# get the outputs from the last *non-masked* timestep for each sentence | ||
last_outputs = self.last_timestep(unpacked, unpacked_len) | ||
last_outputs = self.dropout(last_outputs) | ||
hidden_1 = self.hidden2hidden1(last_outputs) | ||
hidden_1 = self.relu1(hidden_1) | ||
y = self.hidden2label(hidden_1) | ||
return y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
# encoding: utf-8 | ||
import json | ||
import numpy as np | ||
import sys | ||
import shutil | ||
if sys.version_info[0]==2: | ||
import cPickle as pickle | ||
else: | ||
import pickle | ||
import os | ||
|
||
def create_folder(filename): | ||
if "\\" in filename: | ||
a = '\\'.join(filename.split('\\')[:-1]) | ||
else: | ||
a = '/'.join(filename.split('/')[:-1]) | ||
if not os.path.exists(a): | ||
os.makedirs(a) | ||
|
||
|
||
|
||
def savein_json(filename, array): | ||
create_folder(filename) | ||
with open(filename+'.txt', 'w') as outfile: | ||
json.dump(array, outfile) | ||
print("Save into files: ",filename) | ||
outfile.close() | ||
|
||
def readfrom_json(filename): | ||
with open(filename+'.txt', 'r') as outfile: | ||
data = json.load(outfile) | ||
outfile.close() | ||
return data | ||
|
||
def savein_pickle(file,array): | ||
create_folder(file) | ||
with open(file, 'wb') as handle: | ||
pickle.dump(array, handle) | ||
|
||
def readfrom_pickle(file): | ||
with open(file, 'rb') as handle: | ||
if sys.version_info[0] == 2: | ||
data = pickle.load(handle) | ||
else: | ||
data = pickle.load(handle,encoding='latin1') | ||
return data | ||
|
||
def readfrom_txt(path): | ||
data =open(path).read() | ||
return data | ||
|
||
def textfile2list(path): | ||
data = readfrom_txt(path) | ||
txt_list =list() | ||
for line in data.splitlines(): | ||
txt_list.append(line) | ||
return txt_list | ||
|
||
|
||
def movefiles(dir_simples,old_address,new_address,abbr=""): | ||
for dir_simple in dir_simples: | ||
desti = dir_simple.replace(old_address,new_address) | ||
desti = desti.replace("TimeNorm.gold.completed.xml","TimeNorm.system.completed.xml") | ||
create_folder(desti) | ||
shutil.copy(dir_simple+abbr,desti) | ||
|
||
def movefiles_folders(dir_simples,old_address,new_address,abbr=""): | ||
for dir_simple in dir_simples: | ||
if not os.path.exists(new_address+"/"+dir_simple): | ||
os.makedirs(new_address+"/"+dir_simple) | ||
shutil.copy(old_address+"/"+dir_simple+"/"+dir_simple+".TimeNorm.gold.completed.xml",new_address+"/"+dir_simple+"/"+dir_simple+".TimeNorm.gold.completed.xml") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.