/// <summary> /// Pick an array element with probability proportional to exp(-cost). /// </summary> public static int sample_by_costs(Floatarray costs) { Doublearray p = new Doublearray(); p.Copy(costs); double mincost = NarrayUtil.Min(costs); p -= mincost; for (int i = 0; i < p.Length(); i++) { p.UnsafePut1d(i, Math.Exp(-p.UnsafeAt1d(i))); } double sump = NarrayUtil.Sum(p); p /= sump; double choice = rnd.NextDouble(); double s = 0; for (int i = 0; i < p.Length(); i++) { s += p[i]; if (choice < s) { return(i); } } // shouldn't happen... return(costs.Length() - 1); }
public static void Gauss1d <T>(Narray <T> outa, Narray <T> ina, float sigma) { outa.Resize(ina.Dim(0)); // make a normalized mask int range = 1 + (int)(3.0 * sigma); Doublearray mask = new Doublearray(2 * range + 1); for (int i = 0; i <= range; i++) { double y = Math.Exp(-i * i / 2.0 / sigma / sigma); mask[range + i] = mask[range - i] = y; } double total = 0.0; for (int i = 0; i < mask.Dim(0); i++) { total += mask[i]; } for (int i = 0; i < mask.Dim(0); i++) { mask[i] /= total; } // apply it int n = ina.Length(); for (int i = 0; i < n; i++) { total = 0.0; for (int j = 0; j < mask.Dim(0); j++) { int index = i + j - range; if (index < 0) { index = 0; } if (index >= n) { index = n - 1; } total += Convert.ToDouble(ina[index]) * mask[j]; // it's symmetric } outa[i] = (T)Convert.ChangeType(total, typeof(T)); } }
public LenetIOWrapper(LenetWrapper lenet, string name) { this.lenetWrap = lenet; this.lenetparam = new Doublearray(); this.name = name; }
/// <summary> /// Pick an array element with probability proportional to exp(-cost). /// </summary> public static int sample_by_costs(Floatarray costs) { Doublearray p = new Doublearray(); p.Copy(costs); double mincost = NarrayUtil.Min(costs); p -= mincost; for (int i = 0; i < p.Length(); i++) p.UnsafePut1d(i, Math.Exp(-p.UnsafeAt1d(i))); double sump = NarrayUtil.Sum(p); p /= sump; double choice = rnd.NextDouble(); double s = 0; for (int i = 0; i < p.Length(); i++) { s += p[i]; if (choice < s) return i; } // shouldn't happen... return costs.Length() - 1; }