easygraph.functions.graph_embedding package
Submodules
easygraph.functions.graph_embedding.NOBE module
- easygraph.functions.graph_embedding.NOBE.NOBE(G, K)[source]
Graph embedding via NOBE[1].
- Parameters
G (easygraph.Graph) – An unweighted and undirected graph.
K (int) – Embedding dimension k
- Returns
Y – list of embedding vectors (y1, y2, · · · , yn)
- Return type
list
Examples
>>> NOBE(G,K=15)
References
- easygraph.functions.graph_embedding.NOBE.NOBE_GA(G, K)[source]
Graph embedding via NOBE-GA[1].
- Parameters
G (easygraph.Graph) – An unweighted and undirected graph.
K (int) – Embedding dimension k
- Returns
Y – list of embedding vectors (y1, y2, · · · , yn)
- Return type
list
Examples
>>> NOBE_GA(G,K=15)
References
easygraph.functions.graph_embedding.deepwalk module
- easygraph.functions.graph_embedding.deepwalk.deepwalk(G, dimensions=128, walk_length=80, num_walks=10, **skip_gram_params)[source]
Graph embedding via DeepWalk.
- Parameters
G (easygraph.Graph or easygraph.DiGraph) –
dimensions (int) – Embedding dimensions, optional(default: 128)
walk_length (int) – Number of nodes in each walk, optional(default: 80)
num_walks (int) – Number of walks per node, optional(default: 10)
skip_gram_params (dict) – Parameters for gensim.models.Word2Vec - do not supply size, it is taken from the dimensions parameter
- Returns
embedding_vector (dict) – The embedding vector of each node
most_similar_nodes_of_node (dict) – The most similar nodes of each node and its similarity
Examples
>>> deepwalk(G, ... dimensions=128, # The graph embedding dimensions. ... walk_length=80, # Walk length of each random walks. ... num_walks=10, # Number of random walks. ... skip_gram_params = dict( # The skip_gram parameters in Python package gensim. ... window=10, ... min_count=1, ... batch_words=4, ... iter=15 ... ))
References
easygraph.functions.graph_embedding.line module
- class easygraph.functions.graph_embedding.line.LINE(dimension=128, walk_length=80, walk_num=20, negative=5, batch_size=128, init_alpha=0.025, order=3)[source]
Bases:
Module
Graph embedding via LINE. :param G: :type G: easygraph.Graph or easygraph.DiGraph :param dimension: :type dimension: int :param walk_length: :type walk_length: int :param walk_num: :type walk_num: int :param negative: :type negative: int :param batch_size: :type batch_size: int :param init_alpha: :type init_alpha: float :param order: :type order: int
- Returns
embedding_vector – The embedding vector of each node
- Return type
dict
Examples
>>> model = LINE( ... dimension=128, ... walk_length=80, ... walk_num=20, ... negative=5, ... batch_size=128, ... init_alpha=0.025, ... order=3 ) >>> model.train() >>> emb = model(g, return_dict=True) # g: easygraph.Graph or easygraph.DiGraph
References
- 1
Tang, J., Qu, M., Wang, M., Zhang, M., Yan, J., & Mei, Q. (2015, May). Line: Large-scale information network embedding. In Proceedings of the 24th international conference on world wide web (pp. 1067-1077).
https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/frp0228-Tang.pdf
Methods
add_args
(parser)Add model-specific arguments to the parser.
add_module
(name, module)Adds a child module to the current module.
apply
(fn)Applies
fn
recursively to every submodule (as returned by.children()
) as well as self.bfloat16
()Casts all floating point parameters and buffers to
bfloat16
datatype.buffers
([recurse])Returns an iterator over module buffers.
children
()Returns an iterator over immediate children modules.
cpu
()Moves all model parameters and buffers to the CPU.
cuda
([device])Moves all model parameters and buffers to the GPU.
double
()Casts all floating point parameters and buffers to
double
datatype.eval
()Sets the module in evaluation mode.
extra_repr
()Set the extra representation of the module
float
()Casts all floating point parameters and buffers to
float
datatype.forward
(g[, return_dict])Defines the computation performed at every call.
get_buffer
(target)Returns the buffer given by
target
if it exists, otherwise throws an error.get_extra_state
()Returns any extra state to include in the module's state_dict.
get_parameter
(target)Returns the parameter given by
target
if it exists, otherwise throws an error.get_submodule
(target)Returns the submodule given by
target
if it exists, otherwise throws an error.half
()Casts all floating point parameters and buffers to
half
datatype.ipu
([device])Moves all model parameters and buffers to the IPU.
load_state_dict
(state_dict[, strict])Copies parameters and buffers from
state_dict
into this module and its descendants.modules
()Returns an iterator over all modules in the network.
named_buffers
([prefix, recurse, ...])Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children
()Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules
([memo, prefix, remove_duplicate])Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters
([prefix, recurse, ...])Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters
([recurse])Returns an iterator over module parameters.
register_backward_hook
(hook)Registers a backward hook on the module.
register_buffer
(name, tensor[, persistent])Adds a buffer to the module.
register_forward_hook
(hook, *[, prepend, ...])Registers a forward hook on the module.
register_forward_pre_hook
(hook, *[, ...])Registers a forward pre-hook on the module.
register_full_backward_hook
(hook[, prepend])Registers a backward hook on the module.
register_full_backward_pre_hook
(hook[, prepend])Registers a backward pre-hook on the module.
register_load_state_dict_post_hook
(hook)Registers a post hook to be run after module's
load_state_dict
is called.register_module
(name, module)Alias for
add_module()
.register_parameter
(name, param)Adds a parameter to the module.
register_state_dict_pre_hook
(hook)These hooks will be called with arguments:
self
,prefix
, andkeep_vars
before callingstate_dict
onself
.requires_grad_
([requires_grad])Change if autograd should record operations on parameters in this module.
set_extra_state
(state)This function is called from
load_state_dict()
to handle any extra state found within the state_dict.share_memory
()See
torch.Tensor.share_memory_()
state_dict
(*args[, destination, prefix, ...])Returns a dictionary containing references to the whole state of the module.
to
(*args, **kwargs)Moves and/or casts the parameters and buffers.
to_empty
(*, device)Moves the parameters and buffers to the specified device without copying storage.
train
([mode])Sets the module in training mode.
type
(dst_type)Casts all parameters and buffers to
dst_type
.xpu
([device])Moves all model parameters and buffers to the XPU.
zero_grad
([set_to_none])Sets gradients of all model parameters to zero.
__call__
build_model_from_args
- forward(g, return_dict=True)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
easygraph.functions.graph_embedding.net_emb_example_citeseer module
easygraph.functions.graph_embedding.node2vec module
- easygraph.functions.graph_embedding.node2vec.node2vec(G, dimensions=128, walk_length=80, num_walks=10, p=1.0, q=1.0, weight_key=None, workers=None, **skip_gram_params)[source]
Graph embedding via Node2Vec.
- Parameters
G (easygraph.Graph or easygraph.DiGraph) –
dimensions (int) – Embedding dimensions, optional(default: 128)
walk_length (int) – Number of nodes in each walk, optional(default: 80)
num_walks (int) – Number of walks per node, optional(default: 10)
p (float) – The return hyper parameter, optional(default: 1.0)
q (float) – The input parameter, optional(default: 1.0)
weight_key (string or None (default: None)) – On weighted graphs, this is the key for the weight attribute
workers (int or None, optional(default : None)) – The number of workers generating random walks (default: None). None if not using only one worker.
skip_gram_params (dict) – Parameters for gensim.models.Word2Vec - do not supply ‘size’, it is taken from the ‘dimensions’ parameter
- Returns
embedding_vector (dict) – The embedding vector of each node
most_similar_nodes_of_node (dict) – The most similar nodes of each node and its similarity
Examples
>>> node2vec(G, ... dimensions=128, # The graph embedding dimensions. ... walk_length=80, # Walk length of each random walks. ... num_walks=10, # Number of random walks. ... p=1.0, # The `p` possibility in random walk in [Rcb3fa857cc7d-1]_ ... q=1.0, # The `q` possibility in random walk in [Rcb3fa857cc7d-1]_ ... weight_key='weight', ... skip_gram_params=dict( # The skip_gram parameters in Python package gensim. ... window=10, ... min_count=1, ... batch_words=4 ... ))
References
easygraph.functions.graph_embedding.sdne module
- class easygraph.functions.graph_embedding.sdne.SDNE(graph, node_size, nhid0, nhid1, dropout=0.06, alpha=0.02, beta=10.0)[source]
Bases:
Module
Graph embedding via SDNE.
- Parameters
- grapheasygraph.Graph or easygraph.DiGraph
node: Size of nodes
nhid0, nhid1: Two dimensions of two hiddenlayers, default: 128, 64
dropout: One parameter for regularization, default: 0.025
alpha, beta: Twe parameters graph=g: : easygraph.Graph or easygraph.DiGraph
- .. rubric:: Examples
- >>> import easygraph as eg
- >>> model = eg.SDNE(graph=g, node_size= len(g.nodes), nhid0=128, nhid1=64, dropout=0.025, alpha=2e-2, beta=10)
- >>> emb = model.train(model, epochs, lr, bs, step_size, gamma, nu1, nu2, device, output)
- epochs, “–epochs”, default=400, type=int, help=”The training epochs of SDNE”
- alpha, “–alpha”, default=2e-2, type=float, help=”alhpa is a hyperparameter in SDNE”
- beta, “–beta”, default=10.0, type=float, help=”beta is a hyperparameter in SDNE”
- lr, “–lr”, default=0.006, type=float, help=”learning rate”
- bs, “–bs”, default=100, type=int, help=”batch size of SDNE”
- step_size, “–step_size”, default=10, type=int, help=”The step size for lr”
- gamma, # “–gamma”, default=0.9, type=int, help=”The gamma for lr”
- step_size, “–step_size”, default=10, type=int, help=”The step size for lr”
- nu1, # “–nu1”, default=1e-5, type=float, help=”nu1 is a hyperparameter in SDNE”
- nu2, “–nu2”, default=1e-4, type=float, help=”nu2 is a hyperparameter in SDNE”
- device, “– device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”) “
- output “–output”, default=”node.emb”, help=”Output representation file”
Methods
add_module
(name, module)Adds a child module to the current module.
apply
(fn)Applies
fn
recursively to every submodule (as returned by.children()
) as well as self.bfloat16
()Casts all floating point parameters and buffers to
bfloat16
datatype.buffers
([recurse])Returns an iterator over module buffers.
children
()Returns an iterator over immediate children modules.
cpu
()Moves all model parameters and buffers to the CPU.
cuda
([device])Moves all model parameters and buffers to the GPU.
double
()Casts all floating point parameters and buffers to
double
datatype.eval
()Sets the module in evaluation mode.
extra_repr
()Set the extra representation of the module
float
()Casts all floating point parameters and buffers to
float
datatype.forward
(adj_batch, adj_mat, b_mat)Defines the computation performed at every call.
get_buffer
(target)Returns the buffer given by
target
if it exists, otherwise throws an error.get_extra_state
()Returns any extra state to include in the module's state_dict.
get_parameter
(target)Returns the parameter given by
target
if it exists, otherwise throws an error.get_submodule
(target)Returns the submodule given by
target
if it exists, otherwise throws an error.half
()Casts all floating point parameters and buffers to
half
datatype.ipu
([device])Moves all model parameters and buffers to the IPU.
load_state_dict
(state_dict[, strict])Copies parameters and buffers from
state_dict
into this module and its descendants.modules
()Returns an iterator over all modules in the network.
named_buffers
([prefix, recurse, ...])Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children
()Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules
([memo, prefix, remove_duplicate])Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters
([prefix, recurse, ...])Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters
([recurse])Returns an iterator over module parameters.
register_backward_hook
(hook)Registers a backward hook on the module.
register_buffer
(name, tensor[, persistent])Adds a buffer to the module.
register_forward_hook
(hook, *[, prepend, ...])Registers a forward hook on the module.
register_forward_pre_hook
(hook, *[, ...])Registers a forward pre-hook on the module.
register_full_backward_hook
(hook[, prepend])Registers a backward hook on the module.
register_full_backward_pre_hook
(hook[, prepend])Registers a backward pre-hook on the module.
register_load_state_dict_post_hook
(hook)Registers a post hook to be run after module's
load_state_dict
is called.register_module
(name, module)Alias for
add_module()
.register_parameter
(name, param)Adds a parameter to the module.
register_state_dict_pre_hook
(hook)These hooks will be called with arguments:
self
,prefix
, andkeep_vars
before callingstate_dict
onself
.requires_grad_
([requires_grad])Change if autograd should record operations on parameters in this module.
set_extra_state
(state)This function is called from
load_state_dict()
to handle any extra state found within the state_dict.share_memory
()See
torch.Tensor.share_memory_()
state_dict
(*args[, destination, prefix, ...])Returns a dictionary containing references to the whole state of the module.
to
(*args, **kwargs)Moves and/or casts the parameters and buffers.
to_empty
(*, device)Moves the parameters and buffers to the specified device without copying storage.
train
(model[, epochs, lr, bs, step_size, ...])Sets the module in training mode.
type
(dst_type)Casts all parameters and buffers to
dst_type
.xpu
([device])Moves all model parameters and buffers to the XPU.
zero_grad
([set_to_none])Sets gradients of all model parameters to zero.
__call__
savector
- forward(adj_batch, adj_mat, b_mat)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- train(model, epochs=100, lr=0.006, bs=100, step_size=10, gamma=0.9, nu1=1e-05, nu2=0.0001, device='cpu', output='out.emb')[source]
Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns
self
- Return type
Module
- training: bool