diff --git a/Sources/CliTool/Subcommands/EvaluateCommand.swift b/Sources/CliTool/Subcommands/EvaluateCommand.swift index 4256273..9c59159 100644 --- a/Sources/CliTool/Subcommands/EvaluateCommand.swift +++ b/Sources/CliTool/Subcommands/EvaluateCommand.swift @@ -66,7 +66,9 @@ extension Subcommands { } } } - let json = try JSONEncoder().encode(result) + let encoder = JSONEncoder() + encoder.outputFormatting = [.prettyPrinted, .sortedKeys] + let json = try encoder.encode(result) if let outputFilePath { try json.write(to: URL(fileURLWithPath: outputFilePath)) @@ -149,12 +151,12 @@ extension Subcommands { self.outputs = outputs do { // entropyを示す - let expValues = outputs.map { exp(Double($0.score)) } + let mean = outputs.reduce(into: 0) { $0 += Double($1.score) } / Double(outputs.count) + let expValues = outputs.map { exp(Double($0.score) - mean) } let sumOfExpValues = expValues.reduce(into: 0, +=) // 確率値に補正 - let probs = expValues.map { $0 / sumOfExpValues } - let entropy = -probs.reduce(into: 0) { $0 += $1 * log($1) } - self.entropy = entropy + let probs = outputs.map { exp(Double($0.score) - mean) / sumOfExpValues } + self.entropy = -probs.reduce(into: 0) { $0 += $1 * log($1) } } do { self.max_rank = outputs.firstIndex { diff --git a/Sources/CliTool/Subcommands/RunCommand.swift b/Sources/CliTool/Subcommands/RunCommand.swift index 0c9c0b7..cbe9395 100644 --- a/Sources/CliTool/Subcommands/RunCommand.swift +++ b/Sources/CliTool/Subcommands/RunCommand.swift @@ -40,10 +40,11 @@ extension Subcommands { } if self.onlyWholeConversion { // entropyを示す - let expValues = mainResults.map { exp(Double($0.value)) } + let mean = mainResults.reduce(into: 0) { $0 += Double($1.value) } / Double(mainResults.count) + let expValues = mainResults.map { exp(Double($0.value) - mean) } let sumOfExpValues = expValues.reduce(into: 0, +=) // 確率値に補正 - let probs = expValues.map { $0 / sumOfExpValues } + let probs = mainResults.map { exp(Double($0.value) - mean) / sumOfExpValues } let entropy = -probs.reduce(into: 0) { $0 += $1 * log($1) } print("\(bold: "Entropy:") \(entropy)") } diff --git a/install_cli.sh b/install_cli.sh old mode 100644 new mode 100755