如何将一个矩阵数组标准化,使得每个矩阵等于其自身除以其l-2范数?

huangapple go评论60阅读模式
英文:

How to normalize an ndarray of matrices, such that each matrix equals itself divided by its l-2 norm?

问题

我有一个形状为(60000, 1, 28, 28)的ndarray,实际上是60000个28x28的矩阵。它们代表像素值,我需要通过它们的l-2范数来归一化每个图像。基本上,我需要将每个28x28矩阵除以其自己的l-2范数。

有没有关于如何高效地完成这个任务的建议,而不必遍历整个数组?

我尝试了以下代码,但似乎没有起作用:

norms = np.linalg.norm(training_data, ord=2, axis=0)
normalized_training_data = training_data / norms

但我得到了一堆0和NaN。

英文:

So I have a an ndarray of shape (60000, 1, 28, 28) which is essentially 60000 28x28 matrices. They represent pixel values and I need to normalize each image by its l-2 norm. Essentially, I have to divide each 28x28 matrix by its own l-2 norm.

Any suggestions as to how I can do this efficiently without looping through the entire array?

I tried the following code but it didn't seem to work:

norms = np.linalg.norm(training_data, ord=2, axis=0)
normalized_training_data = training_data / norms

but I just got a numch of 0s and NaNs.

答案1

得分: 1

你需要指定正确的轴来计算范数:

norms = np.linalg.norm(training_data, ord=2, axis=(2, 3))
英文:

You need to specify the right axes to compute the norm over:

norms = np.linalg.norm(training_data, ord=2, axis=(2, 3))

huangapple
  • 本文由 发表于 2023年2月18日 11:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定