{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Generating a FunctionProto\n\nThe example below shows how we can define Selu as a function in onnxscript.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, import the ONNX opset used to define the function.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from onnxscript import opset15 as op\nfrom onnxscript import script" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, define Selu as an ONNXScript function.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@script()\ndef Selu(X, alpha: float, gamma: float):\n alphaX = op.CastLike(alpha, X)\n gammaX = op.CastLike(gamma, X)\n neg = gammaX * (alphaX * op.Exp(X) - alphaX)\n pos = gammaX * X\n zero = op.CastLike(0, X)\n return op.Where(zero >= X, neg, pos)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can convert the ONNXScript function to an ONNX function (FunctionProto) as below:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "onnx_fun = Selu.to_function_proto()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what the translated function looks like:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import onnx # noqa: E402\n\nprint(onnx.printer.to_text(onnx_fun))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.15" } }, "nbformat": 4, "nbformat_minor": 0 }