-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
32 lines (28 loc) · 1.56 KB
/
utils.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
import tensorflow as tf
import numpy as np
from tensorflow.python.framework import ops
# from tflearn.initializations import trunAAAcated_normal
# from tflearn.activations import relu
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1,dtype=tf.float32)
return tf.Variable(initial, dtype=tf.float32) # 设置默认的变量w
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape,dtype=tf.float32)
return tf.Variable(initial, dtype=tf.float32)##设置默认的变量b
def a_layer(x,units):
W = weight_variable([x.get_shape().as_list()[1],units])
b = bias_variable([units])
tf.add_to_collection('l2_reg', tf.contrib.layers.l2_regularizer(1.0)(W))
return tf.nn.relu(tf.matmul(x, W) + b)
# 从特征中选择适合的特征,然后通过relu输出,最终和相似性进行相乘,相加上从drug_disease中得到的特征,还有本身的w
def bi_layer(x0,x1,sym,dim_pred):
if sym == False:
W0p = weight_variable([x0.get_shape().as_list()[1],dim_pred])
W1p = weight_variable([x1.get_shape().as_list()[1],dim_pred])
tf.add_to_collection('l2_reg', tf.contrib.layers.l2_regularizer(1.0)(W0p))
tf.add_to_collection('l2_reg', tf.contrib.layers.l2_regularizer(1.0)(W1p))
return tf.matmul(tf.matmul(x0, W0p), tf.matmul(x1, W1p),transpose_b=True)
else:
W0p = weight_variable([x0.get_shape().as_list()[1],dim_pred])
tf.add_to_collection('l2_reg', tf.contrib.layers.l2_regularizer(1.0)(W0p))
return tf.matmul(tf.matmul(x0, W0p), tf.matmul(x1, W0p),transpose_b=True)