Skip to content

Instantly share code, notes, and snippets.

@corba777
Created October 30, 2019 06:16
Show Gist options
  • Select an option

  • Save corba777/310fd23605366a0e873975b213ce1a58 to your computer and use it in GitHub Desktop.

Select an option

Save corba777/310fd23605366a0e873975b213ce1a58 to your computer and use it in GitHub Desktop.
Differentiable argmax
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Pkg"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Pkg.add(\"ForwardDiff\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"using ForwardDiff"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"example (generic function with 1 method)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function example(x)\n",
" if x==0\n",
" return x\n",
" end\n",
" return sin(pi/x)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"argmax (generic function with 1 method)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function argmax(f,x)\n",
" max=f(x[1]) \n",
" for i in 2:length(x)\n",
" el=x[i]\n",
" if f(el) > f(max)\n",
" max=el\n",
" end\n",
" end\n",
" return max\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"argmax(example,[1.0,2.0,3.0, 4.0])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4-element Array{Float64,1}:\n",
" 0.0\n",
" 1.0\n",
" 0.0\n",
" 0.0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ForwardDiff.gradient(x->argmax(example,x),[1.0,2.0,3.0, 4.0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Pkg.add(\"ReverseDiff\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"using ReverseDiff"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4-element Array{Float64,1}:\n",
" 0.0\n",
" 1.0\n",
" 0.0\n",
" 0.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ReverseDiff.gradient(x->argmax(example,x),[1.0,2.0,3.0, 4.0])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.2.0",
"language": "julia",
"name": "julia-1.2"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
#!/usr/bin/env python
# coding: utf-8
# In[1]:
using Pkg
# In[ ]:
Pkg.add("ForwardDiff")
# In[3]:
using ForwardDiff
# In[4]:
function example(x)
if x==0
return x
end
return sin(pi/x)
end
# In[5]:
function argmax(f,x)
max=f(x[1])
for i in 2:length(x)
el=x[i]
if f(el) > f(max)
max=el
end
end
return max
end
# In[6]:
argmax(example,[1.0,2.0,3.0, 4.0])
# In[7]:
ForwardDiff.gradient(x->argmax(example,x),[1.0,2.0,3.0, 4.0])
# In[ ]:
Pkg.add("ReverseDiff")
# In[9]:
using ReverseDiff
# In[10]:
ReverseDiff.gradient(x->argmax(example,x),[1.0,2.0,3.0, 4.0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment