override public Dictionary <string, Matrix> CalcGradient(Vector p_error = null) { NeuralGroup core = _groups["core"]; core.CalcDerivs(false); if (p_error == null) { core.CalcDelta(false); } else { core.Delta = Vector.HadamardProduct(core.Derivs, p_error); } foreach (Connection c in core.GetInConnections()) { if (c.Trainable) { c.CalcGradient(false); _gradients[c.Id] = c.Gradient; } } return(_gradients); }
override public Dictionary <string, Tensor> CalcGradientT(Tensor p_error = null) { NeuralGroup core = _groups["core"]; core.CalcDerivs(true); if (p_error == null) { core.CalcDelta(true); } else { core.DeltaT = core.DerivsT * p_error; } foreach (Connection c in core.GetInConnections()) { if (c.Trainable) { c.CalcGradient(true); _gradients_t[c.Id] = c.GradientT; } } return(_gradients_t); }
public InputLayer(int p_dim) : base(TYPE.INPUT, INPUT) { NeuralGroup core = new NeuralGroup("input", p_dim, ACTIVATION.IDENTITY); _groups.Add("input", core); _inputGroup = _outputGroup = core; }
public CoreLayer(int p_dim, ACTIVATION p_activationFunction, TYPE p_type) : base(p_type, CORE) { NeuralGroup core = new NeuralGroup("core", p_dim, p_activationFunction); _groups.Add("core", core); _inputGroup = _outputGroup = core; }
protected Connection Connect(NeuralGroup p_g1, NeuralGroup p_g2, bool p_trainable = true) { Connection c = new Connection(p_g1, p_g2, p_trainable); p_g1.AddOutConnection(c); p_g2.AddInConnection(c); _connections.Add(c.Id, c); return(c); }
override public Vector Activate(Vector p_input) { NeuralGroup core = _groups["input"]; if (p_input != null) { core.Output = p_input; } return(core.Output); }
public BaseLayer(JSONObject p_data) { _gradients = new Dictionary <string, Matrix>(); JSONObject groups = p_data["groups"]; _id = p_data["id"].str; _type = (TYPE)Enum.Parse(typeof(TYPE), p_data["layer_type"].str); _groups = new Dictionary <string, NeuralGroup>(); foreach (string key in groups.keys) { JSONObject layer = groups[key]; NeuralGroup g = new NeuralGroup(key, layer); _groups.Add(key, g); if (key.Equals(p_data["ingroup"].str)) { _inputGroup = g; } if (key.Equals(p_data["outgroup"].str)) { _outputGroup = g; } } _connections = new Dictionary <string, Connection>(); if (p_data.HasField("connections")) { JSONObject connections = p_data["connections"]; foreach (string key in connections.keys) { JSONObject connection = connections[key]; NeuralGroup inGroup = connection.HasField("ingroup") ? _groups[connection["ingroup"].str] : null; NeuralGroup outGroup = _groups[connection["outgroup"].str]; Connection c = new Connection(key, inGroup, outGroup, connection); _connections.Add(key, c); if (inGroup != null) { inGroup.AddOutConnection(c); } outGroup.AddInConnection(c); } } }
override public Tensor ActivateT(Tensor p_input) { NeuralGroup core = _groups["input"]; if (p_input != null) { core.Batch = (int)p_input.GetShape(0); core.OutputT = p_input; } return(core.OutputT); }
public RecurrentLayer(int p_dim, ACTIVATION p_activationFunction, TYPE p_type) : base(p_type, RECURRENT) { NeuralGroup core = new NeuralGroup("core", p_dim, p_activationFunction); NeuralGroup context = new NeuralGroup("context", p_dim, ACTIVATION.IDENTITY); _groups.Add("core", core); _groups.Add("context", context); Connection c = Connect(context, core, true); c.Init(Connection.INIT.LECUN_UNIFORM, 0.05f); Connection r = Connect(core, context, false); r.Weights = Matrix.Identity(p_dim, p_dim); _inputGroup = _outputGroup = core; }
protected void ActivateGroup(string p_group, bool p_bias, bool p_tensor) { NeuralGroup group = _groups[p_group]; foreach (Connection c in group.GetInConnections()) { if (p_tensor) { group.Integrate(c.InGroup.OutputT, c.Weights); } else { group.Integrate(c.InGroup.Output, c.Weights); } } if (p_bias) { group.AddBias(p_tensor); } group.Activate(p_tensor); }
public InputLayer(JSONObject p_data) : base(p_data) { NeuralGroup input = _groups["input"]; }