def train_Amazon(): num_walks = 5 walk_length = 5 window_size = 5 neighbor_samples = 5 n_hidden = 200 n_layers = 3 activation = F.relu dropout = 0.5 weight_decay = 0.001 lr = 0.01 epochs = 1 g, features, train_pairs, neg_neighbors, word2index, index2word = load_amazon(num_walks, walk_length, window_size, neighbor_samples) neg_features = [] for index, neighbors in enumerate(neg_neighbors): neg_features.append([features[neighbor] for neighbor in neighbors]) features = torch.FloatTensor(features) neg_features = torch.FloatTensor(neg_features) print(features.shape) print(neg_features.shape) device = torch.device("cpu") model = UN_GCN(g=g, in_feats=features.shape[1], n_hidden=n_hidden, n_layers=n_layers, activation=activation, dropout=dropout ) optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay) model = model.to(device) for epoch in range(epochs): model.train() h = model(features) loss = nce_loss(h, train_pairs, neg_neighbors) print(loss) optimizer.zero_grad() loss.backward() optimizer.step() h = model(features) embeddings = get_embeddings(h, list(word2index)) evaluate_amazon(embeddings)
def train_amazon(): num_walks = 5 walk_length = 5 window_size = 5 neighbor_samples = 5 hidden_uints = 32 num_heads = [8] dropout = 0.5 out_size = 200 lr = 0.1 weight_decay = 0.001 epochs = 5 g, features, train_pairs, neg_neighbors, vocab, edge_data_by_type = load_amazon( num_walks, neighbor_samples, window_size) features = torch.FloatTensor(features) print("特征的shape是:", features.shape) metapaths = [] for edge_type in edge_data_by_type.keys(): metapaths.append([edge_type] * walk_length) model = HAN(meta_paths=metapaths, in_size=features.shape[1], hidden_size=hidden_uints, out_size=out_size, num_heads=num_heads, dropout=dropout) optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay) for epoch in range(epochs): model.train() _, h = model(g, features) loss = nce_loss(h, train_pairs, neg_neighbors) print(loss) optimizer.zero_grad() loss.backward() optimizer.step() _, h = model(g, features) embeddings = get_embeddings(h, list(vocab)) evaluate_amazon(embeddings)
def train_amazon(): num_walks = 5 walk_length = 5 window_size = 5 neighbor_samples = 5 weight_decay = 0.001 lr = 0.01 epochs = 1 hidden_dim = 16 out_dim = 200 num_heads = 8 g, features, train_pairs, neg_neighbors, word2index, index2word = load_amazon( num_walks, walk_length, window_size, neighbor_samples) features = torch.FloatTensor(features) model = UN_GAT(g, in_dim=features.shape[1], hidden_dim=hidden_dim, out_dim=out_dim, num_heads=num_heads) optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay) device = torch.device("cpu") model = model.to(device) for epoch in range(epochs): model.train() h = model(features) loss = nce_loss(h, train_pairs, neg_neighbors) print(loss) optimizer.zero_grad() loss.backward() optimizer.step() h = model(features) embeddings = get_embeddings(h, list(word2index)) evaluate_amazon(embeddings)
from metapath2vec_model import Metapath2vec from metapath2vec_utils import load_dataset from Linear_evaluation import evaluate_DBLP from Amazon_evaluation import evaluate_amazon if __name__ == "__main__": dataset = 'Amazon' if dataset == 'DBLP': metapaths = [['ap', 'pa'] * 3, ['ap', 'pc', 'cp', 'pa'] * 2] num_walk = 10 graph, sentences, node_list = load_dataset(dataset, num_walk, metapaths) model = Metapath2vec(sentences, graph) model.train() embeddings = model.get_embeddings(node_list) evaluate_DBLP(embeddings) if dataset == 'Amazon': num_walk = 10 graph, sentences, index2word = load_dataset(dataset, num_walk) model = Metapath2vec(sentences, graph) model.train() embeddings = model.get_embeddings(index2word) evaluate_amazon(embeddings)