smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
embed.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_EMBED_H_
2#define SMOLLERGRAPH_EMBED_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <string_view>
7#include <utility>
8#include <vector>
9
10#include "smollergraph/graph.h"
11
12namespace smollergraph {
13
14// Returns an embedding for each node, in the order of their ids, computed with
15// FastRP, as Neo4j's Graph Data Science library does: a sparse random vector
16// for each node, then five rounds, in each of which a node's vector is the sum
17// of its neighbours' vectors from the round before, along relationships of
18// `type` followed both ways, scaled to a length of one. The embedding adds up
19// the rounds from the second on. A seed gives the same vectors each time.
20std::vector<Embedding> FastRP(const Graph& graph, std::string_view type,
21 uint32_t seed);
22
23// How alike two embeddings are: the cosine of the angle between them, 1 when
24// they point the same way, 0 at right angles, and -1 when they're opposite.
25float Cosine(const Embedding& a, const Embedding& b);
26
27// Returns the `k` nodes whose embeddings are most like `node`'s, with their
28// cosines, the most alike first. It compares `node` with each of the others.
29std::vector<std::pair<NodeId, float>> MostSimilar(
30 const std::vector<Embedding>& embeddings, NodeId node, size_t k);
31
32} // namespace smollergraph
33
34#endif // SMOLLERGRAPH_EMBED_H_
std::array< float, kEmbeddingSize > Embedding
A node's embedding: a vector of numbers that stands for where the node is in the graph,...
Definition graph.h:96
uint32_t NodeId
Definition graph.h:20
float Cosine(const Embedding &a, const Embedding &b)
How alike two embeddings are: the cosine of the angle between them, 1 when they point the same way,...
Definition embed.cc:68
std::vector< std::pair< NodeId, float > > MostSimilar(const std::vector< Embedding > &embeddings, NodeId node, size_t k)
Returns the k nodes whose embeddings are most like node's, with their cosines, the most alike first.
Definition embed.cc:81
std::vector< Embedding > FastRP(const Graph &graph, std::string_view type, uint32_t seed)
Returns an embedding for each node, in the order of their ids, computed with FastRP,...
Definition embed.cc:25