如何使用梯度下降算法优化权重以使其正常工作?

我在Visual Studio中有一个神经网络。我使用基本的成本函数(pred-target)**2作为损失函数,并且在完成一个epoch后优化参数函数,但算法不起作用。

无论我的网络配置如何,预测结果都不正确(所有输入的输出都是相同的),并且损失函数没有得到优化。它在所有epoch中保持不变。

void calc_lyr(int x, int y, int idx, float target) // 此函数根据前一层计算神经元值{
    if (x == -1 || y == 0) // 如果是第一层,从输入节点获取数据
    {
        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("训练过程开始 ----------------- " + 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("第" + (i + 1).ToString() + "个epoch开始 ---> " + 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("第" + (i+1).ToString() + "个epoch结束 ----- " + DateTime.Now.ToString() + "\n");
        log("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("训练完成!");
    SavePrompt sp = new SavePrompt();
    sp.Show();
}

如何更改以优化权重、偏置和损失函数?目前,当我尝试调试时,权重在变化,但损失函数的值保持不变。


回答:

我通过使用AForge.NET解决了这个问题:链接

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注