Skip to content

Commit

Permalink
make improvements of the arrow shape render
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguigang committed Feb 10, 2025
1 parent eb75022 commit dd43f09
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 174 deletions.
27 changes: 12 additions & 15 deletions Data_science/MachineLearning/DeepLearning/RNN/CharRNN.vb
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,18 @@ Namespace RNN
End If
End Function

' Trains the network.
''' <summary>
''' Trains the network.
''' </summary>
''' <param name="options"></param>
''' <param name="net"></param>
''' <param name="snapshotName"></param>
Public Shared Sub train(options As Options, net As CharLevelRNN, snapshotName As String)
' Load the training set.

Dim trainingSet = StringTrainingSet.fromFile(options.inputFile)

Console.WriteLine("Data size: " & trainingSet.size().ToString() & ", vocabulary size: " & trainingSet.vocabularySize().ToString())
VBDebugger.EchoLine("Data size: " & trainingSet.size().ToString() & ", vocabulary size: " & trainingSet.vocabularySize().ToString())

' Initialize the network and its trainer.

Expand Down Expand Up @@ -158,16 +163,12 @@ Namespace RNN
End While
End Sub

' Saves a network snapshot with this name to file.
''' <summary>
''' Saves a network snapshot with this name to file.
''' </summary>
''' <param name="name"></param>
''' <param name="net"></param>
Public Shared Sub saveASnapshot(name As String, net As CharLevelRNN)
If ReferenceEquals(name, Nothing) Then
Throw New NullReferenceException("Network name can't be null.")
End If

If net Is Nothing Then
Throw New NullReferenceException("Network can't be null.")
End If

' Take a snapshot
Try
Using str As FileStream = New FileStream(name & ".snapshot", FileMode.Create, FileAccess.Write)
Expand All @@ -188,10 +189,6 @@ Namespace RNN
''' <param name="name"></param>
''' <returns></returns>
Public Shared Function loadASnapshot(name As String) As CharLevelRNN
If ReferenceEquals(name, Nothing) Then
Throw New NullReferenceException("Name can't be null.")
End If

Dim net As CharLevelRNN = Nothing

' Load the snapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Namespace RNN
' Immutable training set for a character level RNN.
Public Class StringTrainingSet
Implements TrainingSet

Private dataField As String ' Data from file.
Private alphabetField As Alphabet ' Alphabet extracted from data.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
#Region "Microsoft.VisualBasic::5b68e0afbe63fac08c109499e401e6b1, Data_science\MachineLearning\DeepLearning\RNN\net\interfaces\Trainable.vb"

' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.



' /********************************************************************************/

' Summaries:


' Code Statistics:

' Total Lines: 17
' Code Lines: 5 (29.41%)
' Comment Lines: 9 (52.94%)
' - Xml Docs: 0.00%
'
' Blank Lines: 3 (17.65%)
' File Size: 487 B


' Interface Trainable
'
' Function: forwardBackward
'
'
' /********************************************************************************/
' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.



' /********************************************************************************/

' Summaries:


' Code Statistics:

' Total Lines: 17
' Code Lines: 5 (29.41%)
' Comment Lines: 9 (52.94%)
' - Xml Docs: 0.00%
'
' Blank Lines: 3 (17.65%)
' File Size: 487 B


' Interface Trainable
'
' Function: forwardBackward
'
'
' /********************************************************************************/

#End Region

Namespace RNN
' Trainable neural network.

''' <summary>
''' Trainable neural network.
''' </summary>
Public Interface Trainable

'
Expand All @@ -65,7 +68,7 @@ Namespace RNN
' Returns the cross-entropy loss.
'
Function forwardBackward(ix As Integer(), iy As Integer()) As Double
End Interface

End Interface

End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports std = System.Math

Namespace Drawing2D

Expand All @@ -69,14 +70,22 @@ Namespace Drawing2D
Return s.shape
End Operator

#If NET48 Then
Shared ReadOnly boidArrow As Point() = New Point() {
New Point(0, 0), New Point(-4, -1),
New Point(0, 8),
New Point(4, -1), New Point(0, 0)
}

#If NET48 Then
#Disable Warning
''' <summary>
''' Arrow drawing for the winform application
''' </summary>
''' <param name="gfx"></param>
''' <param name="x"></param>
''' <param name="y"></param>
''' <param name="angle"></param>
''' <param name="color"></param>
Public Shared Sub RenderBoid(gfx As Graphics, x As Single, y As Single, angle As Single, color As Color)
Using brush = New SolidBrush(color)
gfx.TranslateTransform(x, y)
Expand All @@ -87,5 +96,43 @@ Namespace Drawing2D
End Sub
#Enable Warning
#End If

''' <summary>
''' Arrow drawing for the .net core console application
''' </summary>
Public Shared Sub RenderBoid(gfx As IGraphics, x As Single, y As Single, vx As Single, vy As Single, color As Color,
Optional l As Single = 8,
Optional w As Single = 3)
' 计算单位方向向量
Dim length As Single = std.Sqrt(vx * vx + vy * vy)
Dim ux As Single = vx / length
Dim uy As Single = vy / length

' 计算箭头的终点
Dim endX As Single = x + l * ux
Dim endY As Single = y + l * uy

' 定义箭头的头部大小
Dim arrowHeadSize As Single = 10

' 计算箭头两翼的顶点
Dim wing1X As Single = endX - arrowHeadSize * uy
Dim wing1Y As Single = endY + arrowHeadSize * ux
Dim wing2X As Single = endX - arrowHeadSize * uy
Dim wing2Y As Single = endY - arrowHeadSize * ux

' 绘制箭身
Call gfx.DrawLine(Pens.Black, x, y, endX, endY)

' 定义三角形的三个顶点
Dim points As PointF() = {
New PointF(endX, endY),
New PointF(wing1X, wing1Y),
New PointF(wing2X, wing2Y)
}

' 绘制实心黑色三角形作为箭头
Call gfx.FillPolygon(Brushes.Black, points)
End Sub
End Class
End Namespace
5 changes: 4 additions & 1 deletion gr/Microsoft.VisualBasic.Imaging/Drawing2D/Shapes/Shape.vb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ Imports Microsoft.VisualBasic.Imaging
Namespace Drawing2D.Shapes

''' <summary>
''' An abstract shape element with layout information data.(矢量图形)
''' An abstract shape element with layout information data.
''' </summary>
''' <remarks>
''' (矢量图形)
''' </remarks>
Public MustInherit Class Shape

''' <summary>
Expand Down
30 changes: 24 additions & 6 deletions gr/Microsoft.VisualBasic.Imaging/Drawing2D/Shapes/Triangle.vb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ Namespace Drawing2D.Shapes
Public Property Vertex3 As PointF
Public Property Angle As Single

' vertex1
' / \
' vertex2 -- vertex3

''' <summary>
''' get the possible shape size of the triangle shape object
''' </summary>
''' <returns>
''' width is the delta of the vertex2 and vertex3 x location;
''' height is the delta of the vertex1 with mean of vertex2 and vertex3 y location value.
''' </returns>
Public Overrides ReadOnly Property Size As SizeF
Get
Dim height As Double = Vertex1.Y - (Vertex2.Y + Vertex3.Y) / 2
Dim width As Double = Vertex3.X - Vertex2.X

Return New SizeF(width, height)
End Get
End Property

Sub New(Location As Point, Color As Color)
Call MyBase.New(Location)
Me.Color = Color
Expand All @@ -85,6 +105,10 @@ Namespace Drawing2D.Shapes
Me.Color = Color
End Sub

Sub New()
Call MyBase.New(Nothing)
End Sub

''' <summary>
''' 直角三角形
''' </summary>
Expand All @@ -94,12 +118,6 @@ Namespace Drawing2D.Shapes
Throw New NotImplementedException
End Function

Public Overrides ReadOnly Property Size As SizeF
Get
Throw New NotImplementedException
End Get
End Property

''' <summary>
'''
''' </summary>
Expand Down
Loading

0 comments on commit dd43f09

Please sign in to comment.