英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论