Skip to content

Instantly share code, notes, and snippets.

@mattn
Created October 14, 2019 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/b24c6e6e34abc8837cbaa30cb7291e1c to your computer and use it in GitHub Desktop.
Save mattn/b24c6e6e34abc8837cbaa30cb7291e1c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using gorgonia in gophernotes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import gorgonia package"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import (\n",
" \"fmt\"\n",
" \"log\"\n",
" \"unsafe\"\n",
"\n",
" \"gorgonia.org/gorgonia\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g := gorgonia.NewGraph()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define scalar variable a and b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a := gorgonia.NewScalar(g, gorgonia.Float32, gorgonia.WithName(\"a\"))\n",
"b := gorgonia.NewScalar(g, gorgonia.Float32, gorgonia.WithName(\"b\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make graph \"a + b\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"c, err := gorgonia.Add(a, b)\n",
"if err != nil {\n",
" log.Fatal(err)\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create tape-machine"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"machine := gorgonia.NewTapeMachine(g)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let a = 1, b = 2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"gorgonia.Let(a, 1)\n",
"gorgonia.Let(b, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Evaluate graph"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"machine.RunAll()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.Value()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Go",
"language": "go",
"name": "gophernotes"
},
"language_info": {
"codemirror_mode": "",
"file_extension": ".go",
"mimetype": "",
"name": "go",
"nbconvert_exporter": "",
"pygments_lexer": "",
"version": "go1.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment