Skip to content

Commit 0e47f98

Browse files
committed
gpu runnable
1 parent 229ec8f commit 0e47f98

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def mr(text_field, label_field, **kargs):
7575
# update args and print
7676
args.embed_num = len(text_field.vocab)
7777
args.class_num = len(label_field.vocab) - 1
78-
args.cuda = args.no_cuda and torch.cuda.is_available(); del args.no_cuda
78+
args.cuda = (not args.no_cuda) and torch.cuda.is_available(); del args.no_cuda
7979
args.kernel_sizes = [int(k) for k in args.kernel_sizes.split(',')]
8080
args.save_dir = os.path.join(args.save_dir, datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
8181

@@ -93,6 +93,9 @@ def mr(text_field, label_field, **kargs):
9393
cnn = torch.load(args.snapshot)
9494
except :
9595
print("Sorry, This snapshot doesn't exist."); exit()
96+
97+
if args.cuda:
98+
cnn = cnn.cuda()
9699

97100

98101
# train or predict

model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def __init__(self, args):
1616
Ks = args.kernel_sizes
1717

1818
self.embed = nn.Embedding(V, D)
19-
self.convs1 = [nn.Conv2d(Ci, Co, (K, D)) for K in Ks]
19+
#self.convs1 = [nn.Conv2d(Ci, Co, (K, D)) for K in Ks]
20+
self.convs1 = nn.ModuleList([nn.Conv2d(Ci, Co, (K, D)) for K in Ks])
2021
'''
2122
self.conv13 = nn.Conv2d(Ci, Co, (3, D))
2223
self.conv14 = nn.Conv2d(Ci, Co, (4, D))
@@ -38,9 +39,14 @@ def forward(self, x):
3839
x = Variable(x)
3940

4041
x = x.unsqueeze(1) # (N,Ci,W,D)
42+
4143
x = [F.relu(conv(x)).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks)
44+
45+
4246
x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] #[(N,Co), ...]*len(Ks)
47+
4348
x = torch.cat(x, 1)
49+
4450
'''
4551
x1 = self.conv_and_pool(x,self.conv13) #(N,Co)
4652
x2 = self.conv_and_pool(x,self.conv14) #(N,Co)
@@ -49,4 +55,4 @@ def forward(self, x):
4955
'''
5056
x = self.dropout(x) # (N,len(Ks)*Co)
5157
logit = self.fc1(x) # (N,C)
52-
return logit
58+
return logit

mydatasets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ def clean_str(string):
7777
if examples is None:
7878
path = self.dirname if path is None else path
7979
examples = []
80-
with open(os.path.join(path, 'rt-polarity.neg')) as f:
80+
with open(os.path.join(path, 'rt-polarity.neg'), errors='ignore') as f:
8181
examples += [
8282
data.Example.fromlist([line, 'negative'], fields) for line in f]
83-
with open(os.path.join(path, 'rt-polarity.pos')) as f:
83+
with open(os.path.join(path, 'rt-polarity.pos'), errors='ignore') as f:
8484
examples += [
8585
data.Example.fromlist([line, 'positive'], fields) for line in f]
8686
super(MR, self).__init__(examples, fields, **kwargs)

train.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ def train(train_iter, dev_iter, model, args):
1818
feature, target = batch.text, batch.label
1919
feature.data.t_(), target.data.sub_(1) # batch first, index align
2020
if args.cuda:
21-
feature, target = feature.cuda(), feature.cuda()
21+
feature, target = feature.cuda(), target.cuda()
2222

2323
optimizer.zero_grad()
2424
logit = model(feature)
25+
26+
#print('logit vector', logit.size())
27+
#print('target vector', target.size())
2528
loss = F.cross_entropy(logit, target)
2629
loss.backward()
2730
optimizer.step()
@@ -52,7 +55,7 @@ def eval(data_iter, model, args):
5255
feature, target = batch.text, batch.label
5356
feature.data.t_(), target.data.sub_(1) # batch first, index align
5457
if args.cuda:
55-
feature, target = feature.cuda(), feature.cuda()
58+
feature, target = feature.cuda(), target.cuda()
5659

5760
logit = model(feature)
5861
loss = F.cross_entropy(logit, target, size_average=False)

0 commit comments

Comments
 (0)