|
| 1 | +from inits import * |
| 2 | +import tensorflow as tf |
| 3 | +from tensorflow import keras |
| 4 | +from tensorflow.keras import layers |
| 5 | +from config import args |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +# global unique layer ID dictionary for layer name assignment |
| 11 | +_LAYER_UIDS = {} |
| 12 | + |
| 13 | + |
| 14 | +def get_layer_uid(layer_name=''): |
| 15 | + """Helper function, assigns unique layer IDs.""" |
| 16 | + if layer_name not in _LAYER_UIDS: |
| 17 | + _LAYER_UIDS[layer_name] = 1 |
| 18 | + return 1 |
| 19 | + else: |
| 20 | + _LAYER_UIDS[layer_name] += 1 |
| 21 | + return _LAYER_UIDS[layer_name] |
| 22 | + |
| 23 | + |
| 24 | +def sparse_dropout(x, rate, noise_shape): |
| 25 | + """ |
| 26 | + Dropout for sparse tensors. |
| 27 | + """ |
| 28 | + random_tensor = 1 - rate |
| 29 | + random_tensor += tf.random.uniform(noise_shape) |
| 30 | + dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool) |
| 31 | + pre_out = tf.sparse.retain(x, dropout_mask) |
| 32 | + return pre_out * (1./(1 - rate)) |
| 33 | + |
| 34 | + |
| 35 | +def dot(x, y, sparse=False): |
| 36 | + """ |
| 37 | + Wrapper for tf.matmul (sparse vs dense). |
| 38 | + """ |
| 39 | + if sparse: |
| 40 | + res = tf.sparse.sparse_dense_matmul(x, y) |
| 41 | + else: |
| 42 | + res = tf.matmul(x, y) |
| 43 | + return res |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +class Dense(layers.Layer): |
| 49 | + """Dense layer.""" |
| 50 | + def __init__(self, input_dim, output_dim, placeholders, dropout=0., sparse_inputs=False, |
| 51 | + act=tf.nn.relu, bias=False, featureless=False, **kwargs): |
| 52 | + super(Dense, self).__init__(**kwargs) |
| 53 | + |
| 54 | + if dropout: |
| 55 | + self.dropout = placeholders['dropout'] |
| 56 | + else: |
| 57 | + self.dropout = 0. |
| 58 | + |
| 59 | + self.act = act |
| 60 | + self.sparse_inputs = sparse_inputs |
| 61 | + self.featureless = featureless |
| 62 | + self.bias = bias |
| 63 | + |
| 64 | + # helper variable for sparse dropout |
| 65 | + self.num_features_nonzero = placeholders['num_features_nonzero'] |
| 66 | + |
| 67 | + with tf.variable_scope(self.name + '_vars'): |
| 68 | + self.vars['weights'] = glorot([input_dim, output_dim], |
| 69 | + name='weights') |
| 70 | + if self.bias: |
| 71 | + self.vars['bias'] = zeros([output_dim], name='bias') |
| 72 | + |
| 73 | + if self.logging: |
| 74 | + self._log_vars() |
| 75 | + |
| 76 | + def _call(self, inputs): |
| 77 | + x = inputs |
| 78 | + |
| 79 | + # dropout |
| 80 | + if self.sparse_inputs: |
| 81 | + x = sparse_dropout(x, 1-self.dropout, self.num_features_nonzero) |
| 82 | + else: |
| 83 | + x = tf.nn.dropout(x, 1-self.dropout) |
| 84 | + |
| 85 | + # transform |
| 86 | + output = dot(x, self.vars['weights'], sparse=self.sparse_inputs) |
| 87 | + |
| 88 | + # bias |
| 89 | + if self.bias: |
| 90 | + output += self.vars['bias'] |
| 91 | + |
| 92 | + return self.act(output) |
| 93 | + |
| 94 | + |
| 95 | +class GraphConvolution(layers.Layer): |
| 96 | + """ |
| 97 | + Graph convolution layer. |
| 98 | + """ |
| 99 | + def __init__(self, input_dim, output_dim, num_features_nonzero, |
| 100 | + dropout=0., |
| 101 | + is_sparse_inputs=False, |
| 102 | + activation=tf.nn.relu, |
| 103 | + bias=False, |
| 104 | + featureless=False, **kwargs): |
| 105 | + super(GraphConvolution, self).__init__(**kwargs) |
| 106 | + |
| 107 | + self.dropout = dropout |
| 108 | + self.activation = activation |
| 109 | + self.is_sparse_inputs = is_sparse_inputs |
| 110 | + self.featureless = featureless |
| 111 | + self.bias = bias |
| 112 | + self.num_features_nonzero = num_features_nonzero |
| 113 | + |
| 114 | + self.weights_ = [] |
| 115 | + for i in range(1): |
| 116 | + w = self.add_variable('weight' + str(i), [input_dim, output_dim]) |
| 117 | + self.weights_.append(w) |
| 118 | + if self.bias: |
| 119 | + self.bias = self.add_variable('bias', [output_dim]) |
| 120 | + |
| 121 | + |
| 122 | + # for p in self.trainable_variables: |
| 123 | + # print(p.name, p.shape) |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + def call(self, inputs, training=None): |
| 128 | + x, support_ = inputs |
| 129 | + |
| 130 | + # dropout |
| 131 | + if training is not False and self.is_sparse_inputs: |
| 132 | + x = sparse_dropout(x, self.dropout, self.num_features_nonzero) |
| 133 | + elif training is not False: |
| 134 | + x = tf.nn.dropout(x, self.dropout) |
| 135 | + |
| 136 | + |
| 137 | + # convolve |
| 138 | + supports = list() |
| 139 | + for i in range(len(support_)): |
| 140 | + if not self.featureless: # if it has features x |
| 141 | + pre_sup = dot(x, self.weights_[i], sparse=self.is_sparse_inputs) |
| 142 | + else: |
| 143 | + pre_sup = self.weights_[i] |
| 144 | + |
| 145 | + support = dot(support_[i], pre_sup, sparse=True) |
| 146 | + supports.append(support) |
| 147 | + |
| 148 | + output = tf.add_n(supports) |
| 149 | + |
| 150 | + # bias |
| 151 | + if self.bias: |
| 152 | + output += self.bias |
| 153 | + |
| 154 | + return self.activation(output) |
0 commit comments