简体   繁体   English

ValueError:形状(50,50)和(3,1)不对齐:50(dim 1)!= 3(dim 0)

[英]ValueError: shapes (50,50) and (3,1) not aligned: 50 (dim 1) != 3 (dim 0)

I'm trying to create my second machinelearning, but i'm currently stock in a really annoying problem. 我正在尝试创建第二个机器学习,但是目前我遇到了一个非常烦人的问题。 I keep getting this error: 我不断收到此错误:

ValueError: shapes (50,50) and (3,1) not aligned: 50 (dim 1) != 3 (dim 0) ValueError:形状(50,50)和(3,1)不对齐:50(dim 1)!= 3(dim 0)

How can i fix the error? 我该如何解决错误?

My neural network 我的神经网络

class neural_net:

def __init__(self):
    self.weight = 2 * np.random.random((3, 1)) - 1

def sigmoid(self, x):
    return 1/(1 + np.exp(-x))

def sigmoid_derivative(self, x):
    return x * (1 - x)

def train(self, training_input, training_output, iteration):
    for _ in range(iteration):
        output = self.think(training_input)
        error = training_output - output
        adjust = np.dot(training_input, error * self.sigmoid_derivative(output))
        self.weight += adjust

def think(self, input):
    input = input.astype(float)
    output = self.sigmoid(np.dot(input, self.weight))
    return output

X = train_data[len(train_data) - 1][0] -> [152 147 146 143 150 151 151 150 147 149 156 143 121  69  58 105  70  69

66 145 107 144 154 145 154 160 154 158 159 171 167 164 167 160 123 152 152 144 148 116 148 155 165 162 147 130 137 129 106 93] Y = train_data[len(train_data) - 1][1] 66 145 107 144 154 145 154 154 160 158 158 159 171 167 164 167 160 123 152 152 144 148 116 148 155 155 165 162 147 130 137 129 106 93] Y = train_data [len(train_data)-1] [1]

nn = neural_net()
nn.train(X, Y, 3)

To multiply two matrices the number of columns of the first matrix must be equal to number of rows of the second matrix. 为了将两个矩阵相乘,第一个矩阵的列数必须等于第二个矩阵的行数。 In your case columns of X should be equal to rows of self.weights . 在您的情况下, X列应等于self.weights行。 But the number of columns of X is 50 and the number of rows of self.weights is 3. 但是X的列数是50,而self.weights的行self.weights是3。

While defining weights for your neural network you should always consider the channels of the inputs and outupts. 在为神经网络定义权重时,应始终考虑输入和输出的通道。

If inputs have channels of n and outputs have channels of m the shape of the weights should be (n, m). 如果输入的通道数为n,而输出的通道数为m,则权重的形状应为(n,m)。

Assuming from the way you have accessed X ( train_data[len(train_data) - 1][0] ), X is a single sample of shape (50,50). 假设您访问X的方式( train_data[len(train_data) - 1][0] ),则X是形状为(50,50)的单个样本。 If this is correct then X has to be converted into vector before feeding into neural network(Assuming your network's input layer is fully connected to hidden layer). 如果这是正确的,则必须先将X转换为矢量,然后再输入神经网络(假设您网络的输入层已完全连接到隐藏层)。 The same goes to Y. Y也是如此。

X = X.reshape(1, -1) # X.shape == (1, 2500) 
Y = Y.reshape(1, -1) # Y.shape == (1, 2)

Now the weight's shape should be (2500, 2) 现在重量的形状应该是(2500,2)

self.weight = 2 * np.random.random((2500, 2)) - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 形状 (335476,50) 和 (3,50) 未对齐:50 (dim 1) != 3 (dim 0) - shapes (335476,50) and (3,50) not aligned: 50 (dim 1) != 3 (dim 0) ValueError:形状(4,1)和(3,1)未对齐:1(dim 1)!= 3(dim 0) - ValueError: shapes (4,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0) ValueError:形状 (3,3,1) 和 (3,1) 未对齐:1 (dim 2) != 3 (dim 0) - ValueError: shapes (3,3,1) and (3,1) not aligned: 1 (dim 2) != 3 (dim 0) 显示 ValueError:形状 (1,2) 和 (3,1) 未对齐:2 (dim 1) != 3 (dim 0) - Showing ValueError: shapes (1,2) and (3,1) not aligned: 2 (dim 1) != 3 (dim 0) 多项式回归:ValueError:形状(88,1)和(3,1)未对齐:1(dim 1)!= 3(dim 0) - Polynomial Regression :ValueError: shapes (88,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0) ValueError: 形状 (2,) 和 (5,) 未对齐:2 (dim 0) != 5 (dim 0) - ValueError: shapes (2,) and (5,) not aligned: 2 (dim 0) != 5 (dim 0) ValueError:形状(3,)和(0,)未对齐:3(dim 0)!= 0(dim 0) - ValueError: shapes (3,) and (0,) not aligned: 3 (dim 0) != 0 (dim 0) ValueError:形状和未对齐:(dim 2)!= 4(dim 0) - ValueError: shapes and not aligned: (dim 2) != 4 (dim 0) ValueError:形状 (3,) 和 (4,) 未对齐:3 (dim 0) != 4 - ValueError: shapes (3,) and (4,) not aligned: 3 (dim 0) != 4 ValueError:形状 (1,6) 和 (5,5) 未对齐:6 (dim 1) != 5 (dim 0) - ValueError: shapes (1,6) and (5,5) not aligned: 6 (dim 1) != 5 (dim 0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM