public void Update(PointF2D point, PointF2D grad) { _sum[0] = _q * _sum[0] + grad.X * grad.X; _sum[1] = _q * _sum[1] + grad.Y * grad.Y; point.X = point.X - _lr * grad.X / (float)Math.Sqrt(_sum[0]); point.Y = point.Y - _lr * grad.Y / (float)Math.Sqrt(_sum[1]); }
public void Update(PointF2D point, PointF2D grad) { _l2[0] = _l2[0] + grad.X * grad.X; _l2[1] = _l2[1] + grad.Y * grad.Y; point.X = point.X - _lr * grad.X / (float)Math.Sqrt(_l2[0] + 0.0000001f); point.Y = point.Y - _lr * grad.Y / (float)Math.Sqrt(_l2[1] + 0.0000001f); }
public void Update(PointF2D point, PointF2D grad) { _v[0] = _m * _v[0] + grad.X * _lr; _v[1] = _m * _v[1] + grad.Y * _lr; point.X = point.X - _v[0]; point.Y = point.Y - _v[1]; }
public PointF2D getBlockPoint(float x, float y) { PointF2D point = new PointF2D(); point.X = (_center.X / _offset + x) * _offset; point.Y = (_center.Y / _offset - y) * _offset; return(point); }
public void drawGradients(Graphics graphics, ArrayList xyPoints, ArrayList uvPoints) { float maxUV = getMaxUV(uvPoints); for (int index = 0; index < xyPoints.Count; index++) { PointF2D xyPoint = (PointF2D)xyPoints[index]; PointF2D uvPoint = (PointF2D)uvPoints[index]; drawGradient(graphics, xyPoint, uvPoint.X, uvPoint.Y, maxUV); } }
private float getMaxUV(ArrayList uvPoints) { float maxUV = 0.0f; for (int index = 0; index < uvPoints.Count; index++) { PointF2D uvPoint = (PointF2D)uvPoints[index]; float sum = Math.Abs(uvPoint.X) + Math.Abs(uvPoint.Y); if (maxUV < sum) { maxUV = sum; } } return(maxUV); }
PointF2D Fun(Function fun, float x, float y) { PointF2D grad = new PointF2D(); float fun1 = 0.0f; float fun2 = 0.0f; float h = 1e-4f; fun1 = fun.Formula((x + h), y); fun2 = fun.Formula((x - h), y); grad.X = (fun1 - fun2) / (h * 2); fun1 = fun.Formula(x, (y + h)); fun2 = fun.Formula(x, (y - h)); grad.Y = (fun1 - fun2) / (h * 2); return(grad); }
public void Update(PointF2D point, PointF2D grad) { _b1Sum[0] = _beta1 * _b1Sum[0] + (1.0f - _beta1) * grad.X; _b1Sum[1] = _beta1 * _b1Sum[1] + (1.0f - _beta1) * grad.Y; _b2Sum[0] = _beta2 * _b2Sum[0] + (1.0f - _beta2) * grad.X * grad.X; _b2Sum[1] = _beta2 * _b2Sum[1] + (1.0f - _beta2) * grad.Y * grad.Y; float[] b1Fix = new float[2]; b1Fix[0] = _b1Sum[0] / (1.0f - (float)Math.Pow(_beta1, _iter) + 0.00000001f); b1Fix[1] = _b1Sum[1] / (1.0f - (float)Math.Pow(_beta1, _iter) + 0.00000001f); float[] b2Fix = new float[2]; b2Fix[0] = _b2Sum[0] / (1.0f - (float)Math.Pow(_beta2, _iter) + 0.00000001f); b2Fix[1] = _b2Sum[1] / (1.0f - (float)Math.Pow(_beta2, _iter) + 0.00000001f); point.X = point.X - _lr * b1Fix[0] / ((float)Math.Sqrt(b2Fix[0]) + 0.00000001f); point.Y = point.Y - _lr * b1Fix[1] / ((float)Math.Sqrt(b2Fix[1]) + 0.00000001f); _iter++; }
public void drawGradient(Graphics graphics, PointF2D point, float U, float V, float maxSum) { // 鄰邊 = U = x // 對邊 = V = y float absU = Math.Abs(U); float absV = Math.Abs(V); // atan取弧度(tan = 對邊 / 鄰邊 float radian = absU != 0 ? (float)Math.Atan(absV / absU) : (float)(Math.PI * 0.5); Console.WriteLine(radian / Math.PI * 180); // cos = 鄰邊 / 斜邊, sin = 對邊 / 斜邊 float xCos = U != 0.0f ? (float)Math.Cos(radian) : 1.0f; float ySin = V != 0.0f ? (float)Math.Sin(radian) : 0.0f; float xDirection = U < 0.0f ? -1.0f : U > 0 ? 1.0f : 0.0f; float yDirection = V < 0.0f ? 1.0f : V > 0 ? -1.0f : 0.0f; // 計算顯示長度比例 float max = absU > absV ? absU : absV; float maxLen = (max / maxSum * _offset); for (float index = 0; index < maxLen; index += 0.01f) { // 取得斜邊 // 取得x + 方向和y + 方向(對邊) float bevel = index / xCos; float x = index * xDirection; float y = ySin * bevel * yDirection; if (Math.Abs(y) > maxLen) { return; } drawPointLine(graphics, Brushes.Black, point.X + x, point.Y + y); } }
private void DrawMomentum(Graphics graphics, DrawF draw) { ArrayList history = new ArrayList(); PointF2D point = new PointF2D(-4.0f, 2.0f); Function2 fun = new Function2(); Momentum optimizer = new Momentum(0.07f, 0.9f); for (int index = 0; index < 30; index++) { PointF2D xyPoint = draw.getBlockPoint(point.X, point.Y); history.Add(xyPoint); PointF2D diff = fun.DiffFormula(point.X, point.Y); optimizer.Update(point, diff); } PointF2D prePoint = ((PointF2D)history[0]); for (int index = 0; index < 30; index++) { draw.drawPoint(graphics, Brushes.Blue, ((PointF2D)history[index])); draw.drawLine(graphics, prePoint, ((PointF2D)history[index])); prePoint = ((PointF2D)history[index]); } }
private void DrawGradient(Graphics graphics, DrawF draw) { ArrayList xyPoints = new ArrayList(); ArrayList uvPoints = new ArrayList(); //float[] x = { -2, -1, 0, 1, -2, -1, 0, 1 }; //float[] y = { -2, -2, -2, -2, 1, 1, 1, 1 }; float[] x = { -2, -1, 0, 1, -2, -1, 0, 1 }; float[] y = { -2, -2, -2, -2, 1, 1, 1, 1 }; Function2 fun = new Function2(); for (int index = 0; index < 8; index++) { PointF2D xyPoint = draw.getBlockPoint(x[index], y[index]); PointF2D uvPoint = Fun(fun, -x[index], -y[index]); Console.Write(uvPoint.X + " " + uvPoint.Y); xyPoints.Add(xyPoint); uvPoints.Add(uvPoint); draw.drawPoint(graphics, Brushes.Blue, xyPoint); } draw.drawGradients(graphics, xyPoints, uvPoints); }
public void drawLine(Graphics graphics, PointF2D pStart, PointF2D pEnd) { drawLine(graphics, pStart.X, pStart.Y, pEnd.X, pEnd.Y); }
public DrawF(float offset) { _center = new PointF2D(5.0f * offset, 5.0f * offset); _offset = offset; }
public void drawGradient(Graphics graphics, PointF2D point, float U, float V) { float sum = Math.Abs(V) + Math.Abs(U); drawGradient(graphics, point, U, V, sum); }
public void drawGradient(Graphics graphics, PointF2D xyPoint, PointF2D uvPoint) { float sum = Math.Abs(uvPoint.X) + Math.Abs(uvPoint.Y); drawGradient(graphics, xyPoint, uvPoint.X, uvPoint.Y, sum); }
/* DrawF Params: * offset :point's size * x : x's count * y : y's count */ public DrawF(float offset, float x, float y) { _center = new PointF2D(x * offset, y * offset); _offset = offset; }
public void drawPoint(Graphics graphics, Brush color, PointF2D point) { drawPoint(graphics, color, point.X, point.Y); }
public void Update(PointF2D point, PointF2D grad) { point.X = point.X - grad.X * _lr; point.Y = point.Y - grad.Y * _lr; }