{
"cells": [
{
"cell_type": "markdown",
"id": "16da5ed1-982b-40d7-ba84-8b39c3cf5e0c",
"metadata": {},
"source": [
"# Customize EncoderMap: Custom loss functions\n",
"\n",
"**Welcome**\n",
"\n",
"Welcome to the second part of the customization section.\n",
"\n",
"Run this notebook on Google Colab:\n",
"\n",
"[](https://colab.research.google.com/github/AG-Peter/encodermap/blob/main/tutorials/notebooks_customization/02_custom_loss_functions.ipynb)\n",
"\n",
"Find the documentation of EncoderMap:\n",
"\n",
"https://ag-peter.github.io/encodermap\n",
"\n",
"**Goals:**\n",
"\n",
"In this tuorial you will learn:\n",
"- [A few general concepts about loss functions.](#what-are-loss-functions)\n",
"- [What loss functions are implemented in EncoderMap.](#cost-functions)\n",
"- [How you can write your own loss functions.](#custom-cost-functions)\n",
"- [Another example for a custom loss function.](#adding-a-unit-circle-loss)\n",
"\n",
"**For Google colab only**\n",
"\n",
"If you're on Google colab, please uncomment these lines and install EncoderMap."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "79ff2eeb-f982-4f3b-9bfc-dc9574d197d8",
"metadata": {
"execution": {
"iopub.execute_input": "2025-06-19T23:04:43.092664Z",
"iopub.status.busy": "2025-06-19T23:04:43.092413Z",
"iopub.status.idle": "2025-06-19T23:04:43.094535Z",
"shell.execute_reply": "2025-06-19T23:04:43.094274Z"
}
},
"outputs": [],
"source": [
"# !wget https://gist.githubusercontent.com/kevinsawade/deda578a3c6f26640ae905a3557e4ed1/raw/b7403a37710cb881839186da96d4d117e50abf36/install_encodermap_google_colab.sh\n",
"# !sudo bash install_encodermap_google_colab.sh"
]
},
{
"cell_type": "markdown",
"id": "a9da4dcf-3e0e-40ce-a114-578ee8a9054a",
"metadata": {},
"source": [
"## Import Libraries\n",
"\n",
"In this tutorial we will learn how to write our own loss functions and add them to EncoderMap. Let us start with the imports:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "db4eaab1",
"metadata": {
"execution": {
"iopub.execute_input": "2025-06-19T23:04:43.095887Z",
"iopub.status.busy": "2025-06-19T23:04:43.095755Z",
"iopub.status.idle": "2025-06-19T23:04:46.024715Z",
"shell.execute_reply": "2025-06-19T23:04:46.024209Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/kevin/git/encoder_map_private/encodermap/__init__.py:194: GPUsAreDisabledWarning: EncoderMap disables the GPU per default because most tensorflow code runs with a higher compatibility when the GPU is disabled. If you want to enable GPUs manually, set the environment variable 'ENCODERMAP_ENABLE_GPU' to 'True' before importing EncoderMap. To do this in python you can run:\n",
"\n",
"import os; os.environ['ENCODERMAP_ENABLE_GPU'] = 'True'\n",
"\n",
"before importing encodermap.\n",
" _warnings.warn(\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ba5199ce29964a4b91ab30fb0dc75ef2",
"version_major": 2,
"version_minor": 0
},
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import plotly\n",
"import plotly.express as px\n",
"import plotly.graph_objects as go\n",
"import plotly.subplots as ps\n",
"import tensorflow as tf\n",
"\n",
"import encodermap as em\n",
"\n",
"from pathlib import Path\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "markdown",
"id": "a938cf02-38b9-4f94-a629-1141d8f54f7a",
"metadata": {},
"source": [
"\n",
"\n",
"## What are loss functions\n",
"\n",
"Loss functions in EncoderMap are small pieces of code, that take some inputs and return scalar values. We can easily come up with a loss function ourselves, when we think about a linear regression. First let us create some example data in a $\\mathbb{R}^2$ space. Each point $i$ is defined by an x-value $x^{(i)}$ and a true y-value $y^{(i)}$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1b5306dd-2062-4ac4-8a85-cca1f291cbd9",
"metadata": {
"execution": {
"iopub.execute_input": "2025-06-19T23:04:46.026729Z",
"iopub.status.busy": "2025-06-19T23:04:46.026165Z",
"iopub.status.idle": "2025-06-19T23:04:46.376884Z",
"shell.execute_reply": "2025-06-19T23:04:46.376463Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hovertemplate": "x=%{x}
y=%{y}