-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (115 loc) · 4.93 KB
/
main.py
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
import numpy as np
import scipy.sparse as sp
import tensorflow as tf
import gc
import random
import csv
from code.clac_metric import cv_model_evaluate
from code.utils import *
from code.model import GCNModel
from code.opt import Optimizer
def PredictScore(train_drug_target_matrix, drug_matrix, target_matrix, seed, epochs, emb_dim, dp, lr, adjdp):
np.random.seed(seed)
tf.reset_default_graph()
tf.set_random_seed(seed)
adj = constructHNet(train_drug_target_matrix, drug_matrix, target_matrix)
adj = sp.csr_matrix(adj)
association_nam = train_drug_target_matrix.sum()
X = constructNet(train_drug_target_matrix)
features = sparse_to_tuple(sp.csr_matrix(X))
num_features = features[2][1]
features_nonzero = features[1].shape[0]
adj_orig = train_drug_target_matrix.copy()
adj_orig = sparse_to_tuple(sp.csr_matrix(adj_orig))
adj_norm = preprocess_graph(adj)
adj_nonzero = adj_norm[1].shape[0]
placeholders = {
'features': tf.sparse_placeholder(tf.float32),
'adj': tf.sparse_placeholder(tf.float32),
'adj_orig': tf.sparse_placeholder(tf.float32),
'dropout': tf.placeholder_with_default(0., shape=()),
'adjdp': tf.placeholder_with_default(0., shape=())
}
model = GCHNModel(placeholders, num_features, emb_dim,
features_nonzero, adj_nonzero, train_drug_target_matrix.shape[0], name='GCHN')
with tf.name_scope('optimizer'):
opt = Optimizer(
preds=model.reconstructions,
labels=tf.reshape(tf.sparse_tensor_to_dense(
placeholders['adj_orig'], validate_indices=False), [-1]),
model=model,
lr=lr, num_u=train_drug_target_matrix.shape[0], num_v=train_drug_target_matrix.shape[1], association_nam=association_nam)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(epochs):
feed_dict = dict()
feed_dict.update({placeholders['features']: features})
feed_dict.update({placeholders['adj']: adj_norm})
feed_dict.update({placeholders['adj_orig']: adj_orig})
feed_dict.update({placeholders['dropout']: dp})
feed_dict.update({placeholders['adjdp']: adjdp})
_, avg_cost = sess.run([opt.opt_op, opt.cost], feed_dict=feed_dict)
if epoch % 100 == 0:
feed_dict.update({placeholders['dropout']: 0})
feed_dict.update({placeholders['adjdp']: 0})
res = sess.run(model.reconstructions, feed_dict=feed_dict)
print("Epoch:", '%04d' % (epoch + 1),
"train_loss=", "{:.5f}".format(avg_cost))
print('Optimization Finished!')
feed_dict.update({placeholders['dropout']: 0})
feed_dict.update({placeholders['adjdp']: 0})
res = sess.run(model.reconstructions, feed_dict=feed_dict)
sess.close()
return res
def cross_validation_experiment(drug_target_matrix, drug_matrix, target_matrix, seed, epochs, emb_dim, dp, lr, adjdp):
index_matrix = np.mat(np.where(drug_target_matrix == 1))
association_nam = index_matrix.shape[1]
random_index = index_matrix.T.tolist()
random.seed(seed)
random.shuffle(random_index)
k_folds = 5
CV_size = int(association_nam / k_folds)
temp = np.array(random_index[:association_nam - association_nam %
k_folds]).reshape(k_folds, CV_size, -1).tolist()
temp[k_folds - 1] = temp[k_folds - 1] + \
random_index[association_nam - association_nam % k_folds:]
random_index = temp
metric = np.zeros((1, 7))
print("seed=%d, evaluating drug-target...." % (seed))
for k in range(k_folds):
print("------this is %dth cross validation------" % (k+1))
train_matrix = np.matrix(drug_target_matrix, copy=True)
train_matrix[tuple(np.array(random_index[k]).T)] = 0
drug_len = drug_target_matrix.shape[0]
target_len = drug_target_matrix.shape[1]
drug_target_res = PredictScore(
train_matrix, drug_matrix, target_matrix, seed, epochs, emb_dim, dp, lr, adjdp)
predict_y_proba = drug_target_res.reshape(drug_len, target_len)
print(predict_y_proba)
metric_tmp = cv_model_evaluate(
drug_target_matrix, predict_y_proba, train_matrix)
print(metric_tmp)
metric += metric_tmp
del train_matrix
gc.collect()
print(metric / k_folds)
metric = np.array(metric / k_folds)
return metric
if __name__ == "__main__":
drug_sim = np.loadtxt()
target_sim = np.loadtxt()
drug_target_matrix = np.loadtxt()
epoch = 4000
emb_dim = 64
lr = 0.01
adjdp = 0.6
dp = 0.4
simw = 6
result = np.zeros((1, 7), float)
average_result = np.zeros((1, 7), float)
circle_time = 1
for i in range(circle_time):
result += cross_validation_experiment(
drug_target_matrix, drug_sim*simw, target_sim*simw, i, epoch, emb_dim, dp, lr, adjdp)
average_result = result / circle_time
print(average_result)