Keras One Hot Inputs

时间:2018-05-21 19:04:04

标签: python keras one-hot-encoding

我有一个语言模型,我正在训练输出一个“下一个字符”给定一个最多50个输入字符的序列。

目前,由于输入维度不正确,模型失败,并出现以下错误:

$ python -u neural-lm.py
C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
loading training data generator...
skipped 107 lines where sequence length > padded_seq_length...
loading dev data generator...
skipped 5 lines where sequence length > padded_seq_length...
Traceback (most recent call last):
  File "neural-lm.py", line 29, in <module>
    clnlm.add(Bidirectional(LSTM(units=padded_seq_length, name='lstm_layer_initial', return_sequences=True, recurrent_dropout=0.9, kernel_regularizer=l2, activity_regularizer=l2 ), input_shape=(padded_seq_length, alphabet_length)))
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\models.py", line 497, in add
    layer(x)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\layers\wrappers.py", line 325, in __call__
    return super(Bidirectional, self).__call__(inputs, **kwargs)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 592, in __call__
    self.build(input_shapes[0])
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\layers\wrappers.py", line 445, in build
    self.forward_layer.build(input_shape)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\layers\recurrent.py", line 461, in build
    self.cell.build(step_input_shape)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\layers\recurrent.py", line 1799, in build
    constraint=self.kernel_constraint)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 418, in add_weight
    self.add_loss(regularizer(weight))
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\regularizers.py", line 58, in l2
    return L1L2(l2=l)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\regularizers.py", line 35, in __init__
    self.l2 = K.cast_to_floatx(l2)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\keras\backend\common.py", line 110, in cast_to_floatx
    return np.asarray(x, dtype=_FLOATX)
  File "C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
(tensorflow)

我根据Seqeuntial模型指南指定了我的输入:

alphabet_length = 28
padded_seq_length = 50
clnlm = Sequential()
clnlm.add(Bidirectional(LSTM(units=padded_seq_length, name='lstm_layer_initial', return_sequences=True, recurrent_dropout=0.9, kernel_regularizer=l2, activity_regularizer=l2 ), input_shape=(padded_seq_length, alphabet_length)))
...

生成器提供的输入数据,但我可以测试一个热编码裸,输出如下:

$ python -u lm_utils.py
C:\Users\username\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
_______________________here is an example sentence
________________________________the cat in the hat
_________________________________________________z
_________________________________________________x
_________________________________________________
one_hot:  [[0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 1. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
(50, 28)
one_hot:  [[0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [1. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
(50, 28)
one_hot:  [[0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 ...
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 1. 0. 0.]]
(50, 28)
one_hot:  [[0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 ...
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 0.]]
(50, 28)
one_hot:  [[0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 ...
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 0. 1.]
 [0. 0. 0. ... 0. 1. 0.]]
(50, 28)
(tensorflow)

我理解错误意味着其中一个输入错误地将序列应用于错误的单个元素。我希望有人可以权衡并给我一些关于我可以改变和/或检查以便继续前进的指导。

0 个答案:

没有答案