Beispiel #1
0
    def __init__(self, Graph):
        self.reward_range = (0, 1)

        # 环境获取的状态的维度128+404(邻接点和边被压缩成128表示,404维度是历史信息维度)
        self.state_dim = NODE_DIM + EDGE_DIM

        # 读取self.Graph = nx.read_gpickle("./data/DBLP_labeled.Graph")
        # 或者直接获取返回值buildNetworkGraph.get_graph()
        self.Graph = Graph
        self.Graph = get_graph()

        # 初始化属性,current_node,
        self.current_node = None
        self.path = []
        self.target_node = None
        self.query = None

        self.qt = None
        self.st = None
        self.done = False
        # 所有的树
        self.TreeList = []
Beispiel #2
0
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Normal, Categorical
from torch.utils.data.sampler import BatchSampler, SubsetRandomSampler
from tensorboardX import SummaryWriter

from BiYeSheJi.Module.environment import Environment
from BiYeSheJi.Script.buildNetworkGraph import get_relation_num, get_graph
from BiYeSheJi.Module.state_encoder import StateEncoder
from BiYeSheJi.Script.get_embedding import get_node_emb_matrix, get_relation_emb_dic
from BiYeSheJi.Configuration import config

# Hyper-parameters
seed = 1
render = False
Graph = get_graph()
num_episodes = 400000
env = Environment(Graph)
num_state = env.state_dim
num_action = 128
torch.manual_seed(seed)
random.seed(seed)

Transition = namedtuple('Transition',
                        ['state', 'action', 'reward', 'next_state'])

relation_dim = get_relation_num()
INPUT_SIZE_liner = relation_dim + 128
INPUT_SIZE_GRU = 3 * 128

OUT_SIZE_liner = 128
Beispiel #3
0
import sys
import math
import random
import torch

import numpy as np
from torch import nn

from BiYeSheJi.Script.buildNetworkGraph import get_graph
from BiYeSheJi.Script.get_embedding import get_node_emb_matrix, get_relation_emb_dic
from BiYeSheJi.Configuration import config

GRAPH = get_graph()
AVAILABLE_CHOICES = [1, -1, 2, -2]
AVAILABLE_CHOICE_NUMBER = len(AVAILABLE_CHOICES)
MAX_ROUND_NUMBER = 10
node_emb_matrix = get_node_emb_matrix()
edge_emb_dic = get_relation_emb_dic()
Graph = get_graph()


def _Max(matrix):
    """
    取矩阵每一列最大值得到一行向量
    :param matrix:
    :return:
    """

    return matrix[torch.argmax(matrix, dim=0),
                  torch.LongTensor(range(matrix.shape[1]))].view(
                      1, matrix.shape[1])
Beispiel #4
0
import json
import sys
import math
import random
import numpy as np
from BiYeSheJi.Script.buildNetworkGraph import get_graph

GRAPH = get_graph()
AVAILABLE_CHOICES = [1, -1, 2, -2]
AVAILABLE_CHOICE_NUMBER = len(AVAILABLE_CHOICES)
MAX_ROUND_NUMBER = 10


def select_best_child(node, is_exploration):  # 若子节点都扩展完了,求UCB值最大的子节点
    best_score = -sys.maxsize
    best_sub_node = None
    for sub_node in node.get_children():
        if is_exploration:
            # C = 1 / math.sqrt(2.0)  # C越大越偏向于广度搜索,越小越偏向于深度搜索
            C = 10
        else:
            C = 0.0
        # UCT:
        # left = sub_node.get_quality_value() / sub_node.get_visit_times()
        # right = math.log(node.get_visit_times()) / sub_node.get_visit_times()
        # score = left + C * math.sqrt(right)

        # UCT--
        left = sub_node.get_quality_value() / sub_node.get_visit_times()
        right = math.sqrt(node.get_visit_times()) / sub_node.get_visit_times()
        score = left + C * right
Beispiel #5
0
from BiYeSheJi.Script import buildNetworkGraph
from BiYeSheJi.Script.node2vec import Node2Vec
from BiYeSheJi.Configuration import config
import numpy as np
import json

entity_num = buildNetworkGraph.get_entity_num()
relation_num = buildNetworkGraph.get_relation_num()
entity_dic = buildNetworkGraph.get_entity_dic()
relation_dic = buildNetworkGraph.get_relation_dic()

GRAPH = buildNetworkGraph.get_graph()


def node_embedding():
    if "node2vec" == config.node_encode:
        node2vec = Node2Vec(GRAPH,
                            dimensions=config.node_encode_dim,
                            walk_length=20,
                            num_walks=20,
                            workers=4)
        model = node2vec.fit(window=4, min_count=1, batch_words=4, workers=4)
        # Look for most similar nodes
        model.wv.most_similar('2')  # Output node names are always strings
        # Save embeddings for later use
        model.wv.save_word2vec_format(config.node_encode_filename)
        # Save model for later use
        model.save(config.node_model_filename)


def edge_embedding():
Beispiel #6
0
import random

from BiYeSheJi.Module.environment import Environment
from BiYeSheJi.Script.buildNetworkGraph import get_graph
import torch
import json

random.seed(1)
start_node_id, target_node_id, query_id = 2, 3, 4
env = Environment(get_graph())
env.reset(start_node_id, target_node_id, query_id)
print(env.st)
for i in range(5):
    action_node_id = random.choice(list(env.Graph[env.current_node].keys()))
    env.step(action_node_id)
    print(env.current_node)
    print(env.path)
    print(env.st)