<jupyter_start><jupyter_text>// Copyright 2020 Twitter, Inc.// SPDX-License-Identifier: Apache-2.0<jupyter_code>!pip install node2vec==0.4.1 #0.4.1
!pip install networkx==2.5 # 2.5 , gensim-3.8.3
!pip install pandas==1.0.3
!pip install ipywidgets
from ipywidgets import IntProgress
import node2vec
import itertools
import networkx as nx
import pickle
import pandas as pd
mkdir -p ./label_embeddings/
def embed_labels(G):
  import node2vec
  from node2vec import Node2Vec
  # Precompute probabilities and generate walks - **ON WINDOWS ONLY WORKS WITH workers=1**
  node2vec = Node2Vec(G, dimensions=768, walk_length=5, num_walks=1000, workers=4)  # Use temp_folder for big graphs
  # Embed nodes
  label_embedder = node2vec.fit(window=10, min_count=1, batch_words=4)  # Any keywords acceptable by gensim.Word2Vec can be passed, `dimensions` and `workers` are automatically passed (from the Node2Vec constructor)
  return label_embedder

import numpy as np
time_graph = nx.Graph()
for i in np.arange(1900, 2000):
  time_graph.add_edge(i, i + 1)

output_prefix = 'toy_president_time'
time_graph_embedder = embed_labels(time_graph)
time_graph_embedder.wv.save_word2vec_format(fname='./label_embeddings/{}.txt'.format(output_prefix))
pickle.dump(time_graph_embedder, open('./label_embeddings/{}.pkl'.format(output_prefix), 'wb'))
time_graph_embedder.most_similar('1900')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1082437275985663, 'fraction_numerical': 0.043010752688172046, 'mean_word_length': 6.05050505050505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '84942', 'license': 'Apache-2.0', 'n_tokens_mistral': 521, 'n_tokens_neox': 484, 'n_words': 121, 'path': 'notebooks/embed_time_toy_task.ipynb', 'repo_name': 'twitter-research/lmsoc', 'size': '1395'}