Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用梯度下降算法优化权重才能工作?_C#_Machine Learning_Neural Network_Artificial Intelligence - Fatal编程技术网

C# 如何使用梯度下降算法优化权重才能工作?

C# 如何使用梯度下降算法优化权重才能工作?,c#,machine-learning,neural-network,artificial-intelligence,C#,Machine Learning,Neural Network,Artificial Intelligence,我在visual studio中有一个神经网络。对于损失函数,我使用一个基本成本函数(pred target)**2,在我完成一个历元之后,我优化了参数函数,但算法不起作用 无论我的网络配置是什么,预测都不会被写入(所有输入的输出都是相同的),并且损耗函数也没有得到优化。它在所有时代都保持不变 void calc_lyr(int x, int y, int idx, float target) // thus function calculates the neuron value based

我在visual studio中有一个神经网络。对于损失函数,我使用一个基本成本函数(pred target)**2,在我完成一个历元之后,我优化了参数函数,但算法不起作用

无论我的网络配置是什么,预测都不会被写入(所有输入的输出都是相同的),并且损耗函数也没有得到优化。它在所有时代都保持不变

void calc_lyr(int x, int y, int idx, float target) // thus function calculates the neuron value based on the previous layer
{
    if (x == -1 || y == 0) // if its the first layer, get the data from input nodes
    {
        for (int i = 0; i < neurons[y]; i++)
        {
            float sum = 0;

            for (int j = 0; j < inputTypes.Count; j++)
            {
                sum += weights[x+1][j][i] * training_test[idx][j];
            }
            sum = relu(sum);
            vals[y+1][i] = sum;

        }
    }
    else
    {
        for(int i = 0; i < neurons[y]; i++)
        {
            float sum = 0;

            for(int j = 0; j < neurons[x]; j++)
            {
                sum += weights[x+1][j][i] * vals[x+1][j] + biases[y][i];
            }
            sum = relu(sum);
            vals[y+1][i] = sum;
        }
    }
}

void train()
{
    log("Proces de antrenare inceput ----------------- " + DateTime.Now.ToString());

    vals = new List<List<float>>();
    weights = new List<List<List<float>>>();
    biases = new List<List<float>>();

    Random randB = new Random(DateTime.Now.Millisecond);
    Random randW = new Random(DateTime.Now.Millisecond);

    for (int i = 0; i <= nrLayers; i++)
    {
        progressEpochs.Value =(int)(((float)i * (float)nrLayers) / 100.0f);

        vals.Add(new List<float>());

        weights.Add(new List<List<float>>());
        if (i == 0)
        {
            for (int j = 0; j < inputTypes.Count; j++)
            {
                vals[i].Add(0);
            }
        }
        else
        {
            biases.Add(new List<float>());

            for (int j = 0; j < neurons[i-1]; j++)
            {
                vals[i].Add(0);

                float valB = (float)randB.NextDouble();
                biases[i-1].Add(valB - ((int)valB));
            }
        }
    }

    float valLB = (float)randB.NextDouble();

    biases.Add(new List<float>());
    biases[nrLayers].Add(valLB - ((int)valLB));


    for (int i = 0; i <= nrLayers; i++)
    {
        if (i == 0)
        {
            for (int j = 0; j < inputTypes.Count; j++)
            {
                weights[i].Add(new List<float>());

                for (int x = 0; x < neurons[i]; x++)
                {
                    float valW = (float)randW.NextDouble();
                    weights[i][j].Add(valW);
                }
            }
        }
        else if (i == nrLayers)
        {
            for (int j = 0; j < neurons[i-1]; j++) {
                weights[i].Add(new List<float>());
                weights[i][j].Add(0);
            }
        }
        else
        {
            for (int j = 0; j < neurons[i - 1]; j++)
            {
                weights[i].Add(new List<float>());

                for (int x = 0; x < neurons[i]; x++)
                {
                    float valW = (float)randW.NextDouble();
                    weights[i][j].Add(valW);
                }
            }
        }
    }


    Random rand = new Random(DateTime.Now.Millisecond);
    log("\n\n");
    for (int i = 0; i < epochs; i++)
    {
        log("Epoch " + (i + 1).ToString() + " inceput ---> " + DateTime.Now.ToString());
        int idx = rand.Next() % training_test.Count;

        float target = outputsPossible.IndexOf(training_labels[idx]);

        for (int j = 0; j < nrLayers; j++)
        {
            calc_lyr(j - 1, j, idx, target);
        }

        float total_val = 0;

        for(int x = 0; x < neurons[nrLayers - 1]; x++)
        {
            float val = relu(weights[nrLayers][x][0] * vals[nrLayers][x] + biases[nrLayers][0]);
            total_val += val;
        }

        total_val = sigmoid(total_val);

        float cost_res = cost(total_val, target);

        log("Epoch " + (i+1).ToString() + " terminat ----- " + DateTime.Now.ToString() + "\n");
        log("Eroare epoch ---> " + (cost_res<1?"0":"") + cost_res.ToString("##.##") + "\n\n\n");

        float cost_der = cost_d(total_val, target);

        for (int a = 0; a < weights.Count; a++)
        {
            for (int b = 0; b < weights[a].Count; b++)
            {
                for (int c = 0; c < weights[a][b].Count; c++)
                {
                    weights[a][b][c]-=cost_der*learning_rate * sigmoid_d(weights[a][b][c]);
                }
            }
        }

        for (int a = 0; a < nrLayers; a++)
        {
            for (int b = 0; b < neurons[a]; b++)
            {
                biases[a][b] -= cost_der * learning_rate;
            }
        }

    }
    hasTrained = true;
    testBut.Enabled = hasTrained;
    MessageBox.Show("Antrenament complet!");

    SavePrompt sp = new SavePrompt();
    sp.Show();
}

void calc\u lyr(int x,int y,int idx,float target)//因此函数根据上一层计算神经元值
{
如果(x==-1 | | y==0)//如果是第一层,则从输入节点获取数据
{
对于(int i=0;i<神经元[y];i++)
{
浮点数和=0;
对于(int j=0;j对于(int i=0;i我使用forge.NET解决了它: