[英]How to implement the mean function in NumPy without using the np.mean()?
[英]How to calculate np.mean for images of 3D array?
我想使用均值減法和標准化作為我的 CNN 模型的標准化。 我正在研究 Keras 分類圖像。
但是,我還沒有完全理解使用均值減法、標准化和簡單過程(例如重新縮放圖像 =/255)之間的區別。
在這個問題中提到了三種方法:
np.mean(x) # calculates the mean of the array x
x-np.mean(x) # this is equivalent to subtracting the mean of x from each value in x
x-=np.mean(x) # the -= can be read as x = x- np.mean(x)
我目前使用的是簡單的重新縮放:
train_data = train_data / 255
但是我的模型性能很低。 因此,我決定更改歸一化並使用均值減法,但我不知道如何對 3D 數組執行此操作。
有多種方法可以進行圖像歸一化。 在這里解釋。
對於您的情況,您想通過減去數組的平均值來進行歸一化。 您可以使用np.mean
沿兩個軸使用 3D 數組的平均值。 它將為您提供一個標量值,然后您可以從原始數組x
中減去該值。
train_data = np.random.rand(28,28,3)
mean = np.mean(train_data)
train_data -= mean
如果你想減去每個通道的平均值,你可以在mean
函數中使用axis
參數。
mean = np.mean(train_data,axis=(0, 1))
這將給出每個通道的平均值並減去上述train_data-=mean
的平均使用。
此外,您可以通過減去均值並除以標准差來標准化數據。 它在機器學習應用程序中使用很多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.