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

神经网络无论输入什么均输出相同结果的问题求助

舞蹈姿势分类神经网络问题

我正在编写一个用于特定舞蹈姿势分类的神经网络:

  • 输入是33个点的x、y坐标,对应输入层66个神经元
  • 输出层设置为3个神经元,对应3种舞蹈姿势(Neutral、Disco 1、Disco 2)

最初我用了1个含20个神经元的隐藏层,之后改成2个各含40个神经元的隐藏层,但这只是无依据的测试——我不知道该如何确定合适的隐藏层层数和神经元数量。尝试这个调整后没有效果,我猜测问题根源是输入值过大,经过sigmoid激活函数处理后输出值变得十分相似。

这个神经网络是基于YouTube上手写数字分类的教程构建的,我完全理解每一行代码。目前仅尝试过调整隐藏层的层数和神经元数量,但未观察到明显变化。

完整代码

import os.path
import numpy as np


class NeuralNetwork:
    def __init__(self, learn_rate):
        self.dances = ["Neutral", "Disco 1", "Disco 2"]
        self.lr = learn_rate
        self.correct = 0
        self.accuracy = 0

        self.total = 10

        if not os.path.isfile("data/weights_i_h1.npy"):
            self.w_i_h1, self.w_h1_h2, self.w_h2_o, self.b_i_h1, self.b_h1_h2, self.b_h2_o = self.initialize_values()

        else:

            self.w_i_h1 = np.load("data/weights_i_h1.npy")
            self.w_h1_h2 = np.load("data/weights_h1_h2.npy")
            self.w_h2_o = np.load("data/weights_h2_o.npy")
            self.b_i_h1 = np.load("data/biases_i_h1.npy")
            self.b_h1_h2 = np.load("data/biases_h1_h2.npy")
            self.b_h2_o = np.load("data/biases_h2_o.npy")

    def initialize_values(self):
        w_i_h1 = np.random.uniform(-.5, .5, (40, 66))
        w_h2_o = np.random.uniform(-.5, .5, (40, 40))
        w_h1_h2 = np.random.uniform(-.5, .5, (3, 40))
        b_i_h1 = np.zeros((40, 1))
        b_h1_h2 = np.zeros((40, 1))
        b_h2_o = np.zeros((3, 1))

        return w_i_h1, w_h2_o, w_h1_h2, b_i_h1, b_h1_h2, b_h2_o

    def getAccuracy(self):
        accuracy = round((self.correct / (10 * self.epochs)) * 100, 2)
        return accuracy

    """
    labels:
    1: Neutral
    2: Disco 1 (up)
    3: Disco 2 (down)
    """

    def training(self, points, label):
        l = np.array(label)
        l.shape += (1,)
        points = np.array(points)
        points.shape += (1,)

        h1_pre = self.b_i_h1 + self.w_i_h1 @ points
        h1 = 1 / (1 + np.exp(-h1_pre))

        h2_pre = self.b_h1_h2 + self.w_h1_h2 @ h1
        h2 = 1 / (1 + np.exp(-h2_pre))
        # Output layer
        o_pre = self.b_h2_o + self.w_h2_o @ h2
        o = 1 / (1 + np.exp(-o_pre))

        # Cost

        e = 1 / len(o) * np.sum((o - l) ** 2, axis=0)
        self.correct += int(np.argmax(o) == np.argmax(l))

        # Backpropagation
        delta_o = o - l
        self.w_h2_o += -self.lr * delta_o @ np.transpose(h2)
        self.b_h2_o += -self.lr * delta_o

        delta_h2 = np.transpose(self.w_h2_o) @ delta_o * (h2 * (1 - h2))
        self.w_h1_h2 += -self.lr * delta_h2 @ np.transpose(h1)
        self.b_h1_h2 += -self.lr * delta_h2

        delta_h1 = self.w_h1_h2 @ delta_h2 * (h1 * (1 - h1))
        self.w_i_h1 += -self.lr * delta_h1 @ np.transpose(points)
        self.b_i_h1 += -self.lr * delta_h1
        if np.argmax(o) == np.argmax(l):
            print("---------------------\")
            print(self.dances[o.argmax()])
            print(self.correct)
            print("---------------------\")

    def save(self):
        np.save("data/weights_i_h1.npy", self.w_i_h1)
        np.save("data/weights_h1_h2.npy", self.w_h1_h2)
        np.save("data/weights_h2_o.npy", self.w_h2_o)
        np.save("data/biases_i_h1.npy", self.b_i_h1)
        np.save("data/biases_h1_h2.npy", self.b_h1_h2)
        np.save("data/biases_h2_o.npy", self.b_h2_o)

    def classify_input(self, points):
        points = np.array(points)
        points.shape += (1,)
        points = (points - 450) / 248.3277

        h1_pre = self.b_i_h1 + self.w_i_h1 @ points
        h1 = 1 / (1 + np.exp(-h1_pre))

        h2_pre = self.b_h1_h2 + self.w_h1_h2 @ h1
        h2 = 1 / (1 + np.exp(-h2_pre))
        # Output layer
        o_pre = self.b_h2_o + self.w_h2_o @ h2
        o = 1 / (1 + np.exp(-o_pre))
        print(self.dances[o.argmax()])

        """
        print("--------------------------------\")
        print("Input:", points)
        print("H1:", h1)
        print("H2:", h2)
        print("o:", o)
        print("--------------------------------\")"""

        return o.argmax()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 03:37:08