public static bool Test2() { var i1 = new InputNode(); var i2 = new InputNode(); var h1 = new Perceptron(new Heaviside(1.0)); var h2 = new Perceptron(new Heaviside(2.0)); var h3 = new Perceptron(new Heaviside(1.0)); LinearWeight.Connect(i1, h1, 1.0); LinearWeight.Connect(i1, h2, 1.0); LinearWeight.Connect(i2, h2, 1.0); LinearWeight.Connect(i2, h3, 1.0); var o = new Perceptron(new Linear()); LinearWeight.Connect(h1, o, 1.0); LinearWeight.Connect(h2, o, -2.0); LinearWeight.Connect(h3, o, 1.0); var network = new GeneralNetwork(new List <InputNode> { i1, i2 }, new List <INode> { o }); network.Forward(new[] { 0.0, 0.0 }); network.Forward(new[] { 1.0, 0.0 }); network.Forward(new[] { 0.0, 1.0 }); network.Forward(new[] { 1.0, 1.0 }); return(true); }
public LinearWeight[] GetLinearWeigths(GeoCellTuple cell, Nodes nodes) { int N = nodes.Lons.Length; LinearWeight[] weights = new LinearWeight[N]; for (int i = 0; i < N; i++) { weights[i] = new LinearWeight(N - i - 1, cell.LatMax * (i + 1)); } return(weights); }
public async Task LinearCombinationAggregatorTest() { LinearCombinationAggregator lca = new LinearCombinationAggregator(); var nodes = new RealValueNodes(new double[3], new double[3], new double[] { 2.0, 3.0, 5.0 }); var weights = new LinearWeight[] { new LinearWeight(1, 7.0), new LinearWeight(0, 11.0), new LinearWeight(2, 13.0) }; LinearCombinationContext lcc = new LinearCombinationContext(new Tuple <ICellRequest, RealValueNodes, IEnumerable <LinearWeight> >[] { new Tuple <ICellRequest, RealValueNodes, IEnumerable <LinearWeight> >(new RequestStubs(), nodes, (IEnumerable <LinearWeight>)weights) }); double[] result = await lca.AggregateCellsBatchAsync(lcc, new ICellRequest[] { new RequestStubs() }); Assert.AreEqual(1, result.Length); Assert.AreEqual(108, result[0]); }
public async Task <LinearWeight[]> GetLinearWeigthsAsync(INodes nodes, ICellRequest cell) { int N = nodes.Lons.Length; LinearWeight[] weights = new LinearWeight[N]; for (int i = 0; i < N; i++) { weights[i] = new LinearWeight(N - i - 1, cell.LatMax * (i + 1)); } return(weights); }
/// <summary> /// Returns a set of linear weights along with timeSegmint for with dataIndeces in linear weights are valid /// </summary> /// <param name="context"></param> /// <param name="computationalContext"></param> /// <param name="cells"></param> /// <returns></returns> protected IEnumerable <Tuple <ITimeSegment, LinearWeight[]> > CalcLinearWeights(ComputationalContext computationalContext, IEnumerable <GeoCellTuple> cells) { if (!computationalContext.ContainsKey("observations")) { throw new InvalidOperationException("Call SaveObservationsAndDalanayDiagsForCells prior calling CompleteAggregateCellsBatch"); } ISpatPointsLinearInterpolator2D spli2d = (ISpatPointsLinearInterpolator2D)spatialIntegrator; Dictionary <ITimeSegment, IObservationsInformation> observations = (Dictionary <ITimeSegment, IObservationsInformation>)computationalContext["observations"]; Dictionary <ITimeSegment, object> delanays = (Dictionary <ITimeSegment, object>)computationalContext["dalanays"]; var sw1 = System.Diagnostics.Stopwatch.StartNew(); TraceVerbose("Computing field values"); //WARNING: can't be paralleled as dalanays are not thread safe foreach (var cell in cells) { var timeSegment = cell.Time; var observation = observations[timeSegment].Observations; object delanay = delanays[timeSegment]; double latmax = cell.LatMax, latmin = cell.LatMin, lonmax = cell.LonMax, lonmin = cell.LonMin; if (latmax == latmin && lonmax == lonmin) { yield return(Tuple.Create(timeSegment, spli2d.GetLinearWeigths(latmin, lonmin, delanay))); } else { var sizeSqrt = (int)Math.Sqrt(cellAveragingGridSize); LinearWeight[][] toFlatten = new LinearWeight[sizeSqrt * sizeSqrt][]; double latStep = (latmax - latmin) / (sizeSqrt - 1); double lonStep = (lonmax - lonmin) / (sizeSqrt - 1); for (int i = 0; i < sizeSqrt; i++) { for (int j = 0; j < sizeSqrt; j++) { var w = spli2d.GetLinearWeigths(latmin + latStep * i, lonmin + lonStep * j, delanay); toFlatten[sizeSqrt * i + j] = w; } } var devisor = sizeSqrt * sizeSqrt; var flattenedWeights = toFlatten.SelectMany(weights => weights).GroupBy(w => w.DataIndex).Select(g => new LinearWeight(g.Key, g.Select(g1 => g1.Weight).Sum() / devisor)).ToArray(); yield return(Tuple.Create(timeSegment, flattenedWeights)); } } TraceVerbose("Computing field values finished in {0}", sw1.Elapsed); }
public static bool Test1() { var i1 = new InputNode(); var i2 = new InputNode(); var h1 = new Perceptron(new Linear()); var h2 = new Perceptron(new Linear()); var h3 = new Perceptron(new Linear()); LinearWeight.Connect(i1, h1, 1.0); LinearWeight.Connect(i1, h2, 1.0); LinearWeight.Connect(i2, h2, 1.0); LinearWeight.Connect(i2, h3, 1.0); var ib = new BiasNode(); LinearWeight.Connect(ib, h1, 1.0); LinearWeight.Connect(ib, h2, 2.0); LinearWeight.Connect(ib, h3, 1.0); var o = new Perceptron(new Linear()); LinearWeight.Connect(h1, o, 1.0); LinearWeight.Connect(h2, o, -2.0); LinearWeight.Connect(h3, o, 1.0); var hb = new BiasNode(); var whbo = new LinearWeight(hb, o); whbo[0] = 1.0; var network = new GeneralNetwork(new List <InputNode> { i1, i2 }, new List <INode> { o }); network.Forward(new[] { 0.0, 0.0 }); network.Forward(new[] { 1.0, 0.0 }); network.Forward(new[] { 0.0, 1.0 }); network.Forward(new[] { 1.0, 1.0 }); return(true); }