You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

神经网络训练报错:shapes (5,6)与(5,6)维度不匹配求助

神经网络训练报错:矩阵维度不匹配

报错信息

ValueError: shapes (5,6) and (5,6) not aligned: 6 (dim 1) != 5 (dim 0)

问题根源

你的输入数据X形状定义反了:

  • 当前X的形状是(5,6),代表5个特征、6个样本,但神经网络输入要求每行对应一个样本,每列对应一个特征,也就是形状应为(6,5)。
  • 在feedForward方法执行np.dot(X, self.W1)时,self.W1的形状是(5,6)(输入特征数→隐藏层节点数),此时X的第二维度(6)和W1的第一维度(5)不匹配,导致矩阵乘法失败。

修复方案

将X转置,使其形状变为(6,5),确保训练时传入转置后的X即可解决维度不兼容问题。

修改后的完整代码

import numpy as np
# X = (Feed rate (fa),Cutting speed (vc),Gangzahl (zo),Heat flux (q),Energy (E)), y = Grinding burn

# 转置X,将形状从(5,6)改为(6,5),每行对应一个样本
X = np.array(([0.100,0.300,0.500,0.100,0.500,0.300], 
              [65.000,65.000,65.000,65.000,65.000,35.000], 
              [1.000,1.000,1.000,3.000,3.000,1.000],
              [1388.830,1279.338,1635.627,1779.128,5905.937,974.872],
              [10.032,13.176,21.846,4.284,26.295,18.646]),dtype=float).T

y = np.array(([1.000], [1.000], [1.000],[1.000],[3.000],[1.000]), dtype=float)

class Training(object):
    def __init__(self):
        # parameters
        self.inputSize = 5
        self.outputSize = 1
        self.hiddenSize = 6
        
        # weights
        self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # weight matrix from input to hidden layer
        self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # weight matrix from hidden to output layer
        
    def feedForward(self, X):
        # forward propogation through the network
        self.z = np.dot(X, self.W1) # dot product of X (input) and first set of weights 
        self.z2 = self.sigmoid(self.z) # activation function
        self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of weights 
        output = self.sigmoid(self.z3)
        return output
        
    def sigmoid(self, s, deriv=False):
        if deriv == True:
            return s * (1 - s) # derivation of sigmoid function
        return 1/(1 + np.exp(-s)) # actual sigmoid function
    
    def backward(self, X, y, output):
        # backward propogation through the network
        self.output_error = y - output # error in output
        self.output_delta = self.output_error * self.sigmoid(output, deriv=True)
        
        self.z2_error = self.output_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contribute to output error
        self.z2_delta = self.z2_error * self.sigmoid(self.z2, deriv=True) # applying derivative of sigmoid to z2 error
        
        self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input -> hidden) weights
        self.W2 += self.z2.T.dot(self.output_delta) # adjusting second set (hidden -> output) weights
        
    def train(self, X, y):
        output = self.feedForward(X)
        self.backward(X, y, output)
        
NN = Training()

for i in range(1000): # trains the NN 1000 times
    if i % 100 == 0:
        print("Loss: " + str(np.mean(np.square(y - NN.feedForward(X)))))
    NN.train(X, y)
        
print("Input: " + str(X))
print("Actual Output: " + str(y))
print("Loss: " + str(np.mean(np.square(y - NN.feedForward(X)))))
print("\n")
print("Predicted Output: " + str(NN.feedForward(X)))

额外说明

转置X后,(6,5)的形状与W1(5,6)进行矩阵乘法会得到(6,6)的隐藏层输入,符合你定义的hiddenSize=6的设置,后续反向传播的矩阵运算维度也会自动匹配。

内容的提问来源于stack exchange,提问作者mmk62

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 08:42:21