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

Commit de7fc0b

Browse files
Shashi456rxwei
authored andcommitted
Add L1 & L2 losses and respective tests (#231)
1 parent e5a7d2c commit de7fc0b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Sources/TensorFlow/Loss.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// Returns the L1 loss between predictions and labels.
16+
///
17+
/// - Parameters:
18+
/// - predicted: Predicted outputs from a neural network.
19+
/// - labels: Expected values, i.e. targets, that correspond to the correct output.
20+
@differentiable(wrt: predicted)
21+
public func l1Loss<Scalar: TensorFlowFloatingPoint>(
22+
predicted: Tensor<Scalar>, expected: Tensor<Scalar>
23+
) -> Tensor<Scalar> {
24+
return abs(expected - predicted).sum()
25+
}
26+
27+
/// Returns the L2 loss between predictions and labels.
28+
///
29+
/// - Parameters:
30+
/// - predicted: Predicted outputs from a neural network.
31+
/// - labels: Expected values, i.e. targets, that correspond to the correct output.
32+
@differentiable(wrt: predicted)
33+
public func l2Loss<Scalar: TensorFlowFloatingPoint>(
34+
predicted: Tensor<Scalar>, expected: Tensor<Scalar>
35+
) -> Tensor<Scalar> {
36+
return (expected - predicted).squared().sum()
37+
}
38+
1539
/// Returns the mean squared error between predictions and labels.
1640
///
1741
/// - Parameters:

Tests/TensorFlowTests/LossTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import XCTest
1616
@testable import TensorFlow
1717

1818
final class LossTests: XCTestCase {
19+
func testL1Loss() {
20+
let predicted = Tensor<Float>([1, 2, 3, 4])
21+
let expected = Tensor<Float>([0.1, 0.2, 0.3, 0.4])
22+
let loss = l1Loss(predicted: predicted, expected: expected)
23+
let expectedLoss: Float = 9.0
24+
assertElementsEqual(expected: Tensor(expectedLoss), actual: loss)
25+
}
26+
27+
func testL2Loss() {
28+
let predicted = Tensor<Float>([1, 2, 3, 4])
29+
let expected = Tensor<Float>([0.5, 1.5, 2.5, 3.5])
30+
let loss = l2Loss(predicted: predicted, expected: expected)
31+
let expectedLoss: Float = 1.0
32+
assertElementsEqual(expected: Tensor(expectedLoss), actual: loss)
33+
}
34+
1935
func testMeanSquaredErrorLoss() {
2036
let predicted = Tensor<Float>(shape: [2, 4], scalars: [1, 2, 3, 4, 5, 6, 7, 8])
2137
let expected = Tensor<Float>(
@@ -210,6 +226,8 @@ final class LossTests: XCTestCase {
210226
}
211227

212228
static var allTests = [
229+
("testL1Loss", testL1Loss),
230+
("testL2Loss", testL2Loss),
213231
("testMeanSquaredErrorLoss", testMeanSquaredErrorLoss),
214232
("testMeanSquaredErrorGrad", testMeanSquaredErrorGrad),
215233
("testMeanSquaredLogarithmicError", testMeanSquaredLogarithmicError),

0 commit comments

Comments
 (0)