Skip to content

Commit d7587f5

Browse files
committed
BUG: Refuse to standardize a one-channel feature
Its unbiased deviation over one channel is NaN, which the clamp does not catch, so every distance came out non-finite. KonfAI refuses it alike.
1 parent d9440a4 commit d7587f5

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎include/ImpactLoss.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ class Loss
223223

224224
/** Each voxel's (or point's) feature vector along `channelDim` scaled as a model configuration's
225225
* FeatureNormalization says: `l2` to unit length, `standardized` to zero mean and unit (unbiased) standard deviation
226-
* over its channels, `none` unchanged. Applied to every kept layer before PCA, the channel subset and the distance. */
226+
* over its channels, `none` unchanged. Applied to every kept layer before PCA, the channel subset and the distance.
227+
* A one-channel feature has no deviation to standardize by, and is refused. */
227228
inline torch::Tensor
228229
NormalizeFeatureChannels(const torch::Tensor & features, const std::string & mode, int64_t channelDim)
229230
{
@@ -237,6 +238,10 @@ NormalizeFeatureChannels(const torch::Tensor & features, const std::string & mod
237238
}
238239
if (mode == "standardized")
239240
{
241+
if (features.size(channelDim) == 1)
242+
{
243+
throw std::runtime_error("A one-channel feature cannot be standardized over its channels: use none or l2.");
244+
}
240245
return (features - features.mean(channelDim, /*keepdim=*/true)) /
241246
features.std(channelDim, /*unbiased=*/true, /*keepdim=*/true).clamp_min(1e-6);
242247
}

0 commit comments

Comments
 (0)