简体   繁体   中英

Python NumPy Array and Matrix Multiplication

I'm going through the "Make Your Own Neural Networks" book and following through the examples to implement my first NN. I understood the basic concepts and in particular this equation where the output is calculated doing a matrix dot product of the inputs and weights:

X = W * I

Where X is the output before applying the Sigmoid, W the link weights and I the inputs.

Now in the book, they do have a function that takes in this input as an array and then they translate that array to a 2 dimensional one. My understanding is that, the value of X is calculated like this based on:

W = [0.1, 0.2, 0.3
     0.4, 0.5, 0.6
     0.7, 0.8, 0.9]

I = [1
     2
     3]

So if I now pass in an array for my inputs like [1,2,3], why is that I need do the following to have it converted to a 2-D array as it is done in the book:

inputs = numpy.array(inputs, ndmin=2).T

Any ideas?

Your input here is a one-dimensional list (or a one-dimensional array):

I = [1, 2, 3]

The idea behind this one-dimensional array is the following: if these numbers represent the width in centimetres of a flower petal, its length, and its weight in grams: your flower petal will have a width of 1cm , a length of 2cm , and a weight of 3g .

Converting your input I to a 2-D array is necessary here for two things:

  • first, by default, converting this list to a NumPy array using numpy.array(inputs) will yield an array of shape (3,), with the second dimension left undefined. By setting ndmin=2 , it forces the dimensions to be (3, 1), which allows to not generate any NumPy-related problems, for instance when using matrix multiplication, etc.
  • secondly, and perhaps more importantly, as I said in my comment, data in Neural Networks are conventionally stored in arrays this way, under the idea that each row in your array will represent a different feature (so there is a unique list for each feature). In other words, it's just a conventional way to say your not confusing apples and pears (in that case, length and weight)

So when you do inputs = numpy.array(inputs, ndmin=2).T , you end up with:

array([[1],    # width
       [2],    # length
       [3]])   # weight

and not :

array([1, 2, 3])

Hope it made things a bit clearer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM