deep-learning - 字符串的分类(修改)
问题描述
我正在解决一个问题,其中我有一些 32514 行混乱的字符“wewlsfnskfddsl...eredsda”,每行长度为 406 个字符。我们需要预测他们属于哪一类?这里的类是 1-12 本书的名字。
在互联网上搜索后,我尝试了以下方法。然而,我得到一个错误。太感谢了。
#code
y = ytrain.values
#ytrain = y.ravel()
y = to_categorical(y, num_classes=12)
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 1. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
X = X.reshape((1,32514,1))
# define model
model = Sequential()
model.add(LSTM(75, input_shape=(32514,1)))
model.add(Dense(12, activation='softmax'))
print(model.summary())
# compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit model
model.fit(X, y, epochs=100, verbose=2)
# save the model to file
model.save('model.h5')
# save the mapping
dump(mapping, open('mapping.pkl', 'wb'))
#(batch_size, input_dim)
#(batch_size, timesteps, input_dim)
#### 我收到以下错误:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_19 (LSTM) (None, 75) 23100
_________________________________________________________________
dense_13 (Dense) (None, 12) 912
=================================================================
Total params: 24,012
Trainable params: 24,012
Non-trainable params: 0
_________________________________________________________________
None
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-503a6273e5d0> in <module>()
7
8 # fit model
----> 9 model.fit(X, y, epochs=100, verbose=2)
10
11 # save the model to file
/usr/local/lib/python3.6/dist-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1000 initial_epoch=initial_epoch,
1001 steps_per_epoch=steps_per_epoch,
-> 1002 validation_steps=validation_steps)
1003
1004 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1628 sample_weight=sample_weight,
1629 class_weight=class_weight,
-> 1630 batch_size=batch_size)
1631 # Prepare validation data.
1632 do_validation = False
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
1478 output_shapes,
1479 check_batch_axis=False,
-> 1480 exception_prefix='target')
1481 sample_weights = _standardize_sample_weights(sample_weight,
1482 self._feed_output_names)
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
121 ': expected ' + names[i] + ' to have shape ' +
122 str(shape) + ' but got array with shape ' +
--> 123 str(data_shape))
124 return data
125
ValueError: Error when checking target: expected dense_13 to have shape (1,) but got array with shape (12,)
解决方案
在我看来,您可以使用 lstm 解决这个问题。长短期记忆 (LSTM) 单元(或块)是循环神经网络 (RNN) 层的构建单元
这些 LSTM 将帮助我们捕获序列信息,通常用于我们想要学习数据中的序列模式的情况
您可以使用字符级 LSTM 解码此问题。
在这种情况下,您必须在 LSTM 单元格中传递文本的每个字符。在最后一步,您将拥有一个类,它是真正的标签
您可以使用交叉熵损失函数。
https://machinelearningmastery.com/develop-character-based-neural-language-model-keras/
这会给你完整的想法
推荐阅读
- javascript - 从 Foreach php 更改结果链接,使用 Javascript 不工作是否可能?
- db2 - 在 IBM DB2 上运行 SQL 脚本时收到警告消息
- android - 如何计算通知生成器的点击次数
- c++ - 在 C++ 中使用 switch 语句将不同的函数分配为类属性
- python - 如果先前生成的值被显式设置为新种子,为什么 Python random.random() 会给出不同的值?
- reactjs - 更新到 useState 后数据未映射 - React
- python - 用“onclick = (PythonScript(link))”切换所有“href = (link)”
- html - 添加 img 标签会导致溢出。如何避免这种情况
- python - form.is_valid() 在使用手机作为主键时抛出错误
- javascript - Android:Cordova 我需要从相机捕捉照片肖像