private void setGradient(DenseTensor M, int i, int r, double coeff, float[] M1, float[] M2) {
		if(KL) {
			setGradientKL(M, i, r, coeff, M1, M2);
			return;
		}

		//double newVal = M.get(i, r) - step_size * coeff * M1.get(r) * M2.get(r);
		float newVal = (float)(M.get(i, r) - step_size * coeff * M1[r] * M2[r]);
		
		if(Float.isNaN(newVal) || Float.isInfinite(newVal)) { 
			System.out.print("newVal NaN: ");
			System.out.println(i + ", " + r + ", " + coeff + ": " + newVal);
		}

		if(sparse) {
			newVal = softThreshold(newVal,lambda * step_size);
		}

		if(nonNegative && newVal < 0) {
			newVal = 0f;
		}

		//System.out.println("Change: " + (step_size * coeff * M1[r] * M2[r]));
		M.set(i, r, newVal);
	}