public static string EnsureNewArray(this PfaContext ctx) { Contracts.CheckValue(ctx, nameof(ctx)); const string name = "hasChars"; const string refname = "u." + name; if (ctx.ContainsFunc(name)) { return(refname); } var arrType = Type.Array(Type.Double); JObject jobj = null; JArray elseBlock = new JArray(); elseBlock.Add(jobj.AddReturn("let", jobj.AddReturn("halfsize", Call(refname, Call("//", "size", 2))))); elseBlock.Add(jobj.AddReturn("let", jobj.AddReturn("fullsize", Call("a.concat", "halfsize", "halfsize")))); elseBlock.Add(If( Call("==", Call("&", "size", 1), 1), Call("a.append", "fullsize", 0.0), "fullsize")); ctx.AddFunc(name, new JArray(Param("size", Type.Int)), arrType, If(Call("==", "size", 0), jobj.AddReturn("type", arrType).AddReturn("value", new JArray()), elseBlock)); return(refname); }
public JToken SaveAsPfa(BoundPfaContext ctx, JToken input) { Host.CheckValue(ctx, nameof(ctx)); Host.CheckValue(input, nameof(input)); const string typeName = "MCLinearPredictor"; JToken typeDecl = typeName; if (ctx.Pfa.RegisterType(typeName)) { JObject type = new JObject(); type["type"] = "record"; type["name"] = typeName; JArray fields = new JArray(); JObject jobj = null; fields.Add(jobj.AddReturn("name", "coeff").AddReturn("type", PfaUtils.Type.Array(PfaUtils.Type.Array(PfaUtils.Type.Double)))); fields.Add(jobj.AddReturn("name", "const").AddReturn("type", PfaUtils.Type.Array(PfaUtils.Type.Double))); type["fields"] = fields; typeDecl = type; } JObject predictor = new JObject(); predictor["coeff"] = new JArray(_weights.Select(w => new JArray(w.DenseValues()))); predictor["const"] = new JArray(_biases); var cell = ctx.DeclareCell("MCLinearPredictor", typeDecl, predictor); var cellRef = PfaUtils.Cell(cell); return(PfaUtils.Call("m.link.softmax", PfaUtils.Call("model.reg.linear", input, cellRef))); }
/// <summary> /// Builds a "cast" statement to the two vector types. /// </summary> /// <param name="itemType">The type of the item in the vector</param> /// <param name="src">The token we are casting</param> /// <param name="asMapName">The name for the token as it will appear in the <paramref name="mapDo"/></param> /// <param name="mapDo">The map case expression</param> /// <param name="asArrName">The name for the token as it will appear in the <paramref name="arrDo"/></param> /// <param name="arrDo">The array case expression</param> /// <returns>The cast/case expression</returns> public static JObject VectorCase(JToken itemType, JToken src, string asMapName, JToken mapDo, string asArrName, JToken arrDo) { JObject jobj = null; var cases = new JArray(); cases.Add(jobj.AddReturn("as", Type.Map(itemType)).AddReturn( "named", asMapName).AddReturn("do", mapDo)); cases.Add(jobj.AddReturn("as", Type.Array(itemType)).AddReturn( "named", asArrName).AddReturn("do", arrDo)); return(jobj.AddReturn("cast", src).AddReturn("cases", cases)); }
public override JToken SaveAsPfa(BoundPfaContext ctx, JToken input) { Contracts.CheckValue(ctx, nameof(ctx)); Contracts.CheckValue(input, nameof(input)); Contracts.Assert(CanSavePfa); JArray rootObjects = new JArray(); for (int i = 0; i < Predictors.Length; ++i) { var pred = (ISingleCanSavePfa)Predictors[i]; Contracts.Assert(pred.CanSavePfa); rootObjects.Add(ctx.DeclareVar(null, pred.SaveAsPfa(ctx, input))); } JObject jobj = null; return(jobj.AddReturn("type", PfaUtils.Type.Array(PfaUtils.Type.Double)).AddReturn("new", rootObjects)); }
public override JToken SaveAsPfa(BoundPfaContext ctx, JToken input) { Contracts.CheckValue(ctx, nameof(ctx)); Contracts.CheckValue(input, nameof(input)); Contracts.Assert(CanSavePfa); JArray rootObjects = new JArray(); for (int i = 0; i < Predictors.Length; ++i) { var pred = (IDistCanSavePfa)Predictors[i]; Contracts.Assert(pred.CanSavePfa); pred.SaveAsPfa(ctx, input, null, out JToken scoreToken, null, out JToken probToken); rootObjects.Add(probToken); } JObject jobj = null; var rootResult = jobj.AddReturn("type", PfaUtils.Type.Array(PfaUtils.Type.Double)).AddReturn("new", rootObjects); var resultVar = ctx.DeclareVar(null, rootResult); var factorVar = ctx.DeclareVar(null, PfaUtils.Call("/", 1.0, PfaUtils.Call("a.sum", resultVar))); return(PfaUtils.Call("la.scale", resultVar, factorVar)); }