public override TDomain Assert(APC pc, EdgeTag tag, TVar condition, TDomain data) { var boxed = ToBoxedExpression(pc, condition); bool result; if (boxed.IsTrivialCondition(out result)) { return(result ? data : data.Bottom); } data = data.AssumeTrue(boxed); var type = ContextProvider.ValueContext.GetType( ContextProvider.MethodContext.CFG.Post(pc), condition); if (type.IsNormal() && MetaDataProvider.Equal(type.Value, MetaDataProvider.System_Boolean)) { var guard = BoxedExpression.Binary(BinaryOperator.Ceq, boxed, BoxedExpression.Const(1, MetaDataProvider. System_Int32)); data = data.AssumeTrue(guard); } return(data); }
public static DataSet BuildSet(IAllocator allocator, DigitImage[] images) { var inputs = new Tensor(allocator, DType.Float32, images.Length, MnistParser.ImageSize, MnistParser.ImageSize); var outputs = new Tensor(allocator, DType.Float32, images.Length, MnistParser.LabelCount); var cpuAllocator = new TensorSharp.Cpu.CpuAllocator(); for (int i = 0; i < images.Length; ++i) { var target = inputs.TVar().Select(0, i); TVar.FromArray(images[i].pixels, cpuAllocator) .AsType(DType.Float32) .ToDevice(allocator) .Evaluate(target); target.Div(255) .Evaluate(target); } Ops.FillOneHot(outputs, MnistParser.LabelCount, images.Select(x => (int)x.label).ToArray()); var targetValues = Tensor.FromArray(allocator, images.Select(x => (float)x.label).ToArray()); return(new DataSet() { inputs = inputs, targets = outputs, targetValues = targetValues }); }
protected BoxedExpression ToBoxedExpression(APC pc, TVar condition) { return (BoxedExpression.For( ContextProvider.ExpressionContext.Refine(pc, condition), ExpressionDecoder.ExternalDecoder)); }
public override void Forward(Variable x) { Input = x; var p = 1 - Rate; noise = TVar.RandomBernoulli(new SeedSource(), p, Global.Device, x.Data.ElementType, x.Data.Shape) .Div(p) .Evaluate(); Output = Output.TVar().CMul(noise).Evaluate(); }
public static void SetSNOPowerUseDelay(SNOPower power, double delay) { string key = "SpellDelay." + power.ToString(); TVar v = V.Data[key]; bool hasDefaultValue = v.Value == v.DefaultValue; if (hasDefaultValue) { // Create a new TVar (changes the default value) V.Set(new TVar(v.Name, delay, v.Description)); } }
public static TVar L2Normalize(this TVar x, int axis = -1) { TVar y = null; if (axis == -1) { y = x.Pow(2).SumAll().MaxAll(); } else { y = x.Pow(2).Sum(axis).Max(axis); } return(x.CDiv(y.Sqrt())); }
private Tensor ApplyRegularizer(TVar loss) { foreach (var l in Layers) { foreach (var p in l.Params) { if (p.Value.HaveRegularizer) { loss += p.Value.ApplyRegularizer(); } } } return(loss.Evaluate()); }
void Add(TVar var) { int cnt; if (!occurences.TryGetValue(var, out cnt)) { cnt = 0; } occurences[var] = cnt + 1; if (cnt == 1) // if already was occurence { duplicated = duplicated.Cons(var); } }
public void AddVarEvent1(object sender, TVar Var) { if (this.fastColoredTextBox1.InvokeRequired) { try { InsTextCallback d = new InsTextCallback(InsText); this.Invoke(d, new object[] { Var.ToStr() + ";\n" }); } catch (System.ObjectDisposedException) { } } else { AppendText(Var.FName); } }
public bool TryEvaluateToConstant (APC pc, TVar dest, BinaryOperator op, BoxedExpression left, BoxedExpression right, out long value) { var type = context_provider.ValueContext.GetType ( context_provider.MethodContext.CFG.Post (pc), dest); long l; long r; if (type.IsNormal () && TryEvaluateToConstant (pc, left, out l) && TryEvaluateToConstant (pc, right, out r)) return TryEvaluate (type.Value, op, l, r, out value); return false.Without (out value); }
public static TVar Softmax(this TVar x) { long[] shape = x.Evaluate().Shape; List <float> data = new List <float>(); for (long i = 0; i < shape[0]; i++) { var s_x = x.Select(0, i); var exp = s_x.Exp(); var sum = exp.SumAll(); var s_t = (exp / sum.ToScalar()).View(1, shape[1]).Evaluate(); data.AddRange(s_t.ToArray().Cast <float>()); } return(TVar.FromArray(data.ToArray(), Global.Device).View(shape)); }
public void AddVarEvent1(object sender, TVar Var) { if (this.fastColoredTextBox1.InvokeRequired) { try { InsTextCallback d = new InsTextCallback(InsText); this.Invoke(d, new object[] { Var.ToStr() + ";\n" }); } catch (System.ObjectDisposedException) { } } else { AppendText(Var.FName); } }
public Tensor UpdateGradInput(Tensor input, Tensor target) { var norm = -1.0f / input.Sizes[0]; TVar.Fill(0, allocator, DType.Float32, gradInput.Sizes) .Evaluate(gradInput); var indices = target.TVar().View(target.Sizes[0], 1); gradInput.TVar() .ScatterFill(norm, 1, indices) .Evaluate(gradInput); return(gradInput); }
public override void Forward(Variable x) { Input = x; Variable beta = BuildVar("beta", x.Data.Shape, x.Data.ElementType, BetaInitializer, BetaConstraint, BetaRegularizer); Variable gamma = BuildVar("gamma", x.Data.Shape, x.Data.ElementType, GammaInitializer, GammaConstraint, GammaRegularizer); mu = BuildVar("mm", x.Data.Shape, x.Data.ElementType, MovingMeanInitializer, null, null, false); mv = BuildVar("mv", x.Data.Shape, x.Data.ElementType, MovingVarianceInitializer, null, null, false); norm = (x.Data - mu.Data.TVar()).CDiv((mv.Data.TVar() + EPSILON).Sqrt()); var @out = gamma.Data.TVar().CMul(norm) + beta.Data; Output = @out.View(x.Data.Shape).Evaluate(); }
public override Tensor Forward(Tensor input, ModelMode mode) { Ops.Copy(activation, input); if (mode == ModelMode.Train) { var p = 1 - pRemove; TVar.RandomBernoulli(seedSource, p, allocator, elementType, noise.Sizes) .Div(p) .Evaluate(noise); activation.TVar() .CMul(noise) .Evaluate(activation); } return(activation); }
public static void SetSNOPowerUseDelay(SNOPower power, double delay) { string key = "SpellDelay." + power.ToString(); if (!V.Data.ContainsKey(key)) { Logger.LogDebug("Failed to Set TVar {0} - key doesnt exist"); return; } TVar v = V.Data[key]; bool hasDefaultValue = v.Value == v.DefaultValue; if (hasDefaultValue) { // Create a new TVar (changes the default value) V.Set(new TVar(v.Name, delay, v.Description)); } }
public override string ToString(MyDictionary ProcList, VarDictionary VarDict, Dictionary <ulong, TFunc> NewSubs) { string s = "$" + ins.ToAsmString(); if (bytes[0] == 0xE8) { if (ins.Operand1.ValueType == TUP.AsmResolver.ASM.OperandType.Normal) { s = AddProc(((Offset)ins.Operand1.Value).FileOffset + Addr, ProcList, NewSubs); } } if (bytes[0] == 0xFF) { if (bytes[1] == 0x15) { if (ins.Operand1.ValueType == TUP.AsmResolver.ASM.OperandType.DwordPointer) { s = AddProc(((Offset)ins.Operand1.Value).Va, ProcList, NewSubs); } } } if (bytes[0] == 0xA3)//mov somevar, EAX { TVar Var1 = new TVar(((Offset)ins.Operand1.Value).Va, "", 4); if (!VarDict.ContainsKey(((Offset)ins.Operand1.Value).Va)) { VarDict.AddVar(Var1); } ; s = VarDict[((Offset)ins.Operand1.Value).Va].FName + " = EAX;"; } if ((bytes[0] == 0xC2) | //retn (bytes[0] == 0xC3)) //ret { s = "$ret"; } return(s); }
public override int GetHashCode() { return(TVar.GetHashCode() ^ KVar.GetHashCode()); }
public bool Equals(GenericValuePair <TValue, KValue> other) { return(TVar.Equals(other.TVar) && KVar.Equals(other.KVar)); }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { try { BindingGroup bindingGroup = (BindingGroup)value; KeyValuePair <string, TVar> kvp = (KeyValuePair <string, TVar>)bindingGroup.Items[0]; TVar tvar = kvp.Value; if (tvar.DefaultValue.GetType() == typeof(int)) { try { int val = Int32.Parse(tvar.Value.ToString()); } catch { return(new ValidationResult(false, "Value is not an int")); } } if (tvar.DefaultValue.GetType() == typeof(bool)) { try { bool val = Boolean.Parse(tvar.Value.ToString()); } catch { return(new ValidationResult(false, "Value is not a bool")); } } if (tvar.DefaultValue.GetType() == typeof(float)) { try { float val = Single.Parse(tvar.Value.ToString()); } catch { return(new ValidationResult(false, "Value is not a float")); } } if (tvar.DefaultValue.GetType() == typeof(double)) { try { double val = Double.Parse(tvar.Value.ToString()); } catch { return(new ValidationResult(false, "Value is not a double")); } } return(ValidationResult.ValidResult); } catch (Exception ex) { Logger.LogNormal("Exception in TVar Validator {0}", ex.ToString()); return(new ValidationResult(false, "Unknown error")); } }
public override string ToString(MyDictionary ProcList, VarDictionary VarDict, Dictionary<ulong, TFunc> NewSubs) { if (!(bytes==null)) if (bytes.Count() > 0) switch (bytes[0]) { case 0x74: return "$jz Loc_" + OpToString(0).Remove(0, 2); case 0x75: return "$jnz Loc_" + OpToString(0).Remove(0, 2); case 0xE8://call; return AddProc(ops[0].value.imm.imm64, ProcList, NewSubs); case 0xE9://jmp; case 0xEB://jmp; return "$jmp Loc_" + OpToString(0).Remove(0, 2); case 0xA3://mov somevar, EAX { TVar Var1 = new TVar((ulong)disp.value.d64, "", 4); if (!VarDict.ContainsKey((ulong)disp.value.d64)) { VarDict.AddVar(Var1); }; return VarDict[(ulong)disp.value.d64].FName + " = " + OpToString(1) + ";"; } case 0xFF: { if (this.bytes[1] == 0x15) return AddProc(disp.value.d64, ProcList, NewSubs); } break; case 0x0F: { if (this.bytes[1] == 0x86) return "$jbe Loc_" + OpToString(0).Remove(0, 2); }break; } string ret = "$"+mnemonic; if (ops[0].size > 0) ret += " " + OpToString(0); if (ops[1].size > 0) ret += ", " + OpToString(1); if (ops[2].size > 0) ret += ", " + OpToString(2); return ret; }
public void RaiseVarFuncEvent(object sender, TVar Var) { if (OnVarEvent != null) OnVarEvent(sender, Var); }
public WriteTVar(TVar <T> tvar, T value) { this.TVar = tvar; this.Value = value; }
public ReadTVar(TVar <T> tvar) { this.TVar = tvar; }
public static STML <T> ReadTVar <T>(TVar <T> tvar) => new ReadTVar <T>(tvar).ToSTML <ReadTVar <T>, T>();
public static TVar Softplus(this TVar x) { return((x.Exp() + 1).Log()); }
public static STML <Unit> WriteTVar <T>(TVar <T> tvar, T value) => new WriteTVar <T>(tvar, value).ToSTML <WriteTVar <T>, Unit>();
public override string ToString(MyDictionary ProcList, VarDictionary VarDict, Dictionary<ulong, TFunc> NewSubs) { string s = "$"+ins.ToAsmString(); if (bytes[0] == 0xE8) if (ins.Operand1.ValueType == TUP.AsmResolver.ASM.OperandType.Normal) s = AddProc(((Offset)ins.Operand1.Value).FileOffset + Addr, ProcList, NewSubs); if (bytes[0] == 0xFF) if (bytes[1] == 0x15) if (ins.Operand1.ValueType == TUP.AsmResolver.ASM.OperandType.DwordPointer) s = AddProc(((Offset)ins.Operand1.Value).Va, ProcList, NewSubs); if (bytes[0] == 0xA3)//mov somevar, EAX { TVar Var1 = new TVar(((Offset)ins.Operand1.Value).Va, "", 4); if (!VarDict.ContainsKey(((Offset)ins.Operand1.Value).Va)) { VarDict.AddVar(Var1); }; s = VarDict[((Offset)ins.Operand1.Value).Va].FName + " = EAX;"; } if ((bytes[0] == 0xC2) |//retn (bytes[0] == 0xC3))//ret s = "$ret"; return s; }
public override TDomain Assume(APC pc, EdgeTag tag, TVar condition, TDomain data) { var boxed = ToBoxedExpression(pc, condition); if (tag != EdgeTag.False) { bool value; if (boxed.IsTrivialCondition(out value)) { return(!value ? data.Bottom : data); } } List <int> thresholds; if (ThresholdDB.TryGetAThreshold(boxed, expression_decoder, out thresholds)) { ThresholdDB.Add(thresholds); } TDomain result; switch (tag) { case EdgeTag.True: case EdgeTag.Requires: case EdgeTag.Assume: case EdgeTag.Invariant: result = data.AssumeTrue(boxed); break; case EdgeTag.False: result = data.AssumeFalse(boxed); break; default: result = data; break; } if (tag != EdgeTag.False) { var abstractType = ContextProvider.ValueContext.GetType( ContextProvider.MethodContext.CFG.Post(pc), condition); if (abstractType.IsNormal() && MetaDataProvider.Equal(abstractType.Value, MetaDataProvider.System_Boolean)) { var guard = BoxedExpression.Binary(BinaryOperator.Ceq, boxed, BoxedExpression.Const(1, MetaDataProvider . System_Int32)); result = result.AssumeTrue(guard); } } return(result); }
/// <summary> /// Sets a Variable (stores it in the dictionary) /// </summary> /// <param name="var"></param> public static void Set(TVar var) { try { lock (sync) { if (ContainsKey(var.Name)) Data[var.Name] = var; else Data.Add(var.Name, var); } } catch (Exception ex) { Logger.Log("Exception Setting TVar: {0} {1}", var, ex); } }
public FlatDomain <bool> ValidateExplicitAssertion(APC pc, TVar value) { return(FlatDomain <bool> .TopValue); }