有没有办法不用一个热编码器就可以训练RNN?

2024-04-23 08:20:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的日志分析项目开发一个顺序RNN。你知道吗

输入是一个日志序列,例如[1,2,3,4,5,6,1,5,2,7,8,2,1]

目前我正在使用keras库中的分类函数将序列转换为一个热编码。你知道吗

def to_categorical(y, num_classes=None, dtype='float32'):
    """Converts a class vector (integers) to binary class matrix.

    E.g. for use with categorical_crossentropy.

    # Arguments
        y: class vector to be converted into a matrix
            (integers from 0 to num_classes).
        num_classes: total number of classes.
        dtype: The data type expected by the input, as a string
            (`float32`, `float64`, `int32`...)

    # Returns
        A binary matrix representation of the input. The classes axis
        is placed last.

    # Example

    ```python
    # Consider an array of 5 labels out of a set of 3 classes {0, 1, 2}:
    > labels
    array([0, 2, 1, 2, 0])
    # `to_categorical` converts this into a matrix with as many
    # columns as there are classes. The number of rows
    # stays the same.
    > to_categorical(labels)
    array([[ 1.,  0.,  0.],
           [ 0.,  0.,  1.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  1.],
           [ 1.,  0.,  0.]], dtype=float32)
    ```
    """

    y = np.array(y, dtype='int')
    input_shape = y.shape
    if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
        input_shape = tuple(input_shape[:-1])
    y = y.ravel()
    if not num_classes:
        num_classes = np.max(y) + 1
    n = y.shape[0]
    categorical = np.zeros((n, num_classes), dtype=dtype)
    categorical[np.arange(n), y] = 1
    output_shape = input_shape + (num_classes,)
    categorical = np.reshape(categorical, output_shape)
    return categorical

我面临的问题是,可能有一些日志不属于经过训练的数据,比如说[9,10,11]

如果我有2000个日志键和275个唯一日志的序列。你知道吗

总有看不见的日志,但如果我想保存此模型并在新数据上重用它,它可能无法将其转换为相同的分类格式,因为最初在我的RNN中只有275个唯一的日志类,但现在我有275+3个新类。你知道吗

我们如何解决这个问题?你知道吗


Tags: ofthetoinputnp序列arraymatrix
2条回答

您必须具有类一致性,否则您的模型将无法正常工作。你知道吗

如果数字在数字上有意义,你可以用数字代替一个热的。但既然你说它们是课堂,那就没什么意义了。你知道吗

您可以尝试将一些train类分离为未知类,并将它们分组为单个热编码。然后所有新类都将接收相同的编码。你知道吗

但并不能保证这个模型能给你带来好的效果。你知道吗

关于@Dainel关于类一致性的回答,您可以用np.nan替换训练序列中没有出现的任何值,并使用pd.get_dummies,如下所示。你知道吗

train_seq = np.array([1,2,3,4,5])
test_seq = np.array([1,2,3,4,5,6,7,8,9,10], dtype=np.float32)

test_seq[~np.isin(test_seq, train_seq)] = np.nan

df = pd.get_dummies(test_seq, dummy_na=True)
print(df)

它为看不见的数据生成一个单独的类。你知道吗

   1.0  2.0  3.0  4.0  5.0  NaN
0    1    0    0    0    0    0
1    0    1    0    0    0    0
2    0    0    1    0    0    0
3    0    0    0    1    0    0
4    0    0    0    0    1    0
5    0    0    0    0    0    1
6    0    0    0    0    0    1
7    0    0    0    0    0    1
8    0    0    0    0    0    1
9    0    0    0    0    0    1

相关问题 更多 >