-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript5.py
More file actions
135 lines (116 loc) · 3.33 KB
/
script5.py
File metadata and controls
135 lines (116 loc) · 3.33 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
124
125
126
127
128
129
130
131
132
133
134
135
from __future__ import division
from utility import *
import torch
import random
from torch.autograd import Variable
test_data = load_test('SEQ194.txt')
#print test[0]
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,0.0])
# Data preparation
data = pos_data + neg_data
data = np.array(data)
np.random.shuffle(data)
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,12).double()
self.linear3 = torch.nn.Linear(12,1).double()
#self.tanh = torch.nn.Tanh()
self.sigmoid = torch.nn.Sigmoid()
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)
x = self.linear3(x)
return self.sigmoid(x)
loss_fn = torch.nn.MSELoss(size_average=True)
learning_rate = 1e-4
model = Discrim()
#print len(data) --> 633
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
#print data[0]
for epoch in xrange(5):
for index in xrange(633):
#print index
train = data[index]
x , y = train[0] , train[1]
#print x,y
#break
x = Variable(torch.from_numpy(np.array(x)), requires_grad = False).view(1,3,100).double()
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(x)
loss = loss_fn(y_pred, inpt_train_y)
if index%100 == 0:
print loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Testing
window = 20
for test in test_data:
sequence, label = test[1], test[2]
# Create window lists
prediction = [[]] * len(sequence)
indices = range(len(sequence))
indices_list = []
for i in xrange(len(sequence) - window):
a = indices[i:i+window]
indices_list.append(a)
# Predict
embed = embedding(protvec, sequence[i:i+window])
#print embed
x = Variable(torch.from_numpy(np.array(embed)), requires_grad = False).view(1,3,100).double()
pred = model(x)
#print pred[0][0].data.numpy()
for index in a:
prediction[index].append(pred[0][0].data.numpy().tolist())
break
for index, p in enumerate(prediction):
prediction[index] = sum(p) / float(len(p))
for i in prediction:
if i > 0.5:
print 'hi'
break