【发布时间】:2021-07-18 09:16:41
【问题描述】:
我正在尝试创建一个动态分配的输入神经元,在我的训练模型中传递一个 numpy.array 作为神经元突触的输入。
主函数获取numpy.array,它将其.shape 提供给神经元构造函数,以便为第一个生成的偏向突触创建numpy.array。
神经元对两个数组使用 numpy.dot 操作,然后将它们传递给 sigmoid 函数。
但是我得到了shapes (6,3) and (6,3) not aligned: 3 (dim 1) != 6 (dim 0) 点值错误,但对我来说这两个形状应该是相同的。
关于 numpy 数组形状表单我没有得到什么?
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])))
神经网络.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)**
【问题讨论】:
-
你需要 (6,3) 和 (3,6)。
标签: python numpy neural-network