コード例 #1
0
        public void TheoreticalTest()
        {
            int length = 24;

            float[] vval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] tval = (new float[length]).Select((_, idx) => (float)idx / 2).Reverse().ToArray();

            Tensor vtensor = new Tensor(Shape.Vector(length), vval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField v = vtensor;
            VariableField  t = ttensor;

            Field x = TrivectorX(v), y = TrivectorY(v), z = TrivectorZ(v);
            Field norm = x * x + y * y + z * z;
            Field s    = norm / (norm + 1);
            Field u    = TrivectorCast(x * s, y * s, z * s);

            Field err = u - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gv_actual = v.GradTensor.State;

            AssertError.Tolerance(gv_expect, gv_actual, 1e-7f, 1e-5f, $"not equal gv");
        }
コード例 #2
0
        public void ReferenceTest()
        {
            int channels = 7, kwidth = 3, stride = 2, inwidth = 13;
            int outwidth = (inwidth - kwidth) / stride + 1, batch = 2;

            float[] xval = (new float[inwidth * channels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outwidth * channels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * channels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(channels, inwidth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(channels, outwidth, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel1D(channels, 1, kwidth), wval);

            VariableField  x_actual = xtensor;
            ParameterField w        = wtensor;
            ParameterField y        = ytensor;

            Field      x_expect = ChannelwiseDeconvolution1D(y, w, stride, Shape.Map1D(channels, inwidth, batch));
            Field      err      = Abs(x_expect - x_actual);
            OutputNode err_node = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] err_actual = err_node.Tensor.State;
            float[] gy_actual  = y.GradTensor.State;
            float[] gw_actual  = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gy_expect, gy_actual, 1e-7f, 1e-5f, $"not equal gy");
        }
コード例 #3
0
        public void TheoreticalTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 2).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field x_real = ComplexReal(x), x_imag = ComplexImag(x);

            Field y_expect = ComplexCast(Relu(x_real), x_imag);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #4
0
        public void TheoreticalTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] tval = (new float[length]).Select((_, idx) => (float)idx / 2).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField x = xtensor;
            VariableField  t = ttensor;

            Field xr = QuaternionR(x), xi = QuaternionI(x), xj = QuaternionJ(x), xk = QuaternionK(x);
            Field norm = Sqrt(Square(xr) + Square(xi) + Square(xj) + Square(xk));

            Field xy = QuaternionCast(
                xr / norm,
                xi / norm,
                xj / norm,
                xk / norm);
            Field err = xy - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #5
0
        public void ReferenceTest()
        {
            int inchannels = 2, outchannels = 3, width = 7, height = 6, depth = 5, batch = 2;

            float[] xval = (new float[width * height * depth * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[width * height * depth * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[outchannels * inchannels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map3D(inchannels, width, height, depth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map3D(outchannels, width, height, depth, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel0D(inchannels, outchannels), wval);

            ParameterField x        = xtensor;
            ParameterField w        = wtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = PointwiseConvolution3D(x, w);
            Field err      = Abs(y_expect - y_actual);

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #6
0
        public void ReferenceTest()
        {
            int length = 32;

            float[] cval  = (new float[length]).Select((_, idx) => idx % 3 == 0 ? 1f : 0f).ToArray();
            float[] x1val = (new float[length]).Select((_, idx) => (float)idx * 2).ToArray();
            float[] x2val = (new float[length]).Select((_, idx) => (float)idx).Reverse().ToArray();

            float[] yval = (new float[length]).Select((_, idx) => (float)(idx % 13)).ToArray();

            Tensor ctensor  = new Tensor(new Shape(ShapeType.Vector, length), cval);
            Tensor x1tensor = new Tensor(new Shape(ShapeType.Vector, length), x1val);
            Tensor x2tensor = new Tensor(new Shape(ShapeType.Vector, length), x2val);
            Tensor ytensor  = new Tensor(new Shape(ShapeType.Vector, length), yval);

            VariableField  c        = ctensor;
            ParameterField x1       = x1tensor;
            ParameterField x2       = x2tensor;
            VariableField  y_actual = ytensor;

            Field y_expect = Where(c, x1, x2);
            Field err      = Abs(y_expect - y_actual);

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx1_actual = x1.GradTensor.State;

            AssertError.Tolerance(gx1_expect, gx1_actual, 1e-7f, 1e-5f, $"not equal gx1");

            float[] gx2_actual = x2.GradTensor.State;

            AssertError.Tolerance(gx2_expect, gx2_actual, 1e-7f, 1e-5f, $"not equal gx2");
        }
コード例 #7
0
        internal void SetOuterLocalField(ActivationObject parentScope)
        {
            // if we're trying to set the outer local field using a global scope,
            // then ignore this request. This should only do something for scopes with
            // local variables
            if (!(parentScope is GlobalScope))
            {
                // get the field reference for this lookup value
                JSVariableField variableField = parentScope.FindReference(m_name);
                if (variableField != null)
                {
                    // see if this scope already points to this name
                    if (parentScope[m_name] == null)
                    {
                        // create an inner reference so we don't keep walking up the scope chain for this name
                        variableField = parentScope.CreateInnerField(variableField);
                    }

                    // save the local field
                    VariableField = variableField as JSLocalField;
                    // add a reference
                    if (VariableField != null)
                    {
                        VariableField.AddReference(parentScope);
                    }
                }
            }
        }
コード例 #8
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => idx + 0.5f).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = Log2(x);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #9
0
        public void ReferenceTest()
        {
            int scale = 2;
            int channels = 2, inwidth = 4, batch = 2;
            int outwidth = inwidth * scale;

            float[] xval = (new float[channels * inwidth * batch]).Select((_, idx) => idx * 2e-3f).ToArray();
            float[] yval = (new float[channels * outwidth * batch]).Select((_, idx) => idx * 1e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(channels, inwidth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(channels, outwidth, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = BilinearZoom1D(x);
            Field err      = Abs(y_expect - y_actual);

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #10
0
        /// <summary>
        /// 指定された名前の変数を新しいスコープから順に探し,値を代入します.
        /// </summary>
        /// <param name="name">変数名</param>
        /// <param name="value">値</param>
        public void Set(string name, ScriptVariable value)
        {
            // スコープが空だったらとりあえずひとつ入っておく
            if (_variableScopes.Count == 0)
            {
                this.EnterScope();
            }

            VariableField field;

            if (!this.TryLookup(name, out field))
            {
                // ない場合にはグローバル変数に突っ込む
                _variableScopes[0][name] = new VariableField(value, FieldProperty.Default);
                return;
            }
            if (field.Property.Readonly)
            {
                // 読み取り専用だったら変更しない
                this.Parent.Warn(string.Format("Cannot modify readonly variable: {0}", name));
                return;
            }
            // あった場合には新しいスコープから探して最初に見つかったところの値を変える
            foreach (var block in _variableScopes.Reverse())
            {
                if (block.ContainsKey(name))
                {
                    field.Variable = value;
                    block[name]    = field;
                    return;
                }
            }
        }
コード例 #11
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] x1val = (new float[length]).Select((_, idx) => (float)idx - 12).ToArray();
            float[] x2val = (new float[length]).Select((_, idx) => (float)(idx - 12) * (idx - 12) / 8 + 1).ToArray();
            float[] yval  = (new float[length]).Select((_, idx) => (float)idx * 2).ToArray();

            Tensor x1tensor = new Tensor(Shape.Vector(length), x1val);
            Tensor x2tensor = new Tensor(Shape.Vector(length), x2val);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x1       = x1tensor;
            ParameterField x2       = x2tensor;
            VariableField  y_actual = ytensor;

            Field y_expect = LimitAbs(x1, x2);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx1_actual = x1.GradTensor.State;
            float[] gx2_actual = x2.GradTensor.State;

            AssertError.Tolerance(gx1_expect, gx1_actual, 1e-7f, 1e-5f, $"not equal gx1");

            AssertError.Tolerance(gx2_expect, gx2_actual, 1e-7f, 1e-5f, $"not equal gx2");
        }
コード例 #12
0
        public void ReferenceTest()
        {
            int inchannels = 2, outchannels = 3, kwidth = 3, kheight = 5, kdepth = 7, stride = 2, inwidth = 13, inheight = 12, indepth = 11;
            int outwidth = (inwidth - kwidth) / stride + 1, outheight = (inheight - kheight) / stride + 1, outdepth = (indepth - kdepth) / stride + 1, batch = 2;

            float[] xval = (new float[inwidth * inheight * indepth * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outwidth * outheight * outdepth * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * kheight * kdepth * outchannels * inchannels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map3D(inchannels, inwidth, inheight, indepth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map3D(outchannels, outwidth, outheight, outdepth, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel3D(inchannels, outchannels, kwidth, kheight, kdepth), wval);

            VariableField  x_actual = xtensor;
            ParameterField w        = wtensor;
            ParameterField y        = ytensor;

            Field      x_expect = Deconvolution3D(y, w, stride, Shape.Map3D(inchannels, inwidth, inheight, indepth, batch));
            Field      err      = Abs(x_expect - x_actual);
            OutputNode err_node = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gy_actual = y.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gy_expect, gy_actual, 1e-6f, 1e-4f, $"not equal gy"); /*backward tolerance*/
        }
コード例 #13
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx - 12).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            VariableField x        = xtensor;
            VariableField y_actual = ytensor;

            Field y_expect = Step(x);
            Field err      = y_expect - y_actual;

            OutputNode errnode = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] err_actual = errnode.Tensor.State;

            AssertError.Tolerance(err_expect, err_actual, 1e-7f, 1e-5f, $"not equal err");
        }
コード例 #14
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx * 3 - length).Reverse().ToArray();
            float[] tval = (new float[length]).Select((_, idx) => (float)idx / 2).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);
            Tensor ytensor = new Tensor(Shape.Vector(length), yval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField x = xtensor;
            ParameterField y = ytensor;
            VariableField  t = ttensor;

            Field xy  = QuaternionMul(x, y);
            Field err = xy - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gx_actual = x.GradTensor.State;
            float[] gy_actual = y.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");

            AssertError.Tolerance(gy_expect, gy_actual, 1e-7f, 1e-5f, $"not equal gy");
        }
コード例 #15
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)(idx - 12) * (idx - 12) / 8 + 1).ToArray();
            float[] tval = (new float[length]).Select((_, idx) => (float)idx * 2).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField x = xtensor;
            VariableField  t = ttensor;

            Field      loss     = SquareError(x, t);
            OutputNode lossnode = loss.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(loss);

            flow.Execute();

            float[] loss_actual = lossnode.Tensor.State;

            AssertError.Tolerance(loss_expect, loss_actual, 1e-7f, 1e-5f, $"not equal loss");

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #16
0
        public void ReferenceTest()
        {
            int inchannels = 8, outchannels = 12, kwidth = 3, stride = 2, inwidth = 7;
            int outwidth = (inwidth - kwidth) / stride + 1, batch = 3;

            float[] xval = (new float[inwidth * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outwidth * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * outchannels * inchannels / 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(inchannels, inwidth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(outchannels, outwidth, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel1D(inchannels, outchannels / 4, kwidth), wval);

            ParameterField x        = xtensor;
            ParameterField w        = wtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = QuaternionConvolution1D(x, w, stride);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #17
0
        public void ReferenceTest()
        {
            int channel = 5, width = 4, height = 3, batch = 2;

            float[] x1val = (new float[channel]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] x2val = (new float[channel * width * height * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval  = (new float[channel * width * height * batch]).Select((_, idx) => idx * 1e-3f).ToArray();

            Tensor x1tensor = new Tensor(Shape.Vector(channel), x1val);
            Tensor x2tensor = new Tensor(Shape.Map2D(channel, width, height, batch), x2val);

            Tensor ytensor = new Tensor(Shape.Map2D(channel, width, height, batch), yval);

            ParameterField x1       = x1tensor;
            ParameterField x2       = x2tensor;
            VariableField  y_actual = ytensor;

            Field y_expect = x1 * x2;
            Field err      = Abs(y_expect - y_actual);

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx1_actual = x1.GradTensor.State;
            float[] gx2_actual = x2.GradTensor.State;

            AssertError.Tolerance(gx1_expect, gx1_actual, 1e-7f, 1e-5f, $"not equal gx1");

            AssertError.Tolerance(gx2_expect, gx2_actual, 1e-7f, 1e-5f, $"not equal gx2");
        }
コード例 #18
0
        public void TheoreticalTest()
        {
            int length = 24;

            float[] xval = { 1, 2, -3, 4, 5, -6, -1, 2, -3, -4, 5, 6, -1, 2, 3, -4, 5, -6, 1, -2, 3, -4, 5, 6 };
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 2).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field x_real = ComplexReal(x), x_imag = ComplexImag(x);
            Field isphase1 = GreaterThanOrEqual(x_real, 0) * GreaterThanOrEqual(x_imag, 0);

            Field y_expect = ComplexCast(x_real * isphase1, x_imag * isphase1);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #19
0
        public void ReferenceTest()
        {
            int pad_left = 2, pad_right = 1;
            int channels = 5, inwidth = 6, batch = 2;
            int outwidth = inwidth + pad_left + pad_right;

            float[] xval = (new float[channels * inwidth * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[channels * outwidth * batch]).Select((_, idx) => idx * 2e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(channels, inwidth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(channels, outwidth, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = EdgePadding1D(x, pad_left, pad_right);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #20
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (idx - 11.5f) / 12f).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = Arctan(x);
            Field err      = y_expect - y_actual;

            OutputNode n = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] div = n.Tensor.State;

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-6f, 1e-4f, $"not equal gx"); /*nonlinear tolerance*/
        }
コード例 #21
0
        public void ReferenceTest()
        {
            int stride = 3;
            int channels = 2, inwidth = 10, inheight = 9, batch = 2;
            int outwidth = inwidth / stride, outheight = inheight / stride;

            float[] xval = (new float[channels * inwidth * inheight * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[channels * outwidth * outheight * batch]).Select((_, idx) => idx * 2e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map2D(channels, inwidth, inheight, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map2D(channels, outwidth, outheight, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = AveragePooling2D(x, stride);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
コード例 #22
0
        public void ReferenceMethod()
        {
            int channels = 3, batch = 16;

            float[] xval =
            { 1,     2, 3, //2 -  0
              2, 1, 3,     //2 -  1
              3, 2, 1,     //0 -  2
              1, 2, 1,     //1 +  1
              3, 1, 2,     //0 -  2
              2, 1, 3,     //2 +  2
              1, 2, 1,     //1 -  0
              2, 1, 3,     //2 +  2
              1, 2, 1,     //1 -  0
              3, 1, 2,     //0 -  1
              2, 1, 3,     //2 -  0
              3, 2, 1,     //0 -  1
              1, 2, 1,     //1 -  2
              2, 1, 3,     //2 -  1
              1, 2, 1,     //1 -  2
              3, 1, 2,     //0 +  0
            };

            float[] tval =
            { 0,
              1,
              2,
              1,
              2,
              2,
              0,
              2,
              0,
              1,
              0,
              1,
              2,
              1,
              2,
              0 };

            Tensor xtensor = new Tensor(Shape.Map0D(channels, batch), xval);
            Tensor ttensor = new Tensor(Shape.Vector(batch), tval);

            VariableField x = xtensor;
            VariableField t = ttensor;

            Field      acc     = Accuracy(x, t);
            OutputNode accnode = acc.Value.Save();

            Flow flow = Flow.Inference(accnode);

            flow.Execute();

            float[] acc_actual = accnode.Tensor.State;

            Assert.AreEqual(1, acc_actual.Length);
            Assert.AreEqual(4 / 16.0f, acc_actual[0]);
        }
コード例 #23
0
        public void ReferenceMethod()
        {
            int channels = 3, batch = 16;

            float[] xval =
            { 1,     2, 3, //2 -  0
              2, 1, 3,     //2 -  1
              3, 2, 1,     //0 -  2
              1, 2, 1,     //1 +  1
              3, 1, 2,     //0 -  2
              2, 1, 3,     //2 +  2
              1, 2, 1,     //1 -  0
              2, 1, 3,     //2 +  2
              1, 2, 1,     //1 -  0
              3, 1, 2,     //0 -  1
              2, 1, 3,     //2 -  0
              3, 2, 1,     //0 -  1
              1, 2, 1,     //1 -  2
              2, 1, 3,     //2 -  1
              1, 2, 1,     //1 -  2
              3, 1, 2,     //0 +  0
            };

            float[] tval =
            { 0,
              1,
              2,
              1,
              2,
              2,
              0,
              2,
              0,
              1,
              0,
              1,
              2,
              1,
              2,
              0 };

            Tensor xtensor = new Tensor(Shape.Map0D(channels, batch), xval);
            Tensor ttensor = new Tensor(Shape.Vector(batch), tval);

            VariableField x = xtensor;
            VariableField t = ttensor;

            Field      matches      = Matches(x, t);
            OutputNode matches_node = matches.Value.Save();

            Flow flow = Flow.Inference(matches_node);

            flow.Execute();

            float[] matches_actual = matches_node.Tensor.State;

            Assert.AreEqual(Shape.Vector(channels), matches_node.Shape);
            CollectionAssert.AreEqual(new float[] { 1, 1, 2 }, matches_actual);
        }
コード例 #24
0
 public override string ToCode(ToCodeFormat format)
 {
     // if we have a local field pointer that has a crunched name,
     // the return the crunched name. Otherwise just return our given name;
     return(VariableField != null
         ? VariableField.ToString()
         : m_name);
 }
コード例 #25
0
        protected override void DoCustomDrawing()
        {
            VariableField.Draw();
            GetRenderedNodeAs <SetAnimatorVariableWireNode>().Variable = VariableField.FieldValue;

            ActorField.Draw();
            GetRenderedNodeAs <SetAnimatorVariableWireNode>().TargetActor = ActorField.FieldValue;
        }
コード例 #26
0
        /// <summary>
        /// Find all properties from the model and fill this.properties.
        /// </summary>
        /// <param name="model">The mode object</param>
        public void FindAllProperties(Model model)
        {
            this.model = model;
            this.properties.Clear();
            if (this.model != null)
            {
                var members = from member in this.model.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public)
                              where Attribute.IsDefined(member, typeof(DescriptionAttribute)) &&
                              (member is PropertyInfo || member is FieldInfo)
                              orderby((DescriptionAttribute)member
                                      .GetCustomAttributes(typeof(DescriptionAttribute), false)
                                      .Single()).LineNumber
                              select member;

                foreach (MemberInfo member in members)
                {
                    IVariable property = null;
                    if (member is PropertyInfo)
                    {
                        property = new VariableProperty(this.model, member as PropertyInfo);
                    }
                    else if (member is FieldInfo)
                    {
                        property = new VariableField(this.model, member as FieldInfo);
                    }

                    if (property != null && property.Description != null && property.Writable)
                    {
                        // Only allow lists that are double[], int[] or string[]
                        bool includeProperty = true;
                        if (property.DataType.GetInterface("IList") != null)
                        {
                            includeProperty = property.DataType == typeof(double[]) ||
                                              property.DataType == typeof(int[]) ||
                                              property.DataType == typeof(string[]);
                        }

                        if (Attribute.IsDefined(member, typeof(SeparatorAttribute)))
                        {
                            SeparatorAttribute separator = Attribute.GetCustomAttribute(member, typeof(SeparatorAttribute)) as SeparatorAttribute;
                            properties.Add(new VariableObject(separator.ToString()));  // use a VariableObject for separators
                        }
                        if (includeProperty)
                        {
                            this.properties.Add(property);
                        }

                        if (property.DataType == typeof(DataTable))
                        {
                            this.grid.DataSource = property.Value as DataTable;
                        }
                    }
                }
            }
        }
コード例 #27
0
        /// <summary>ソート</summary>
        /// <remarks>安定ソート</remarks>
        public static Field ArgSort(Field x, int axis)
        {
            Field         y     = new Field();
            VariableField index = Tensor.Index(x.Shape, axis);

            Link link = new Links.ArrayManipulation.ArgSort(x, index, y, axis);

            link.Forward();

            return(y);
        }
コード例 #28
0
        /// <summary>
        /// Find all properties from the model and fill this.properties.
        /// </summary>
        /// <param name="model">The mode object</param>
        public void FindAllProperties(Model model)
        {
            this.model = model;
            properties.Clear();
            if (this.model != null)
            {
                var orderedMembers = GetMembers(model);

                foreach (MemberInfo member in orderedMembers)
                {
                    IVariable property = null;
                    if (member is PropertyInfo)
                    {
                        property = new VariableProperty(this.model, member as PropertyInfo);
                    }
                    else if (member is FieldInfo)
                    {
                        property = new VariableField(this.model, member as FieldInfo);
                    }

                    if (property != null && property.Description != null && property.Writable)
                    {
                        // Only allow lists that are double[], int[] or string[]
                        bool includeProperty = true;
                        if (property.DataType.GetInterface("IList") != null)
                        {
                            includeProperty = property.DataType == typeof(double[]) ||
                                              property.DataType == typeof(int[]) ||
                                              property.DataType == typeof(string[]);
                        }

                        if (Attribute.IsDefined(member, typeof(SeparatorAttribute)))
                        {
                            SeparatorAttribute separator = Attribute.GetCustomAttribute(member, typeof(SeparatorAttribute)) as SeparatorAttribute;
                            properties.Add(new VariableObject(separator.ToString()));  // use a VariableObject for separators
                        }
                        if (includeProperty)
                        {
                            properties.Add(property);
                        }

                        if (property.DataType == typeof(DataTable))
                        {
                            grid.DataSource = property.Value as DataTable;
                        }
                    }
                }
            }
        }
コード例 #29
0
        public void ChangeUpdaterValueTest()
        {
            Tensor in1tensor = new Tensor(Shape.Scalar(), new float[] { 1 });
            Tensor in2tensor = new Tensor(Shape.Scalar(), new float[] { 2 });
            Tensor outtensor = new Tensor(Shape.Scalar(), new float[] { -3 });

            {
                ParameterField f1 = new ParameterField(in1tensor);
                ParameterField f2 = new ParameterField(in2tensor);
                VariableField  fo = new VariableField(outtensor);

                Field f3   = f1 + f2;
                Field ferr = f3 - fo;

                (Flow flow, Parameters parameters) = Flow.Optimize(ferr);
                parameters.AddUpdater((parameter) => new SGD(parameter, 0.1f));

                Assert.AreEqual(0.1f, (float)parameters["SGD.Lambda"], 1e-6f);

                parameters["SGD.Lambda"] = 0.2;

                Assert.AreEqual(0.2f, (float)parameters["SGD.Lambda"], 1e-6f);

                flow.Execute();
                parameters.Update();

                Assert.AreEqual(-0.2f, f1.ValueTensor.State[0], 1e-6f);
                Assert.AreEqual(0.8f, f2.ValueTensor.State[0], 1e-6f);

                Snapshot snapshot = parameters.Save();

                parameters["SGD.Lambda"] = 0.1;
                f1.ValueTensor.State     = new float[] { 1.5f };
                f2.ValueTensor.State     = new float[] { 1.5f };

                Assert.AreEqual(0.1f, (float)parameters["SGD.Lambda"], 1e-6f);

                parameters.Load(snapshot);

                Assert.AreEqual(-0.2f, f1.ValueTensor.State[0], 1e-6f);
                Assert.AreEqual(0.8f, f2.ValueTensor.State[0], 1e-6f);

                parameters.Where((parameter) => parameter == f1)["SGD.Lambda"] = 0.3;
                Assert.ThrowsException <System.ArgumentException>(() =>
                                                                  _ = (float)parameters["SGD.Lambda"]
                                                                  );
            }
        }
コード例 #30
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length / 4]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] yval = (new float[length / 4]).Select((_, idx) => (float)idx * 3 - length).Reverse().ToArray();
            float[] zval = (new float[length / 4]).Select((_, idx) => (float)idx * 4 - length).ToArray();
            float[] wval = (new float[length / 4]).Select((_, idx) => (float)idx * 5 - length).Reverse().ToArray();

            float[] tval = (new float[length]).Select((_, idx) => (float)idx / 2).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length / 4), xval);
            Tensor ytensor = new Tensor(Shape.Vector(length / 4), yval);
            Tensor ztensor = new Tensor(Shape.Vector(length / 4), zval);
            Tensor wtensor = new Tensor(Shape.Vector(length / 4), wval);

            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField x        = xtensor;
            ParameterField y        = ytensor;
            ParameterField z        = ztensor;
            ParameterField w        = wtensor;
            VariableField  t_actual = ttensor;

            Field      t_expect = QuaternionCast(x, y, z, w);
            Field      err      = t_expect - t_actual;
            StoreField errnode  = err.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");

            float[] gy_actual = y.GradTensor.State;

            AssertError.Tolerance(gy_expect, gy_actual, 1e-7f, 1e-5f, $"not equal gy");

            float[] gz_actual = z.GradTensor.State;

            AssertError.Tolerance(gz_expect, gz_actual, 1e-7f, 1e-5f, $"not equal gz");

            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");
        }