Class: Daimond::Utils::TrainingLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/daimond/utils/training_logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = 'training_log.csv') ⇒ TrainingLogger

Returns a new instance of TrainingLogger.



6
7
8
9
10
11
12
13
14
# File 'lib/daimond/utils/training_logger.rb', line 6

def initialize(filename = 'training_log.csv')
  @filename = filename
  @history = []
  @start_time = Time.now

  CSV.open(@filename, 'w') do |csv|
    csv << ['epoch', 'loss', 'accuracy', 'time_elapsed']
  end
end

Instance Method Details

#log(epoch, loss, accuracy) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/daimond/utils/training_logger.rb', line 16

def log(epoch, loss, accuracy)
  elapsed = Time.now - @start_time
  @history << {epoch: epoch, loss: loss, accuracy: accuracy, time: elapsed}

  CSV.open(@filename, 'a') do |csv|
    csv << [epoch, loss, accuracy, elapsed.round(2)]
  end
end

#plot_accuracy(width: 60, height: 10) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/daimond/utils/training_logger.rb', line 58

def plot_accuracy(width: 60, height: 10)
  return if @history.empty?

  accs = @history.map { |h| h[:accuracy] }
  min_acc = [accs.min, 0].min
  max_acc = [accs.max, 100].max
  range = max_acc - min_acc
  range = 100 if range == 0

  puts "\nšŸ“ˆ Accuracy Curve:"
  puts "-" * (width + 10)

  height.times do |row|
    y_val = min_acc + ((height - row) * range / height)
    line = sprintf("%6.1f%%|", y_val)
    line = line.ljust(width + 9)  # <-- Важно!

    @history.each_with_index do |h, i|
      x_pos = (i * (width - 1) / [@history.size - 1, 1].max).to_i
      idx = 8 + x_pos

      if idx < line.length && (h[:accuracy] - y_val).abs < (range / height / 2)
        line[idx] = "ā˜…"
      end
    end

    puts line
  end

  puts "       +" + "-" * width
  puts "       Epoch: 1" + " " * (width - 10) + "#{@history.size}"
end

#plot_loss(width: 60, height: 10) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/daimond/utils/training_logger.rb', line 25

def plot_loss(width: 60, height: 10)
  return if @history.empty?

  losses = @history.map { |h| h[:loss] }
  min_loss = losses.min
  max_loss = losses.max
  range = max_loss - min_loss
  range = 1.0 if range == 0

  puts "\nšŸ“‰ Loss Curve:"
  puts "-" * (width + 10)

  height.times do |row|
    y_val = max_loss - (row * range / height)
    line = sprintf("%6.3f |", y_val)
    line = line.ljust(width + 9)  # <-- Важно!

    @history.each_with_index do |h, i|
      x_pos = (i * (width - 1) / [@history.size - 1, 1].max).to_i
      idx = 8 + x_pos

      if idx < line.length && (h[:loss] - y_val).abs < (range / height / 2)
        line[idx] = "ā—"
      end
    end

    puts line
  end

  puts "       +" + "-" * width
  puts "       Epoch: 1" + " " * (width - 10) + "#{@history.size}"
end

#summaryObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/daimond/utils/training_logger.rb', line 91

def summary
  return if @history.empty?

  first = @history.first
  last = @history.last

  puts "\nšŸ“Š Training Summary:"
  puts "=" * 50
  puts "Duration:        #{(last[:time] / 60).round(1)} minutes"
  puts "Epochs:          #{last[:epoch]}"
  puts "Initial Loss:    #{first[:loss].round(4)}"
  puts "Final Loss:      #{last[:loss].round(4)}"
  puts "Initial Acc:     #{first[:accuracy].round(2)}%"
  puts "Final Acc:       #{last[:accuracy].round(2)}%"
  puts "Improvement:     +#{(last[:accuracy] - first[:accuracy]).round(2)}%"
  puts "=" * 50
  puts "Log saved to:    #{@filename}"
end