0

I'm developing a model based on neural network principals. I have an entry layer, weights and an output layer:

[1,2] -- [ [1,1] , [1,1] ] --> [3,3]

Simple Neural Network based on two layers and one weight layer

My question is whether Python has a simple way (with numpy) to compute the output layers without doing loops and loops.

The current implementation is:

for i in range(0,number_of_out_neurons):
    out_neuron_adder_toWrap = weights[i] * all_input_layer
    out_neuron[i] = sum(out_neuron_adder)         <-- wrapping
3
  • 5
    Could you implement the loopy version, so that we would have a better idea on what exactly you have in mind and would additionally helps us cross-check against any vectorized approach that could be suggested? Commented Jun 6, 2016 at 17:40
  • 1
    Considering this is just an addition of each input multiplied by each weight, you can just do a dot-product. But this will only work if the input dimensions work with the weight dimensions Commented Jun 6, 2016 at 17:50
  • Could you also add their shape information (shapes of input arrays)? Would be best to add a minimal representative sample case with numeric data and the final output. Commented Jun 6, 2016 at 17:50

1 Answer 1

2

You can implement this with numpy.dot

In [1]: import numpy as np
In [2]: a
Out[2]: array([1, 2])
In [3]: b
Out[3]: 
array([[1, 1],
       [1, 1]])
In [4]: np.dot(a,b)
Out[4]: array([3, 3])

Here is more Reference about numpy.dot

Sign up to request clarification or add additional context in comments.

7 Comments

I was thinking the same thing. The only issue is if @hugodecasta has a weight of unequal dimensions, i.e. he has 3 outputs and 2 inputs
For same input/output shapes it work but for like 3/2 it fail as expected but i thinck it should be easy to fix that
The rule already implemented in numpy.dot. We don't want to worry about that.
Yes but the problem is still here [1,2,3] DOT [ [1,1,1], [1,1,1] ] Does not wrok... because the two vectors are not aligned
My miss sory Instead of using [1,2,3] DOT [ [1,1,1], [1,1,1] ] we have to use [ [1,1,1], [1,1,1] ] DOT [1,2,3]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.