Skip to content

Instantly share code, notes, and snippets.

@mikk-c
Created August 6, 2024 11:38
Show Gist options
  • Select an option

  • Save mikk-c/de604758983f5b7e9aaf02c7108e5665 to your computer and use it in GitHub Desktop.

Select an option

Save mikk-c/de604758983f5b7e9aaf02c7108e5665 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "17ce4d10-0c5c-4dc8-8931-475e0095e4cd",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import networkx as nx\n",
"from numpy.linalg import svd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "da125bf6-3c7c-4019-bbe2-4d55344d0aa5",
"metadata": {},
"outputs": [],
"source": [
"def loss(emb, A):\n",
" l = 0\n",
" for u in range(A.shape[0]):\n",
" v_vects = np.zeros(2)\n",
" for v in range(A.shape[0]):\n",
" if u != v:\n",
" v_vects += emb[v] * A[u,v]\n",
" l += np.linalg.norm(emb[u] - np.sum(v_vects)) ** 2\n",
" return l"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "38296e3d-b4fb-4c0b-83e9-bca14446d560",
"metadata": {},
"outputs": [],
"source": [
"H = nx.read_edgelist(\"1/data.txt\", delimiter = \"\\t\", nodetype = int)\n",
"G = nx.Graph()\n",
"G.add_nodes_from(sorted(H.nodes))\n",
"G.add_edges_from(H.edges)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2895ecd2-95b7-4f89-be7b-62d6ca4727a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-0.20831658, -0.28388127],\n",
" [-0.14119158, -0.19244675],\n",
" [-0.30732743, -0.28644885],\n",
" [-0.23195072, -0.20727592],\n",
" [-0.28108414, -0.32997234],\n",
" [-0.23352491, -0.27090093],\n",
" [-0.25762119, -0.33830885],\n",
" [-0.17767727, -0.15268286],\n",
" [-0.37952009, 0.3508772 ],\n",
" [-0.24939078, 0.19509741],\n",
" [-0.36128451, 0.30608578],\n",
" [-0.20868972, 0.19763654],\n",
" [-0.20868972, 0.19763654],\n",
" [-0.10382218, 0.08737961],\n",
" [-0.34971158, 0.31097189]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D = np.array(list(dict(G.degree).values())) ** (-1/2)\n",
"A = nx.adjacency_matrix(G).todense()\n",
"L = D * A * D\n",
"U, s, Vt = svd(L)\n",
"embeddings_1 = U[:,:2]\n",
"\n",
"embeddings_1"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e597854c-140a-44db-b477-3bff3706a70e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.26479243, -0.23687981],\n",
" [-0.20096649, -0.19758656],\n",
" [ 0.42348481, -0.19945908],\n",
" [-0.36246555, -0.16107308],\n",
" [ 0.00189407, -0.21310377],\n",
" [-0.04132578, -0.25978185],\n",
" [-0.13225023, -0.26626225],\n",
" [-0.08072732, -0.12716786],\n",
" [ 0.04347252, 0.33342402],\n",
" [-0.35983281, 0.30059236],\n",
" [ 0.33667111, 0.29722324],\n",
" [-0.12140961, 0.33599228],\n",
" [-0.12140961, 0.33599228],\n",
" [-0.294756 , 0.198633 ],\n",
" [ 0.4381549 , 0.28990733]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"I = np.eye(A.shape[0])\n",
"L = (I - A).T.dot(I - A)\n",
"vals, vects = np.linalg.eig(L)\n",
"embeddings_2 = vects[:,1:3]\n",
"\n",
"embeddings_2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "15de2697-54b7-4683-abcf-4ec8d7c06c93",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"99.24721158803553\n",
"56.654180252402206\n"
]
}
],
"source": [
"print(loss(embeddings_1, A))\n",
"print(loss(embeddings_2, A))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment