-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathTensorBoardWriter.swift
59 lines (52 loc) · 2.08 KB
/
TensorBoardWriter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
import Foundation
import TensorFlow
import TrainingLoop
/// Returns a TrainingLoop callback that logs training and validation statistics
/// to be consumed by tensorboard.
public func tensorBoardStatisticsLogger<L: TrainingLoopProtocol>(
logRootDirectory: String = "run/tensorboard/stats"
) -> TrainingLoopCallback<L> {
let logDirTimestampPrefix = String(Date().timeIntervalSince1970)
// globalTrainBatchIndex will hold value iff the stats recorded per batch and in training phrase.
var globalTrainBatchIndex: Int? = nil
var summaryWriters: [String: SummaryWriter] = [:]
let globalQueue = DispatchQueue.global()
return { (loop, event) throws -> Void in
if event != .batchEnd { return }
guard let epochIndex = loop.epochIndex, let batchIndex = loop.batchIndex,
let batchCount = loop.batchCount, let stats = loop.lastStatsLog
else {
return
}
let learningPhase = Context.local.learningPhase == .inference ? "validation" : "train"
if learningPhase == "train" && batchIndex + 1 != batchCount {
globalTrainBatchIndex = 0
} else if learningPhase == "validation" {
globalTrainBatchIndex = nil
}
if globalTrainBatchIndex != nil {
globalTrainBatchIndex = epochIndex * batchCount + batchIndex
}
for stat in stats {
let writerKey = stat.name + "/" + learningPhase
var writer: SummaryWriter
if let stored = summaryWriters[writerKey] {
writer = stored
} else {
writer = SummaryWriter(
logDirectory: URL(fileURLWithPath: logRootDirectory, isDirectory: true)
.appendingPathComponent(logDirTimestampPrefix).appendingPathComponent(writerKey).path)
summaryWriters[writerKey] = writer
}
globalQueue.async {
if batchIndex + 1 == batchCount {
writer.addScalar(tag: "epoch_" + stat.name, value: stat.value, step: Int64(epochIndex))
}
if let globalTrainBatchIndex = globalTrainBatchIndex {
writer.addScalar(
tag: "batch_" + stat.name, value: stat.value, step: Int64(globalTrainBatchIndex))
}
}
}
}
}