[英]Numpy.dot gives same shapes (X, Y) (X,Y) not aligned
我正在尝试创建一个动态分配的输入神经元,在我的训练 model 中传递一个 numpy.array 作为神经元突触的输入。
主要的 function 获取numpy.array
,该数组会将其.shape提供给神经元构造函数,以便为第一次生成的偏置创建一个numpy.array
。
神经元对两个 arrays 使用numpy.dot操作,然后将其传递到 sigmoid function。
但是我得到的shapes (6,3) and (6,3) not aligned: 3 (dim 1) != 6 (dim 0)
dot value error ,但对我来说这两个形状应该是相同的。
我对 numpy 阵列形状表格有什么误解?
主要.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])))
神经网络.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 给出了这个:
> 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)**
点运算是矩阵乘法,所以A点B = C,其中dim (A) = (n, m),dim (B) = (m, k),dim (C) = (n, k)。 如果我理解您的概念(具有 3 个输入和 1 个输出的神经元),您需要将输入向量 (1, 3) 和权重矩阵 (3, 1) 相乘,并对所有输入重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.