@@ -15,6 +15,7 @@ limitations under the License.
1515******************************************************************************/
1616
1717using System ;
18+ using System . Xml . Linq ;
1819using Tensorflow . Keras . Losses ;
1920using static Tensorflow . Binding ;
2021
@@ -37,15 +38,57 @@ public static Tensor compute_weighted_loss(Tensor losses, Tensor sample_weight =
3738 } ) ;
3839 }
3940
40- public static ( Tensor , Tensor ) squeeze_or_expand_dimensions ( Tensor y_pred , Tensor sample_weight )
41+ public static ( Tensor , Tensor ) squeeze_or_expand_dimensions ( Tensor y_pred , Tensor y_true = null , Tensor sample_weight = null )
4142 {
43+ var y_pred_shape = y_pred . shape ;
44+ var y_pred_rank = y_pred_shape . ndim ;
45+ if ( y_true != null )
46+ {
47+ var y_true_shape = y_true . shape ;
48+ var y_true_rank = y_true_shape . ndim ;
49+ if ( y_true_rank > - 1 && y_pred_rank > - 1 )
50+ {
51+ if ( y_pred_rank - y_true_rank != 1 || y_pred_shape [ - 1 ] == 1 )
52+ {
53+ ( y_true , y_pred ) = remove_squeezable_dimensions ( y_true , y_pred ) ;
54+ }
55+ }
56+ }
57+
58+ if ( sample_weight == null )
59+ {
60+ return ( y_pred , y_true ) ;
61+ }
62+
4263 var weights_shape = sample_weight . shape ;
4364 var weights_rank = weights_shape . ndim ;
4465 if ( weights_rank == 0 )
4566 return ( y_pred , sample_weight ) ;
67+
68+ if ( y_pred_rank > - 1 && weights_rank > - 1 )
69+ {
70+ if ( weights_rank - y_pred_rank == 1 )
71+ {
72+ sample_weight = tf . squeeze ( sample_weight , - 1 ) ;
73+ }
74+ else if ( y_pred_rank - weights_rank == 1 )
75+ {
76+ sample_weight = tf . expand_dims ( sample_weight , - 1 ) ;
77+ }
78+ else
79+ {
80+ return ( y_pred , sample_weight ) ;
81+ }
82+ }
83+
4684 throw new NotImplementedException ( "" ) ;
4785 }
4886
87+ public static ( Tensor , Tensor ) remove_squeezable_dimensions ( Tensor labels , Tensor predictions , int expected_rank_diff = 0 , string name = null )
88+ {
89+ return ( labels , predictions ) ;
90+ }
91+
4992 public static Tensor reduce_weighted_loss ( Tensor weighted_losses , string reduction )
5093 {
5194 if ( reduction == ReductionV2 . NONE )
0 commit comments