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

Adding kullback Leibler Divergence #226

Merged
merged 1 commit into from
Jun 13, 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
12 changes: 12 additions & 0 deletions Sources/TensorFlow/Loss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public func poissonLoss<Scalar: TensorFlowFloatingPoint>(
return (predicted - expected * log(predicted)).mean()
}

/// Returns the Kullback-Leibler divergence between predictions and expectations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably say "between expectations and predictions" and also add the actual equation because KL divergence is not symmetric and so this may lead people to thinking the opposite of what's intended.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shashi456 could you possibly make a new PR addressing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it. I'll have it up in a few hours.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

///
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
public func kullbackLeiblerDivergence<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>, expected: Tensor<Scalar>
) -> Tensor<Scalar> {
return (expected * log(expected / predicted)).sum()
}

/// Computes the softmax cross entropy (categorical cross entropy) between logits and labels.
///
/// - Parameters:
Expand Down
9 changes: 9 additions & 0 deletions Tests/TensorFlowTests/LossTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ final class LossTests: XCTestCase {
assertElementsEqual(expected: Tensor(expectedLoss), actual: loss)
}

func testKullbackLeiblerDivergence() {
let predicted = Tensor<Float>([0.2, 0.3, 0.4])
let expected = Tensor<Float>([1.0, 4.0, 3.0])
let loss = kullbackLeiblerDivergence(predicted: predicted, expected: expected)
let expectedLoss: Float = 18.015217
assertElementsEqual(expected: Tensor(expectedLoss), actual: loss)
}

func testSoftmaxCrossEntropyWithProbabilitiesLoss() {
let logits = Tensor<Float>(shape: [2, 4], scalars: [1, 2, 3, 4, 5, 6, 7, 8])
let labels = Tensor<Float>(
Expand Down Expand Up @@ -199,6 +207,7 @@ final class LossTests: XCTestCase {
("testMeanSquaredLogarithmicError", testMeanSquaredLogarithmicError),
("testMeanAbsoluteError", testMeanAbsoluteError),
("testHingeLoss", testHingeLoss),
("testKullbackLeiblerDivergence", testKullbackLeiblerDivergence),
("testCategoricalHingeLoss", testCategoricalHingeLoss),
("testSquaredHingeLoss", testSquaredHingeLoss),
("testPoissonLoss",testPoissonLoss),
Expand Down