-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* maxpool and convolution fixes * softmax working * dropout and softmax loss * fix relu. whoops * mnist * big stability changes and QOL * conv fallback * fallback * use matrix multiplyication
- Loading branch information
1 parent
0464462
commit 6ba53ea
Showing
43 changed files
with
1,564 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
## MNIST | ||
|
||
This is an example of how to use convolutional layers | ||
and max pooling to learn the MNIST dataset. Using this approach, | ||
our network achieves > 97% accuracy on the training dataset. | ||
|
||
```crystal | ||
net = Num::NN::Network.new(ctx) do | ||
input [1, 28, 28] | ||
conv2d 20, 5, 5 | ||
relu | ||
maxpool({2, 2}, {0, 0}, {2, 2}) | ||
conv2d 20, 5, 5 | ||
maxpool({2, 2}, {0, 0}, {2, 2}) | ||
flatten | ||
linear 10 | ||
relu | ||
linear 10 | ||
softmax_cross_entropy_loss | ||
sgd 0.01 | ||
end | ||
``` | ||
|
||
``` | ||
Epoch: 0 | Accuracy: 0.8644276947705443 | ||
Epoch: 1 | Accuracy: 0.9558931430096052 | ||
Epoch: 2 | Accuracy: 0.9677494663820705 | ||
Epoch: 3 | Accuracy: 0.9735358858057631 | ||
Epoch: 4 | Accuracy: 0.9770711045891142 | ||
``` | ||
|
||
### Accuracy over time | ||
|
||
![mnist](mnist.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require "../../src/num" | ||
|
||
dataset = Num::NN.load_mnist_dataset | ||
ctx = Num::Grad::Context(Tensor(Float32)).new | ||
|
||
batch_size = 32 | ||
|
||
net = Num::NN::Network.new(ctx) do | ||
input [1, 28, 28] | ||
conv2d 20, 5, 5 | ||
relu | ||
maxpool({2, 2}, {0, 0}, {2, 2}) | ||
conv2d 20, 5, 5 | ||
maxpool({2, 2}, {0, 0}, {2, 2}) | ||
flatten | ||
linear 10 | ||
relu | ||
linear 10 | ||
softmax_cross_entropy_loss | ||
sgd 0.01 | ||
end | ||
|
||
x_train = ctx.variable((dataset.features / 255_f32).reshape(-1, 1, 28, 28)) | ||
y_train = dataset.labels | ||
|
||
losses = [] of Float32 | ||
|
||
5.times do |epoch| | ||
y_trues = [] of Int32 | ||
y_preds = [] of Int32 | ||
|
||
(x_train.value.shape[0] // batch_size).times do |batch_id| | ||
offset = batch_id * batch_size | ||
x = x_train[offset...offset + batch_size] | ||
target = y_train[offset...offset + batch_size] | ||
|
||
output = net.forward(x) | ||
|
||
loss = net.loss(output, target) | ||
losses << loss.value.value | ||
|
||
y_trues += target.argmax(axis: 1).to_a | ||
y_preds += output.value.argmax(axis: 1).to_a | ||
|
||
loss.backprop | ||
net.optimizer.update | ||
end | ||
|
||
accuracy = y_trues.zip(y_preds).map { |t, p| (t == p).to_unsafe }.sum / y_trues.size | ||
|
||
puts "Epoch: #{epoch} | Accuracy: #{accuracy}" | ||
end | ||
|
||
Num::Plot::Plot.plot do | ||
scatter (0...losses.size), losses | ||
x_label "Epochs" | ||
y_label "Loss" | ||
label "MNIST Accuracy" | ||
end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright (c) 2020 Crystal Data Contributors | ||
# | ||
# MIT License | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
struct Float32 | ||
macro add_operator(name, operator) | ||
def {{operator.id}}(other : Num::Grad::Variable(Tensor(Float32))) | ||
other.context.variable(self) {{operator.id}} other | ||
end | ||
end | ||
|
||
add_operator add, :+ | ||
add_operator subtract, :- | ||
add_operator multiply, :* | ||
add_operator divide, :/ | ||
add_operator power, :** | ||
end | ||
|
||
struct Float64 | ||
macro add_operator(name, operator) | ||
def {{operator.id}}(other : Num::Grad::Variable(Tensor(Float64))) | ||
other.context.variable(self) {{operator.id}} other | ||
end | ||
end | ||
|
||
add_operator add, :+ | ||
add_operator subtract, :- | ||
add_operator multiply, :* | ||
add_operator divide, :/ | ||
add_operator power, :** | ||
|
||
def exp | ||
Math.exp(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright (c) 2020 Crystal Data Contributors | ||
# | ||
# MIT License | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
class Num::Grad::ExpGate(T) < Num::Grad::Gate(T) | ||
getter a : Num::Grad::Variable(T) | ||
|
||
def initialize(@a : Num::Grad::Variable(T)) | ||
end | ||
|
||
def backward(payload : Num::Grad::Payload(T)) : Array(T) | ||
gradient = payload.variable.grad | ||
r0 = gradient.map(a.value) do |i, j| | ||
i * Math.exp(j) | ||
end | ||
[r0] | ||
end | ||
|
||
def cache(result : Num::Grad::Variable(T), *args) | ||
a = args[0] | ||
result.grad = T.zeros_like(result.value) | ||
result.requires_grad = true | ||
Num::Grad.register("Exp", self, result, a) | ||
end | ||
end |
Oops, something went wrong.