double gradCorrect(double g, double eta)
    {
        int nPos = sequence.length();

        int y = sequence.getY(0);
        double[] grad = new double[]{g};
        updateU(grad, 0, y, 1, eta);
        double sum = u[0][y];
        for (int pos = 1; pos < nPos; ++pos)
        {
            int lasty = y;
            y = sequence.getY(pos);
            if (y >= 0 && lasty >= 0)
            {
                sum += b[pos-1][y][lasty];
            }
            if (y >= 0)
            {
                sum += u[pos][y];
            }
            if (y >= 0 && lasty >= 0)
            {
                updateB(grad, pos - 1, lasty, y, 1, eta);
            }
            if (y >= 0)
            {
                updateU(grad, pos, y, 1, eta);
            }
        }

        return sum;
    }