Skip to content

Commit

Permalink
decrease size of bias and weights, improve is_odd example
Browse files Browse the repository at this point in the history
Signed-off-by: Lucian Buzzo <lucian.buzzo@gmail.com>
  • Loading branch information
LucianBuzzo committed Jul 23, 2023
1 parent b993e54 commit 53c36a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions example/is_odd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ include Gradnite
# BIT_SIZE is our input layer size
BIT_SIZE = 8

puts "running with #{BIT_SIZE} bits"

# Layer size is approximately 2/3 of the input size
LAYER_SIZE = (BIT_SIZE * 2/3).ceil.to_i

mlp = MLP.new(BIT_SIZE, [LAYER_SIZE, LAYER_SIZE, 1])

max = 255
nums = (1..max).to_a
# Generate all the numbers that can be represented by BIT_SIZE bits
max = (2 ** BIT_SIZE) - 1

puts "building training set of numbers with a ceiling of #{max}"

nums = (0..max).to_a

def num_to_binary_array(n)
BIT_SIZE.times.map { |bit|
Expand All @@ -30,7 +36,7 @@ ypred = [] of Node

loss = Node.new(0.0)

epochs = 100
epochs = 50

epochs.times do |k|
# forward pass
Expand All @@ -49,6 +55,12 @@ epochs.times do |k|
}
loss.backward

if loss.value < 0.0001
puts "loss: #{loss.value}"
puts "converged at epoch #{k}"
break
end

# Gradient descent. Nudge all the parameters in the opposite direction of the gradient.
# The gradient is showing us the direction that increases the loss, so we want to go the opposite way.
# Linear decay of learning rate
Expand All @@ -68,8 +80,8 @@ def is_odd?(n, mlp)
return result > 0.0
end

puts is_odd?(201, mlp)
puts is_odd?(202, mlp)
puts is_odd?(203, mlp)
puts "201 true - #{is_odd?(201, mlp)}"
puts "202 false - #{is_odd?(202, mlp)}"
puts "203 true - #{is_odd?(203, mlp)}"

puts "done"
4 changes: 2 additions & 2 deletions src/gradnite/gradnite.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ module Gradnite
property bias : Node

def initialize(input_count : Int64)
@weights = Array.new(input_count) { Node.new(rand) }
@bias = Node.new(rand)
@weights = Array.new(input_count) { Node.new(0.01 * rand) }
@bias = Node.new(0.0)
end

def run(x : Array(Float64) | Array(Node))
Expand Down

0 comments on commit 53c36a0

Please sign in to comment.