22using System ;
33using static TorchSharp . PInvoke . NativeMethods ;
44
5+ #nullable enable
56namespace TorchSharp
67{
78 public static partial class torch
@@ -10,10 +11,27 @@ public static partial class nn
1011 {
1112 public static partial class functional
1213 {
14+ /// <summary>
15+ /// Perform normalization of inputs over specified dimension.
16+ /// </summary>
17+ /// <param name="input">Input tensor of any shape.</param>
18+ /// <param name="p">the exponent value in the norm formulation</param>
19+ /// <param name="dim">the dimension to reduce</param>
20+ /// <param name="eps">small value to avoid division by zero</param>
21+ public static Tensor normalize ( Tensor input , double p = 2.0 , long dim = 1L , double eps = 1e-12 )
22+ {
23+ var res = THSNN_normalize (
24+ input . Handle ,
25+ p , dim , eps ) ;
26+ if ( res == IntPtr . Zero )
27+ torch . CheckForErrors ( ) ;
28+ return new Tensor ( res ) ;
29+ }
30+
1331 /// <summary>
1432 /// Applies Batch Normalization for each channel across a batch of data.
1533 /// </summary>
16- public static Tensor batch_norm ( Tensor input , Tensor running_mean , Tensor running_var , Tensor weight = null , Tensor bias = null , bool training = false , double momentum = 0.1 , double eps = 1e-5 )
34+ public static Tensor batch_norm ( Tensor input , Tensor running_mean , Tensor running_var , Tensor ? weight = null , Tensor ? bias = null , bool training = false , double momentum = 0.1 , double eps = 1e-5 )
1735 {
1836 var res = THSNN_batch_norm (
1937 input . Handle ,
@@ -31,7 +49,7 @@ public static Tensor batch_norm(Tensor input, Tensor running_mean, Tensor runnin
3149 /// <summary>
3250 /// Applies Group Normalization for last certain number of dimensions.
3351 /// </summary>
34- public static Tensor group_norm ( Tensor input , long num_groups , Tensor weight = null , Tensor bias = null , double eps = 1e-5 )
52+ public static Tensor group_norm ( Tensor input , long num_groups , Tensor ? weight = null , Tensor ? bias = null , double eps = 1e-5 )
3553 {
3654 var res = THSNN_group_norm (
3755 input . Handle ,
@@ -47,7 +65,7 @@ public static Tensor group_norm(Tensor input, long num_groups, Tensor weight = n
4765 /// <summary>
4866 /// Applies Instance Normalization for each channel in each data sample in a batch.
4967 /// </summary>
50- public static Tensor instance_norm ( Tensor input , Tensor running_mean = null , Tensor running_var = null , Tensor weight = null , Tensor bias = null , bool use_input_stats = true , double momentum = 0.1 , double eps = 1e-5 )
68+ public static Tensor instance_norm ( Tensor input , Tensor ? running_mean = null , Tensor ? running_var = null , Tensor ? weight = null , Tensor ? bias = null , bool use_input_stats = true , double momentum = 0.1 , double eps = 1e-5 )
5169 {
5270 var res = THSNN_instance_norm (
5371 input . Handle ,
@@ -65,7 +83,7 @@ public static Tensor instance_norm(Tensor input, Tensor running_mean = null, Ten
6583 /// <summary>
6684 /// Applies Layer Normalization for last certain number of dimensions.
6785 /// </summary>
68- public static Tensor layer_norm ( Tensor input , long [ ] normalized_shape , Tensor weight = null , Tensor bias = null , double eps = 1e-5 )
86+ public static Tensor layer_norm ( Tensor input , long [ ] normalized_shape , Tensor ? weight = null , Tensor ? bias = null , double eps = 1e-5 )
6987 {
7088 IntPtr res ;
7189 unsafe {
0 commit comments