Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Created April 10, 2018 16:42
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 carbide-public/754803dd54a226e4eaf6d99f203a4b9b to your computer and use it in GitHub Desktop.
Save carbide-public/754803dd54a226e4eaf6d99f203a4b9b to your computer and use it in GitHub Desktop.
Neural Network XOR
function sigmoid(x) { ///A **sigmoid function** is a [++mathematical function++](https://en.wikipedia.org/wiki/Function_(mathematics%29) having an "S" shaped curve (**sigmoid curve**). Often, _sigmoid function_ refers to the special case of the [++logistic function++](https://en.wikipedia.org/wiki/Logistic_function) shown in the first figure and defined by the formula
return 1/(1 + Math.exp(-x));
}
function XOR(a, b){ ///**Exclusive disjunction** or **exclusive or** is a [++logical operation++](https://en.wikipedia.org/wiki/Logical_connective) that outputs true only when inputs differ (one is true, the other is false).
let hidden = sigmoid(0.1 * a + 0.4 * b + 0.7)
return sigmoid(0.1 * a + 0.4 * b + 0.5 * hidden + 0.7)
}
var loss = ([ ///In [++mathematical optimization++](https://en.wikipedia.org/wiki/Mathematical_optimization), [++statistics++](https://en.wikipedia.org/wiki/Statistics), [++decision theory++](https://en.wikipedia.org/wiki/Decision_theory) and [++machine learning++](https://en.wikipedia.org/wiki/Machine_learning), a **loss function** or **cost function** is a function that maps an [++event++](https://en.wikipedia.org/wiki/Event_(probability_theory%29) or values of one or more variables onto a [++real number++](https://en.wikipedia.org/wiki/Real_number) intuitively representing some "cost" associated with the event. An [++optimization problem++](https://en.wikipedia.org/wiki/Optimization_problem) seeks to minimize a loss function.
XOR(0, 1) - 1,
XOR(1, 1) - 0,
XOR(1, 0) - 1,
XOR(0, 0) - 0
]).map(x => x*x).reduce((a,b) => a+b, 0)
import visualize from './visualize.js'
if(!require('@backprop')) visualize(XOR);
import zeros from "zeros"
import fill from "ndarray-fill"
export default function visualize(fn){
return fill(zeros([100, 100]), (i, j) => fn(i/100, j/100))
}
import {say} from 'cowsay'
say({text: "hello there! \n\nWelcome to the Carbide Alpha\nRelease XOR Example Notebook!\n\nTry dragging the Loss Slider!\n\nSend bugs to\nbugs@trycarbide.com!\n\nSend hellos to\nhello@trycarbide.com!" || ' '})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment