【发布时间】:2019-12-11 14:08:09
【问题描述】:
我正在尝试为我的日志分析项目开发一个顺序 RNN。
输入是一个日志序列,比如 [1,2,3,4,5,6,1,5,2,7,8,2,1]
目前我正在使用 keras 库中的 to_categorical 函数,该函数将序列转换为 one-hot 编码。
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 个唯一日志的序列。
总会有看不见的日志,但如果我想保存这个模型并在新数据上重用它,它可能无法将其转换为相同的分类格式,因为我最初只有 275 个唯一的日志类RNN 但现在我有 275+ 3 个新课程。
我们如何解决这个问题?
【问题讨论】:
-
在不了解全部词汇的情况下,谷歌键盘预测如何工作?它还包括我们自定义词汇表中的单词。他们不使用 RNN 来预测序列吗?如果他们看到新的单词/类,他们不需要重新训练他们的模型吗?
标签: python machine-learning keras recurrent-neural-network