簡體   English   中英

Numpy.dot 給出相同的形狀 (X, Y) (X,Y) 未對齊

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM