keras utils 规范化的目的是什么? [英] What is the purpose of keras utils normalize?

查看:27
本文介绍了keras utils 规范化的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在将训练集传递给我的神经网络之前对其进行标准化,因此我尝试了 keras.utils.normalize() 和我对得到的结果感到惊讶.

I'd like to normalize my training set before passing it to my NN so instead of doing it manually (subtract mean and divide by std), I tried keras.utils.normalize() and I am amazed about the results I got.

运行:

r = np.random.rand(3000) * 1000
nr = normalize(r)
print(np.mean(r))
print(np.mean(nr))
print(np.std(r))
print(np.std(nr))
print(np.min(r))
print(np.min(nr))
print(np.max(r))
print(np.max(nr))

结果:

495.60440066771866
0.015737914577213984
291.4440194021
0.009254802974329002
0.20755517410064872
6.590913227674956e-06
999.7631481267636
0.03174747238214018

不幸的是,文档 没有解释幕后发生的事情.你能解释一下它的作用吗,我是否应该使用 keras.utils.normalize 而不是我手动完成的?

Unfortunately, the docs don't explain what's happening under the hood. Can you please explain what it does and if I should use keras.utils.normalize instead of what I would have done manually?

推荐答案

这不是您期望的那种规范化.实际上,使用np.linalg.norm() 在幕后使用 Lp 范数对给定数据进行归一化:

It is not the kind of normalization you expect. Actually, it uses np.linalg.norm() under the hood to normalize the given data using Lp-norms:

def normalize(x, axis=-1, order=2):
    """Normalizes a Numpy array.
    # Arguments
        x: Numpy array to normalize.
        axis: axis along which to normalize.
        order: Normalization order (e.g. 2 for L2 norm).
    # Returns
        A normalized copy of the array.
    """
    l2 = np.atleast_1d(np.linalg.norm(x, order, axis))
    l2[l2 == 0] = 1
    return x / np.expand_dims(l2, axis)

例如,在默认情况下,它将使用 L2 归一化对数据进行归一化(即元素的平方和等于 1).

For example, in the default case, it would normalize the data using L2-normalization (i.e. the sum of squared of elements would be equal to one).

你可以使用这个函数,或者如果你不想手动做mean和std归一化,你可以使用StandardScaler() 来自 sklearn 甚至 MinMaxScaler().

You can either use this function, or if you don't want to do mean and std normalization manually, you can use StandardScaler() from sklearn or even MinMaxScaler().

这篇关于keras utils 规范化的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆