sentdex / nnfs_book Goto Github PK
View Code? Open in Web Editor NEWSample code from the Neural Networks from Scratch book.
Home Page: https://nnfs.io
License: MIT License
Sample code from the Neural Networks from Scratch book.
Home Page: https://nnfs.io
License: MIT License
The output in the NNFS book for Chapter 14 and commented at the end of the Ch14_Final.py file is as follows:
epoch: 10000, acc: 0.947, loss: 0.217 (data_loss: 0.157, reg_loss: 0.060), lr: 0.019900507413187767
validation, acc: 0.830, loss: 0.435
When I run this file on my machine the output of some values are slightly different:
epoch: 10000, acc: 0.947, loss: 0.217 (data_loss: 0.160, reg_loss: 0.057), lr: 0.019900507413187767
validation, acc: 0.837, loss: 0.509
I am running this in a Docker container with a requirements.txt file ensuring the versions align with those documented in the book:
Python 3.7.5
NumPy 1.15.0
Matplotlib 3.1.1
Is the output documented correctly?
Ch6_final.py imports spiral_data but calls vertical_data()
from nnfs.datasets import vertical_data
I just started chapter 3 and the code will not execute because it can not find nnfs.datasets. I have globally installed the nnfs library using pip3.
It seems that you've mentioned Matplotlib in some chapters but haven't seen you make use of it. Maybe it will be better to understand the learning process if you could add something to visualize the learning process live like you showed in the video. (Sorry for my poor English)
just need to put one together.
In the book p 251, 271 & 276, learning_rate begins with a capitol letter "L" in the class definition, but it doesn't in this code and it shouldn't.
def init(self, learning_rate=1., decay=0., momentum=0.):
self.learning_rate = learning_rate
Need to include this. A design decision of how..
Hi in our final code we calculated Regularization Loss and then sum it with
data_Loss. Now the problem is I think we didn't add our total Loss as a final output of the last layer and then multiply partial gradients with it and instead what we did is just propagated Loss like when we don't have regularization loss.
(In summary, we didn't add the effect of Regularization Loss in forward pass phase).
And I think we should use this
loss_activation.backward(Loss, y)
instead of
loss_activation.backward(loss_activation.output, y).
Would be glad to receive your opinion.
Best Regards
Thank for the great book, just a minor, minor remark..
inputs = [[1, 2, 3, 2.5],
[2., 5., -1., 2], <----- formatted with zeroes
[-1.5, 2.7, 3.3, -0.8]]
inputs = [..
[2.0, 5.0, -1.0, 2.0],
[..]]
just need to put one together
In chapter 10 after the square root example the text is written as
Overall, the impact is the learning rates for parameters with smaller gradients are decreased slowly, while the parameters with larger gradients have their learning rates decreased faster
I am confused over this, we are not updating learning rate anywhere (other than rate decay). Yes the weights will be updated faster for parameters with bigger gradients but much slower than they would have if no normalization is used.
Am I correct or am I understanding it incorrectly.
In Chapter 18, we have the train
method as:
def train(
self,
X: np.ndarray,
y: np.ndarray,
*,
validation: tuple[np.ndarray, np.ndarray] = None,
epochs: int = 100,
print_every: int = 10,
):
self.accuracy.init(y)
for epoch in range(1, 1 + epochs):
output = self.forward(X)
data_loss, reg_loss = self.loss.calculate(output, y)
loss = data_loss + reg_loss
predictions = self.output_layer.predict(output)
acc = self.accuracy.calculate(predictions, y)
self.backward(output, y)
self.optimizer.pre_update_params()
self.optimizer.update()
self.optimizer.post_update_params()
if not epoch % print_every:
print(
f"epoch: {epoch}, "
+ f"acc: {acc:.3f}, "
+ f"loss: {loss:.3f}, "
+ f"data_loss: {data_loss:.3f}, "
+ f"reg_loss: {reg_loss:.3f}, "
+ f"lr: {self.optimizer.curr_lr}"
)
if validation:
x_val, y_val = validation
output = self.forward(x_val, training=False)
loss = self.loss.calculate(output, y_val, include_regularization=False)
predictions = self.output_layer.predict(output)
accuracy = self.accuracy.calculate(predictions, y_val)
print(f"Validation, acc: {accuracy:.3f}, loss: {loss:.3f}")
The if validation
clause is aligned with the outer for
loop, which is understandable since we only need to validate the data once after the training process.
In chapter 19 sample code, we have:
# Main training loop
for epoch in range(1, epochs+1):
# Print epoch number
print(f'epoch: {epoch}')
# Reset accumulated values in loss and accuracy objects
self.loss.new_pass()
self.accuracy.new_pass()
# Iterate over steps
for step in range(train_steps):
# If batch size is not set -
# train using one step and full dataset
if batch_size is None:
batch_X = X
batch_y = y
# Otherwise slice a batch
else:
batch_X = X[step*batch_size:(step+1)*batch_size]
batch_y = y[step*batch_size:(step+1)*batch_size]
# Perform the forward pass
output = self.forward(batch_X, training=True)
# Calculate loss
data_loss, regularization_loss = \
self.loss.calculate(output, batch_y,
include_regularization=True)
loss = data_loss + regularization_loss
# Get predictions and calculate an accuracy
predictions = self.output_layer_activation.predictions(
output)
accuracy = self.accuracy.calculate(predictions,
batch_y)
# Perform backward pass
self.backward(output, batch_y)
# Optimize (update parameters)
self.optimizer.pre_update_params()
for layer in self.trainable_layers:
self.optimizer.update_params(layer)
self.optimizer.post_update_params()
# Print a summary
if not step % print_every or step == train_steps - 1:
print(f'step: {step}, ' +
f'acc: {accuracy:.3f}, ' +
f'loss: {loss:.3f} (' +
f'data_loss: {data_loss:.3f}, ' +
f'reg_loss: {regularization_loss:.3f}), ' +
f'lr: {self.optimizer.current_learning_rate}')
# Get and print epoch loss and accuracy
epoch_data_loss, epoch_regularization_loss = \
self.loss.calculate_accumulated(
include_regularization=True)
epoch_loss = epoch_data_loss + epoch_regularization_loss
epoch_accuracy = self.accuracy.calculate_accumulated()
print(f'training, ' +
f'acc: {epoch_accuracy:.3f}, ' +
f'loss: {epoch_loss:.3f} (' +
f'data_loss: {epoch_data_loss:.3f}, ' +
f'reg_loss: {epoch_regularization_loss:.3f}), ' +
f'lr: {self.optimizer.current_learning_rate}')
# If there is the validation data
if validation_data is not None:
# Reset accumulated values in loss
# and accuracy objects
self.loss.new_pass()
self.accuracy.new_pass()
# Iterate over steps
for step in range(validation_steps):
# If batch size is not set -
# train using one step and full dataset
if batch_size is None:
batch_X = X_val
batch_y = y_val
# Otherwise slice a batch
else:
batch_X = X_val[
step*batch_size:(step+1)*batch_size
]
batch_y = y_val[
step*batch_size:(step+1)*batch_size
]
# Perform the forward pass
output = self.forward(batch_X, training=False)
# Calculate the loss
self.loss.calculate(output, batch_y)
# Get predictions and calculate an accuracy
predictions = self.output_layer_activation.predictions(
output)
self.accuracy.calculate(predictions, batch_y)
# Get and print validation loss and accuracy
validation_loss = self.loss.calculate_accumulated()
validation_accuracy = self.accuracy.calculate_accumulated()
# Print a summary
print(f'validation, ' +
f'acc: {validation_accuracy:.3f}, ' +
f'loss: {validation_loss:.3f}')
If I was correct on
we only need to validate the data once after the training process
then why should we validate the test data through the epochs?
What if I want to load my own data set?
Using version 1.21.0 of numpy the outputs do not match the outputs from the book. I was able to rectify this issue by finding the version you used the first video on the companion series on your Youtube channel version 1.18.2. I am not sure if you would want to add a comment on the readme, update the outputs, or update the nnfs package to work with the newer version. Adding it to the readme would be the least maintenance in the future.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.