Skip to main content

Node2Vec

Struct Node2Vec 

Source
pub struct Node2Vec { /* private fields */ }
Expand description

Builder for configuring and running the Node2Vec algorithm.

Construct with Node2Vec::new, chain optional parameters, then call Node2Vec::fit.

§Example

use rune_node2vec::Node2Vec;

let model = Node2Vec::new()
    .embedding_dim(64)
    .walk_length(40)
    .num_walks(5)
    .p(1.0)
    .q(0.5)
    .n_epochs(3)
    .random_seed(7);

Implementations§

Source§

impl Node2Vec

Source

pub fn new() -> Self

Creates a new Node2Vec instance with sensible defaults.

ParameterDefault
embedding_dim128
walk_length80
num_walks10
window_size10
p1.0
q1.0
n_epochs1
learning_rate0.025
neg_samples5
random_seed42
§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new();
Source

pub fn embedding_dim(self, d: usize) -> Self

Dimensionality of each output embedding vector. Defaults to 128.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().embedding_dim(64);
Source

pub fn walk_length(self, l: usize) -> Self

Number of nodes in each random walk. Defaults to 80.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().walk_length(40);
Source

pub fn num_walks(self, n: usize) -> Self

Number of walks generated from each node. Defaults to 10.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().num_walks(5);
Source

pub fn window_size(self, w: usize) -> Self

Skip-gram context window half-width. Defaults to 10.

Each centre node is paired with all nodes within window_size positions on either side in the walk.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().window_size(5);
Source

pub fn p(self, p: f64) -> Self

Return parameter controlling the likelihood of revisiting a node. Defaults to 1.0.

Low p encourages the walk to backtrack; high p pushes the walk forward. Clamped to a minimum of 1e-9.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().p(0.5);
Source

pub fn q(self, q: f64) -> Self

In-out parameter controlling the walk’s tendency to explore. Defaults to 1.0.

Low q favours DFS-like exploration of the graph; high q favours BFS-like local neighbourhood traversal. Clamped to a minimum of 1e-9.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().q(2.0);
Source

pub fn n_epochs(self, n: usize) -> Self

Number of training epochs over all walks. Defaults to 1.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().n_epochs(5);
Source

pub fn learning_rate(self, lr: f64) -> Self

Initial SGD learning rate; decays linearly to 0.0001 × initial_lr. Defaults to 0.025.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().learning_rate(0.01);
Source

pub fn neg_samples(self, n: usize) -> Self

Number of negative samples drawn per positive (centre, context) pair. Defaults to 5.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().neg_samples(10);
Source

pub fn random_seed(self, s: u64) -> Self

Seed for the internal Xorshift64 PRNG. Identical seeds produce identical embeddings. Defaults to 42.

§Example
use rune_node2vec::Node2Vec;

let model = Node2Vec::new().random_seed(123);
Source

pub fn fit(&self, n_nodes: usize, edges: &[(usize, usize)]) -> EmbedResult

Computes Node2Vec embeddings for an undirected graph.

n_nodes is the total number of nodes (indices 0..n_nodes). edges is a slice of undirected (u, v) pairs; each edge is added in both directions. Self-loops are ignored.

Every node receives an embedding regardless of whether it has any edges. Isolated nodes are not visited during walk training and keep their random initial embedding.

§Panics

Panics if any node index in edges is ≥ n_nodes.

§Example
use rune_node2vec::Node2Vec;

// Two connected triangles sharing no edge.
let edges = vec![
    (0, 1), (1, 2), (2, 0),
    (3, 4), (4, 5), (5, 3),
];
let result = Node2Vec::new()
    .embedding_dim(16)
    .num_walks(5)
    .walk_length(20)
    .n_epochs(3)
    .random_seed(42)
    .fit(6, &edges);

assert_eq!(result.embeddings.len(), 6);
assert!(result.embeddings.iter().all(|e| e.len() == 16));

Trait Implementations§

Source§

impl Clone for Node2Vec

Source§

fn clone(&self) -> Node2Vec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Node2Vec

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.