Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active March 28, 2018 21:52
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 t-nissie/1ced405cff775f73c80bee75b21e029d to your computer and use it in GitHub Desktop.
Save t-nissie/1ced405cff775f73c80bee75b21e029d to your computer and use it in GitHub Desktop.
ゼロからOCamlで作るDeep Learning
(* perceptron : float -> float -> float -> float -> float -> float *)
(* description: perceptron with two inputs x1 and x2*)
let perceptron b w1 w2 x1 x2 = if b +. w1 *. x1 +. w2 *. x2 >= 0.0 then 1.0 else 0.0
let and_gate = perceptron ~-.0.7 0.5 0.5
let nand_gate = perceptron 0.7 ~-.0.5 ~-.0.5
let or_gate = perceptron ~-.0.5 1.0 1.0
let nor_gate = perceptron 0.5 ~-.1.0 ~-.1.0
let xor_gate x1 x2 = and_gate (nand_gate x1 x2) (or_gate x1 x2)
(* tests *)
let and_00 = and_gate 0.0 0.0 = 0.0
let and_10 = and_gate 1.0 0.0 = 0.0
let and_01 = and_gate 0.0 1.0 = 0.0
let and_11 = and_gate 1.0 1.0 = 1.0
let nand_00 = nand_gate 0.0 0.0 = 1.0
let nand_10 = nand_gate 1.0 0.0 = 1.0
let nand_01 = nand_gate 0.0 1.0 = 1.0
let nand_11 = nand_gate 1.0 1.0 = 0.0
let or_00 = or_gate 0.0 0.0 = 0.0
let or_10 = or_gate 1.0 0.0 = 1.0
let or_01 = or_gate 0.0 1.0 = 1.0
let or_11 = or_gate 1.0 1.0 = 1.0
let nor_00 = nor_gate 0.0 0.0 = 1.0
let nor_10 = nor_gate 1.0 0.0 = 0.0
let nor_01 = nor_gate 0.0 1.0 = 0.0
let nor_11 = nor_gate 1.0 1.0 = 0.0
let xor_00 = xor_gate 0.0 0.0 = 0.0
let xor_10 = xor_gate 1.0 0.0 = 1.0
let xor_01 = xor_gate 0.0 1.0 = 1.0
let xor_11 = xor_gate 1.0 1.0 = 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment