2
$\begingroup$

Given a neural network with 3 inputs, 4 hidden layers, and 1 output, should the output neuron be a vector or a scalar? I thought that at the end of the summation only one number between 0 and 1 would be left over for each neuron in the last output layer but my program returns a 1x4 vector.

class Network:
    def __init__(self, inputs, layers):
        # each neuron has n weights attached
        # each neuron has a bias
        self.weights = []
        self.biases = [np.random.randn(y,1) for y in layers[1:]]
        # did this to connect neurons excluding input layer and last neuron
        # because last neuron isn't connecting anything in front
        for x,y in zip(layers[:-1], layers[1:]):
            self.weights.append(np.random.randn(y,x))

    # add bias vector to previous vector matrix product
    def forward(self, inputs):
        for b,w in zip(self.biases, self.weights):
            inputs = sigmoid(np.dot(w, inputs) + b)
            print("layer",inputs)
        self.output = inputs
        print("output",self.output)


input = [random.randrange(256),random.randrange(256),
                random.randrange(256)]

net = Network(input, [3,4,4,1])
net.forward(input)

Output

layer [[7.80671510e-176 2.86955013e-159 1.00000000e+000 9.10681919e-004]
 [8.91574010e-176 3.27719954e-159 1.00000000e+000 1.03991921e-003]
 [3.44313504e-176 1.26560896e-159 1.00000000e+000 4.01858875e-004]
 [1.43702955e-175 5.28215553e-159 1.00000000e+000 1.67506508e-003]]
layer [[0.79051612 0.79051612 0.91469971 0.79084515]
 [0.80073915 0.80073915 0.70932703 0.80058277]
 [0.35187686 0.35187686 0.00982275 0.35113385]
 [0.51881022 0.51881022 0.55334083 0.5188783 ]]
layer [[0.26291072 0.26291072 0.16592316 0.26264314]]
output [[0.26291072 0.26291072 0.16592316 0.26264314]]

If my output is supposed to decide whether the neuron fires, how could I use the vector to determine this?

$\endgroup$
0
$\begingroup$

Look, your code says your network has many outputs. Look at the two lines below. This two lines says the output depends on the dimension of np.dot(w, inputs). In your case it's 4 diminutional vector. And in the last line you are assigning them as output. You can write self.output = sigmoid(np.dot(new_weihts, inputs)) instead of self.output = inputs. Must ensure that new_weights is a vector and has same shape as previous layer's output.

inputs = sigmoid(np.dot(w, inputs) + b)
self.output = inputs

Note: Your network don't look like a 4 layers network. It looks like a one layer network with 4 unit of neutrons.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Why would I create a variable new_weights if w is the new weight from the loop? $\endgroup$ – yemi.JUMP Mar 20 at 1:57
  • $\begingroup$ You can use a loop. If you can mange to do it in a very complicated way. Developing NN from scratch is a huge work. I would suggest you to follow some established tools. Tensorflow or Keras or Pytorch $\endgroup$ – Ta_Req Mar 20 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.