static Real Evaluate(FunctionStack <Real> model, int[] dataset) { FunctionStack <Real> predictModel = DeepCopyHelper <Real> .DeepCopy(model); predictModel.ResetState(); Real totalLoss = 0; long totalLossCount = 0; for (int i = 0; i < dataset.Length - 1; i++) { NdArray <Real> x = new NdArray <Real>(new[] { 1 }, BATCH_SIZE); NdArray <int> t = new NdArray <int>(new[] { 1 }, BATCH_SIZE); for (int j = 0; j < BATCH_SIZE; j++) { x.Data[j] = dataset[j + i]; t.Data[j] = dataset[j + i + 1]; } NdArray <Real> result = predictModel.Forward(x)[0]; Real sumLoss = new SoftmaxCrossEntropy <Real>().Evaluate(result, t); totalLoss += sumLoss; totalLossCount++; } //calc perplexity return(Math.Exp(totalLoss / (totalLossCount - 1))); }
public static void SingleOutputBackward(NdArray <Real> y, NdArray <Real> x) { for (int i = 0; i < y.Grad.Length; i++) { x.Grad[i] += 1 / Math.Sqrt(-x.Data[i] * x.Data[i] + 1) * y.Grad[i]; } }
public void OnRender() { // Use our shader. shader.UseProgram(GameObject.Transform.Matrix); // TODO: This is shit and inefficient. Get rid of it. shader.SetMatrix("view", Camera.ActiveCamera.GetViewMatrix()); shader.SetMatrix("projection", Camera.ActiveCamera.GetProjectionMatrix()); if (tintColor != null) { var start = new Vector4(KauTheme.Lightest.R, KauTheme.Lightest.G, KauTheme.Lightest.B, KauTheme.Lightest.A); var end = new Vector4(KauTheme.HighLight.R, KauTheme.HighLight.G, KauTheme.HighLight.B, KauTheme.HighLight.A); var color = Vector4.Lerp(start, end, 0.5f + Mathf.Sin(Time.GameTime) / 2) / byte.MaxValue; shader.SetVector4((int)tintColor, color); } // Bind the vertex array to use with the triangles. GL.BindVertexArray(vertexArray); // Draw the element buffer object. (triangles) GL.DrawElements(PrimitiveType.Triangles, triangles.Length, DrawElementsType.UnsignedInt, 0); // Un-bind the vertex array. GL.BindVertexArray(0); }
public static NdArray <Real> SingleInputForward(NdArray <Real> x, Real beta, Real betaInv, IFunction <Real> softplus) { Real[] y = new Real[x.Data.Length]; for (int b = 0; b < x.BatchCount; b++) { for (int i = 0; i < x.Length; i++) { y[i + b * x.Length] = x.Data[i + b * x.Length] * beta; } //0との比較が必要 Real maxval = 0; for (int i = 0; i < x.Length; i++) { if (maxval < y[i + b * x.Length]) { maxval = y[i + b * x.Length]; } } for (int i = 0; i < x.Length; i++) { y[i + b * x.Length] = (maxval + Math.Log(1.0f + Math.Exp(-Math.Abs(x.Data[i + b * x.Length] * beta)))) * betaInv; } } return(NdArray.Convert(y, x.Shape, x.BatchCount, softplus)); }
public static Real GetAlphaT(Real alpha, Real beta1, Real beta2, long updateCount) { Real fix1 = 1 - Math.Pow(beta1, updateCount); Real fix2 = 1 - Math.Pow(beta2, updateCount); return(alpha * Math.Sqrt(fix2) / fix1); }
static Real CalPs(FunctionStack <Real> model, List <int> s) { Real sum = 0; NdArray <Real> h = new NdArray <Real>(new Real[N_UNITS]); for (int i = 1; i < s.Count; i++) { //l1 Linear NdArray <Real> xK = model.Functions[0].Forward(s[i])[0]; //l2 Linear NdArray <Real> l2 = model.Functions[1].Forward(h)[0]; for (int j = 0; j < xK.Data.Length; j++) { xK.Data[j] += l2.Data[j]; } //l2 Tanh h = model.Functions[2].Forward(xK)[0]; //l3 Softmax(l3 Linear) NdArray <Real> yv = model.Functions[4].Forward(model.Functions[3].Forward(h))[0]; Real pi = yv.Data[s[i - 1]]; sum -= Math.Log(pi, 2); } return(sum); }
public static void SingleOutputBackward(NdArray <Real> y, NdArray <Real> x) { for (int i = 0; i < y.Grad.Length; i++) { x.Grad[i] += Math.Sin(x.Data[i]) * -y.Grad[i]; } }
public static void SingleOutputBackward(NdArray <Real> y, NdArray <Real> x, Real beta) { for (int i = 0; i < x.Grad.Length; i++) { x.Grad[i] += (1.0f - 1.0f / (1.0f + Math.Exp(beta * y.Data[i]))) * y.Grad[i]; } }
public void Update(float deltaTime) { var positions = new Span <PositionComponent>(positionComponents); var velocityConstraints = new Span <VelocityConstraintComponent>(constraintComponents); var velocities = new Span <VelocityComponent>(velocityComponents); var modifiers = new Span <VelocityModifierComponent>(modifierComponents); var sizes = new Span <SizeComponent>(this.sizeComponents); for (int i = this.velocityModifierOffset; i < this.length; i++) { for (int j = 0; j < this.velocityModifierOffset; j++) { var diff = positions[j].Value - positions[i].Value; var modifierIndex = i - this.velocityModifierOffset; if (MathF.Pow(sizes[modifierIndex].Value, 2) < diff.LengthSquared()) { continue; } var velocity = velocities[j]; var velocityValue = velocity.Value; velocityValue = velocityValue + velocityValue * modifiers[modifierIndex].Value * deltaTime; velocity.Value = velocityValue.ClampMagnitude(velocityConstraints[j].Min, velocityConstraints[j].Max); velocities[j] = velocity; } } }
public void Jump() { HealthManager.Exhaust(IsSprinting ? 0.2f : 0.05f); Velocity += new Vector3(0f, MathF.Sqrt(2f * (float)Gravity * 1.2f), 0f); Network?.EntityAction((int)EntityId, EntityAction.Jump); }
private void GameTick(object state) { if (World == null) { return; } if (_initiated) { _tickTime++; if (World.Player != null && Client.HasSpawned) { // player.IsSpawned = Spawned; if (World.Player.IsFlying != _flying) { _flying = World.Player.IsFlying; McpeAdventureSettings settings = McpeAdventureSettings.CreateObject(); settings.flags = GetAdventureFlags(); Client.SendPacket(settings); //SendPlayerAbilities(player); } var pos = (PlayerLocation)World.Player.KnownPosition.Clone(); if (pos.DistanceTo(_lastSentLocation) > 0.0f || MathF.Abs(pos.HeadYaw - _lastSentLocation.HeadYaw) > 0.0f || MathF.Abs(pos.Pitch - _lastSentLocation.Pitch) > 0.0f) { SendLocation(pos); _lastSentLocation = pos; } if ((pos.DistanceTo(_lastLocation) > 16f || MathF.Abs(pos.HeadYaw - _lastLocation.HeadYaw) >= 5.0f) && (_tickTime - _lastPrioritization >= 10)) { World.ChunkManager.FlagPrioritization(); SendLocation(pos); _lastLocation = pos; UnloadChunks(new ChunkCoordinates(pos), Client.ChunkRadius + 3); _lastPrioritization = _tickTime; } } if (_tickTime % 20 == 0 && CustomConnectedPong.CanPing) { Client.SendPing(); } World.Player.OnTick(); World.EntityManager.Tick(); World.PhysicsEngine.Tick(); } }
public static Real BackwardActivate(Real gy, Real y, Real x) { Real sp = Softplus(x); Real tsp = Math.Tanh(sp); Real grad_tsp = (1 - tsp * tsp) * (1 - Math.Exp(-sp)); return(gy * (x * grad_tsp + tsp)); }
public static void SingleOutputBackward(NdArray <Real> y, NdArray <Real> x) { for (int i = 0; i < y.Grad.Length; i++) { Real gx = Math.Cos(x.Data[i]); x.Grad[i] += 1 / (gx * gx) * y.Grad[i]; } }
//初期値が入力されなかった場合、この関数で初期化を行う public static void InitHeNorm(NdArray <Real> array, Real masterScale = 1) { Real s = masterScale * Math.Sqrt(2.0f / array.Length); for (int i = 0; i < array.Data.Length; i++) { array.Data[i] = Broth.RandomNormal(s); } }
public override RayCastResult Intersection(Ray ray, float nowbest) { { float distance = (Position - ray.Origin).Length(); if (nowbest + R < distance) { return(null); } } Vector3f A = ray.Origin, B = ray.Direction, C = Position; Float a = Vector3f.Dot(B, B); Float b = Vector3f.Dot(B, (A - C)) * 2.0f; Float c = (A - C).LengthSquared() - R * R; float drt = b * b - 4 * a * c; if (drt < 0) { return(null); } drt = Math.Sqrt(drt); float x1 = (-b + drt) / a / 2; float x2 = (-b - drt) / a / 2; if (x1 < 0 && x2 < 0) { return(null); } float d; if (x1 > 0 && x2 > 0) { d = Math.Max(x1, x2); } else if (x1 > 0) { d = x1; } else { d = x2; } RayCastResult result = new RayCastResult(); //result.happened = true; result.obj = this; result.material = Material; result.coords = ray.Origin + ray.Direction * d; result.distance = d; result.normal = Vector3f.Normalize(result.coords - Position); return(result); }
public static void UpdateFunctionParameters(Real learningRate, Real alpha, Real epsilon, NdArray <Real> functionParameter, Real[] ms) { for (int i = 0; i < functionParameter.Data.Length; i++) { Real grad = functionParameter.Grad[i]; ms[i] *= alpha; ms[i] += (1 - alpha) * grad * grad; functionParameter.Data[i] -= learningRate * grad / (Math.Sqrt(ms[i]) + epsilon); } }
public static void UpdateFunctionParameters(Real learningRate, Real epsilon, Real[] h, NdArray <Real> functionParameter) { for (int i = 0; i < functionParameter.Data.Length; i++) { Real grad = functionParameter.Grad[i]; h[i] += grad * grad; functionParameter.Data[i] -= learningRate * grad / (Math.Sqrt(h[i]) + epsilon); } }
public static NdArray <Real> SingleInputForward(NdArray <Real> x, IFunction <Real> arcSin) { Real[] resultData = new Real[x.Data.Length]; for (int i = 0; i < resultData.Length; i++) { resultData[i] = Math.Asin(x.Data[i]); } return(new NdArray <Real>(resultData, x.Shape, x.BatchCount, arcSin)); }
static Real Softplus(Real x) { if (x > 20) { return(x); } if (x < -20) { return(Math.Exp(x)); } return(Math.Log(Math.Exp(x) + 1)); }
static SinCosTableF() { var ct = new T[256]; var att = new T[256]; for (int i = 0; i < ct.Length; i++) { ct[i] = M.Cos(invCosTableFactor * i); att[i] = M.Atan(invAtanTableFactor * i); } _cosTable = ct; _atanTable = att; }
/// <summary> /// 返回折射光方向 /// </summary> /// <param name="dir"></param> /// <param name="niOverNt">入射折射率/出射折射率</param> /// <returns></returns> public static Vector3f Refract(Vector3f dir, Vector3f normal, float niOverNt) { dir = -dir; Float cosAlpha = Vector3f.Dot(dir, normal); Float sinAlphaSq = 1.0f - cosAlpha * cosAlpha; Float sinBetaSq = niOverNt * niOverNt * sinAlphaSq; Float cosBetaSq = 1.0f - sinBetaSq; Float cosBeta = Math.Sqrt(cosBetaSq); Vector3f re = ((normal * cosAlpha) - dir) * niOverNt - normal * cosBeta; return(re); }
public void UpdatePlayerPosition(PlayerLocation location) { var oldPosition = Player.KnownPosition; if (!ChunkManager.TryGetChunk(new ChunkCoordinates(location), out _)) { Player.WaitingOnChunk = true; } Player.KnownPosition = location; Player.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, location)); }
private static Vector3 FlipX(Vector3 origin, Vector3 size) { if (origin.X >= 0) { origin.X = -(((MathF.Abs(origin.X) / size.X) + 1) * size.X); } else { origin.X = ((MathF.Abs(origin.X) / size.X) - 1) * size.X; } return(origin); }
public void Update(GameTime elapsed) { float dt = (float)elapsed.ElapsedGameTime.TotalMilliseconds / 50f; foreach (var entity in PhysicsEntities.ToArray()) { try { if (entity is Entity e) { if (e.NoAi) { continue; } UpdateEntity(e, dt); TruncateVelocity(e, dt); var boundingBox = e.GetBoundingBox(e.KnownPosition); bool anySolid = false; foreach (var corner in boundingBox.GetCorners().Where(x => x.Y == boundingBox.Min.Y)) { var blockcoords = new BlockCoordinates( new PlayerLocation( corner.X, Math.Floor(corner.Y - 0.01f), corner.Z)); var block = World.GetBlock(blockcoords.X, blockcoords.Y, blockcoords.Z); var blockBoundingBox = block.GetBoundingBox(blockcoords); //..onGround = onGround || block.Solid; if (block.Solid && MathF.Abs(boundingBox.Min.Y - blockBoundingBox.Max.Y) <= 0.05f) { anySolid = true; break; } } e.KnownPosition.OnGround = anySolid; } } catch (Exception ex) { Log.Warn(ex, $"Entity tick threw exception: {ex.ToString()}"); } } sw.Restart(); }
public void UpdateEntityPosition(long entityId, PlayerLocation position, bool relative = false, bool updateLook = false, bool updatePitch = false) { if (EntityManager.TryGet(entityId, out Entity entity)) { entity.KnownPosition.OnGround = position.OnGround; if (!relative) { var oldPosition = entity.KnownPosition; entity.KnownPosition = position; //if (entity is PlayerMob p) { entity.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, position)); } } else { var oldPosition = entity.KnownPosition; float offset = 0f; /* if (this.ServerType == ServerType.Bedrock) * { * offset = (float) entity.PositionOffset; * }*/ //TODO: Fix position offset entity.KnownPosition.X += position.X; entity.KnownPosition.Y += (position.Y - offset); entity.KnownPosition.Z += position.Z; //if (entity is PlayerMob p) { entity.DistanceMoved += MathF.Abs(Vector3.Distance(oldPosition, entity.KnownPosition)); } //entity.KnownPosition.Move(position); } if (updateLook) { //entity.KnownPosition.Yaw = position.Yaw; if (updatePitch) { entity.KnownPosition.Pitch = position.Pitch; } entity.KnownPosition.HeadYaw = position.HeadYaw; // entity.UpdateHeadYaw(position.HeadYaw); } } }
static Real PiecewiseConstant(long globalCount, int[] boundaries, Real baseValue) { Real result = baseValue; for (int i = 0; i < boundaries.Length; i++) { if (boundaries[i] < globalCount) { result = baseValue / Math.Pow(3.0f, i + 1); } } return(result); }
public override void genericMaskUpdate(NdArray <Real> mask, NdArray <Real> weight) { Real[] scoreDrop = new Real[weight.Length]; Real[] scoreGrow = new Real[weight.Length]; for (int i = 0; i < scoreDrop.Length; i++) { scoreDrop[i] = Math.Abs(mask.Data[i] * weight.Data[i]);//マスク前の重みにマスクを掛ける 元の実装だとここに1e-5の正規乱数が足される scoreGrow[i] = mask.Grad[i]; } //マスクと重みを更新 Update(scoreDrop, scoreGrow, mask, weight); }
public DebugWorldGenerator() { foreach (var state in BlockFactory.AllBlockstatesByName.OrderByDescending( x => x.Value.GetVariants().Any(xx => xx.Block.Animated))) { foreach (var variant in state.Value.GetVariants()) { ALL_VALID_STATES.Add(variant); } } GRID_WIDTH = (int)MathF.Ceiling(MathF.Sqrt(ALL_VALID_STATES.Count)); GRID_HEIGHT = (int)MathF.Ceiling((float)ALL_VALID_STATES.Count / GRID_WIDTH); }
public NdArray <Real> Make() { NdArray <Real> result = new NdArray <Real>(this.stepsPerCycle * this.numberOfCycles); for (int i = 0; i < this.numberOfCycles; i++) { for (int j = 0; j < this.stepsPerCycle; j++) { result.Data[j + i * this.stepsPerCycle] = Math.Sin(j * 2 * Math.PI / this.stepsPerCycle); } } return(result); }
public static void InitXavier(NdArray <Real> array) { Real fanOut = array.Shape.Length > 1 ? array.Shape[array.Shape.Length - 2] : array.Shape[array.Shape.Length - 1]; Real fanIn = array.Shape[array.Shape.Length - 1]; Real n = (fanIn + fanOut) / 2.0f; Real limit = Math.Sqrt(3.0f / n); for (int i = 0; i < array.Data.Length; i++) { array.Data[i] = (limit * 2.0f) * Broth.Random() - limit; } }