Plots.jl/examples/meetup/nnet.ipynb
Thomas Breloff 949f11a8c9 meetup
2015-10-22 15:10:15 -04:00

417 lines
8.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using Plots; immerse()\n",
"default(size=(500,300), leg=false)\n",
"\n",
"# creates x/y vectors which can define a grid in a zig-zag pattern\n",
"function gridxy(lim, n::Int)\n",
" xs = linspace(lim..., n)\n",
" xypairs = vec([(x,y) for x in vcat(xs,reverse(xs)), y in xs])\n",
" Plots.unzip(xypairs)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The problem... can we classify the functions?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# these are the functions we want to classify\n",
"scalar = 10 # larger is harder... start with 3\n",
"f1(x) = 0.6sin(scalar * x) + 0.1\n",
"f2(x) = f1(x) - 0.2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# On to the fun..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# pick the plotting limits\n",
"lim = (-1,1)\n",
"funcs = [f1, f2]\n",
"n = 40\n",
"gridx, gridy = gridxy(lim, n)\n",
"default(xlim = lim, ylim = lim)\n",
"\n",
"function initialize_plot(funcs, lim, gridx, gridy; kw...)\n",
" # show the grid\n",
" plot([gridx gridy], [gridy gridx], c=:black, kw...)\n",
"\n",
" # show the funcs\n",
" plot!(funcs, lim..., l=(4,[:blue :red]))\n",
"end\n",
"\n",
"# kick off an animation... we can save frames whenever we want, lets save the starting frame\n",
"function initialize_animation()\n",
" anim = Animation()\n",
" frame(anim)\n",
" anim\n",
"end\n",
"\n",
"# lets see what we're dealing with...\n",
"p = initialize_plot(funcs, lim, gridx, gridy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# That looks tricky... lets build a neural net!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using OnlineAI\n",
"net = buildTanhClassificationNet(\n",
" 2, # number of inputs\n",
" 1, # number of outputs\n",
" [4,4,2], # hidden layers structure\n",
"# params = NetParams(gradientModel = SGDModel(η=1e-5))\n",
")\n",
"\n",
"# take x matrix and convert to the first layer's activation\n",
"function activateHidden(net, x)\n",
" @assert net.layers[end].nin == 2\n",
" proj = zeros(nrows(x), 2)\n",
" for i in 1:nrows(x)\n",
" data = row(x,i)\n",
" for layer in net.layers[1:end-1]\n",
" OnlineAI.forward!(layer, data, false)\n",
" data = layer.a\n",
" end\n",
" row!(proj, i, data)\n",
" end\n",
" vec(proj[:,1]), vec(proj[:,2])\n",
"end "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Update our model and the visualization"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = initialize_plot(funcs, lim, gridx, gridy)\n",
"anim = initialize_animation()\n",
"gui()\n",
"\n",
"progressviz = track_progress(net)\n",
"gui(progressviz.subplt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"iterations_per_frame = 2000\n",
"total_frames = 100\n",
"dist = Distributions.Uniform(lim...)\n",
"\n",
"for frm in 1:total_frames\n",
" # pick one of the functions at random, sample from the x line, then update the\n",
" # neural net with [x, f(x)] as the inputsn = 1000\n",
" for i in 1:iterations_per_frame\n",
" f = sample(funcs)\n",
" x = rand(dist)\n",
" y = f == f1 ? 1.0 : -1.0\n",
" update!(net, Float64[x, f(x)], [y])\n",
" end\n",
"\n",
" # update the plot... project each series to the first hidden layer and reset the data\n",
" # NOTE: this works because `getindex` and `setindex` are overloaded to get/set the underlying plot series data\n",
" x = linspace(lim..., 50)\n",
" p[1] = activateHidden(net, hcat(gridx, gridy))\n",
" p[2] = activateHidden(net, hcat(gridy, gridx))\n",
" p[3] = activateHidden(net, hcat(x, map(f1,x)))\n",
" p[4] = activateHidden(net, hcat(x, map(f2,x)))\n",
"\n",
" # show/update the plot\n",
" gui(p)\n",
" frame(anim)\n",
" \n",
" # update the progress visualization\n",
" update!(progressviz, true)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Animate!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gif(anim, fps = 20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# show the network (uses Qwt, visualize isn't available unless you import it)\n",
"import Qwt\n",
"viz = visualize(net);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# update the net representation with weights, etc\n",
"update!(viz)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gui(progressviz.subplt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p1 = plot(rand(20))\n",
"p2 = plot(rand(10))\n",
"p3 = scatter(rand(100))\n",
"p4 = plot(rand(1000))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"subplot(p1,p2,p3,p4, nr=1, leg=false)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"immerse()\n",
"p = plot(rand(10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gui()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"append!(p,1,rand(10))\n",
"gui()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sp = progressviz.subplt.plts[1].o.widget[:minimumSizeHint]()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.4.0",
"language": "julia",
"name": "julia-0.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}