@@ -1251,18 +1251,6 @@ public extension Tensor where Scalar : Numeric & Comparable {
1251
1251
}
1252
1252
1253
1253
public extension Tensor where Scalar : Numeric {
1254
- // NOTE: This overload is necessary, otherwise `mean()` would refer
1255
- // to the variadic method `mean(squeezingAxes:)` with zero indices.
1256
- @differentiable (
1257
- wrt: self , vjp: _vjpMean ( )
1258
- where Scalar : TensorFlowFloatingPoint
1259
- )
1260
- @inlinable @inline ( __always)
1261
- func mean( ) -> Tensor {
1262
- let axes = Tensor < Int32 > ( rangeFrom: 0 , to: rank, stride: 1 )
1263
- return Raw . mean ( self , reductionIndices: axes)
1264
- }
1265
-
1266
1254
// NOTE: This overload is necessary, otherwise `sum()` would refer
1267
1255
// to the variadic method `sum(squeezingAxes:)` with zero indices.
1268
1256
@inlinable @inline ( __always)
@@ -1283,30 +1271,37 @@ public extension Tensor where Scalar : Numeric {
1283
1271
return Raw . prod ( self , reductionIndices: axes)
1284
1272
}
1285
1273
1286
- /// Returns the arithmetic mean along the specified axes. The reduced
1287
- /// dimensions are removed.
1288
- /// - Parameter axes: The dimensions to reduce.
1289
- /// - Precondition: Each value in `axes` must be in the range `-rank...rank`.
1274
+ // NOTE: This overload is necessary, otherwise `mean()` would refer
1275
+ // to the variadic method `mean(squeezingAxes:)` with zero indices.
1276
+ @differentiable (
1277
+ wrt: self , vjp: _vjpMean ( )
1278
+ where Scalar : TensorFlowFloatingPoint
1279
+ )
1290
1280
@inlinable @inline ( __always)
1291
- func mean( squeezingAxes axes : [ Int32 ] ) -> Tensor {
1292
- return Raw . mean ( self , reductionIndices : Tensor < Int32 > ( axes ) ,
1293
- keepDims : false )
1281
+ func mean( ) -> Tensor {
1282
+ let axes = Tensor < Int32 > ( rangeFrom : 0 , to : rank , stride : 1 )
1283
+ return Raw . mean ( self , reductionIndices : axes )
1294
1284
}
1295
1285
1296
- /// Returns the arithmetic mean along the specified axes. The reduced
1297
- /// dimensions are removed.
1298
- /// - Parameter axes: The dimensions to reduce.
1299
- /// - Precondition: Each value in `axes` must be in the range `-rank...rank`.
1286
+ // NOTE: This overload is necessary, otherwise `mean()` would refer
1287
+ // to the variadic method `mean(squeezingAxes:)` with zero indices.
1288
+ @differentiable ( wrt: self where Scalar : TensorFlowFloatingPoint)
1300
1289
@inlinable @inline ( __always)
1301
- func mean( squeezingAxes axes: Int32 ... ) -> Tensor {
1302
- return mean ( squeezingAxes: axes)
1290
+ func variance( ) -> Tensor {
1291
+ let mean = self . mean ( )
1292
+ let squaredDiff = ( self - mean) . squared ( )
1293
+ return squaredDiff. mean ( )
1303
1294
}
1304
1295
1305
1296
/// Returns the sum along the specified axes. The reduced dimensions are
1306
1297
/// removed.
1307
1298
/// - Parameter axes: The dimensions to reduce.
1308
1299
/// - Precondition: Each value in `axes` must be in the range `-rank...rank`.
1309
1300
@inlinable @inline ( __always)
1301
+ @differentiable (
1302
+ wrt: self , vjp: _vjpSum ( squeezingAxes: )
1303
+ where Scalar : TensorFlowFloatingPoint
1304
+ )
1310
1305
func sum( squeezingAxes axes: [ Int32 ] ) -> Tensor {
1311
1306
return Raw . sum ( self , reductionIndices: Tensor < Int32 > ( axes) , keepDims: false )
1312
1307
}
@@ -1340,36 +1335,48 @@ public extension Tensor where Scalar : Numeric {
1340
1335
}
1341
1336
1342
1337
/// Returns the arithmetic mean along the specified axes. The reduced
1343
- /// dimensions are retained with value 1 .
1338
+ /// dimensions are removed .
1344
1339
/// - Parameter axes: The dimensions to reduce.
1345
- /// - Precondition: Each value in `axes` must be in the range `-rank..< rank`.
1340
+ /// - Precondition: Each value in `axes` must be in the range `-rank... rank`.
1346
1341
@inlinable @inline ( __always)
1347
1342
@differentiable (
1348
- wrt: self , vjp: _vjpMean ( alongAxes : )
1343
+ wrt: self , vjp: _vjpMean ( squeezingAxes : )
1349
1344
where Scalar : TensorFlowFloatingPoint
1350
1345
)
1351
- func mean( alongAxes axes: Tensor < Int32 > ) -> Tensor {
1352
- return Raw . mean ( self , reductionIndices: axes, keepDims: true )
1346
+ func mean( squeezingAxes axes: [ Int32 ] ) -> Tensor {
1347
+ return Raw . mean ( self , reductionIndices: Tensor < Int32 > ( axes) ,
1348
+ keepDims: false )
1353
1349
}
1354
1350
1355
1351
/// Returns the arithmetic mean along the specified axes. The reduced
1356
- /// dimensions are retained with value 1.
1352
+ /// dimensions are removed.
1353
+ /// - Parameter axes: The dimensions to reduce.
1354
+ /// - Precondition: Each value in `axes` must be in the range `-rank...rank`.
1355
+ @inlinable @inline ( __always)
1356
+ func mean( squeezingAxes axes: Int32 ... ) -> Tensor {
1357
+ return mean ( squeezingAxes: axes)
1358
+ }
1359
+
1360
+ /// Returns the variance along the specified axes. The reduced dimensions are
1361
+ /// retained with value 1. Does not apply Bessel's correction.
1357
1362
/// - Parameter axes: The dimensions to reduce.
1358
1363
/// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1359
1364
@inlinable @inline ( __always)
1360
1365
@differentiable ( wrt: self where Scalar : TensorFlowFloatingPoint)
1361
- func mean ( alongAxes axes: [ Int32 ] ) -> Tensor {
1362
- return mean ( alongAxes : Tensor < Int32 > ( axes) )
1366
+ func variance ( squeezingAxes axes: Int32 ... ) -> Tensor {
1367
+ return variance ( squeezingAxes : axes)
1363
1368
}
1364
1369
1365
- /// Returns the arithmetic mean along the specified axes. The reduced
1366
- /// dimensions are retained with value 1 .
1370
+ /// Returns the variance along the specified axes. The reduced dimensions are
1371
+ /// removed. Does not apply Bessel's correction .
1367
1372
/// - Parameter axes: The dimensions to reduce.
1368
1373
/// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1369
1374
@inlinable @inline ( __always)
1370
1375
@differentiable ( wrt: self where Scalar : TensorFlowFloatingPoint)
1371
- func mean( alongAxes axes: Int32 ... ) -> Tensor {
1372
- return mean ( alongAxes: axes)
1376
+ func variance( squeezingAxes axes: [ Int32 ] ) -> Tensor {
1377
+ let mean = self . mean ( alongAxes: axes)
1378
+ let squaredDiff = ( self - mean) . squared ( )
1379
+ return squaredDiff. mean ( squeezingAxes: axes)
1373
1380
}
1374
1381
1375
1382
/// Returns the sum along the specified axes. The reduced dimensions are
@@ -1395,6 +1402,60 @@ public extension Tensor where Scalar : Numeric {
1395
1402
return sum ( alongAxes: axes)
1396
1403
}
1397
1404
1405
+ /// Returns the product along the specified axes. The reduced dimensions are
1406
+ /// retained with value 1.
1407
+ /// - Parameter axes: The dimensions to reduce.
1408
+ /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1409
+ @inlinable @inline ( __always)
1410
+ func product( alongAxes axes: [ Int32 ] ) -> Tensor {
1411
+ return Raw . prod ( self , reductionIndices: Tensor < Int32 > ( axes) , keepDims: true )
1412
+ }
1413
+
1414
+ /// Returns the product along the specified axes. The reduced dimensions are
1415
+ /// retained with value 1.
1416
+ /// - Parameter axes: The dimensions to reduce.
1417
+ /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1418
+ @inlinable @inline ( __always)
1419
+ func product( alongAxes axes: Int32 ... ) -> Tensor {
1420
+ return product ( alongAxes: axes)
1421
+ }
1422
+
1423
+ /// Returns the arithmetic mean along the specified axes. The reduced
1424
+ /// dimensions are retained with value 1.
1425
+ /// - Parameter axes: The dimensions to reduce.
1426
+ /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1427
+ @inlinable @inline ( __always)
1428
+ @differentiable (
1429
+ wrt: self , vjp: _vjpMean ( alongAxes: )
1430
+ where Scalar : TensorFlowFloatingPoint
1431
+ )
1432
+ func mean( alongAxes axes: Tensor < Int32 > ) -> Tensor {
1433
+ return Raw . mean ( self , reductionIndices: axes, keepDims: true )
1434
+ }
1435
+
1436
+ /// Returns the arithmetic mean along the specified axes. The reduced
1437
+ /// dimensions are retained with value 1.
1438
+ /// - Parameter axes: The dimensions to reduce.
1439
+ /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1440
+ @inlinable @inline ( __always)
1441
+ @differentiable (
1442
+ wrt: self , vjp: _vjpMean ( alongAxes: )
1443
+ where Scalar : TensorFlowFloatingPoint
1444
+ )
1445
+ func mean( alongAxes axes: [ Int32 ] ) -> Tensor {
1446
+ return mean ( alongAxes: Tensor < Int32 > ( axes) )
1447
+ }
1448
+
1449
+ /// Returns the arithmetic mean along the specified axes. The reduced
1450
+ /// dimensions are retained with value 1.
1451
+ /// - Parameter axes: The dimensions to reduce.
1452
+ /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1453
+ @inlinable @inline ( __always)
1454
+ @differentiable ( wrt: self where Scalar : TensorFlowFloatingPoint)
1455
+ func mean( alongAxes axes: Int32 ... ) -> Tensor {
1456
+ return mean ( alongAxes: axes)
1457
+ }
1458
+
1398
1459
/// Returns the variance along the specified axes. The reduced dimensions are
1399
1460
/// retained with value 1. Does not apply Bessel's correction.
1400
1461
/// - Parameter axes: The dimensions to reduce.
@@ -1426,24 +1487,6 @@ public extension Tensor where Scalar : Numeric {
1426
1487
func variance( alongAxes axes: [ Int32 ] ) -> Tensor {
1427
1488
return variance ( alongAxes: Tensor < Int32 > ( axes) )
1428
1489
}
1429
-
1430
- /// Returns the product along the specified axes. The reduced dimensions are
1431
- /// retained with value 1.
1432
- /// - Parameter axes: The dimensions to reduce.
1433
- /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1434
- @inlinable @inline ( __always)
1435
- func product( alongAxes axes: [ Int32 ] ) -> Tensor {
1436
- return Raw . prod ( self , reductionIndices: Tensor < Int32 > ( axes) , keepDims: true )
1437
- }
1438
-
1439
- /// Returns the product along the specified axes. The reduced dimensions are
1440
- /// retained with value 1.
1441
- /// - Parameter axes: The dimensions to reduce.
1442
- /// - Precondition: Each value in `axes` must be in the range `-rank..<rank`.
1443
- @inlinable @inline ( __always)
1444
- func product( alongAxes axes: Int32 ... ) -> Tensor {
1445
- return product ( alongAxes: axes)
1446
- }
1447
1490
}
1448
1491
1449
1492
//===----------------------------------------------------------------------===//
0 commit comments