neural-network-from-scratch | A Neural Network implemented from scratch | Machine Learning library
kandi X-RAY | neural-network-from-scratch Summary
kandi X-RAY | neural-network-from-scratch Summary
This was written for my blog post Machine Learning for Beginners: An Introduction to Neural Networks.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Train the model
- Derivative of sigmoid
- Sigmoid activation function
- Mean squared error
- Generate a feedforward function
neural-network-from-scratch Key Features
neural-network-from-scratch Examples and Code Snippets
Community Discussions
Trending Discussions on neural-network-from-scratch
QUESTION
I have trained a simple NN by modifying the following code
https://www.kaggle.com/ancientaxe/simple-neural-network-from-scratch-in-python
I would now like to test it on another sample dataset. how should i proceed with it ?
...ANSWER
Answered 2020-Aug-25 at 11:10I see you use a model from scratch. In this case, you should run this code, as indicated in the notebook, after setting your X
and y
for your new test set. For more information, see the the notebook as I did not put here everything:
QUESTION
When using the chain rule to calculate the slope of the cost function relative to the weights at the layer L
, the formula becomes:
d C0 / d W(L) = ... . d a(L) / d z(L) . ...
With :
z (L)
being the induced local field :z (L) = w1(L) * a1(L-1) + w2(L) * a2(L-1) * ...
a (L)
beeing the ouput :a (L) = & (z (L))
&
being the sigmoid function used as an activation function
Note that L
is taken as a layer indicator and not as an index
Now:
d a(L) / d z(L) = &' ( z(L) )
With &'
being the derivative of the sigmoid function
The problem:
But in this post which is written by James Loy on building a simple neural network from scratch with python,
When doing the backpropagation, he didn't give z (L)
as an input to &'
to replace d a(L) / d z(L)
in the chain rule function. Instead he gave it the output = last activation of the layer (L)
as the input the the sigmoid derivative &'
...
ANSWER
Answered 2020-Jun-21 at 23:42You want to use the derivative with respect to the output. During backpropagation we use the weights only to determine how much of the error belongs to each one of the weights and by doing so we can further propagate the error back through the layers.
In the tutorial, the sigmoid is applied to the last layer:
QUESTION
I am reading : https://towardsdatascience.com/how-to-build-your-own-neural-network-from-scratch-in-python-68998a08e4f6
I saw following code:
...ANSWER
Answered 2020-May-06 at 22:04Your input matrix X
suggests that number of samples is 4 and the number of features is 3. The number of neurons in the input layer of a neural network is equal to the number of features*, not number of samples. For example, consider that you have 4 cars and you chose 3 features for each of them: color, number of seats and origin country. For each car sample, you feed these 3 features to the network and train your model. Even if you have 4000 samples, the number of input neurons do not change; it's 3.
So self.weights1
is of shape (3, 4)
where 3 is number of features and 4 is the number of hidden neurons (this 4 has nothing to do with the number of samples), as expected.
*: Sometimes the inputs are augmented by 1
(or -1
) to account for bias, so number of input neurons would be num_features + 1
in that case; but it's a choice of whether to deal with bias seperately or not.
QUESTION
I have been trying to make my own neural networks from scratch. After some time, I made it, but I run into a problem I cannot solve. I have been following a tutorial which shows how to do this. The problem I run into, was how my network updates weights and biases. Well, I know that gradient descent won't be always decreasing loss and for a few epochs it might even increase a bit, bit it still should decrease and work much better than mine does. Sometimes the whole process gets stuck on loss 9 and 13 and it cannot get out of it. I have checked many tutorials, videos and websites, but I couldn't find anything wrong in my code.
self.activate
, self.dactivate
, self.loss
and self.dloss
:
ANSWER
Answered 2020-Mar-05 at 04:32This can be caused by your training data. Either it is too small or too many diverse labels (What i get from your code from the link you share).
I re-run your code several times and it produce different training performance. Sometimes the loss keeps decreasing until last epoch, some times it keep increasing, in one time it decreased until some point and it increasing. (With minimum loss achieved of 0.5)
I think it is your training data that matters this time. The learning rate is good enough though (Assuming you did the calculation for Linear combination, back propagation, etc right).
QUESTION
I've been following a tutorial on applying CNN to classify the MNIST handwritten numbers dataset.
I'm just a bit confused on one point in K-fold Cross-validation. The author of this tutorial mentions in another tutorial that the model should be discarded each time the folds are swapped around. So to quote it:
- Shuffle the dataset randomly.
- Split the dataset into k groups
- For each unique group:
- Take the group as a hold out or test data set
- Take the remaining groups as a training data set
- Fit a model on the training set and evaluate it on the test set
- Retain the evaluation score and discard the model
- Summarize the skill of the model using the sample of model evaluation scores
Although in the CNN tutorial this is how the author applies K-fold validation:
...ANSWER
Answered 2019-Dec-28 at 19:33You and the author should use and should have been used keras.backend.clear_session()
to fit your new model from scratch with the new split of your data set, but the author just wrote about the crossvalidation in general and he/she wrote:
The models are then discarded after they are evaluated as they have served their purpose.
So how do should you do it in your case:
QUESTION
The following code that I pulled from here in an effort to better understand how Machine Learning and Neural Networks work, isn't working. It keeps producing an "invalid syntax" error at line 31:
...ANSWER
Answered 2019-Mar-21 at 20:15You have forgotten to put a brackets at the end of line 4
QUESTION
Trying to reproduce a loss function from an example found here. The provided code doesn't show how to plot from a looped class. Here is the original code:
...ANSWER
Answered 2019-Feb-05 at 22:04Instead of using global variable in your class, you can return the loss instead
QUESTION
So I’m trying to program a basic Artificial Neural Network using Excel VBA. I’ve been following one example in particular:
https://www.analyticsvidhya.com/blog/2017/05/neural-network-from-scratch-in-python-and-r/
I’ve been basing the code off of the Python example in the article (code example is toward the bottom of the article). I don’t have access to Python unfortunately so I’m trying to do this with VBA. I’ve done my best to convert the code into a useable format for Excel, however there is an issue I'm encountering and I'm not sure how to solve it:
Here is my VBA code:
...ANSWER
Answered 2018-Nov-27 at 12:39Unfortunately VBA does not support direct operations on arrays. Instead you have to loop :(
If you really want to do it with VBA you could have a look at this CodeReview post.
If you want to do it using Excel, but cannot accpet the limitations of VBA, there are a few Python tools allowing to program Excel using Python. A quick search will get you plenty, including https://www.xlwings.org/ which has been on my list of things to try for much too long.
QUESTION
I'm watching this series https://www.youtube.com/watch?v=wlnx-7cm4Gg&list=PL5tcWHG-UPH2zBfOz40HSzcGUPAVOOnu1 which is about mining tweets with tweepy (python) and the guy stores the tweets with everything ( such as created_at, id, id_str, text) and then he uses Dataframes in pandas to store only the text. Is this way efficient ? How Can I only store the "text" in the Json file instead of all other details ?
The code:
...ANSWER
Answered 2018-Nov-10 at 17:39It looks like what you're getting from the API and storing in your variable "data" is unicode text in a json format. You are just writing that text directly to a file. Using the API call you do, you're always going to get all of the data so it isn't that inefficient. If you just wanted to get/write the text of the tweet, try using a json load and then processing from there.
QUESTION
I'm a computer science teacher currently building an introductory course on deep learning. Python and the Keras framework are my tools of choice.
I'd like to show my students what is overfitting by training a model of increasing complexity on some predefined 2D data, just like at the end of this example.
The same idea appears in a programming activity for Andrew Ng's course on neural networks tuning.
However, no matter how hard I try, I can't replicate this behavior with Keras. Using the same dataset and hyperparameters, the decision boundaries are always "smoother" and the model never fits the noisy points in the dataset. See my results below and click here to browse the associated code. Here's the relevant extract:
...ANSWER
Answered 2018-Mar-09 at 21:07Your problem is all of your examples are a one-layer neural network with different size! If you print the weights, you'll notice after when you increase size of layer(for example from 5 to 50) the other neurons (45 neurons in example) will have weights near to zero so they are the same.
You have increase depth of your neural network to see the overfitting. For example I changed your code in a way that the first two examples are single-layer NN and the third one([30, 30, 30, 30]) is a four layer NN (the full source code is here):
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install neural-network-from-scratch
You can use neural-network-from-scratch like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page