Keras embedding层的理解与使用

看了很多博客没有解释清楚Embedding层input_dim的意思,下面这篇博客我认为是解释清楚了,欢迎借鉴:这里写图片描述

 首先搞懂这里embedding的原理就是one-hot,那这样就能理解为什么input_dim=|vocalbulary_id|+1

keras.layers.Embedding(input_dim, output_dim, embeddings_initializer='uniform', embeddings_regularizer=None, activity_regularizer=None, embeddings_constraint=None, mask_zero=False, input_length=None)

Keras中Embedding的参数解释(小白)_像蝼蚁这样的人的博客-CSDN博客_embedding第一个参数

再比如在文本数据要处理的时候,一般准备一个字典,形如下面这样的格式:

id word
0
1
2 你好

首先通过id来代表每个词(token),转换为计算机能理解的数字,再通过embedding编码转换为深度网络能够理解的内容。 

比如还有再做时间序列预测的时候,可以考虑是否使用embedding来表征数据,再做对比试验,看是否能够让模型更好的预测。

再讲一下个人理解:

from keras.layers import Embedding
from keras import Sequential
import numpy as np
model = Sequential()
model.add(Embedding(3, 1, input_length=10))
# the model will take as input an integer matrix of size (batch, input_length).
# the largest integer (i.e. word index) in the input should be no larger than 999 (vocabulary size).
# now model.output_shape == (None, 10, 64), where None is the batch dimension.

input_array = np.random.randint(3, size=(1, 10))
print(input_array)


model.compile('rmsprop', 'mse')
output_array = model.predict(input_array)

print(output_array)

 

应用:

(164条消息) Embedding 意义_weixin_30701575的博客-CSDN博客

 补充:

model.add(Embedding(3, 1, input_length=10))这里的3是输入维度,比如我的输入值是可以用0,1,2来表示那么就需要embedding_input=3来进行One-hot编码表示,至于Output_dim再连接一个全连接层就能进行映射输出了。 

参考资料

(171条消息) keras:3)Embedding层详解_Javis486的博客-CSDN博客_embedding keras

(165条消息) Keras中Embedding的参数解释(小白)_像蝼蚁这样的人的博客-CSDN博客_embedding第一个参数

Keras里Embedding层的理解 - 百度文库 (baidu.com)

(173条消息) keras:Embedding层详解(解释了参数的具体含义,有例子)_catbird233的博客-CSDN博客_embedding参数

猜你喜欢

转载自blog.csdn.net/weixin_43332715/article/details/124115764