private static void TestDense() { Tensor tensor = Tensor.FromArray(Global.Device, new float[] { -1, 2, 3, -4, 5, 6, 7, -8, 9 }); tensor = tensor.Reshape(3, -1); Dense d = new Dense(6, ActivationType.Linear, new Ones(), null, null, true, new Ones()); d.Forward(Variable.Create(tensor)); d.Output.Print(); d.Backward(d.Output); d.Input.Grad.Print(); d.Params["w"].Grad.Print(); d.Params["b"].Grad.Print(); }
public void ExecuteTest() { int inchannels = 4, outchannels = 6, batch = 7; VariableField x = new Tensor(Shape.Map0D(inchannels, batch)); Layer layer = new Dense(inchannels, outchannels, use_bias: true, "fc"); Field y = layer.Forward(x); (Flow flow, Parameters parameters) = Flow.Optimize(y); flow.Execute(); Assert.AreEqual(2, parameters.Count); Assert.AreEqual(inchannels, layer.InChannels); Assert.AreEqual(outchannels, layer.OutChannels); }
public static Field Forward(Field x, int classes) { Convolution2D conv1 = new Convolution2D( inchannels: 1, outchannels: 4, kwidth: 3, kheight: 3, stride: 1, use_bias: true, pad_mode: PaddingMode.Zero, label: "conv1"); Convolution2D conv2 = new Convolution2D( inchannels: 4, outchannels: 8, kwidth: 3, kheight: 3, stride: 1, use_bias: true, pad_mode: PaddingMode.Zero, label: "conv2"); Convolution2D conv3 = new Convolution2D( inchannels: 8, outchannels: 16, kwidth: 3, kheight: 3, stride: 1, use_bias: true, pad_mode: PaddingMode.Zero, label: "conv3"); Field h1 = Relu(conv1.Forward(x)); Field h2 = MaxPooling2D(h1, stride: 2); Field h3 = Relu(conv2.Forward(h2)); Field h4 = MaxPooling2D(h3, stride: 2); Field h5 = Relu(conv3.Forward(h4)); Dense fc = new Dense( inchannels: h5.Shape.DataSize, outchannels: classes, use_bias: true, label: "fc"); Field y = fc.Forward(h5); return(y); }