{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Zigmoid\n", "\n", "The zigmoid function is defined as follows:\n", "\n", "$$\n", "zigmoid(x) = \\left\\{\n", " \\begin{array}\\\\\n", " 0 & if\\ x<-0.5 \\\\\n", " x+0.5 & if\\ -0.5\\leq x \\leq 0.5 \\\\\n", " 1 & if x>0.5\n", " \\end{array}\n", " \\right.\n", "$$\n", "\n", "It is an approximation to the sigmoid function, but easier to compute in the MPC context. It is further analagous to the \"cut\" function defined in the literature and similar to approximations used elsewhere such as in SecureML.\n", "\n", "In this example, we secret share a vector of values selected to demonstrate the different piecewise separated areas of interest for the zigmoid function, and compute the zigmoid.\n", "\n", "As always, Cicada's zigmoid function operates element-wise on arrays of any shape." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Player 0 values: [-5. -0.25 0. 0.25 5. ]\n", "INFO:root:Player 1 values: None\n", "INFO:root:Player 0 zigmoid: [0. 0.25 0.5 0.75 1. ]\n", "INFO:root:Player 1 zigmoid: [0. 0.25 0.5 0.75 1. ]\n" ] } ], "source": [ "import logging\n", "\n", "import numpy\n", "\n", "from cicada.additive import AdditiveProtocolSuite\n", "from cicada.communicator import SocketCommunicator\n", "from cicada.logger import Logger\n", "\n", "logging.basicConfig(level=logging.INFO)\n", "\n", "def main(communicator):\n", " log = Logger(logging.getLogger(), communicator)\n", " protocol = AdditiveProtocolSuite(communicator)\n", "\n", " values = numpy.array([-5, -0.25, 0, 0.25, 5]) if communicator.rank == 0 else None\n", " log.info(f\"Player {communicator.rank} values: {values}\")\n", " \n", " values_share = protocol.share(secret=values, src=0, shape=(5,))\n", " zigmoid_share = protocol.zigmoid(values_share)\n", " zigmoid = protocol.reveal(zigmoid_share)\n", " \n", " log.info(f\"Player {communicator.rank} zigmoid: {zigmoid}\")\n", "\n", "SocketCommunicator.run(world_size=2, fn=main);" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 4 }