embedding_Keras嵌入层
Keras提供了一个嵌入层,可用于处理文本数据的神经网络。他要求输入数据进行整数编码,以便每个单词都由唯一的整数表示。该数据准备步骤可以使用提供有Keras的Tokenizer API来执行。
嵌入层使用随机权重初始化,并将学习所有数据集中词的嵌入。
它是一个灵活的层,可以以各种方式使用,如:
1、它可以单独使用来学习一个字嵌入,以后可以在另一个模型中使用。
2、它可以用作深度学习模型的一部分,其中嵌入与模型本身一起被学习。
3、它可以用于加载预训练的词嵌入模型,一种迁移学习。
嵌入层被定义为网络的第一个隐藏层,必须指定3个参数:
*input_dim:这是文本数据中词汇的大小。例如,如果你的数据是整数编码为0-10之间的值,则词表的大小将为11个字。
*output_dim:这是嵌入单词的向量空间的大小。它为每个单词定义了该层的输出向量的大小。例如,它可以是32或100甚至更大,根据问题来定。
*input_length:这是输入序列的长度,正如你为Keras模型的任何输入层定义的那样。例如如果你的输出文档包含1000个单词,则为1000。
例如,下面我们定义一个词汇量为200的嵌入层(例如,从0到199(包括整数)的整数编码单词),将词嵌入到32维的向量空间中,以及每次输入50个单词的输入文档。
e = Embedding(200, 32, input_length=50)
嵌入层输出的是一个二维向量,每个单词对应一个输入序列(输入文档)。如果希望连接密集层直接到嵌入层,必须首先压扁2D输出矩阵。
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.embeddings import Embedding# define documents
docs = ['Well done!','Good work','Great effort','nice work','Excellent!','Weak','Poor effort!','not good','poor work','Could have done better.']# define class labels
labels = [1,1,1,1,1,0,0,0,0,0]# integer encode the documents
vocab_size = 50
encoded_docs = [one_hot(d, vocab_size) for d in docs]
print(encoded_docs)# pad documents to a max length of 4 words
max_length = 4
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
print(padded_docs)# define the model
model = Sequential()
model.add(Embedding(vocab_size, 8, input_length=max_length))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])# summarize the model
print(model.summary())# fit the model
model.fit(padded_docs, labels, epochs=50, verbose=0)# evaluate the model
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
print('Accuracy: %f' % (accuracy*100))
以下为实验结果:
[[33, 37], [18, 16], [31, 24], [33, 16], [5], [11], [34, 24], [11, 18], [34, 16], [48, 38, 37, 7]][[33 37 0 0][18 16 0 0][31 24 0 0][33 16 0 0][ 5 0 0 0][11 0 0 0][34 24 0 0][11 18 0 0][34 16 0 0][48 38 37 7]]
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 4, 8) 400
_________________________________________________________________
flatten_1 (Flatten) (None, 32) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 33
=================================================================
Total params: 433
Trainable params: 433
Non-trainable params: 0
_________________________________________________________________
None
Accuracy: 89.999998Process finished with exit code 0
embedding_Keras嵌入层相关推荐
- BERT的嵌入层是如何实现的?看完你就明白了
2020-05-13 11:03:16 作者:__ 编译:ronghuaiyang 导读 非常简单直白的语言解释了BERT中的嵌入层的组成以及实现的方式. 介绍 在本文中,我将解释BERT中嵌入层的实 ...
- 嵌入层(Embedding Layer)与词向量(Word Embedding)详解
文章目录 常见的语言表示模型 词向量与Embedding区分 Embedding层--keras中的one-hot映射为 向量的层:词向量(word embedding)--一种语言模型表示方法 分布 ...
- Pytorch:RNN、LSTM、GRU 构建人名分类器(one-hot版本、Embedding嵌入层版本)
日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新) 2. RNN经典案例 2.1 使用RNN模型构建人名分类器 学 ...
- Embedding词嵌入层的原理
日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新) Embeddingkeras.layers.Embedding ...
- tf.keras.layers.Embedding 嵌入层 示例
将正整数(索引)转换为固定大小的密集向量.此层只能用作模型中的第一层 import tensorflow as tf import numpy as np embedding_layer = tf.k ...
- 嵌入层 tf.keras.layers.Embedding() 介绍【TensorFlow2入门手册】
函数原型 tf.keras.layers.Embedding(input_dim, output_dim, embeddings_initializer='uniform',embeddings_re ...
- 嵌入(embedding)层的理解
首先,我们有一个one-hot编码的概念. 假设,我们中文,一共只有10个字...只是假设啊,那么我们用0-9就可以表示完 比如,这十个字就是"我从哪里来,要到何处去" 其分别对应 ...
- 深入理解 Embedding层的本质
继上文https://blog.csdn.net/weixin_42078618/article/details/82999906探讨了embedding层的降维效果,时隔一个月,分享一下嵌入层在NP ...
- 【文本分类】深入理解embedding层的模型、结构与文本表示
[1] 名词理解 embedding层:嵌入层,神经网络结构中的一层,由embedding_size个神经元组成,[可调整的模型参数].是input输入层的输出. 词嵌入:也就是word em ...
最新文章
- Python 处理server返回gzip内容
- Leetcode PHP题解--D6 595. Big Countries
- Sentinel(三)之如何使用
- django中URL常用配置方法
- 有关二叉树的相关实现:建树,遍历(递归与非递归实现)
- Netscreen204防火墙配置网络设备的SNMP及Syslog配置
- python 直方图每个bin中的值_python – 如何获取直方图bin中的数据
- 让UITableView数据处理更轻便
- kali安装图像处理软件
- USB redirection
- 2022全新版千月影视源码原生播放器 投屏 选集 下载应有尽有(全开源)
- pad怎么远程服务器,iphone和ipad如何远程控制局域网电脑
- Python数据类型-list得切片(slices)
- Safari浏览器插件 Tampermonkey
- Electron常见问题 30 - 深度填坑之阴影问题
- Linux CentOS 7网络配置基础
- 【redis集群:2. 集群伸缩】
- 使用r语言进行excel表格的分类与汇总
- 华大HC32L130 SPI和GPIO模拟驱动NF-03和NF-01-s模块(SI24R1方案,兼容NRF24L01)
- Python使用geany工具配置方法