示例#1
0
def construct_placeholders(num_classes):
    placeholders = {
        'labels':
        tf.placeholder(DTYPE, shape=(None, num_classes), name='labels'),
        'node_subgraph':
        tf.placeholder(tf.int32, shape=(None), name='node_subgraph'),
        'dropout':
        tf.placeholder(DTYPE, shape=(None), name='dropout'),
        'adj_subgraph':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph', shape=(None, None)),
        'adj_subgraph_0':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_0'),
        'adj_subgraph_1':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_1'),
        'adj_subgraph_2':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_2'),
        'adj_subgraph_3':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_3'),
        'adj_subgraph_4':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_4'),
        'adj_subgraph_5':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_5'),
        'adj_subgraph_6':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_6'),
        'adj_subgraph_7':
        tf.sparse_placeholder(DTYPE, name='adj_subgraph_7'),
        'dim0_adj_sub':
        tf.placeholder(tf.int64, shape=(None), name='dim0_adj_sub'),
        'norm_loss':
        tf.placeholder(DTYPE, shape=(None), name='norm_loss'),
        'is_train':
        tf.placeholder(tf.bool, shape=(None), name='is_train')
    }
    return placeholders
示例#2
0
    def init(self):
        assert self.args.alignment_module == 'mapping'
        assert self.args.neg_triple_num > 1
        assert self.args.learning_rate >= 0.01

        self.num_supports = self.args.support_number
        self.utils = GCN_Utils(self.args, self.kgs)
        self.attr = load_attr(self.kgs.entities_num, self.kgs)
        self.adj, self.ae_input, self.train = self.utils.load_data(self.attr)
        self.e = self.ae_input[2][0]
        self.support = [self.utils.preprocess_adj(self.adj)]
        self.ph_ae = {
            "support": [
                tf.sparse_placeholder(tf.float32)
                for _ in range(self.args.support_number)
            ],
            "features":
            tf.sparse_placeholder(tf.float32),
            "dropout":
            tf.placeholder_with_default(0., shape=()),
            "num_features_nonzero":
            tf.placeholder_with_default(0, shape=())
        }
        self.ph_se = {
            "support": [
                tf.sparse_placeholder(tf.float32)
                for _ in range(self.args.support_number)
            ],
            "features":
            tf.placeholder(tf.float32),
            "dropout":
            tf.placeholder_with_default(0., shape=()),
            "num_features_nonzero":
            tf.placeholder_with_default(0, shape=())
        }
        self.model_ae = GCN_Align_Unit(self.args,
                                       self.ph_ae,
                                       input_dim=self.ae_input[2][1],
                                       output_dim=self.args.ae_dim,
                                       ILL=self.train,
                                       sparse_inputs=True,
                                       featureless=False,
                                       logging=False)
        self.model_se = GCN_Align_Unit(self.args,
                                       self.ph_se,
                                       input_dim=self.e,
                                       output_dim=self.args.se_dim,
                                       ILL=self.train,
                                       sparse_inputs=False,
                                       featureless=True,
                                       logging=False)

        self.session = load_session()
        tf.global_variables_initializer().run(session=self.session)
def get_placeholder(adj):
    tf.compat.v1.disable_eager_execution()
    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=()),
        'real_distribution': tf.placeholder(dtype=tf.float32, shape=[adj.shape[0], FLAGS.hidden2],
                                            name='real_distribution'),
        'sample': tf.placeholder(tf.float32)

    }

    return placeholders
示例#4
0
    def __init__(self,
                 atom_types,
                 offset=0.0,
                 offset_trainable=False,
                 name="ANN"):
        self.atom_types = atom_types
        self.name = name

        self.pairPot = {}
        self.F = _tf.identity
        self.rho = {}

        self.inputs = {}
        self.b_maps = {}
        for t in atom_types:
            self.inputs[t] = _tf.placeholder(shape=(None, 1),
                                             dtype=precision,
                                             name="ANN_input_%s" % t)
            self.b_maps[t] = _tf.sparse_placeholder(dtype=precision,
                                                    name="b_map_{}".format(t))
        self.offset = _tf.Variable(offset,
                                   dtype=precision,
                                   trainable=offset_trainable,
                                   name="offset",
                                   collections=[
                                       _tf.GraphKeys.MODEL_VARIABLES,
                                       _tf.GraphKeys.GLOBAL_VARIABLES
                                   ])
        _tf.summary.scalar("offset", self.offset, family="modelParams")
示例#5
0
def pre_process_sentences(messages):
    input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
    encodings = module(
    inputs=dict(
        values=input_placeholder.values,
        indices=input_placeholder.indices,
        dense_shape=input_placeholder.dense_shape))

    with tf.Session() as sess:
        spm_path = sess.run(module(signature="spm_path"))

    sp = spm.SentencePieceProcessor()
    sp.Load(spm_path)
    print("SentencePiece model loaded at {}.".format(spm_path))
    
    def process_to_IDs_in_sparse_format(sp, sentences):
        ids = [sp.EncodeAsIds(x) for x in sentences]
        max_len = max(len(x) for x in ids)
        dense_shape=(len(ids), max_len)
        values=[item for sublist in ids for item in sublist]
        indices=[[row,col] for row in range(len(ids)) for col in range(len(ids[row]))]
        
        return (values, indices, dense_shape)
    
    values, indices, dense_shape = process_to_IDs_in_sparse_format(sp, messages)
    logging.set_verbosity(logging.ERROR)


    with tf.Session() as session:
      session.run([tf.global_variables_initializer(), tf.tables_initializer()])
      message_embeddings = session.run(encodings,feed_dict={input_placeholder.values: values,
                input_placeholder.indices: indices,
                input_placeholder.dense_shape: dense_shape})
    
    return message_embeddings
示例#6
0
文件: cox.py 项目: SerTelnov/DLF
    def __init__(self, lr, batch_size, dimension, util_train, util_test, campaign, reg_lambda, nn=False):
        # hyperparameters
        self.lr = lr
        self.batch_size = batch_size
        self.util_train = util_train
        self.util_test = util_test
        self.reg_lambda = reg_lambda

        self.train_data_amt = util_train.get_data_amt()
        self.test_data_amt = util_test.get_data_amt()

        # output dir
        model_name = "{}_{}_{}".format(self.lr, self.reg_lambda, self.batch_size)
        if nn:
            self.output_dir = "output/coxnn/{}/{}/".format(campaign, model_name)
        else:
            self.output_dir = "output/cox/{}/{}/".format(campaign, model_name)
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)

        # reset graph
        ops.reset_default_graph()

        # placeholders, sorted value
        tfc.disable_eager_execution()
        self.X = tfc.sparse_placeholder(tf.float64)
        self.z = tfc.placeholder(tf.float64)
        self.b = tfc.placeholder(tf.float64)
        self.y = tfc.placeholder(tf.float64)

        # computation graph, linear estimator or neural network
        if nn:
            hidden_size = 20
            self.w1 = tf.Variable(initial_value=tfc.truncated_normal(shape=[dimension, hidden_size], dtype=tf.float64),
                                  name='w1')
            self.w2 = tf.Variable(initial_value=tfc.truncated_normal(shape=[hidden_size, 1], dtype=tf.float64),
                                  name='w2')
            self.hidden_values = tf.nn.relu(tfc.sparse_tensor_dense_matmul(self.X, self.w1))
            self.index = tf.matmul(self.hidden_values, self.w2)
            self.reg = tf.nn.l2_loss(self.w1[1:, ]) + tf.nn.l2_loss(self.w2[1:, ])
        else:
            self.w = tf.Variable(initial_value=tfc.truncated_normal(shape=[dimension, 1], dtype=tf.float64), name='w')
            self.index = tfc.sparse_tensor_dense_matmul(self.X, self.w)
            self.reg = tf.reduce_sum(tf.abs(self.w[1:, ]))

        self.multiple_times = tf.exp(self.index)
        self.loss = -tf.reduce_sum((self.index - tfc.log(tf.cumsum(self.multiple_times, reverse=True))) * self.y) + \
                    self.reg
        self.optimizer = tfc.train.GradientDescentOptimizer(self.lr)
        self.train_step = self.optimizer.minimize(self.loss)

        # for test h0
        self.base = self.z * self.y + self.b * (1 - self.y)
        self.candidate = (1 / tf.cumsum(tf.exp(self.index), reverse=True)) * self.y

        # session initialization
        config = tfc.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tfc.Session(config=config)
        tfc.global_variables_initializer().run(session=self.sess)
示例#7
0
 def __init__(self):
     # placeholder
     self.sph_user = tf.sparse_placeholder(tf.int32, name='sph_user')
     self.sph_doc = tf.sparse_placeholder(tf.int32, name='sph_doc')
     self.sph_con = tf.sparse_placeholder(tf.int32, name='sph_con')
     self.ph_reward = tf.placeholder(tf.float32, name='ph_reward')
     self.ph_nq = tf.placeholder(
         tf.float32,
         shape=[pd['batch_size'], pd['rnn_max_len']],
         name='ph_nq')
     # main networks
     self.dst_embed, self.mq = self.build_net('main')
     # target networks
     _, self.tq = self.build_net('target')
     diff = tf.reshape(self.ph_reward, [-1]) + tf.scalar_mul(
         tf.constant(pd['gamma']), tf.reshape(
             self.ph_nq, [-1])) - tf.reshape(self.mq, [-1])
     self.loss = tf.reduce_mean(tf.square(diff))
     self.a_grads = tf.clip_by_global_norm(
         tf.gradients(self.mq, self.dst_embed), pd['grad_clip'])[0]
     vs = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                            scope='main/value')
     vs.extend(
         tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                           scope='main/feat_embedding'))
     self.grads = tf.clip_by_global_norm(tf.gradients(self.loss, vs),
                                         pd['grad_clip'])[0]
     with tf.variable_scope('train_value'):
         optimizer = tf.train.AdamOptimizer(pd['lr'])
         self.opt = optimizer.apply_gradients(zip(self.grads, vs))
     self.m_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                       scope="main/value")
     self.m_params.extend(
         tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                           scope='main/feat_embedding'))
     self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                       scope="target/value")
     self.t_params.extend(
         tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                           scope='target/feat_embedding'))
     alpha = pd['double_networks_sync_step']
     self.sync_op = [
         tf.assign(t, (1.0 - alpha) * t + alpha * m)
         for t, m in zip(self.t_params, self.m_params)
     ]
     self.total_loss, self.batch_counter = 0.0, 0
示例#8
0
def get_placeholder(adj):
    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=()),
        'real_distribution':
        tf.placeholder(dtype=tf.float32,
                       shape=[adj.shape[0], FLAGS.hidden2],
                       name='real_distribution')
    }

    return placeholders
示例#9
0
 def __init__(self):
     # placeholder
     self.sph_user = tf.sparse_placeholder(tf.int32, name='sph_user')
     self.sph_doc = tf.sparse_placeholder(tf.int32, name='sph_doc')
     self.sph_con = tf.sparse_placeholder(tf.int32, name='sph_con')
     # policy gradient
     self.a_grads = tf.placeholder(tf.float32)
     # policy network
     self.doc_embed, self.mpa, self.mea = self.build_net('main')
     # target network
     _, self.tpa, self.tea = self.build_net('target')
     # optional supervised signal, to avoid instability of actions in extreme cases
     self.loss = tf.losses.mean_squared_error(self.doc_embed, self.mea)
     params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                scope='main/policy')
     params.extend(
         tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                           scope='main/feat_embedding'))
     self.grads = tf.clip_by_global_norm(tf.gradients(self.loss, params),
                                         pd['grad_clip'])[0]
     policy_grads = \
         tf.clip_by_global_norm(tf.gradients(ys=self.mea, xs=params, grad_ys=self.a_grads), pd['grad_clip'])[0]
     opt1 = tf.train.AdamOptimizer(-pd['lr'])
     opt2 = tf.train.AdamOptimizer(pd['lr'])
     with tf.variable_scope("train_policy"):
         self.opt_a1 = opt1.apply_gradients(zip(policy_grads, params))
         self.opt_a2 = opt2.apply_gradients(zip(self.grads, params))
     self.m_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                       scope="main/policy")
     self.m_params.extend(
         tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                           scope='main/feat_embedding'))
     self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                       scope="target/policy")
     self.t_params.extend(
         tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                           scope='target/feat_embedding'))
     alpha = pd['double_networks_sync_step']
     self.sync_op = [
         tf.assign(t, (1.0 - alpha) * t + alpha * m)
         for t, m in zip(self.t_params, self.m_params)
     ]
     self.total_loss, self.batch_counter = 0.0, 0
 def _get_weighted_json_serving_input_fn(self):
   inputs = {
       'age':
           tf.placeholder(shape=[None], name='age', dtype=tf.float32),
       'language':
           tf.placeholder(shape=[None], name='language', dtype=tf.string),
       'class_identity':
           tf.placeholder(shape=[None], name='class_identity', dtype=tf.int32),
       'language_weights': tf.sparse_placeholder(tf.float32)
   }
   return tf.estimator.export.ServingInputReceiver(inputs, inputs)
示例#11
0
    def __init__(self,
                 d,
                 nc,
                 hidden_size,
                 nlayers,
                 lr,
                 weight_decay,
                 dropout,
                 sparse_features=True):
        self.nc = nc
        self.sparse_features = sparse_features

        if sparse_features:
            self.batch_feats = tf.sparse_placeholder(tf.float32, None,
                                                     'features')
        else:
            self.batch_feats = tf.placeholder(tf.float32, [None, d],
                                              'features')
        self.batch_pprw = tf.placeholder(tf.float32, [None], 'ppr_weights')
        self.batch_idx = tf.placeholder(tf.int32, [None], 'idx')
        self.batch_labels = tf.placeholder(tf.int32, [None], 'labels')

        Ws = [tf.get_variable('W1', [d, hidden_size])]
        for i in range(nlayers - 2):
            Ws.append(tf.get_variable(f'W{i + 2}', [hidden_size, hidden_size]))
        Ws.append(tf.get_variable(f'W{nlayers}', [hidden_size, nc]))

        feats_drop = mixed_dropout(self.batch_feats, dropout)
        if sparse_features:
            h = tf.sparse.sparse_dense_matmul(feats_drop, Ws[0])
        else:
            h = tf.matmul(feats_drop, Ws[0])
        for W in Ws[1:]:
            h = tf.nn.relu(h)
            h_drop = tf.nn.dropout(h, rate=dropout)
            h = tf.matmul(h_drop, W)
        self.logits = h

        weighted_logits = tf.tensor_scatter_nd_add(
            tf.zeros((tf.shape(self.batch_labels)[0], nc)),
            self.batch_idx[:, None], self.logits * self.batch_pprw[:, None])

        loss_per_node = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=self.batch_labels, logits=weighted_logits)

        l2_reg = tf.add_n([tf.nn.l2_loss(weight) for weight in Ws])
        self.loss = tf.reduce_mean(loss_per_node) + weight_decay * l2_reg

        self.preds = tf.argmax(weighted_logits, 1)
        self.update_op = tf.train.AdamOptimizer(lr).minimize(self.loss)

        self.cached = {}
示例#12
0
    def __init__(self, atom_types, error_scaling=1.):
        self.target = _tf.placeholder(shape=(None, ),
                                      dtype=precision,
                                      name="target")
        self.atom_types = atom_types
        self.error_scaling = error_scaling

        self.ANNs = {}
        self.atom_maps = {}

        for t in self.atom_types:
            self.atom_maps[t] = _tf.sparse_placeholder(shape=(None, None),
                                                       dtype=precision,
                                                       name="%s_map" % t)
示例#13
0
    def __init__(self):
        # init some parameters
        self.g_type = 'powerlaw'  #'erdos_renyi', 'powerlaw', 'small-world', 'barabasi_albert'
        self.embedding_size = EMBEDDING_SIZE
        self.learning_rate = LEARNING_RATE
        self.reg_hidden = REG_HIDDEN
        self.TrainSet = graph.py_GSet()
        self.TestSet = graph.py_GSet()
        self.utils = utils.py_Utils()
        self.TrainBetwList = []
        self.TestBetwList = []
        self.metrics = metrics.py_Metrics()
        self.inputs = dict()
        self.activation = tf.nn.leaky_relu  #leaky_relu relu selu elu

        self.ngraph_train = 0
        self.ngraph_test = 0

        # [node_cnt, node_feat_dim]
        self.node_feat = tf.placeholder(tf.float32, name="node_feat")
        # [node_cnt, aux_feat_dim]
        self.aux_feat = tf.placeholder(tf.float32, name="aux_feat")
        # [node_cnt, node_cnt]
        self.n2nsum_param = tf.sparse_placeholder(tf.float64,
                                                  name="n2nsum_param")

        # [node_cnt,1]
        self.label = tf.placeholder(tf.float32, shape=[None, 1], name="label")
        # sample node pairs to compute the ranking loss
        self.pair_ids_src = tf.placeholder(tf.int32,
                                           shape=[1, None],
                                           name='pair_ids_src')
        self.pair_ids_tgt = tf.placeholder(tf.int32,
                                           shape=[1, None],
                                           name='pair_ids_tgt')

        self.loss, self.trainStep, self.betw_pred, self.node_embedding, self.param_list = self.BuildNet(
        )

        self.saver = tf.train.Saver(max_to_keep=None)
        config = tf.ConfigProto(
            device_count={"CPU": 8},  # limit to num_cpu_core CPU usage
            inter_op_parallelism_threads=100,
            intra_op_parallelism_threads=100,
            log_device_placement=False)
        config.gpu_options.allow_growth = True
        self.session = tf.Session(config=config)

        self.session.run(tf.global_variables_initializer())
示例#14
0
文件: models.py 项目: imnitishng/muse
    def load_persisting_model(self):
        '''
        Load the TF module and model alongwith graph to the machine's memory
        to speed up future machine inference calls.
        '''
        with tf.Session(graph=self.graph) as session:
            self.module = hub.Module("./models/use_lite")
            self.input_placeholder = tf.sparse_placeholder(tf.int64,
                                                           shape=[None, None])
            self.encodings = self.module(
                inputs=dict(values=self.input_placeholder.values,
                            indices=self.input_placeholder.indices,
                            dense_shape=self.input_placeholder.dense_shape))

            self.load_sp_model()
示例#15
0
def init_embeddings_model():
    """
    Initialize model for embedding pieces of content into a vector
    :return: None
    """
    g = tfv1.Graph()
    with g.as_default():
        model_path = "https://tfhub.dev/google/universal-sentence-encoder-lite/2"
        module = hub.Module(model_path)
        sts_input1_ = tfv1.sparse_placeholder(tfv1.int64, shape=[None, None])
        embeddings_ = module(inputs=dict(values=sts_input1_.values,
                                         indices=sts_input1_.indices,
                                         dense_shape=sts_input1_.dense_shape))

    return g, sts_input1_, embeddings_, module
示例#16
0
def construct_placeholders(edge_types):  #构建形成稀疏张量
    placeholders = {
        'batch':
        tf.placeholder(tf.int32, name='batch'),
        'batch_edge_type_idx':
        tf.placeholder(tf.int32, shape=(), name='batch_edge_type_idx'),
        'batch_row_edge_type':
        tf.placeholder(tf.int32, shape=(), name='batch_row_edge_type'),
        'batch_col_edge_type':
        tf.placeholder(tf.int32, shape=(), name='batch_col_edge_type'),
        'degrees':
        tf.placeholder(tf.int32, shape=(), name='degress'),
        'dropout':
        tf.placeholder_with_default(0., shape=(), name='dropout'),
    }
    placeholders.update({
        'adj_mats_%d,%d,%d' % (i, j, k): tf.sparse_placeholder(tf.float32)
        for i, j in edge_types for k in range(edge_types[i, j])
    })
    placeholders.update({
        'feat_%d' % i: tf.sparse_placeholder(tf.float32)
        for i, _ in edge_types
    })
    return placeholders
示例#17
0
    def __init__(self):
        _logger.info('Downloading the model from TensorFlow Hub...')

        with tf.Session() as sess:
            module = tfhub.Module("https://tfhub.dev/google/universal-sentence-encoder-lite/2")
            spm_path = sess.run(module(signature="spm_path"))  # spm_path now contains a path to the SentencePiece model stored inside the TF-Hub module
        self.sp = spm.SentencePieceProcessor()
        self.sp.Load(spm_path)

        self.input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
        self.embeddings_model = module(
            inputs=dict(
                values=self.input_placeholder.values,
                indices=self.input_placeholder.indices,
                dense_shape=self.input_placeholder.dense_shape
            )
        )
示例#18
0
def use_lite_embed():
    module = hub.Module(
        "https://tfhub.dev/google/universal-sentence-encoder-lite/2")
    global graph
    with graph.as_default():
        input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
        encodings = module(inputs=dict(
            values=input_placeholder.values,
            indices=input_placeholder.indices,
            dense_shape=input_placeholder.dense_shape,
        ))

    with tf.Session() as sess:
        spm_path = sess.run(module(signature="spm_path"))
    sp = spm.SentencePieceProcessor()
    sp.Load(spm_path)

    def process_to_IDs_in_sparse_format(sp, sentences):
        ids = [sp.EncodeAsIds(x) for x in sentences]
        max_len = max(len(x) for x in ids)
        dense_shape = (len(ids), max_len)
        values = [item for sublist in ids for item in sublist]
        indices = [[row, col] for row in range(len(ids))
                   for col in range(len(ids[row]))]
        return (values, indices, dense_shape)

    def embed(msgs):
        values, indices, dense_shape = process_to_IDs_in_sparse_format(
            sp, msgs)
        global graph
        with graph.as_default(), tf.Session() as session:
            session.run(
                [tf.global_variables_initializer(),
                 tf.tables_initializer()])
            return session.run(
                encodings,
                feed_dict={
                    input_placeholder.values: values,
                    input_placeholder.indices: indices,
                    input_placeholder.dense_shape: dense_shape,
                },
            )

    return embed
    def __init__(
        self,
        model_link="https://tfhub.dev/google/universal-sentence-encoder-lite/2"
    ):
        tf.disable_v2_behavior()
        self.graph = tf.Graph()
        with self.graph.as_default():
            module = hub.Module(model_link)
            self.input_placeholder = tf.sparse_placeholder(tf.int64,
                                                           shape=[None, None])
            self.encodings = module(
                inputs=dict(values=self.input_placeholder.values,
                            indices=self.input_placeholder.indices,
                            dense_shape=self.input_placeholder.dense_shape))

            with tf.Session() as sess:
                spm_path = sess.run(module(signature="spm_path"))

            self.sp = spm.SentencePieceProcessor()
            self.sp.Load(spm_path)
            print("SentencePiece model loaded at {}.".format(spm_path))
    def rank_semantic(self, query, hits):
        values, indices, dense_shape = self.process_to_IDs_in_sparse_format([query] + hits)
        with tf.Session(graph=self.graph) as sess:
            input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
            encodings = self.embed_module(
                inputs=dict(
                    values=input_placeholder.values,
                    indices=input_placeholder.indices,
                    dense_shape=input_placeholder.dense_shape))

            sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
            message_embeddings = sess.run(
                encodings,
                feed_dict={input_placeholder.values: values,
                           input_placeholder.indices: indices,
                           input_placeholder.dense_shape: dense_shape})

            message_embeddings = np.array(message_embeddings).tolist()

        query_embedding, hits_embeddings = message_embeddings[0], message_embeddings[1:]

        scores = np.inner(query_embedding, hits_embeddings)

        return scores
示例#21
0
def GNN(A, X):
    # 使用graph_hi构造图邻接矩阵A
    np.random.seed(0)  # for reproducibility
    ITER = 10000
    # Parameters
    P = OrderedDict([
        ('es_patience', ITER),
        ('dataset', ['cora'
                     ]),  # 'cora', 'citeseer', 'pubmed', 'cloud', or 'synth'
        ('H_', [None]),
        ('n_channels', [16]),
        ('learning_rate', [5e-4])
    ])
    ############################################################################
    # LOAD DATASET
    ############################################################################

    A = np.maximum(A, A.T)
    A = sp.csr_matrix(A, dtype=np.float32)

    X = X.todense()
    n_feat = X.shape[-1]

    ############################################################################
    # GNN MODEL
    ############################################################################
    X_in = Input(
        tensor=tf.placeholder(tf.float32, shape=(None, n_feat), name='X_in'))
    A_in = Input(tensor=tf.sparse_placeholder(tf.float32, shape=(None, None)),
                 name='A_in',
                 sparse=True)
    # S_in = Input(tensor=tf.placeholder(tf.int32, shape=(None,), name='segment_ids_in'))

    A_norm = normalized_adjacency(A)
    X_1 = GCSConv(P['n_channels'],
                  kernel_initializer='he_normal',
                  activation='elu')([X_in, A_in])

    pool1, adj1, C = MinCutPool(k=n_classes,
                                h=P['H_'],
                                activation='elu',
                                return_mask=True)([X_1, A_in])

    model = Model([X_in, A_in], [pool1, adj1, C])
    model.compile('adam', None)

    ############################################################################
    # TRAINING
    ############################################################################
    # Setup
    sess = K.get_session()
    loss = model.total_loss
    opt = tf.train.AdamOptimizer(learning_rate=P['learning_rate'])
    train_step = opt.minimize(loss)

    # Initialize all variables
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    # Fit layer
    tr_feed_dict = {X_in: X, A_in: sp_matrix_to_sp_tensor_value(A_norm)}

    best_loss = np.inf
    patience = P['es_patience']
    tol = 1e-5
    for _ in tqdm(range(ITER)):
        outs = sess.run([train_step, model.losses[0], model.losses[1], C],
                        feed_dict=tr_feed_dict)
        # c = np.argmax(outs[3], axis=-1)
        if outs[1] + outs[2] + tol < best_loss:
            best_loss = outs[1] + outs[2]
            patience = P['es_patience']
        else:
            patience -= 1
            if patience == 0:
                break

    ############################################################################
    # RESULTS
    ############################################################################
    C_ = sess.run([C], feed_dict=tr_feed_dict)[0]
    c = np.argmax(C_, axis=-1)
    K.clear_session()
    return c
示例#22
0
    def __init__(self, lr_1, lr_2, l2_loss_weight, batch_size, dimension,
                 theta0, util_train, util_test, campaign):
        self.lr_1 = lr_1
        self.lr_2 = lr_2
        self.util_train = util_train
        self.util_test = util_test
        self.train_data_amt = util_train.get_data_amt()
        self.test_data_amt = util_test.get_data_amt()
        self.batch_size = batch_size
        self.batch_num = int(self.train_data_amt / self.batch_size)
        self.l2_loss_weight = l2_loss_weight
        self.campaign = campaign

        # output directory
        model_name = "{0}_{1}_{2}_{3}".format(self.lr_1, self.lr_2,
                                              self.l2_loss_weight,
                                              self.batch_size)
        self.output_dir = 'output/gamma/{}/{}/'.format(campaign, model_name)
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)

        # reset graph
        tf.reset_default_graph()

        # placeholders
        self.X = tf.sparse_placeholder(tf.float64)
        self.z = tf.placeholder(tf.float64)
        self.b = tf.placeholder(tf.float64)
        self.y = tf.placeholder(tf.float64)

        # trainable variables
        self.theta = tf.Variable([theta0], name='theta', dtype=tf.float64)
        # tf.reshape(self.theta, [1, 1])

        all_train_data = self.util_train.get_all_data_origin()
        self.init_ks_value = all_train_data[3] * all_train_data[2] / theta0 + (
            1 - all_train_data[3]) * all_train_data[1] / theta0
        self.ks = tf.Variable(self.init_ks_value, name='ks', dtype=tf.float64)
        self.w = tf.Variable(initial_value=tf.truncated_normal(
            shape=[dimension, 1], dtype=tf.float64),
                             name='w')
        # computation graph phase1
        self.ps = tf.pow(self.z, (self.ks - 1.)) * tf.exp(-self.z / self.theta) \
             / tf.exp(tf.lgamma(self.ks)) / tf.pow(self.theta, self.ks)
        self.cs = tf.igamma(self.ks, self.b / self.theta) / tf.exp(
            tf.lgamma(self.ks))

        self.loss_win = tf.log(tf.clip_by_value(self.ps, 1e-8, 1.0))
        self.loss_lose = tf.log(tf.clip_by_value(1 - self.cs, 1e-8, 1.0))
        self.loss_phase1 = -tf.reduce_mean(self.y * self.loss_win +
                                           (1 - self.y) * self.loss_lose)
        self.optimizer1 = tf.train.GradientDescentOptimizer(self.lr_1)
        self.train_step1 = self.optimizer1.minimize(self.loss_phase1)

        # phase 2
        self.label_phase2 = tf.placeholder(tf.float64)
        self.log_label_phase2 = tf.log(
            tf.clip_by_value(self.label_phase2, 1e-8, 1.0))
        self.loss_phase2 = tf.reduce_mean(tf.square(tf.sparse_tensor_dense_matmul(self.X, self.w) - self.log_label_phase2)) \
                           + self.l2_loss_weight * tf.nn.l2_loss(self.w)
        self.optimizer2 = tf.train.MomentumOptimizer(self.lr_2, 0.9)
        self.train_step2 = self.optimizer2.minimize(self.loss_phase2)

        # session initialization
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        tf.global_variables_initializer().run(session=self.sess)
    DATA_FILE = "data/posts/posts.json"
    ENDPOINT = "http://localhost:9200/"
    BATCH_SIZE = 1000

    SEARCH_SIZE = 5
    GPU_LIMIT = 0.5

    print("Downloading pre-trained embeddings from tensorflow hub...")
    embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-lite/2")
    with tf.Session() as sess:
        spm_path = sess.run(module(signature="spm_path"))

    sp = spm.SentencePieceProcessor()
    sp.Load(spm_path)
    print("SentencePiece model loaded at {}.".format(spm_path))
    input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
    inputs=dict(
        values=input_placeholder.values,
        indices=input_placeholder.indices,
        dense_shape=input_placeholder.dense_shape)
    embeddings = embed(inputs)
    print("Done.")

    print("Creating tensorflow session...")
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = GPU_LIMIT
    session = tf.Session(config=config)
    session.run(tf.global_variables_initializer())
    session.run(tf.tables_initializer())
    print("Done.")
示例#24
0
import numpy as np
import pickle
tf.disable_v2_behavior()

bert_config = bm.BertConfig.from_json_file(BERT_CONFIG_PATH)  # 配置文件地址。

# Define placeholders
placeholders = {
    'input_ids': tf.placeholder(tf.int64,
                                shape=(None, bert_config.max_length)),
    'input_mask': tf.placeholder(tf.int64,
                                 shape=(None, bert_config.max_length)),
    'targets': tf.placeholder(tf.int64, shape=(None, bert_config.max_length)),
    'targets_pos': tf.placeholder(tf.int64,
                                  shape=(None, bert_config.max_length)),
    'adj_graph': [tf.sparse_placeholder(tf.float32) for _ in range(1)],
}


def construct_feed_dict(input_ids, input_mask, targets, targets_pos,
                        placeholders):
    """Construct feed dictionary."""
    feed_dict = dict()
    feed_dict.update({placeholders['input_ids']: input_ids})
    feed_dict.update({placeholders['input_mask']: input_mask})
    feed_dict.update({placeholders['targets']: targets})
    feed_dict.update({placeholders['targets_pos']: targets_pos})
    # feed_dict.update({placeholders['adj_graph'][i]: support[i] for i in range(len(support))})
    return feed_dict

示例#25
0
    def __init__(self, lr, batch_size, dimension, util_train, util_test, campaign, reg_lambda, sigma):
        # hyperparameters
        self.lr = lr
        self.batch_size = batch_size
        self.util_train = util_train
        self.util_test = util_test
        self.reg_lambda = reg_lambda
        self.sigma = sigma
        self.emb_size = 20

        self.train_data_amt = util_train.get_data_amt()
        self.test_data_amt = util_test.get_data_amt()

        # output dir
        model_name = "{}_{}_{}_{}".format(self.lr, self.reg_lambda, self.batch_size, self.sigma)
        self.output_dir = "output/deephit/{}/{}/".format(campaign, model_name)
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)
        
        # reset graph
        tf.reset_default_graph()

        # field params
        self.field_sizes = self.util_train.feat_sizes
        self.field_num = len(self.field_sizes)

        # placeholders
        self.X = [tf.sparse_placeholder(tf.float64) for i in range(0, self.field_num)]
        self.z = tf.placeholder(tf.float64)
        self.b = tf.placeholder(tf.float64)
        self.y = tf.placeholder(tf.float64)

        # embedding layer
        self.var_map = {}
        # for truncated
        self.var_map['embed_0'] = tf.Variable(
                tf.truncated_normal([self.field_sizes[0], 1], dtype=tf.float64))
        for i in range(1, self.field_num):
            self.var_map['embed_%d' % i] = tf.Variable(
                tf.truncated_normal([self.field_sizes[i], self.emb_size], dtype=tf.float64))
        
        # after embedding
        w0 = [self.var_map['embed_%d' % i] for i in range(self.field_num)]
        self.dense_input = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(self.field_num)], 1)

        # shared network
        self.hidden1 = tf.Variable(initial_value=tf.truncated_normal(shape=[(self.field_num - 1) * self.emb_size + 1, HIDDEN_SIZE1], dtype=tf.float64), name='h1')
        self.out1 = tf.Variable(initial_value=tf.truncated_normal(shape=[HIDDEN_SIZE1, OUT_SIZE1], dtype=tf.float64), name='o1')
        self.hidden2 = tf.Variable(initial_value=tf.truncated_normal(shape=[OUT_SIZE1, HIDDEN_SIZE2], dtype=tf.float64), name='h2')
        self.out2 = tf.Variable(initial_value=tf.truncated_normal(shape=[HIDDEN_SIZE2, OUT_SIZE2], dtype=tf.float64), name='o2')

        # cause-specific network
        self.hidden1_val = tf.nn.relu(tf.matmul(self.dense_input, self.hidden1))
        self.out1_val = tf.sigmoid(tf.matmul(self.hidden1_val, self.out1))
        self.hidden2_val = tf.nn.relu(tf.matmul(self.out1_val, self.hidden2))
        self.out2_val = tf.sigmoid(tf.matmul(self.hidden2_val, self.out2))

        # p_z and w_b
        self.p = tf.nn.softmax(self.out2_val)
        self.w = tf.cumsum(self.p, exclusive=True, axis = 1)

        idx_z = tf.stack([tf.reshape(tf.range(tf.shape(self.z)[0]), (-1,1)), tf.cast(self.z - 1, tf.int32)], axis=-1)
        idx_b = tf.stack([tf.reshape(tf.range(tf.shape(self.b)[0]), (-1,1)), tf.cast(self.b - 1, tf.int32)], axis=-1)

        self.pz = tf.gather_nd(self.p, idx_z)
        self.wb = tf.gather_nd(self.w, idx_b)
        self.wz = tf.gather_nd(self.w, idx_z)

        # loss and train step
        self.loss1 = -tf.reduce_sum(tf.log(tf.clip_by_value(self.pz, 1e-8, 1.0)) * self.y)
        self.loss2 = -tf.reduce_sum(tf.log(tf.clip_by_value(1 - self.wb, 1e-8, 1.0)) * (1 - self.y))
        self.reg_loss = tf.nn.l2_loss(self.hidden1[1:,]) + tf.nn.l2_loss(self.hidden2[1:,]) + \
                        tf.nn.l2_loss(self.out1[1:,]) + tf.nn.l2_loss(self.out2[1:,])

        # get ranking loss
        self.w_of_pair = tf.transpose(tf.nn.embedding_lookup(tf.transpose(self.w), tf.cast(self.z[:,0] - 1, tf.int32)))
        self.w_of_self = tf.reshape(tf.tile(tf.reshape(self.wz, (self.batch_size, )), [self.batch_size]), (self.batch_size, self.batch_size))
        self.win_label = tf.reshape(tf.tile(tf.reshape(self.y, (self.batch_size, )), [self.batch_size]), (self.batch_size, self.batch_size))
        self.delta = self.w_of_self - self.w_of_pair
        self.candidate = tf.exp(-self.delta / self.sigma)
        self.rank_loss = tf.reduce_sum(tf.matrix_band_part(self.candidate, -1, 0) * self.win_label)

        self.loss = self.loss1 + self.loss2 + self.reg_lambda * self.reg_loss + self.rank_loss

        self.optimizer = tf.train.GradientDescentOptimizer(self.lr)
        self.train_step = self.optimizer.minimize(self.loss)

        # session initialization
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        tf.global_variables_initializer().run(session=self.sess)
示例#26
0
def get_full_graph(sess, seed, edges, vertices, adj, labels, source, sink, reward_states, other_sources=[],other_sinks=[]):


    
    features = np.eye(len(vertices), dtype=np.int32) # One-hot encoding for the features (i.e. feature-less)
    features = sparse_to_tuple(sp.lil_matrix(features))

    np.random.seed(seed)
    tf.set_random_seed(seed)


    y_train, y_val, train_mask, val_mask = get_splits(labels, source, sink, reward_states)


    support = [preprocess_adj(adj)]
    num_supports = 1
    model_func = GCN

    adj =adj.toarray()
    deg = np.diag(np.sum(adj,axis=1))
    laplacian = deg - adj



    # Define placeholders
    placeholders = {
        'adj': tf.placeholder(tf.float32, shape=(None, None)) , #unnormalized adjancy matrix
        'support': [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
        'features': tf.sparse_placeholder(tf.float32, shape=tf.constant(features[2], dtype=tf.int64)),
        'labels': tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
        'labels_mask': tf.placeholder(tf.int32),
        'dropout': tf.placeholder_with_default(0., shape=()),
        'num_features_nonzero': tf.placeholder(tf.int32),  # helper variable for sparse dropout
        'learning_rate': tf.placeholder(tf.float32)
    }





    model = model_func(placeholders,edges,laplacian, input_dim=features[2][1], logging=True, FLAGS=FLAGS)
    
    sess.run(tf.global_variables_initializer())


    feed_dict = construct_feed_dict(adj, features, support, y_train, train_mask, placeholders)
    feed_dict.update({placeholders['learning_rate']: FLAGS.learning_rate})

    cost_val = []

    start = time.time()
    for epoch in range(FLAGS.epochs):




        t = time.time()
        feed_dict = construct_feed_dict(adj, features, support, y_train, train_mask, placeholders)
        feed_dict.update({placeholders['learning_rate']: FLAGS.learning_rate})
        outs = sess.run([model.opt_op, model.loss, model.accuracy,model.learning_rate], feed_dict=feed_dict)



    print("Total time for gcn {}".format(time.time()-start))
    print("Optimization Finished!")


    outputs = sess.run([tf.nn.softmax(model.outputs)], feed_dict=feed_dict)[0]


    return outputs[:,1]
def ctc_crnn(params):
    # TODO Assert parameters

    input = tf.placeholder(
        shape=(None, params['img_height'], params['img_width'],
               params['img_channels']),  # [batch, height, width, channels]
        dtype=tf.float32,
        name='model_input')

    input_shape = tf.shape(input)

    width_reduction = 1
    height_reduction = 1

    # Convolutional blocks
    x = input
    for i in range(params['conv_blocks']):
        x = tf.layers.conv2d(inputs=x,
                             filters=params['conv_filter_n'][i],
                             kernel_size=params['conv_filter_size'][i],
                             padding="same",
                             activation=None)

        x = tf.layers.batch_normalization(x)
        x = leaky_relu(x)

        x = tf.layers.max_pooling2d(inputs=x,
                                    pool_size=params['conv_pooling_size'][i],
                                    strides=params['conv_pooling_size'][i])

        width_reduction = width_reduction * params['conv_pooling_size'][i][1]
        height_reduction = height_reduction * params['conv_pooling_size'][i][0]

    # Prepare output of conv block for recurrent blocks
    features = tf.transpose(
        x, perm=[2, 0, 3,
                 1])  # -> [width, batch, height, channels] (time_major=True)
    feature_dim = params['conv_filter_n'][-1] * (params['img_height'] /
                                                 height_reduction)
    feature_width = input_shape[2] / width_reduction
    features = tf.reshape(features,
                          tf.stack([
                              tf.cast(feature_width, 'int32'), input_shape[0],
                              tf.cast(feature_dim, 'int32')
                          ]))  # -> [width, batch, features]

    tf.constant(params['img_height'], name='input_height')
    tf.constant(width_reduction, name='width_reduction')

    # Recurrent block
    rnn_keep_prob = tf.placeholder(dtype=tf.float32, name="keep_prob")
    rnn_hidden_units = params['rnn_units']
    rnn_hidden_layers = params['rnn_layers']

    rnn_outputs, _ = tf.nn.bidirectional_dynamic_rnn(
        tf.contrib.rnn.MultiRNNCell([
            tf.nn.rnn_cell.DropoutWrapper(
                tf.contrib.rnn.BasicLSTMCell(rnn_hidden_units),
                input_keep_prob=rnn_keep_prob)
            for _ in range(rnn_hidden_layers)
        ]),
        tf.contrib.rnn.MultiRNNCell([
            tf.nn.rnn_cell.DropoutWrapper(
                tf.contrib.rnn.BasicLSTMCell(rnn_hidden_units),
                input_keep_prob=rnn_keep_prob)
            for _ in range(rnn_hidden_layers)
        ]),
        features,
        dtype=tf.float32,
        time_major=True,
    )

    rnn_outputs = tf.concat(rnn_outputs, 2)

    logits = tf.contrib.layers.fully_connected(
        rnn_outputs,
        params['vocabulary_size'] + 1,  # BLANK
        activation_fn=None,
    )

    tf.add_to_collection("logits", logits)  # for restoring purposes

    # CTC Loss computation
    seq_len = tf.placeholder(tf.int32, [None], name='seq_lengths')
    targets = tf.sparse_placeholder(dtype=tf.int32, name='target')
    ctc_loss = tf.nn.ctc_loss(labels=targets,
                              inputs=logits,
                              sequence_length=seq_len,
                              time_major=True)
    loss = tf.reduce_mean(ctc_loss)

    # CTC decoding
    decoded, log_prob = tf.nn.ctc_greedy_decoder(logits, seq_len)
    # decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits,seq_len,beam_width=50,top_paths=1,merge_repeated=True)

    return input, seq_len, targets, decoded, loss, rnn_keep_prob
示例#28
0
        read_data = read_data_tsp
    elif args.problem == 'vrp':
        read_data = read_data_vrp
    elif args.problem in ['mis', 'ds', 'vc', 'ca']:
        read_data = read_data_general
    elif args.problem == 'sc':
        read_data = read_data_sc
    else:
        raise Exception('unknown problem!')

    ####### model #######
    feat_dim = 57
    FLAGS = set_train_params()
    placeholders = {
        'support':
        [tf.sparse_placeholder(tf.float32)
         for _ in range(FLAGS.num_supports)] if FLAGS.matrix_type == 'sparse'
        else [tf.placeholder(tf.float32) for _ in range(FLAGS.num_supports)],
        'features':
        tf.sparse_placeholder(tf.float32, shape=(None, feat_dim))
        if FLAGS.matrix_type == 'sparse' else tf.placeholder(
            tf.float32, shape=(None, feat_dim)),  # featureless: #points
        'labels':
        tf.placeholder(tf.float32, shape=(None, 2)),  # 0: not linked, 1:linked
        'labels_mask':
        tf.placeholder(tf.int32),
        'dropout':
        tf.placeholder_with_default(0., shape=()),
        'num_features_nonzero':
        tf.placeholder(tf.int32)  # helper variable for sparse dropout
    }
示例#29
0
def main(unused_argv):
    """Main function for running experiments."""
    # Load data
    (train_adj, full_adj, train_feats, test_feats, y_train, y_val, y_test,
     train_mask, val_mask, test_mask, _, val_data, test_data, num_data,
     visible_data) = load_data(FLAGS.data_prefix, FLAGS.dataset, FLAGS.precalc)

    # Partition graph and do preprocessing
    if FLAGS.bsize > 1:
        _, parts = partition_utils.partition_graph(train_adj, visible_data,
                                                   FLAGS.num_clusters)
        parts = [np.array(pt) for pt in parts]
    else:
        (parts, features_batches, support_batches, y_train_batches,
         train_mask_batches) = utils.preprocess(train_adj, train_feats,
                                                y_train, train_mask,
                                                visible_data,
                                                FLAGS.num_clusters,
                                                FLAGS.diag_lambda)

    (_, val_features_batches, val_support_batches, y_val_batches,
     val_mask_batches) = utils.preprocess(full_adj, test_feats, y_val,
                                          val_mask, np.arange(num_data),
                                          FLAGS.num_clusters_val,
                                          FLAGS.diag_lambda)

    (_, test_features_batches, test_support_batches, y_test_batches,
     test_mask_batches) = utils.preprocess(full_adj, test_feats, y_test,
                                           test_mask, np.arange(num_data),
                                           FLAGS.num_clusters_test,
                                           FLAGS.diag_lambda)
    idx_parts = list(range(len(parts)))

    # Some preprocessing
    model_func = models.GCN

    # Define placeholders
    placeholders = {
        'support': tf.sparse_placeholder(tf.float32),
        'features': tf.placeholder(tf.float32),
        'labels': tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
        'labels_mask': tf.placeholder(tf.int32),
        'dropout': tf.placeholder_with_default(0., shape=()),
        'num_features_nonzero':
        tf.placeholder(tf.int32)  # helper variable for sparse dropout
    }

    # Create model
    model = model_func(placeholders,
                       input_dim=test_feats.shape[1],
                       logging=True,
                       multilabel=FLAGS.multilabel,
                       norm=FLAGS.layernorm,
                       precalc=FLAGS.precalc,
                       num_layers=FLAGS.num_layers)

    # Initialize session
    sess = tf.Session()
    tf.set_random_seed(seed)

    # Init variables
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    cost_val = []
    total_training_time = 0.0
    # Train model
    for epoch in range(FLAGS.epochs):
        t = time.time()
        np.random.shuffle(idx_parts)
        if FLAGS.bsize > 1:
            (features_batches, support_batches, y_train_batches,
             train_mask_batches) = utils.preprocess_multicluster(
                 train_adj, parts, train_feats, y_train, train_mask,
                 FLAGS.num_clusters, FLAGS.bsize, FLAGS.diag_lambda)
            for pid in range(len(features_batches)):
                # Use preprocessed batch data
                features_b = features_batches[pid]
                support_b = support_batches[pid]
                y_train_b = y_train_batches[pid]
                train_mask_b = train_mask_batches[pid]
                # Construct feed dictionary
                feed_dict = utils.construct_feed_dict(features_b, support_b,
                                                      y_train_b, train_mask_b,
                                                      placeholders)
                feed_dict.update({placeholders['dropout']: FLAGS.dropout})
                # Training step
                outs = sess.run([model.opt_op, model.loss, model.accuracy],
                                feed_dict=feed_dict)
        else:
            np.random.shuffle(idx_parts)
            for pid in idx_parts:
                # Use preprocessed batch data
                features_b = features_batches[pid]
                support_b = support_batches[pid]
                y_train_b = y_train_batches[pid]
                train_mask_b = train_mask_batches[pid]
                # Construct feed dictionary
                feed_dict = utils.construct_feed_dict(features_b, support_b,
                                                      y_train_b, train_mask_b,
                                                      placeholders)
                feed_dict.update({placeholders['dropout']: FLAGS.dropout})
                # Training step
                outs = sess.run([model.opt_op, model.loss, model.accuracy],
                                feed_dict=feed_dict)

        total_training_time += time.time() - t
        print_str = 'Epoch: %04d ' % (
            epoch + 1) + 'training time: {:.5f} '.format(
                total_training_time) + 'train_acc= {:.5f} '.format(outs[2])

        # Validation
        if FLAGS.validation:
            cost, acc, micro, macro = evaluate(sess, model,
                                               val_features_batches,
                                               val_support_batches,
                                               y_val_batches, val_mask_batches,
                                               val_data, placeholders)
            cost_val.append(cost)
            print_str += 'val_acc= {:.5f} '.format(
                acc) + 'mi F1= {:.5f} ma F1= {:.5f} '.format(micro, macro)

        tf.logging.info(print_str)

        if epoch > FLAGS.early_stopping and cost_val[-1] > np.mean(
                cost_val[-(FLAGS.early_stopping + 1):-1]):
            tf.logging.info('Early stopping...')
            break

    tf.logging.info('Optimization Finished!')

    # Save model
    saver.save(sess, FLAGS.save_name)

    # Load model (using CPU for inference)
    with tf.device('/cpu:0'):
        sess_cpu = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))
        sess_cpu.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        saver.restore(sess_cpu, FLAGS.save_name)
        # Testing
        test_cost, test_acc, micro, macro = evaluate(
            sess_cpu, model, test_features_batches, test_support_batches,
            y_test_batches, test_mask_batches, test_data, placeholders)
        print_str = 'Test set results: ' + 'cost= {:.5f} '.format(
            test_cost) + 'accuracy= {:.5f} '.format(
                test_acc) + 'mi F1= {:.5f} ma F1= {:.5f}'.format(micro, macro)
        tf.logging.info(print_str)
示例#30
0
文件: train.py 项目: Rory602/gcn
    model_func = GCN
elif FLAGS.model == 'gcn_cheby':
    support = chebyshev_polynomials(adj, FLAGS.max_degree)
    num_supports = 1 + FLAGS.max_degree
    model_func = GCN
elif FLAGS.model == 'dense':
    support = [preprocess_adj(adj)]  # Not used
    num_supports = 1
    model_func = MLP
else:
    raise ValueError('Invalid argument for model: ' + str(FLAGS.model))

# Define placeholders
placeholders = {
    'support':
    [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
    'features':
    tf.sparse_placeholder(tf.float32,
                          shape=tf.constant(features[2], dtype=tf.int64)),
    'labels':
    tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
    'labels_mask':
    tf.placeholder(tf.int32),
    'dropout':
    tf.placeholder_with_default(0., shape=()),
    'num_features_nonzero':
    tf.placeholder(tf.int32)  # helper variable for sparse dropout
}

# Create model
model = model_func(placeholders, input_dim=features[2][1], logging=True)