{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9gQRr-GWd1AP"
},
"source": [
"# Exercise 10.1 - Solution\n",
"## Signal Classification using Dynamic Graph Convolutional Neural Networks\n",
"After a long journey through the universe before reaching the earth, the cosmic particles interact with the galactic magnetic field $B$.\n",
"As these particles carry a charge $q$ they are deflected in the field by the Lorentz force $F = q \\cdot v × B$.\n",
"Sources of cosmic particles are located all over the sky, thus arrival distributions of the cosmic particles are isotropic in general. However, particles originating from the same source generate on top of the isotropic\n",
"arrival directions, street-like patterns from galactic magnetic field deflections.\n",
"\n",
"In this tasks we want to classify whether a simulated set of $500$ arriving cosmic particles contains street-like patterns (signal), or originates from an isotropic background.\n",
"\n",
"Training graph networks can be computationally demanding, thus, we recommend to use a GPU for this task."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "1I5AZeped1AR"
},
"outputs": [],
"source": [
"from tensorflow import keras\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"\n",
"layers = keras.layers\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "n3uLF5hxd1AT"
},
"source": [
"#### Download EdgeConv Layer"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "Hh6iRC-Kd1AT",
"outputId": "49efac63-2b2d-4e3c-9bb9-d2cb9a30d7de",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Downloading...\n",
"From: https://raw.githubusercontent.com/DeepLearningForPhysicsResearchBook/deep-learning-physics/main/edgeconv.py\n",
"To: /content/edgeconv.py\n",
"6.36kB [00:00, 13.5MB/s] \n"
]
}
],
"source": [
"import gdown\n",
"import os\n",
"\n",
"url = \"https://raw.githubusercontent.com/DeepLearningForPhysicsResearchBook/deep-learning-physics/main/edgeconv.py\"\n",
"output = 'edgeconv.py'\n",
"\n",
"if os.path.exists(output) == False:\n",
" gdown.download(url, output, quiet=False)\n",
"\n",
"from edgeconv import EdgeConv"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UWhpNlasd1AU"
},
"source": [
"### Download Data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "DR1mzMPid1AV"
},
"outputs": [],
"source": [
"url = \"https://drive.google.com/u/0/uc?export=download&confirm=HgGH&id=1XKN-Ik7BDyMWdQ230zWS2bNxXL3_9jZq\"\n",
"output = 'cr_sphere.npz'\n",
"\n",
"if os.path.exists(output) == False:\n",
" gdown.download(url, output, quiet=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "1Dv-rn6rd1AW"
},
"outputs": [],
"source": [
"f = np.load(output)\n",
"n_train = 10000\n",
"x_train, x_test = f['data'][:-n_train], f['data'][-n_train:]\n",
"labels = keras.utils.to_categorical(f['label'], num_classes=2)\n",
"y_train, y_test = labels[:-n_train], labels[-n_train:]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "RyYzmN05d1AW",
"outputId": "2fb346ae-4603-4fca-83ca-78d8fb109053",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"x_train.shape (40000, 500, 4)\n",
"y_train.shape (40000, 2)\n"
]
}
],
"source": [
"print(\"x_train.shape\", x_train.shape)\n",
"print(\"y_train.shape\", y_train.shape)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "5fdLMSL4d1Af"
},
"outputs": [],
"source": [
"# define coordinates for very first EdgeConv\n",
"train_points, test_points = x_train[..., :3], x_test[..., :3]\n",
"\n",
"# Use normalized Energy as features for convolutional layers\n",
"train_features, test_features = x_train[..., -1, np.newaxis], x_test[..., -1, np.newaxis]\n",
"train_features = np.concatenate([train_features, train_points], axis=-1)\n",
"\n",
"train_input_data = [train_points, train_features]\n",
"test_input_data = [test_points, test_features]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pSNGE7Brd1Ag"
},
"source": [
"### Plot an example sky map"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "vH0pb73id1Ag",
"outputId": "9a12e040-bfb8-4fbb-a0bb-e3f6a40c995c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 591
}
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"