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

Adding L1 & L2 Losses and respective tests #231

Merged
merged 6 commits into from
Jun 14, 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
24 changes: 24 additions & 0 deletions Sources/TensorFlow/Loss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// Returns the L1 loss between predictions and labels.
///
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - labels: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
public func l1Loss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>, expected: Tensor<Scalar>
) -> Tensor<Scalar> {
return abs(expected - predicted).sum()
}

/// Returns the L2 loss between predictions and labels.
///
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - labels: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
public func l2Loss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>, expected: Tensor<Scalar>
) -> Tensor<Scalar> {
return (expected - predicted).squared().sum()
}

/// Returns the mean squared error between predictions and labels.
///
/// - Parameters:
Expand Down
18 changes: 18 additions & 0 deletions Tests/TensorFlowTests/LossTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ import XCTest
@testable import TensorFlow

final class LossTests: XCTestCase {
func testL1Loss() {
let predicted = Tensor<Float>([1, 2, 3, 4])
let expected = Tensor<Float>([0.1, 0.2, 0.3, 0.4])
let loss = l1Loss(predicted: predicted, expected: expected)
let expectedLoss: Float = 9.0
assertElementsEqual(expected: Tensor(expectedLoss), actual: loss)
}

func testL2Loss() {
let predicted = Tensor<Float>([1, 2, 3, 4])
let expected = Tensor<Float>([0.5, 1.5, 2.5, 3.5])
let loss = l2Loss(predicted: predicted, expected: expected)
let expectedLoss: Float = 1.0
assertElementsEqual(expected: Tensor(expectedLoss), actual: loss)
}

func testMeanSquaredErrorLoss() {
let predicted = Tensor<Float>(shape: [2, 4], scalars: [1, 2, 3, 4, 5, 6, 7, 8])
let expected = Tensor<Float>(
Expand Down Expand Up @@ -202,6 +218,8 @@ final class LossTests: XCTestCase {
}

static var allTests = [
("testL1Loss", testL1Loss),
("testL2Loss", testL2Loss),
("testMeanSquaredErrorLoss", testMeanSquaredErrorLoss),
("testMeanSquaredErrorGrad", testMeanSquaredErrorGrad),
("testMeanSquaredLogarithmicError", testMeanSquaredLogarithmicError),
Expand Down