Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
// ParanoidMirrorLstm_ImplicitAttention.cs // Single-file prototype: Paranoid-Mirror LSTM with implicit self-attention slots inside the cell, // plus N-time memory, Depth controller, Participant gate, and LoopGate. // Compile with: dotnet new console; replace Program.cs with this file; dotnet run using System; using System.Collections.Generic; namespace ParanoidMirrorLstmImplicitAttn { // =============================== // Vector / matrix helpers // =============================== public static class Vm { private static readonly Random Rng = new Random(); public static float[] Zeros(int n) => new float[n]; public static float[] RandomVector(int n, float scale = 0.1f) { var v = new float[n]; for (int i = 0; i < n; i++) v[i] = (float)(Rng.NextDouble() * 2 - 1) * scale; return v; } public static float[,] RandomMatrix(int rows, int cols, float scale = 0.1f) { var m = new float[rows, cols]; for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) m[r, c] = (float)(Rng.NextDouble() * 2 - 1) * scale; return m; } public static float[] MatVec(float[,] W, float[] x) { int rows = W.GetLength(0); int cols = W.GetLength(1); if (cols != x.Length) throw new ArgumentException("MatVec dimension mismatch"); var y = new float[rows]; for (int r = 0; r < rows; r++) { float sum = 0f; for (int c = 0; c < cols; c++) sum += W[r, c] * x[c]; y[r] = sum; } return y; } public static float[] Add(float[] a, float[] b) { if (a.Length != b.Length) throw new ArgumentException("Add dimension mismatch"); var y = new float[a.Length]; for (int i = 0; i < a.Length; i++) y[i] = a[i] + b[i]; return y; } // Subtract with padding: returns a - b with result length = max(a.Length,b.Length) public static float[] SubtractPad(float[] a, float[] b) { int n = Math.Max(a.Length, b.Length); var y = new float[n]; for (int i = 0; i < n; i++) { float va = (i < a.Length) ? a[i] : 0f; float vb = (i < b.Length) ? b[i] : 0f; y[i] = va - vb; } return y; } public static float[] Mul(float[] a, float[] b) { if (a.Length != b.Length) throw new ArgumentException("Mul dimension mismatch"); var y = new float[a.Length]; for (int i = 0; i < a.Length; i++) y[i] = a[i] * b[i]; return y; } public static float[] Mul(float[] a, float s) { var y = new float[a.Length]; for (int i = 0; i < a.Length; i++) y[i] = a[i] * s; return y; } public static float[] Sigmoid(float[] x) { var y = new float[x.Length]; for (int i = 0; i < x.Length; i++) y[i] = 1f / (1f + (float)Math.Exp(-x[i])); return y; } public static float[] Tanh(float[] x) { var y = new float[x.Length]; for (int i = 0; i < x.Length; i++) y[i] = (float)Math.Tanh(x[i]); return y; } public static float[] Relu(float[] x) { var y = new float[x.Length]; for (int i = 0; i < x.Length; i++) y[i] = Math.Max(0f, x[i]); return y; } public static float[] Concat(params float[][] parts) { int total = 0; foreach (var p in parts) total += p.Length; var y = new float[total]; int idx = 0; foreach (var p in parts) { Array.Copy(p, 0, y, idx, p.Length); idx += p.Length; } return y; } public static float[] Softmax(float[] x) { var y = new float[x.Length]; float max = float.NegativeInfinity; for (int i = 0; i < x.Length; i++) if (x[i] > max) max = x[i]; float sum = 0f; for (int i = 0; i < x.Length; i++) { y[i] = (float)Math.Exp(x[i] - max); sum += y[i]; } for (int i = 0; i < x.Length; i++) y[i] /= sum; return y; } public static float Norm2(float[] x) { double sum = 0; for (int i = 0; i < x.Length; i++) sum += x[i] * x[i]; return (float)Math.Sqrt(sum); } public static float Clamp(float v, float lo, float hi) { if (v < lo) return lo; if (v > hi) return hi; return v; } public static float[] ExtractRow(float[,] A, int row) { int cols = A.GetLength(1); var v = new float[cols]; for (int c = 0; c < cols; c++) v[c] = A[row, c]; return v; } public static void SetRow(float[,] A, int row, float[] v) { int cols = A.GetLength(1); if (v.Length != cols) throw new ArgumentException("Row size mismatch"); for (int c = 0; c < cols; c++) A[row, c] = v[c]; } } // =============================== // LoopGate: controls whether internal simulation loops may run // =============================== public class LoopGate { public readonly int MaxDepth; public readonly TimeSpan MaxActiveTime; public readonly int MaxIterationsPerActivation; private DateTime activationStart; private int iterations; private bool active; // Learnable gate stub: replace with trained MLP if desired private readonly Func<float[], float> LearnableGate; public Func<bool> HumanOverride; // out-of-band allow public Func<bool> KillSwitch; // out-of-band force stop public readonly List<string> Audit = new List<string>(); public LoopGate(int maxDepth = 10, int maxSeconds = 5, int maxIters = 100) { MaxDepth = maxDepth; MaxActiveTime = TimeSpan.FromSeconds(maxSeconds); MaxIterationsPerActivation = maxIters; LearnableGate = (features) => 0.8f; // conservative default: allow sometimes HumanOverride = () => false; KillSwitch = () => false; active = false; } public bool TryEnter(float paranoia, float depthDerivative, float resourceFraction, float taskUrgency, int requestedDepth) { if (KillSwitch()) { Log("enter denied: kill switch"); return false; } if (requestedDepth > MaxDepth) { Log($"enter denied: requestedDepth {requestedDepth} > MaxDepth {MaxDepth}"); return false; } if (resourceFraction <= 0.05f) { Log("enter denied: insufficient resources"); return false; } if (HumanOverride()) { StartActivation(); Log("enter allowed: human override"); return true; } if (paranoia > 0.95f) { Log($"enter denied: high paranoia {paranoia:F2}"); return false; } if (depthDerivative > 0.98f) { Log($"enter denied: unstable depth derivative {depthDerivative:F2}"); return false; } var features = new float[] { paranoia, depthDerivative, resourceFraction, taskUrgency, requestedDepth }; float p = LearnableGate(features); Log($"learnable p_continue={p:F3}"); if (p < 0.6f) { Log("enter denied: learnable gate low probability"); return false; } StartActivation(); Log("enter allowed: learnable gate"); return true; } private void StartActivation() { active = true; activationStart = DateTime.UtcNow; iterations = 0; } public bool Step() { if (!active) return false; iterations++; if (KillSwitch()) { Log("step stop: kill switch"); ForceIdle(); return false; } if ((DateTime.UtcNow - activationStart) > MaxActiveTime) { Log("step stop: time cap"); ForceIdle(); return false; } if (iterations >= MaxIterationsPerActivation) { Log("step stop: iteration cap"); ForceIdle(); return false; } return true; } public void ForceIdle() { active = false; Log("forced idle and cleared sensitive state"); } private void Log(string msg) { var entry = $"{DateTime.UtcNow:O} | {msg}"; Audit.Add(entry); Console.WriteLine(entry); } } // =============================== // Adversarial detector // =============================== public class AdversarialDetector { private readonly int inputDim; private readonly float[,] W1; private readonly float[] b1; private readonly float[,] W2; private readonly float[] b2; public AdversarialDetector(int inputDim) { this.inputDim = inputDim; // W1 maps input vector (length inputDim) to hidden(16): shape [16, inputDim] W1 = Vm.RandomMatrix(16, inputDim); b1 = Vm.Zeros(16); // W2 maps hidden(16) -> output(1): shape [1,16] W2 = Vm.RandomMatrix(1, 16); b2 = Vm.Zeros(1); } public float Suspicion(float[] opponentSignal) { var h = Vm.MatVec(W1, opponentSignal); h = Vm.Add(h, b1); h = Vm.Relu(h); var s = Vm.MatVec(W2, h); s = Vm.Add(s, b2); s = Vm.Sigmoid(s); return s[0]; } } // =============================== // Depth derivative estimator (discrete simulation) // =============================== public class DepthDerivativeEstimator { private readonly int beliefDim; private readonly float[,] Wg; private readonly float[] bg; private readonly int maxDepth; public DepthDerivativeEstimator(int beliefDim, int maxDepth = 3) { this.beliefDim = beliefDim; this.maxDepth = Math.Max(1, maxDepth); Wg = Vm.RandomMatrix(beliefDim, beliefDim); bg = Vm.Zeros(beliefDim); } public float[] BeliefStep(float[] b) { var y = Vm.MatVec(Wg, b); y = Vm.Add(y, bg); return Vm.Tanh(y); } public float EstimateDepthDerivative(float[] stateVector, int d) { if (stateVector.Length != beliefDim) throw new ArgumentException("DepthDerivativeEstimator beliefDim mismatch"); if (d < 0) d = 0; if (d >= maxDepth) d = maxDepth - 1; var b_d = (float[])stateVector.Clone(); var b_dp1 = (float[])stateVector.Clone(); for (int i = 0; i < d; i++) b_d = BeliefStep(b_d); for (int i = 0; i < d + 1; i++) b_dp1 = BeliefStep(b_dp1); var diff = new float[beliefDim]; for (int i = 0; i < beliefDim; i++) diff[i] = b_dp1[i] - b_d[i]; float raw = Vm.Norm2(diff); float bounded = raw / (1f + raw); return bounded; } } // =============================== // Mirror model conditioned on opponent signal // =============================== public class MirrorModel { private readonly int stateDim; private readonly int oppDim; private readonly float[,] W; private readonly float[] b; public MirrorModel(int stateDim, int oppDim) { this.stateDim = stateDim; this.oppDim = oppDim; int inputDim = stateDim + oppDim; W = Vm.RandomMatrix(stateDim, inputDim); b = Vm.Zeros(stateDim); } public float[] PredictMirrorState(float[] flatState, float[] opponentSignal) { if (opponentSignal == null) opponentSignal = Vm.Zeros(oppDim); var inp = Vm.Concat(flatState, opponentSignal); var y = Vm.MatVec(W, inp); y = Vm.Add(y, b); return Vm.Tanh(y); } } // =============================== // Participant count gate (learned) // =============================== public class ParticipantCountGate { private readonly int inputDim; private readonly int maxParticipants; private readonly float[,] W; private readonly float[] b; public ParticipantCountGate(int inputDim, int maxParticipants) { this.inputDim = inputDim; this.maxParticipants = Math.Max(1, maxParticipants); W = Vm.RandomMatrix(this.maxParticipants, inputDim); b = Vm.Zeros(this.maxParticipants); } public (float expectedCount, float[] probs) EstimateCount(float[] features) { if (features.Length != inputDim) throw new ArgumentException("ParticipantCountGate feature size mismatch"); var logits = Vm.MatVec(W, features); logits = Vm.Add(logits, b); var probs = Vm.Softmax(logits); float expected = 0f; for (int i = 0; i < probs.Length; i++) { float count = i + 1; expected += probs[i] * count; } return (expected, probs); } } // =============================== // Depth controller (chooses discrete depth) // =============================== public class DepthController { private readonly int inputDim; private readonly int maxDepth; private readonly float[,] W; private readonly float[] b; public DepthController(int inputDim, int maxDepth) { this.inputDim = inputDim; this.maxDepth = Math.Max(1, maxDepth); W = Vm.RandomMatrix(this.maxDepth, inputDim); b = Vm.Zeros(this.maxDepth); } public (int depth, float[] probs) ChooseDepth(float[] features) { if (features.Length != inputDim) throw new ArgumentException("DepthController feature size mismatch"); var logits = Vm.MatVec(W, features); logits = Vm.Add(logits, b); var probs = Vm.Softmax(logits); int bestIdx = 0; float bestVal = probs[0]; for (int i = 1; i < probs.Length; i++) if (probs[i] > bestVal) { bestVal = probs[i]; bestIdx = i; } return (bestIdx, probs); } } // =============================== // N-time-term memory backend // =============================== public class NTimeTermMemory { private readonly int inputSize; private readonly int memorySize; private readonly int numTracks; private readonly int modSize; private readonly float[][,] Wf; private readonly float[][] bf; private readonly float[][,] Wi; private readonly float[][] bi; private readonly float[][] tracks; public NTimeTermMemory(int inputSize, int memorySize, int numTracks, int modSize = 0) { this.inputSize = inputSize; this.memorySize = memorySize; this.numTracks = numTracks; this.modSize = modSize; int gateInputSize = inputSize + modSize; Wf = new float[numTracks][,]; bf = new float[numTracks][]; Wi = new float[numTracks][,]; bi = new float[numTracks][]; tracks = new float[numTracks][]; for (int k = 0; k < numTracks; k++) { Wf[k] = Vm.RandomMatrix(memorySize, gateInputSize); bf[k] = Vm.Zeros(memorySize); Wi[k] = Vm.RandomMatrix(memorySize, gateInputSize); bi[k] = Vm.Zeros(memorySize); tracks[k] = Vm.Zeros(memorySize); } } public void Reset() { for (int k = 0; k < numTracks; k++) tracks[k] = Vm.Zeros(memorySize); } public float[] Step(float[] x, float[] mods = null) { if (x.Length != inputSize) throw new ArgumentException("x size mismatch"); if (modSize > 0 && (mods == null || mods.Length != modSize)) throw new ArgumentException("mods size mismatch"); float[] gateInput = (modSize > 0) ? Vm.Concat(x, mods) : x; for (int k = 0; k < numTracks; k++) { var f_raw = Vm.MatVec(Wf[k], gateInput); f_raw = Vm.Add(f_raw, bf[k]); var i_raw = Vm.MatVec(Wi[k], gateInput); i_raw = Vm.Add(i_raw, bi[k]); var f = Vm.Sigmoid(f_raw); var i = Vm.Sigmoid(i_raw); var xAligned = AlignInputToMemory(x, memorySize); var forgetPart = Vm.Mul(f, tracks[k]); var inputPart = Vm.Mul(i, xAligned); tracks[k] = Vm.Add(forgetPart, inputPart); } var all = new float[numTracks * memorySize]; for (int k = 0; k < numTracks; k++) Array.Copy(tracks[k], 0, all, k * memorySize, memorySize); return all; } public float[] ReadConcat() { var all = new float[numTracks * memorySize]; for (int k = 0; k < numTracks; k++) Array.Copy(tracks[k], 0, all, k * memorySize, memorySize); return all; } public float[][] Tracks => tracks; private static float[] AlignInputToMemory(float[] x, int memorySize) { var y = new float[memorySize]; if (x.Length >= memorySize) Array.Copy(x, y, memorySize); else { int idx = 0; while (idx < memorySize) { int toCopy = Math.Min(x.Length, memorySize - idx); Array.Copy(x, 0, y, idx, toCopy); idx += toCopy; } } return y; } } // =============================== // Paranoid Mirror LSTM cell with implicit self-attention slots // =============================== public class ParanoidMirrorLstmCell { private readonly int inputSize; private readonly int hiddenSize; private readonly int advDim; private readonly int nTracks; private readonly int trackSize; // LSTM-like params private readonly float[,] Wf; private readonly float[] bf; private readonly float[,] Wi; private readonly float[] bi; private readonly float[,] Wo; private readonly float[] bo; private readonly float[,] Wc; private readonly float[] bc; // implicit attention slot params private readonly int slotCount; private readonly int slotDim; private readonly float[,] Wq; // project hidden -> query private readonly float[,] Wk; // project slot -> key private readonly float[,] Wv; // project slot -> value private readonly float[] slotWriteBias; private readonly float[][] slots; // [slotCount][slotDim] private readonly AdversarialDetector adversarial; private readonly DepthDerivativeEstimator depthEstimator; private readonly MirrorModel mirrorModel; private readonly NTimeTermMemory nTimeMemory; private readonly DepthController depthController; private readonly ParticipantCountGate participantGate; public readonly LoopGate LoopGate; public float[,] MemoryBank { get; set; } public int LastChosenDepth { get; private set; } public float LastEstimatedParticipants { get; private set; } public bool LastLoopAllowed { get; private set; } int maxparticipants = 10; public ParanoidMirrorLstmCell(int inputSize, int hiddenSize, int advDim, int nTracks, int trackSize, int slotCount = 8, int maxDepth = 3, int maxParticipants = 4) { this.inputSize = inputSize; this.hiddenSize = hiddenSize; this.advDim = advDim; this.nTracks = nTracks; this.trackSize = trackSize; Wf = Vm.RandomMatrix(hiddenSize, inputSize + hiddenSize); bf = Vm.Zeros(hiddenSize); Wi = Vm.RandomMatrix(hiddenSize, inputSize + hiddenSize); bi = Vm.Zeros(hiddenSize); Wo = Vm.RandomMatrix(hiddenSize, inputSize + hiddenSize); bo = Vm.Zeros(hiddenSize); Wc = Vm.RandomMatrix(hiddenSize, inputSize + hiddenSize); bc = Vm.Zeros(hiddenSize); // slots this.slotCount = slotCount; this.slotDim = hiddenSize; // keep same as hidden for simplicity Wq = Vm.RandomMatrix(slotDim, hiddenSize); Wk = Vm.RandomMatrix(slotDim, slotDim); Wv = Vm.RandomMatrix(slotDim, slotDim); slotWriteBias = Vm.Zeros(slotCount); slots = new float[slotCount][]; for (int s = 0; s < slotCount; s++) slots[s] = Vm.Zeros(slotDim); adversarial = new AdversarialDetector(advDim); int beliefDim = hiddenSize + advDim; depthEstimator = new DepthDerivativeEstimator(beliefDim, maxDepth: maxDepth); int flatStateDim = hiddenSize + (nTracks * trackSize); mirrorModel = new MirrorModel(flatStateDim, oppDim: advDim); nTimeMemory = new NTimeTermMemory(inputSize: hiddenSize, memorySize: trackSize, numTracks: nTracks, modSize: 3); int depthCtrlInputDim = 6; depthController = new DepthController(depthCtrlInputDim, maxDepth); participantGate = new ParticipantCountGate(inputDim: 5, maxParticipants: maxParticipants); LoopGate = new LoopGate(maxDepth: maxDepth, maxSeconds: 3, maxIters: 50); } public (float[] h, float[] c, float[][] memTracks, float[] mirrorState, float paranoia, float depthDeriv) Step(float[] x, float[] hPrev, float[] cPrev, float[] mirrorPrev, float[] opponentSignal, float resourceFraction = 1.0f, float taskUrgency = 0.5f) { // 1) Implicit self-attention: compute query from hPrev var q = Vm.MatVec(Wq, hPrev); var logits = new float[slotCount]; for (int s = 0; s < slotCount; s++) { var k = Vm.MatVec(Wk, slots[s]); float dot = 0f; for (int kidx = 0; kidx < slotDim; kidx++) dot += q[kidx] * k[kidx]; logits[s] = dot / (float)Math.Sqrt(slotDim); } var weights = Vm.Softmax(logits); var attnSummary = Vm.Zeros(slotDim); for (int s = 0; s < slotCount; s++) { var v = Vm.MatVec(Wv, slots[s]); for (int kidx = 0; kidx < slotDim; kidx++) attnSummary[kidx] += weights[s] * v[kidx]; } // 2) Build gate input: concat x and attnSummary var gateInput = Vm.Concat(x, attnSummary); // 3) LSTM-like gates var f_raw = Vm.Add(Vm.MatVec(Wf, gateInput), bf); var i_raw = Vm.Add(Vm.MatVec(Wi, gateInput), bi); var o_raw = Vm.Add(Vm.MatVec(Wo, gateInput), bo); var c_raw = Vm.Add(Vm.MatVec(Wc, gateInput), bc); var f = Vm.Sigmoid(f_raw); var i = Vm.Sigmoid(i_raw); var o = Vm.Sigmoid(o_raw); var cCand = Vm.Tanh(c_raw); // 4) Mirror prediction and paranoia var memConcatPrev = nTimeMemory.ReadConcat(); var flatStatePrev = Vm.Concat(hPrev, memConcatPrev); var mirrorPred = mirrorModel.PredictMirrorState(flatStatePrev, opponentSignal); float paranoia = opponentSignal != null ? adversarial.Suspicion(opponentSignal) : 0f; // 5) Participant Tokenization (Replacing static ParticipantCountGate) // We calculate "Uniqueness" by seeing how much the opponent signal deviates from our Mirror expectation float[] oppSig = opponentSignal ?? Vm.Zeros(advDim); float diff = oppSig.Length > 0 ? oppSig[0] : 0f; float prod = oppSig.Length > 1 ? oppSig[1] : 0f; float lastOurAction = oppSig.Length > 2 ? oppSig[2] : 0f; // Neuron-firing logic: If the Mirror Model fails to predict the signal, we 'fire' the uniqueness neuron var mirrorDiff = Vm.SubtractPad(mirrorPred, Vm.Mul(oppSig, -1f)); float mirrorError = Vm.Norm2(mirrorDiff); float participantToken = (float)Math.Tanh(mirrorError * (1f + paranoia)); // Update the public property to reflect this new implicit 'count' for logging LastEstimatedParticipants = 1.0f + (participantToken * (maxparticipants - 1)); // 6) Depth controller and LoopGate float hNorm = Vm.Norm2(hPrev); var depthFeatures = new[] { paranoia, diff, prod, lastOurAction, hNorm, LastEstimatedParticipants }; var (chosenDepth, depthProbs) = depthController.ChooseDepth(depthFeatures); bool loopAllowed = LoopGate.TryEnter(paranoia, 0f, resourceFraction, taskUrgency, chosenDepth); LastLoopAllowed = loopAllowed; float depthDeriv = 0f; if (loopAllowed) { var beliefBase = Vm.Concat(hPrev, oppSig); var b_d = (float[])beliefBase.Clone(); for (int dIter = 0; dIter < chosenDepth; dIter++) { if (!LoopGate.Step()) { loopAllowed = false; break; } b_d = depthEstimator.BeliefStep(b_d); } var b_dp1 = (float[])beliefBase.Clone(); for (int dIter = 0; dIter < chosenDepth + 1; dIter++) { if (!LoopGate.Step()) { loopAllowed = false; break; } b_dp1 = depthEstimator.BeliefStep(b_dp1); } if (loopAllowed) { var depthDiff = new float[b_d.Length]; for (int kidx = 0; kidx < b_d.Length; kidx++) depthDiff[kidx] = b_dp1[kidx] - b_d[kidx]; float raw = Vm.Norm2(depthDiff); depthDeriv = raw / (1f + raw); } } LastChosenDepth = chosenDepth; // 7) Memory modulation and cell update // Use the participantToken directly in the memory modulation var mods = new[] { paranoia, depthDeriv, participantToken }; nTimeMemory.Step(hPrev, mods); // --- ENHANCED GATING & RESIDUAL LOGIC --- float alpha = 0.5f; var iMod = new float[hiddenSize]; var fMod = new float[hiddenSize]; // Aggression Scaling: reduces dampening if simulation was successful float aggressionScale = (loopAllowed && paranoia > 0.4f) ? 0.5f : 1.0f; for (int j = 0; j < hiddenSize; j++) { float paranoiaDampening = paranoia * (1f + 0.5f * participantToken); iMod[j] = i[j] * (1f - (paranoiaDampening * aggressionScale)); fMod[j] = Vm.Clamp(f[j] + alpha * paranoiaDampening, 0f, 1f); } float depthGate = 1f - depthDeriv; var c = new float[hiddenSize]; for (int j = 0; j < hiddenSize; j++) { // Residual path: tiny whisper of the candidate always gets through float candidate = cCand[j] * depthGate; c[j] = (fMod[j] * cPrev[j]) + (iMod[j] * candidate) + (0.01f * cCand[j]); } var tanhC = Vm.Tanh(c); var h = new float[hiddenSize]; for (int j = 0; j < hiddenSize; j++) { // Final Output with participant-aware neuron firing // This 'detokenizes' the participantToken back into the hidden state float baseH = o[j] * tanhC[j]; h[j] = (baseH * (1f - 0.1f * participantToken)) + (0.05f * hPrev[j]); } // 8) Update slots with gated writes for (int s = 0; s < slotCount; s++) { float sim = 0f; for (int kidx = 0; kidx < slotDim; kidx++) sim += h[kidx] * slots[s][kidx]; float gate = 1f / (1f + (float)Math.Exp(-(sim * 0.01f + slotWriteBias[s]))); for (int kidx = 0; kidx < slotDim; kidx++) { float candidate = (float)Math.Tanh(slots[s][kidx] + 0.05f * h[kidx]); slots[s][kidx] = (1f - gate) * slots[s][kidx] + gate * candidate; } } // 9) Update mirror state via EMA var mirrorNew = new float[mirrorPrev.Length]; float mirrorUpdateRate = 0.5f; for (int j = 0; j < mirrorPrev.Length; j++) mirrorNew[j] = (1f - mirrorUpdateRate) * mirrorPrev[j] + mirrorUpdateRate * mirrorPred[j]; return (h, c, nTimeMemory.Tracks, mirrorNew, paranoia, depthDeriv); } // =============================== // Simple opponent policy // =============================== public class SimpleOpponent { private readonly Random rng = new Random(); public float ChooseAction(float ourAction) { float baseVal = -ourAction; float noise = (float)(rng.NextDouble() * 0.4 - 0.2); float outVal = baseVal + noise; if (outVal > 1f) outVal = 1f; if (outVal < -1f) outVal = -1f; return outVal; } } // =============================== // Game demo // =============================== public static class Program2 { public static void Main() { int timeSteps = 30; int inputSize = 8; // must match hiddenSize for simplicity in gateInput concat int hiddenSize = 8; int advDim = 3; int memLen = 5; int nTracks = 3; int trackSize = 6; int slotCount = 8; int maxDepth = 4; int maxParticipants = 5; var cell = new ParanoidMirrorLstmCell(inputSize, hiddenSize, advDim, nTracks, trackSize, slotCount, maxDepth, maxParticipants); // Wire human override and kill switch for demonstration cell.LoopGate.HumanOverride = () => false; // set true to force allow cell.LoopGate.KillSwitch = () => false; // set true to force stop var memoryBank = new float[memLen, inputSize]; for (int t = 0; t < memLen; t++) { var row = Vm.RandomVector(inputSize, 0.2f); for (int j = 0; j < inputSize; j++) memoryBank[t, j] = row[j]; } cell.MemoryBank = memoryBank; var h = Vm.Zeros(hiddenSize); var c = Vm.Zeros(hiddenSize); var mirror = Vm.Zeros(hiddenSize + nTracks * trackSize); var rnd = new Random(); float[] RandomInput() { var v = new float[inputSize]; for (int i = 0; i < inputSize; i++) v[i] = (float)(rnd.NextDouble() * 2 - 1); return v; } var opponent = new SimpleOpponent(); float lastOurAction = 0f; Console.WriteLine("t\tour\topp\tdiff\tparanoia\tdepthDeriv\tdepth\tloopAllowed\testParts\th0"); for (int t = 0; t < timeSteps; t++) { var x = RandomInput(); float ourAction = (float)Math.Tanh(h[0]); float oppAction = opponent.ChooseAction(ourAction); float diff = ourAction - oppAction; float prod = ourAction * oppAction; var opponentSignal = new[] { diff, prod, lastOurAction }; var (hNew, cNew, memTracks, mirrorNew, paranoia, depthDeriv) = cell.Step(x, h, c, mirror, opponentSignal, resourceFraction: 0.9f, taskUrgency: 0.5f); h = hNew; c = cNew; mirror = mirrorNew; lastOurAction = ourAction; Console.WriteLine($"{t}\t{ourAction:F3}\t{oppAction:F3}\t{diff:F3}\t{paranoia:F3}\t\t{depthDeriv:F3}\t\t{cell.LastChosenDepth}\t{cell.LastLoopAllowed}\t{cell.LastEstimatedParticipants:F2}\t{h[0]:F3}"); } Console.WriteLine("Final hidden state sample:"); Console.WriteLine(string.Join(", ", h)); } } } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
✅ API Glitch (Docs Leak)
CSS | 5 min ago | 0.45 KB
I made $15,000 in 2 days
CSS | 5 min ago | 0.45 KB
This summer smells like money
CSS | 6 min ago | 0.45 KB
HELLO PROGRAMMER
1 hour ago | 0.03 KB
Untitled
5 hours ago | 2.26 KB
FB2600 User Handbook v0.91
1 day ago | 6.06 KB
FB2600 Administrator & Moderator SOP
1 day ago | 8.13 KB
Untitled
1 day ago | 7.38 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!