-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript3.py
More file actions
123 lines (107 loc) · 3.08 KB
/
script3.py
File metadata and controls
123 lines (107 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from __future__ import division
from sklearn import svm
import numpy as np
import pandas as pd
import scipy as scip
from scipy import linalg
from scipy.spatial.distance import pdist, squareform
import torch
from torch.autograd import Variable
from utility import *
from sklearn import metrics
protvec = initialize()
# Positive and Negative Datasets
pos_data = []
neg_data = []
# Read in the Positive Dataset
with open('pos.data') as f:
for line in f:
line = line.rstrip()
p = embedding(protvec,line)
pos_data.append([p,1.0])
# Read in the Negative Dataset
with open('neg.data') as f:
for line in f:
n = []
line = line.rstrip()
n = embedding(protvec, line)
neg_data.append([n,-1.0])
# Data preparation
data = pos_data + neg_data
data = np.array(data)
np.random.shuffle(data)
data, test = np.array_split(data,2)
print len(data), len(test)
data = data.tolist()
x,y = data[0][0], data[0][1]
print x
print y
input = Variable(torch.from_numpy(np.array(x))).view(1,3,100).double()
print input
# 1D convolution
class Discrim(torch.nn.Module):
def __init__(self):
super(Discrim, self).__init__()
self.c1 = torch.nn.Conv1d(3,20,3).double()
self.relu = torch.nn.LeakyReLU(0.1)
self.drop = torch.nn.Dropout()
self.p1 = torch.nn.MaxPool1d(2)
self.c2 = torch.nn.Conv1d(20,1,2).double()
#torch.nn.LeakyReLU(0.1)
#torch.nn.Dropout()
self.p2 = torch.nn.MaxPool1d(2)
self.linear = torch.nn.Linear(24,12).double()
self.linear2 = torch.nn.Linear(12,1).double()
self.tanh = torch.nn.Tanh()
def forward(self, input):
x = self.c1(input)
x = self.relu(x)
x = self.drop(x)
x = self.p1(x)
x = self.c2(x)
x = self.relu(x)
x = self.drop(x)
x = self.p2(x)
x = self.linear(x.view(1,24))
x = self.drop(x)
x = self.linear2(x)
return self.tanh(x)
loss_fn = torch.nn.MSELoss(size_average=True)
learning_rate = 1e-4
model = Discrim()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for iteration in range(1000):
inpt_train_y = torch.from_numpy(np.array([y]))
inpt_train_y = inpt_train_y.double()
inpt_train_y = Variable(inpt_train_y, requires_grad=False)
print inpt_train_y
y_pred = model(input)
loss = loss_fn(y_pred, inpt_train_y)
if iteration%100 == 0:
print loss[0].data.numpy()
optimizer.zero_grad()
loss.backward()
optimizer.step()
total = 0
correct = 0
prediction = -1
test = test.tolist()
for item in xrange(len(test)):
features ,label = test[item][0], test[item][1]
features = np.array(features)
label = np.array(label, dtype=np.float64)
features = torch.from_numpy(features)
features = features.float()
features = Variable(features)
predict = model.forward(features.view(1,3,100).double()).data.numpy()
if predict[0][0] < 0.0:
prediction = -1.0
#print "hi"
else:
prediction = 1.0
#print "world!"
if prediction == label:
correct = correct + 1
total = total + 1
print correct, correct/total, total
print "done"