Skip to content

Commit 06597a9

Browse files
committed
simplify #sign
1 parent 92d1469 commit 06597a9

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

examples/mnist.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

9595
puts "\nTraining the network with #{train_size} data samples...\n\n"
9696
t = Time.now
97-
result = nn.train(x_train, y_train, log_every: 1, max_iterations: 100)
97+
result = nn.train(x_train, y_train, log_every: 1, max_iterations: 100, error_threshold: 0.005)
9898

9999
puts "\nDone training the network: #{result[:iterations]} iterations, error #{result[:error].round(5)}, #{(Time.now - t).round(1)}s"
100100

neural_net.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,12 @@ def mean_squared_error errors
229229
ZERO_TOLERANCE = Math.exp(-16)
230230

231231
def sign x
232-
if x < ZERO_TOLERANCE && x > -ZERO_TOLERANCE # if float very close to 0
233-
0
232+
if x > ZERO_TOLERANCE
233+
1
234+
elsif x < -ZERO_TOLERANCE
235+
-1
234236
else
235-
x <=> 0 # returns 1 if postitive, -1 if negative
237+
0 # x is zero, or a float very close to zero
236238
end
237239
end
238240

0 commit comments

Comments
 (0)