简体   繁体   中英

Numpy.dot gives same shapes (X, Y) (X,Y) not aligned

I am trying to create a dynamically allocated input neuron, to pass a numpy.array as input for neuron's synapses in my training model.

The main function gets the numpy.array that will give its .shape to the neuron constructor, in order to create a numpy.array for the firstly generated biased synapses.

The neuron uses the numpy.dot operation for the two arrays that are then being passed in to the sigmoid function.

However I get the shapes (6,3) and (6,3) not aligned: 3 (dim 1) != 6 (dim 0) dot value error , but to me the two shapes should be the same.

What am I not getting about the numpy array shapes form?

main.py:

# Training data for the small neuron
training_inputs = np.array([[1.7, 3.4, 5.0],
                            [1.95, 3.2, 3.6],
                            [2.38, 3.3, 2.75],
                            [1.44, 3.75, 7.5],
                            [1.54, 3.84, 7.0],
                            [1.54, 3.82, 6.9]])

neural_network = NeuralNetwork.NeuralNetwork(training_inputs.shape)

training_outputs = np.array([[0.5, 1, 0, 1, 1, 1]]).T

print("Synaptic Weights BEFORE training (random) for SMALL ")
print(neural_network.synaptic_weights)

neural_network.train(training_inputs, training_outputs, 10)
# Our model is now trained
print("Synaptic Weights AFTER Training for SMALL: ")
print(neural_network.synaptic_weights)
# User needs to input the testing values

user_input_1 = str(input("User Input 1: "))
user_input_2 = str(input("User Input 2: "))
user_input_3 = str(input("User Input 3: "))
# Neuron is now calculating

print("Neuron is calculating result for given input: ", user_input_1, user_input_2, user_input_3)
print("Trained output data: ")
print(neural_network.think(np.array([user_input_1, user_input_2, user_input_3])))

NeuralNetwork.py:

import numpy as np


# Class for our Neurons
class NeuralNetwork():

def __init__(self, shape):

    # Seeds random number generator that will be our bias
    np.random.seed(1)
    # Converting weights for our sensors to a 3 by 1 matrix
    # This bias needs to be fixed
    self.synaptic_weights = 2 * np.random.random(shape) - 1

# The sigmoid function that will have values from -1 to +1

# The sigmoid: f(x) = 1/[1 + e^(-x)]
@staticmethod
def sigmoid(x):
    # applying the sigmoid function
    return 1 / (1 + np.exp(-x))

# The derivative of the sigmoid: f'(x) = x*(1-x)
@staticmethod
def sigmoid_derivative(x):
    # computing derivative to the Sigmoid function
    return x * (1 - x)

# The training procedure of the neuron
def train(self, training_inputs, training_outputs, training_iterations):
    # The neuron is being trained #training_iteration times for the weights to be adjusted
    for iteration in range(training_iterations):

        # The output of the neuron that is being used as an adjustment to the next iteration
        output = self.think(training_inputs)

        # Error is being calculated for the back - propagation
        error = training_outputs - output

        # Adjusting the weights by multiplying the inputs * error * f'(output)
        adjustments = np.dot(training_inputs.T, self.sigmoid_derivative(output))

        # Adjusting the weights
        self.synaptic_weights += adjustments

# The thinking procedure of the neuron
def think(self, inputs):

    # Floating the values
    inputs = inputs.astype(float)

    # Neuron uses the inputs to produce its output
    output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
    return output

Traceback gives this:

> Traceback (most recent call last):   File
> "C:/../football_project/main.py", line 94
> in <module> main() 
> File "C:/../football_project/main.py", line 55
> in main
>     neural_network.train(training_inputs, training_outputs, 1)   
> File "C:\..\NeuralNetwork.py", line 35, in train
>     output = self.think(training_inputs)   File "C:..\football_project\NeuralNetwork.py", line 53, in think
>     output = self.sigmoid(np.dot(inputs, self.synaptic_weights))   
> File "<__array_function__ internals>", line 6, in dot ValueError:
> **shapes (6,3) and (6,3) not aligned: 3 (dim 1) != 6 (dim 0)**

Dot operation is the matrix multiplication, so A dot B = C, where dim (A) = (n, m), dim (B) = (m, k) and dim (C) = (n, k). If I understand your concept (neurons with 3 inputs and 1 output), you need to multiply the input vector (1, 3) and the weight matrix (3, 1) and repeat for all inputs.

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