神经网络代码中的数组越界异常

本文关键字:越界 异常 数组 代码 神经网络 | 更新日期: 2023-09-27 18:06:51

我知道已经有很多这样的问题被问到,但我的问题是特定于代码的。由于这个原因,我不知道如何使它与大多数人相关。

我正在练习迭代,为创建我的第一个神经网络做准备(试图理解我可以编写的可能结构)。该程序旨在以类似于神经网络的方式遍历和分配权重。这并不使用数学,它仅用于迭代目的。因此,如果有任何可能的建议/建议,如何最好地写它,我将不胜感激。

主要问题:

我能看到的第一个越界。然而,我看不见或不明白我错在哪里。

using System;
namespace Layers
{
    class Program
    {
        private static Random Rand = new Random();
        public static void Main(string[] args)
        {
            // NOTE: The first hidden layer (0) will be the input layer.
            // Initialize Layers.
            Console.WriteLine("How many layers?");
            int[][] HiddenLayers = new int[Convert.ToInt32(Console.ReadLine())][];
            for (int HLCount = 0; HLCount < HiddenLayers.Length; HLCount++)
            {
                // Make the first layer the input.
                if (HLCount == 0)
                {
                    Console.WriteLine("How many inputs?");
                } else
                {
                    Console.WriteLine("How many cells in Hidden Layer " + HLCount + "?");
                }
                HiddenLayers[HLCount] = new int[Convert.ToInt32(Console.ReadLine())];
            }
            // Set the values for the inputs.
            for (int InputCount = 0; InputCount < HiddenLayers[0].Length; InputCount++)
            {
                Console.WriteLine("Enter the value for input " + (InputCount + 1) + ":");
                HiddenLayers[0][InputCount] = Convert.ToInt32(Console.ReadLine());
            }
            // Initialize the weights.
            int[][] Weights = new int[HiddenLayers.Length][]; // Set of weights for each layer.
            for (int WeightCount = 0; WeightCount < Weights.Length; WeightCount++)
            {
                try
                {
                    // Create weights for the layers underneath. +1 attaches it to the layer below but would exceed the array.
                    Weights[WeightCount] = new int[HiddenLayers[WeightCount +1].Length]; 
                } catch (Exception ex)
                {
                }
            }
            for (int inputCount = 0; inputCount < Weights[0].Length; inputCount++)
            {
                Weights[0][inputCount] = Rand.Next(10); // Set first layers weights.
            }
            int intCount = 0;
            for (int LayerIndex = 1; LayerIndex < HiddenLayers.Length; LayerIndex++)
            {
                // Re-calculate weights. Go through layer and change weights.
                if (intCount < Weights[LayerIndex-1].Length)
                {
                    Weights[LayerIndex][intCount] = Rand.Next(1, 10);
                    intCount++;
                }
                Console.WriteLine("Layer: " + Convert.ToString(LayerIndex));
                // Go through the cells on the Layer.
                for (int CellIndex = 0; CellIndex < HiddenLayers[LayerIndex].Length; CellIndex++)
                {
                    // Out of bounds exception. Catching it affects the overall performance of the outcome.
                    HiddenLayers[LayerIndex][CellIndex] += (Convert.ToInt32(HiddenLayers[LayerIndex - 1][CellIndex]) + Convert.ToInt32(Weights[LayerIndex][CellIndex]));
                }
            }
            Console.ReadLine();
        }
        static void getWeights()
        {
        }
    }
}

这里有一些关于我想做什么的更多信息。通过一些修改,例如用户可以确定图层大小,输入等

https://i.stack.imgur.com/x9cN3.png

我使用的输入是:3层3输入隐藏层1:3隐藏层2:3输入1:12输入2:13

神经网络代码中的数组越界异常

最重要的:永远不要使用try/catch来"解决"IndexOutOfRangeException问题。就此而言,不要使用它来解决任何您不希望得到的异常。这样做肯定永远是错误的。如果您没有预料到可能发生的异常发生了,那么您有一个bug,您需要修复这个bug,而不是试图用try/catch把它掩盖起来。

至于问题的其余部分& help;


当我运行代码时,由于未初始化的数组成员(因为您捕获了先前的异常而不是修复代码),我得到了NullReferenceException。这发生在代码到达您声称发生IndexOutOfRangeException的行之前。

看看你所询问的程序语句,我看不出它应该抛出异常的理由。但是很难确定,因为代码的编写方式不可能重现您所描述的问题。

请注意,因为您强制我们输入数据来运行程序,我们无法知道我们是否使用与您相同的数据运行。因此,您没有提供一个良好的、最小的、完整的代码示例来可靠地再现问题。

当我修改代码使第一个计算循环看起来像这样时:

for (int WeightCount = 0; WeightCount < Weights.Length; WeightCount++)
{
    // Create weights for the layers underneath (caught out of bounds exception which doesn't seem to matter).
    Weights[WeightCount] = new int[HiddenLayers[WeightCount].Length];
}

& helplip;我能够重现的两个异常都消失了,代码运行到完成。这是不是你想要的,我不知道。这似乎是一件合理的事情,但如果没有更多关于你试图实现的实际算法的细节,就不可能确切地知道。


如果以上似乎没有以有用的方式解决你的问题,请改进问题,以便a)你包括一个好的代码示例,b)清楚你在问什么。