Tensorflow 2.0 教程持续更新 :https://blog.csdn.net/qq_31456593/article/details/88606284

完整tensorflow2.0教程代码请看tensorflow2.0:中文教程tensorflow2_tutorials_chinese(欢迎star)

入门教程:
TensorFlow 2.0 教程- Keras 快速入门
TensorFlow 2.0 教程-keras 函数api
TensorFlow 2.0 教程-使用keras训练模型
TensorFlow 2.0 教程-用keras构建自己的网络层
TensorFlow 2.0 教程-keras模型保存和序列化

TensorFlow 2.0 教程-词嵌入

1.载入数据

vocab_size = 10000
(train_x, train_y), (test_x, text_y) = keras.datasets.imdb.load_data(num_words=vocab_size)
print(train_x[0])
print(train_x[1])
[1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 2, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 2, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 2, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 2, 8, 4, 107, 117, 5952, 15, 256, 4, 2, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 2, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32]
[1, 194, 1153, 194, 8255, 78, 228, 5, 6, 1463, 4369, 5012, 134, 26, 4, 715, 8, 118, 1634, 14, 394, 20, 13, 119, 954, 189, 102, 5, 207, 110, 3103, 21, 14, 69, 188, 8, 30, 23, 7, 4, 249, 126, 93, 4, 114, 9, 2300, 1523, 5, 647, 4, 116, 9, 35, 8163, 4, 229, 9, 340, 1322, 4, 118, 9, 4, 130, 4901, 19, 4, 1002, 5, 89, 29, 952, 46, 37, 4, 455, 9, 45, 43, 38, 1543, 1905, 398, 4, 1649, 26, 6853, 5, 163, 11, 3215, 2, 4, 1153, 9, 194, 775, 7, 8255, 2, 349, 2637, 148, 605, 2, 8003, 15, 123, 125, 68, 2, 6853, 15, 349, 165, 4362, 98, 5, 4, 228, 9, 43, 2, 1157, 15, 299, 120, 5, 120, 174, 11, 220, 175, 136, 50, 9, 4373, 228, 8255, 5, 2, 656, 245, 2350, 5, 4, 9837, 131, 152, 491, 18, 2, 32, 7464, 1212, 14, 9, 6, 371, 78, 22, 625, 64, 1382, 9, 8, 168, 145, 23, 4, 1690, 15, 16, 4, 1355, 5, 28, 6, 52, 154, 462, 33, 89, 78, 285, 16, 145, 95]
word_index = keras.datasets.imdb.get_word_index()
word_index = {k:(v+3) for k,v in word_index.items()}
word_index['<PAD>'] = 0
word_index['<START>'] = 1
word_index['<UNK>'] = 2
word_index['<UNUSED>'] = 3
reverse_word_index = {v:k for k, v in word_index.items()}
def decode_review(text):return ' '.join([reverse_word_index.get(i, '?') for i in text])
print(decode_review(train_x[0]))
<START> this film was just brilliant casting location scenery story direction everyone's really suited the part they played and you could just imagine being there robert <UNK> is an amazing actor and now the same being director <UNK> father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for <UNK> and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also <UNK> to the two little boy's that played the <UNK> of norman and paul they were just brilliant children are often left out of the <UNK> list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don't you think the whole story was so lovely because it was true and was someone's life after all that was shared with us all
maxlen = 500
train_x = keras.preprocessing.sequence.pad_sequences(train_x,value=word_index['<PAD>'],padding='post', maxlen=maxlen)
test_x = keras.preprocessing.sequence.pad_sequences(test_x,value=word_index['<PAD>'],padding='post', maxlen=maxlen)

2.构建模型

embedding_dim = 16
model = keras.Sequential([layers.Embedding(vocab_size, embedding_dim, input_length=maxlen),layers.GlobalAveragePooling1D(),layers.Dense(16, activation='relu'),layers.Dense(1, activation='sigmoid')])
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding (Embedding)        (None, 500, 16)           160000
_________________________________________________________________
global_average_pooling1d (Gl (None, 16)                0
_________________________________________________________________
dense (Dense)                (None, 16)                272
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 17
=================================================================
Total params: 160,289
Trainable params: 160,289
Non-trainable params: 0
_________________________________________________________________
model.compile(optimizer=keras.optimizers.Adam(),loss=keras.losses.BinaryCrossentropy(),metrics=['accuracy'])
history = model.fit(train_x, train_y, epochs=30, batch_size=512, validation_split=0.1)
Train on 22500 samples, validate on 2500 samplesEpoch 30/30
22500/22500 [==============================] - 1s 66us/sample - loss: 0.1290 - accuracy: 0.9596 - val_loss: 0.3055 - val_accuracy: 0.8948
import matplotlib.pyplot as pltacc = history.history['accuracy']
val_acc = history.history['val_accuracy']epochs = range(1, len(acc) + 1)plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.figure(figsize=(16,9))plt.show()

<Figure size 1152x648 with 0 Axes>
e = model.layers[0]
weights = e.get_weights()[0]
print(weights.shape)
(10000, 16)
out_v = open('vecs.tsv', 'w')
out_m = open('meta.tsv', 'w')
for word_num in range(vocab_size):word = reverse_word_index[word_num]embeddings = weights[word_num]out_m.write(word + "\n")out_v.write('\t'.join([str(x) for x in embeddings]) + "\n")
out_v.close()
out_m.close()

放到 Embedding Projector:http://projector.tensorflow.org/
上进行可视化


TensorFlow 2.0 教程-词嵌入相关推荐

  1. word2vec python实现_教程 | 在Python和TensorFlow上构建Word2Vec词嵌入模型

    原标题:教程 | 在Python和TensorFlow上构建Word2Vec词嵌入模型 选自adventuresinmachinelearning 参与:李诗萌.刘晓坤 本文详细介绍了 word2ve ...

  2. 在Python和TensorFlow上构建Word2Vec词嵌入模型

    本文详细介绍了 word2vector 模型的模型架构,以及 TensorFlow 的实现过程,包括数据准备.建立模型.构建验证集,并给出了运行结果示例. GitHub 链接:https://gith ...

  3. TensorFlow 2.0 教程

    最全 TensorFlow2.0 教程-持续更新 原文地址:https://blog.csdn.net/qq_31456593/article/details/88606284 最新tensorflo ...

  4. TensorFlow 2.0 教程25:Transformer

    这里我们将实现一个Transformer模型,将葡萄牙语翻译为英语.Transformer的核心思想是self-attention–通过关注序列不同位置的内容获取句子的表示. Transformer的 ...

  5. 001-TensorFlow 2.0 教程-Transformer

    TensorFlow 2.0 教程-Transformer 原文地址:https://blog.csdn.net/qq_31456593/article/details/89923913 Tensor ...

  6. 词向量表示:word2vec与词嵌入

    在NLP任务中,训练数据一般是一句话(中文或英文),输入序列数据的每一步是一个字母.我们需要对数据进行的预处理是:先对这些字母使用独热编码再把它输入到RNN中,如字母a表示为(1, 0, 0, 0, ...

  7. TensorFlow2.0教程-keras 函数api

    TensorFlow2.0教程-keras 函数api Tensorflow 2.0 教程持续更新: https://blog.csdn.net/qq_31456593/article/details ...

  8. 词嵌入 网络嵌入_词嵌入深入实践

    词嵌入 网络嵌入 介绍 (Introduction) I'm sure most of you would stumble sooner or later on the term "Word ...

  9. 官方资源帖!手把手教你在TensorFlow 2.0中实现CycleGAN,推特上百赞

    铜灵 发自 凹非寺 量子位 出品| 公众号 QbitAI CycleGAN,一个可以将一张图像的特征迁移到另一张图像的酷算法,此前可以完成马变斑马.冬天变夏天.苹果变桔子等一颗赛艇的效果. 这行被顶会 ...

  10. 【NLP】词嵌入基础和Word2vec

    0.导语 词嵌入是自然语言处理(NLP)中语言模型与表征学习技术的统称.概念上而言,它是指把一个维数为所有词的数量的高维空间嵌入到一个维数低得多的连续向量空间中,每个单词或词组被映射为实数域上的向量. ...

最新文章

  1. libevent mysql_在 libevent 中使用 MariaDB(MySQL)
  2. 如何及时获得AI顶尖科研团队的最新论文与进展?只需一份AI内参!
  3. 浅谈JavaScript错误
  4. 速卖通物流发货怎么操作?“解读”重点国家市场物流状况
  5. pycharm 如何通过VCS快速提交代码?
  6. 封装、继承、多态的理解
  7. How is error message Could not create note displayed from backend to ui
  8. UVa 10905 孩子们的游戏
  9. 软件工程学习总结(4)——软件工程概论
  10. QT程序在windows下部署发布
  11. 前端为什么有的接口明明是成功回调却执行了.catch失败回调_前端知识整理
  12. matlab hopty,运行Matlab时出现错误?Attempted to access rxd(500); index out of bou
  13. 数据挖掘项目——金融反欺诈
  14. excel公式识别html,POI/Excel/HTML单元格公式问题
  15. js 字符串转二维数组
  16. python汇率兑换_美元与人民币汇率 Python
  17. 最新 NCBI 上传测序数据教程 (图文详解)
  18. JAVA-仿微信九宫格头像
  19. 内网ip如何变成公网ip?快解析转换域名映射外网访问
  20. Hadoop-JAVA编写HDFS客户端进行HDFS操作

热门文章

  1. Error: Unbalanced delimiter found in string
  2. 理解容器中超易混淆 Attach 和 Exec 的异同
  3. 计算机理论什么是信道容量,(信道容量知识总结.doc
  4. 本地网络出现了一个意外的情况,不能完成所有你在设置中所要求的更改
  5. 汉语是世界上唯一一种面向对象的高级语言【转】
  6. python调用sin_Python sin() 函数 - Python 教程 - 自强学堂
  7. 什么是 360 评估?
  8. Java8新特性学习第一天
  9. 苹果电脑上的Word打不开怎么办?Word文件怎么恢复?
  10. 天天向上python题目答案_Python练习:天天向上的力量