Example #1
0
        public override LPSTerm[] EvaluateSymbolic(LPSState state, LPSTerm[] input)
        {
            if (!symbolic_output_storage.IsValueCreated)
            {
                symbolic_output_storage.Value = default(NumInstLPSTermArr).CreateVector(OutputDimension);
            }

            Evaluate <NumInstLPSTermArr, LPSTerm, LPSTerm[]>(input, symbolic_output_storage.Value);
            return(symbolic_output_storage.Value);
        }
        private LPSTerm doInnerProduct(LPSState state, LPSTerm[] vs, Vector <double> ds)
        {
            LPSTerm result = LPSTerm.Const(0.0);

            for (int i = 0; i < vs.Length; i++)
            {
                result.AddMul(vs[i], ds[i]);
            }
            return(result);
        }
 public override LPSTerm[] EvaluateSymbolic(LPSState state, LPSTerm[] input)
 {
     LPSTerm[] output = new LPSTerm[OutputDimension];
     for (int i = 0; i < OutputDimension; i++)
     {
         output[i] = doInnerProduct(state, input, weightMatrixRows_[i]);
         output[i].Add(interceptVector_[i]);
     }
     return(output);
 }
Example #4
0
        public LPSTerm[] EvaluateNNSymbolicPostCrop(LPSState state, LPSTerm[] input)
        {
            var v = input;

            for (int i = 0; i < LayerCount; i++)
            {
                Layer curr      = Layers[i];
                var   stopwatch = new Stopwatch();
                stopwatch.Start();
                var w = curr.EvaluateSymbolic(state, v);
                stopwatch.Stop();
                v = w;

                Console.WriteLine("Symbolic interpreter: layer index: {0,2}, elapsed milliseconds = {1}", curr.Index, stopwatch.ElapsedMilliseconds);
            }
            return(v);
        }
        public override LPSTerm[] EvaluateSymbolic(LPSState state, LPSTerm[] input)
        {
            LPSTerm[] cur = default(NumInstLPSTermArr).CreateVector(input.Length);

            // If we have a meanImage ...
            if (meanImage_ != null)
            {
                for (int i = 0; i < input.Length; i++)
                {
                    cur[i].Sub(LPSTerm.Const(meanImage_[i]));  // - mean
                    cur[i].Add(input[i]);                      // + input
                    cur[i].Mul(scale_);
                }

                return(cur);
            }

            // If we have a meanChannel ...
            if (meanChannel_ != null && meanChannel_.Count() > 0)
            {
                for (int channel = 0; channel < InputCoordinates.ChannelCount; channel++)
                {
                    for (int r = 0; r < InputCoordinates.RowCount; r++)
                    {
                        for (int c = 0; c < InputCoordinates.ColumnCount; c++)
                        {
                            int index = InputCoordinates.GetIndex(channel, r, c);
                            cur[index].Sub(LPSTerm.Const(meanChannel_[channel]));
                            cur[index].Add(input[index]);
                            cur[index].Mul(scale_);
                        }
                    }
                }
                return(cur);
            }

            // Finally, if we are only doing scaling ...
            for (int i = 0; i < input.Length; i++)
            {
                cur[i].Add(input[i]);
                cur[i].Mul(scale_);
            }
            return(cur);
        }
Example #6
0
        public override LPSTerm ApplyKernelSymbolic(LPSState state, LPSTerm[] input, int outIndex, int channel, int row, int column)
        {
            int[] selections = state.Instrumentation[Index].Selections;
            int   maxIndex   = selections[outIndex];

            LPSTerm maxInput = input[maxIndex];

            for (int i = 0; i < KernelDimension; i++)
            {
                for (int j = 0; j < KernelDimension; j++)
                {
                    int x = row - Padding + i;
                    int y = column - Padding + j;
                    if (x >= InputCoordinates.RowCount || y >= InputCoordinates.ColumnCount)
                    {
                        continue;
                    }

                    int curIndex = InputCoordinates.GetIndex(channel, x, y);
                    if (curIndex == maxIndex)
                    {
                        continue;
                    }
                    if (curIndex < 0 || curIndex >= input.Length)
                    {
                        continue;
                    }

                    // maxInput - input[curIndex] >= 0
                    LPSTerm t = LPSTerm.Const(0.0);
                    t.Add(maxInput);
                    t.AddMul(input[curIndex], -1.0);
                    state.DeferredCts.And(t, InequalityType.GE);
                }
            }
            return(maxInput);
        }
        public override LPSTerm[] EvaluateSymbolic(LPSState state, LPSTerm[] input)
        {
            DisjunctionChoice[] disjunctionChoices = state.Instrumentation[Index].DisjunctionConstraints;
            Debug.Assert(InputDimension == disjunctionChoices.Length);
            LPSTerm[] output = new LPSTerm[OutputDimension];

            Random r = new Random(System.DateTime.Now.Millisecond);

            // int uncertain = 0;

            for (int i = 0; i < OutputDimension; i++)
            {
                switch (disjunctionChoices[i])
                {
                case DisjunctionChoice.ACTIVE:
                    output[i] = input[i];
                    // If we are supposed to do sampling
                    if (Utils.RobustnessOptions.LiveConstraintSamplingRatio != 1.0)
                    {
                        // Console.WriteLine("Sampling!");
                        // if we are above threshold defer
                        if (r.Next(0, 100) > (int)Utils.RobustnessOptions.LiveConstraintSamplingRatio * 100)
                        {
                            state.DeferredCts.And(input[i], InequalityType.GE);
                        }
                        else
                        {
                            state.CurrentCts.And(input[i], InequalityType.GE);
                        }
                    }
                    else
                    {
                        state.CurrentCts.And(input[i], InequalityType.GE);
                    }
                    break;

                case DisjunctionChoice.INACTIVE:
                    output[i] = LPSTerm.Const(0.0);
                    // CEGAR version: defer 'dead' constraint
                    state.DeferredCts.And(input[i], InequalityType.LT);
                    // Original version
                    // state.CurrentCts.And(input[i],InequalityType.LT);
                    break;

                default:
                    throw new Exception("Invalid disjunction choice type!");
                }

                /* This is more of an experiment really ...
                 * if (IsActivationWobbly(input[i], state.Origin))
                 * {
                 *  uncertain++;
                 *  // Mutate state to have new disjunction choices to explore in the future
                 *  disjunctionChoices[i] = Instrumentation.FlipDisjunctionChoice(disjunctionChoices[i]);
                 * }
                 */
            }

            // Console.WriteLine("** Ultra-sensitive ReLU activations {0}/{1}", uncertain, OutputDimension);

            return(output);
        }
Example #8
0
 public override LPSTerm ApplyKernelSymbolic(LPSState state, LPSTerm[] input, int outIndex, int channel, int row, int column)
 {
     return(ApplyKernel <NumInstLPSTermArr, LPSTerm, LPSTerm[]>(input, channel, row, column));
 }
Example #9
0
 public abstract LPSTerm ApplyKernelSymbolic(LPSState state, LPSTerm[] input, int outIndex, int channel, int row, int column);
Example #10
0
 public override LPSTerm[] EvaluateSymbolic(LPSState state, LPSTerm[] input)
 {
     return(ApplyKernels <NumInstLPSTermArr, LPSTerm, LPSTerm[], LPSState>(state, ApplyKernelSymbolic, input));
 }
Example #11
0
 public abstract LPSTerm[] EvaluateSymbolic(LPSState state, LPSTerm[] input);