本文整理汇总了C#中Volume.Set方法的典型用法代码示例。如果您正苦于以下问题:C# Volume.Set方法的具体用法?C# Volume.Set怎么用?C# Volume.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Volume
的用法示例。
在下文中一共展示了Volume.Set方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GradientCheck
public static void GradientCheck(LayerBase layer, int inputWidth, int inputHeight, int inputDepth, double epsilon = 1e-4)
{
layer.Init(inputWidth, inputHeight, inputDepth);
// Forward pass
var input = new Volume(inputWidth, inputHeight, inputDepth);
var output = layer.Forward(input, true);
// Set output gradients to 1
for (var n = 0; n < output.WeightGradients.Length; n++)
{
output.WeightGradients[n] = 1.0;
}
// Backward pass to retrieve gradients
layer.Backward();
var computedGradients = input;
// Now let's approximate gradient using derivate definition
for (var d = 0; d < inputDepth; d++)
{
for (var y = 0; y < inputHeight; y++)
{
for (var x = 0; x < inputWidth; x++)
{
var oldValue = input.Get(x, y, d);
input.Set(x, y, d, oldValue + epsilon);
var output1 = layer.Forward(input);
input.Set(x, y, d, oldValue - epsilon);
var output2 = layer.Forward(input);
input.Set(x, y, d, oldValue);
output1.AddFromScaled(output2, -1.0); // output1 = output1 - output2
var grad = new double[output.WeightGradients.Length];
for (var j = 0; j < output.WeightGradients.Length; j++)
{
grad[j] = output1.Weights[j] / (2.0 * epsilon);
}
var gradient = grad.Sum(); // approximated gradient
var actual = computedGradients.GetGradient(x, y, d);
Assert.AreEqual(gradient, actual, 1e-4); // compare layer gradient to the approximated gradient
}
}
}
}