Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Bug fix for Tensor.logSumExp. #551

Merged
merged 13 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/TensorFlow/Operators/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2372,8 +2372,8 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
func logSumExp(squeezingAxes axes: Tensor<Int32>) -> Tensor {
let rawMax = max(alongAxes: axes)
let offset = withoutDerivative(at: rawMax) { rawMax in
rawMax.replacing(
with: Tensor<Scalar>(zerosLike: rawMax),
Tensor<Scalar>(zerosLike: rawMax).replacing(
with: rawMax,
where: rawMax.isFinite)
}
let result = TensorFlow.log(TensorFlow.exp(self - offset).sum(squeezingAxes: axes))
Expand Down Expand Up @@ -2436,8 +2436,8 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
func logSumExp(alongAxes axes: Tensor<Int32>) -> Tensor {
let rawMax = max(alongAxes: axes)
let offset = withoutDerivative(at: rawMax) { rawMax in
rawMax.replacing(
with: Tensor<Scalar>(zerosLike: rawMax),
Tensor<Scalar>(zerosLike: rawMax).replacing(
with: rawMax,
where: rawMax.isFinite)
}
let result = TensorFlow.log(TensorFlow.exp(self - offset).sum(alongAxes: axes))
Expand Down
6 changes: 6 additions & 0 deletions Tests/TensorFlowTests/OperatorTests/MathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ final class MathOperatorTests: XCTestCase {
assertEqual(y0, expectedY0, accuracy: 0.0001)
assertEqual(y1, expectedY1, accuracy: 0.0001)
assertEqual(y2, expectedY2, accuracy: 0.0001)

let xSmall = Tensor<Float>([
-301.9475, -265.2244, -275.77475, -235.28029, -277.2509, -396.6921, -400.01385])
let ySmall = xSmall.logSumExp()
let expectedYSmall = Tensor<Float>(-235.28029)
assertEqual(ySmall, expectedYSmall, accuracy: 0.0001)
}

func testMoments() {
Expand Down