Something went wrong. Try again.
hackzurich 2023: finch - a simple neural net framework and visualizer that uses genetic networks to train
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091package ui
import ( "fmt" "log" "time"
"github.com/stepbrobd/finch/genetic"
stopwatch "github.com/charmbracelet/bubbles/stopwatch" tea "github.com/charmbracelet/bubbletea" lipgloss "github.com/charmbracelet/lipgloss" term "golang.org/x/term")
type Model struct { Model genetic.Algo Watch stopwatch.Model Msg AlgoMsg}
func InitialModel(algo *genetic.Algo) Model { return Model{ Model: *algo, Watch: stopwatch.NewWithInterval(time.Second), }}
func (m Model) Init() tea.Cmd { return tea.Batch( m.Generation(), m.Watch.Init(), tea.ClearScreen, tea.EnterAltScreen, )}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case AlgoMsg: m.Msg = msg return m, m.Generation() case tea.KeyMsg: switch msg.String() { case "esc", "ctrl+c": return m, tea.Quit } default: var cmd tea.Cmd m.Watch, cmd = m.Watch.Update(msg) return m, cmd } return m, nil}
func (m Model) View() string { w, _, err := term.GetSize(0) if err != nil { log.Fatalf("failed to get terminal size: %v", err) }
view := lipgloss.NewStyle(). Bold(true). Underline(true). Render("Finch") + "\n\n"
view += "Elapsed: " + m.Watch.View() + fmt.Sprintf(" | Generation: %d", (m.Msg.Generation)) + fmt.Sprintf(" | Error: %.25f", m.Msg.ErrorRate) + "\n\n\n"
view += "Weights" + "\n\n" for i := range m.Msg.Weights { view += MkViewRed(m.Model.GetWeights(), i) + "\n" } view += "\n\n"
view += "Biases" + "\n\n" for i := range m.Msg.Biases { view += MkViewGreen(m.Model.GetBiases(), i) + "\n" } view += "\n\n"
view += "Outputs" + "\n\n" for i := range m.Msg.Outputs { view += MkViewBlue(m.Model.GetOutputs(), i) + "\n" }
return lipgloss.PlaceHorizontal(w, lipgloss.Center, view)}