Live data from Hacker News

Implementing a Neural Network from Scratch in Python

victorzhou.com

61–70 of 104 posts

Re: Implementing a Neural Network from Scratch in Python

#61
post #20

I didn't have time to read all of it, but half way through. So far it has bee a pretty good write up, however, just one question. Can the author or anyone explain how were the amount for values shifted chosen? I.e. Weight (minus 135) and Height (minus 66). Why was -135 and -66 chosen? An explanation would be helpful. Thanks!

"I arbitrarily chose the shift amounts (135 and 66) to make the numbers look nice. Normally, you’d shift by the mean."

I am pretty sure that note wasn't written there before. The author must have updated the article after I posted this question. Though "nice" is still somewhat vague to me. Nice in what way? Smaller numbers are nicer? Negative and positives make it nice?

The second part of the answer is more useful information, "Normally, you’d shift by the mean."

Re: Implementing a Neural Network from Scratch in Python

#62
post #25

> Real neural net code looks nothing like this. This negates a lot of promise of the article. If one understands principles, but needs to know how to apply them, this statement makes the article practically useless. Usually after reading like this goes a jump in explanations, which directs to apply TensorFlow et. al. and even, if you're lucky, explains how to do that - but doesn't explain what it is that TensorFlow d…

What is being meant is that the example is one of "unrolled loops" - that is, to an extent, some of the matrix math has been unrolled in order to show the discrete steps involved in the calculations that would be lost (more opaque) for someone learning.

Real systems like TensorFlow (and Keras, which is a wrapper that simplifies TensorFlow - which is already pretty simple; if you come to love TensorFlow, you will really love Keras - but you can't really appreciate TensorFlow until you understand the basics of what it does - which the posted tutorial tries to show) hide all of these steps behind a variety of API that tries to mimic the conceptual ideas behind neural networks (ie - "layers" of "neurons" with "connections") while hiding the lower-level "complex mathematics".

That's a good thing on one level, because it opens up ANNs for more users. But it's a bad thing at another level, for those who want to know what is going on in those "black boxes", as well as for those who sometimes will stumble into certain issues with their networks, and struggle to understand certain things if they don't understand those underlying systems.

No one implements neural network code the way it is shown in this tutorial for much the same reason most people don't code in assembler: Because there are higher level means to work with the same operations and do it quicker, more efficiently, and more correctly (also, the lower level operations used in such libraries have been rigorously tested to be correct - it's similar to the reasoning on how you don't roll your own crypto, because you will likely get it wrong - even if you are an expert in the domain).

Re: Implementing a Neural Network from Scratch in Python

#63
post #20

I didn't have time to read all of it, but half way through. So far it has bee a pretty good write up, however, just one question. Can the author or anyone explain how were the amount for values shifted chosen? I.e. Weight (minus 135) and Height (minus 66). Why was -135 and -66 chosen? An explanation would be helpful. Thanks!

I think this a form of normalizing a normal distribution into a standard normal distribution ( a normal distribution has two parameters \mu and \sigma which in standard form gives respectively 0 and 1. Any normal distribution is a special case of the standard normal distribution N(0,1)

Re: Implementing a Neural Network from Scratch in Python

#64
This is very similar to Andrew Ng's Deep Learning Specialization course [1]. If you found this blog post enjoyable be sure to check it out. It is a great course and the intuitions behind NNs are explained very clearly.

1. https://www.coursera.org/specializations/deep-learning

Re: Implementing a Neural Network from Scratch in Python

#65

It's fun, but it's cheating a bit if you use numpy. Can someone implement one with all the matrix math from scratch too?

I don't see why using matrix math shouldn't be viewed as a basic primitive. Implementing a matrix library would obscure the point that is being made.

Re: Implementing a Neural Network from Scratch in Python

#66
post #26
post #25

> Real neural net code looks nothing like this. This negates a lot of promise of the article. If one understands principles, but needs to know how to apply them, this statement makes the article practically useless. Usually after reading like this goes a jump in explanations, which directs to apply TensorFlow et. al. and even, if you're lucky, explains how to do that - but doesn't explain what it is that TensorFlow d…

I still think it's a useful exercise for people like me who learn best by implementing a thing. Tensorflow, Pytorch, etc introduce a lot of magic for someone who is completely unfamiliar with the area.

If you want a great introduction, check out Andrew Ng's Coursera course:

https://www.coursera.org/learn/machine-learning

This is the same course that was originally called "ML Class" in 2011 when it was offered via Stanford (it, plus the other course called "AI Class" - helped to launch Coursera and Udacity, respectively). It uses Octave as it's development language to teach the concepts.

Octave has as it's "primitives" the concept of the Vector and Matrix; that is, it is a language designed around the concepts of linear algebra. So, you can easily set a variable to the values that make up a vector, set another to say the identity matrix, multiply them together, and it will return a matrix.

But the course is initially taught showing how to do all of this "by hand" (much like this tutorial) - using for-next loops and such; essentially rolling your own matrix calcs. Then, once you know that, you are introduced into how to use the built-in calcs provided by octave, etc.

It is really a great course, and I encourage you to try it. It opened my eyes and mind to a lot of concepts I had been trying to grasp for a while that just didn't click until I took that course in 2011.

Of course, once it did, I was like "that's it? why couldn't I see that?" - such is the nature of learning I guess!

His method of building up from the basics all the way to a neural network using Octave will be a good foundation for further learning or playing with other tools like TensorFlow; I ended up taking a couple of other later MOOCs offered by Udacity (focused mainly on self-driving vehicle concepts), and ML Class (and what I took of AI Class) really helped to prepare me.

It also showed me things I was deficient in (something I need to correct some day). But that's a different story.

Suffice to say, that course will "unveil the magic"; it was really an amazing course that I am glad I took. Oh - and to show you what you can do with what you learn in that course, a fellow student during that time ended up making this (and the course was not even finished when he did it):

https://blog.davidsingleton.org/nnrccar/

Throughout the course, the ALVINN autonomous vehicle was referenced quite often, and it inspired him to recreate it successfully in miniature. It also serves to show just how far computer technology has come - what used to take a van-load of equipment can now be played around with using a device that fits in your pocket!

Re: Implementing a Neural Network from Scratch in Python

#67

Earlier quoted context omitted.

These are meaningful variable names in the context of a code implementation of a math algorithm. The variables directly map to the algorithmic notation.

And when trying to do algebraic manipulation on paper, having an 8 or 10 letter variable name is incredibly cumbersome. Frankly in the middle of a numerical algorithm it is typically also cumbersome in code to have descriptive variable names for everything. However, mathematical code (especially when written by scientists, etc.) often takes this too far, introducing many 1- or few-letter variable names without enough…

>h = hidden, o = output, y_pred = predicted value of y, etc. are quite clear

  ∀ abbr + to the cogntv load prsn wht mntn in thr hd.

  ∀ -> every
  + -> adds
  wht ->  who has to
  h -> head
Was that easier to read than the following?

  Every abbreviation adds to the cognitive load the person has to maintain in their head.
This isn't code, but the principles of making code (un)readable are exactly the same.

If you see that full-word variable names create too much clutter, it means your code structure is wrong. So you fix it.

You go from this:

    self.w1 = np.random.normal()
    self.w2 = np.random.normal()
    self.w3 = np.random.normal()
    self.w4 = np.random.normal()
    self.w5 = np.random.normal()
    self.w6 = np.random.normal()
To this:

    set_to_random_numbers(self.weights)
After 9 years of maintaining several dozen legacy codebases (which sometimes involved financial math) I am fully convinced that the only people who like terse code are people who write it and never read it after.

Re: Implementing a Neural Network from Scratch in Python

#68

It's fun, but it's cheating a bit if you use numpy. Can someone implement one with all the matrix math from scratch too?

shameless self-promotion: https://github.com/siekmanj/sieknet

Looks great. Even got yourself a genetic algorithm in there, eh? How well does it work?

Re: Implementing a Neural Network from Scratch in Python

#69

how come every information like this, intended for beginners, don't say why you even need NN or when would you even use it.

That is a good question - I will attempt to answer it for you (probably poorly):

First off, for most problems you don't need a neural network - many, many problems can be solved using simple statistical regression techniques - what could be called "classical machine learning".

It's when your data starts to involve more than a few variables (usually more than 2 or 3) that you need an answer, and/or when you don't know exactly what the best "classical" machine learning technique to use, is when you may find a neural network to be a potential solution.

Ultimately, what a regular neural network does (I'm not going to get into anything like GANs or CNNs or anything special like that - though arguably they all work in a similar manner) is come up with a solution that encapsulates and encodes, through "learning", the proper algorithm(s) needed to provide a solution for the problem - whether that is classification or something else.

It's basically the classic argument that all a neural network is, is a complicated form of linear regression. And to an extent, that is true - but it's done in a "black box" manner, which may or may not be important to you for your problem (the issue becomes "how does it do what it does?" - and that's where things can become tricky to answer).

Neural networks basically take information - various attributes of a problem set - and then, once trained, can output an answer for an example (that it hasn't seen before!) that fits that problem set criteria. It does this by having been "trained" by seeing a lot of different examples, with each example matched up to a "labeled" output. Depending on whether it is right or wrong for the labeled output, it will then propagate the error backwards through the network to correct things by a small amount, and try again (note: your training data needs to be extremely varied, and should include both positive examples and negative examples, and shouldn't be biased toward any particular set of examples).

Anyhow - your data must ultimately be represented by numbers in some manner, usually continuous, and can be anything from a single input to multiple inputs. For instance, you might have the single input of "temperature" to control the output of "turn on/off the furnace" - ie, modeling a thermostat (note that you would never do this actually, outside of learning how a simple perceptron works I guess).

Usually you don't use a single continuous input - you would use multiple inputs - maybe a date, coupled with gps coordinates (like say for housing prices), with an output of "price". Or maybe 10,000 grayscale values (0.00-1.00), representing a 100x100 pixel image, with a classification output of the numbers 0-9 and letters A-Z (36 symbols).

In other words, you are trying to figure out the answer to a problem (is this a picture of a letter or number?) that would be difficult or impossible to code a set of rules by hand to answer with any statistical certainty.

Note that last part: Neural networks do not give you an absolute result; they output a continuous value or a set of values that represent the most probable likelihood of a correct answer based upon what the model has learned in the past. In the case of a single output, that could represent a "yes/no" or "true/false" answer (a value between 1 and 0 respectively); it could also be "left/right", "up/down" or something similar if controlling direction/pressure/flow rate, etc based on inputs.

For classification, it will be a set of continuous values, indicating for each "class" which is the higher probability (0-1) of being correct. For instance, if you had three categories like:

  Monkey: 0.1
  Banana: 0.8
  Coconut: 0.2
Then it is likely that the picture was of a banana. But if you saw this:

  Monkey: 0.7
  Banana: 0.9
  Coconut: 0.1
Maybe the picture contained a monkey holding a banana? It might be possible - even if the network was never trained on that particular imagery!

Note that it is also possible to fool such a network, as numerous studies have shown.

It's also possible to have the network output an image (ie - by having it output a very large array of node values that represents the pixel values of the image); this is how neural networks generate or alter images. Sound data can also be done in a similar manner.

Alright - I think I am rambling a bit, so I'm going to leave it here; I hope this answers at least something of your questions (and I hope it didn't confuse you - if so, I apologize and that wasn't my intent).

Post reply on HN