深度学习-循环神经网络(RNN)
作者: 明天依旧可好
QQ交流群: 807041986
注:关于深度学习的相关问题,若本文未涉及可在下方留言告诉我,我会在文章中进行补充的。
原文链接:https://mtyjkh.blog.csdn.net/article/details/111088248
深度学习系列:深度学习(TensorFlow 2)简单入门
代码|数据: 微信公众号(明天依旧可好)中回复:深度学习
导入数据
import pandas as pd
import tensorflow as tf
import osdf = pd.read_csv("Tweets.csv",usecols=["airline_sentiment","text"])
df
# categorical 实际上是计算一个列表型数据中的类别数,即不重复项,
# 它返回的是一个CategoricalDtype 类型的对象,相当于在原来数据上附加上类别信息 ,
# 具体的类别可以通过和对应的序号可以通过 codes 和 categories
df.airline_sentiment = pd.Categorical(df.airline_sentiment).codes
df
建立词汇表
import tensorflow_datasets as tfds
import ostokenizer = tfds.features.text.Tokenizer()vocabulary_set = set()
for text in df["text"]:some_tokens = tokenizer.tokenize(text)vocabulary_set.update(some_tokens)vocab_size = len(vocabulary_set)
vocab_size
'''
输出:
18027
'''
样本编码(测试)
encoder = tfds.features.text.TokenTextEncoder(vocabulary_set)
encoded_example = encoder.encode(text)
print(encoded_example)
'''
text为:
'@AmericanAir we have 8 ppl so we need 2 know how many seats are on the next flight. Plz put us on standby for 4 people on the next flight?'
输出:
[12939, 13052, 13579, 11267, 14825, 8674, 13052, 12213, 12082, 12156, 5329, 5401, 10099, 3100, 7974, 7804, 5671, 2947, 9873, 7864, 9704, 7974, 3564, 11759, 15266, 11250, 7974, 7804, 5671, 2947]
'''
将文本编码成数字形式
df["encoded_text"] = [encoder.encode(text) for text in df["text"]]
df
train_x = df["encoded_text"][:10000]
train_y = df["airline_sentiment"][:10000]
test_x = df["encoded_text"][10000:]
test_y = df["airline_sentiment"][10000:]from tensorflow import keras
train_x = keras.preprocessing.sequence.pad_sequences(train_x,maxlen=50)
test_x = keras.preprocessing.sequence.pad_sequences(test_x,maxlen=50)train_x.shape,train_y.shape,test_x.shape,test_y.shape
'''
输出:
((10000, 50), (10000,), (4640, 50), (4640,))
'''
构建模型
import tensorflow as tfmodel = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size+1, 64),tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),tf.keras.layers.Dense(64, activation='relu'),tf.keras.layers.Dense(1)
])model.summary()
'''
输出:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_2 (Embedding) (None, None, 64) 1153792
_________________________________________________________________
bidirectional_2 (Bidirection (None, 128) 66048
_________________________________________________________________
dense_4 (Dense) (None, 64) 8256
_________________________________________________________________
dense_5 (Dense) (None, 1) 65
=================================================================
Total params: 1,228,161
Trainable params: 1,228,161
Non-trainable params: 0
_________________________________________________________________
'''
激活
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),optimizer=tf.keras.optimizers.Adam(1e-4),metrics=['accuracy'])
训练模型
history = model.fit(train_x,train_y,epochs=20,batch_size=200,validation_data=(test_x, test_y),verbose=1)
'''
输出:
Epoch 1/20
50/50 [==============================] - 6s 117ms/step - loss: -4.8196 - accuracy: 0.6652 - val_loss: -0.7605 - val_accuracy: 0.7071
......
Epoch 19/20
50/50 [==============================] - 6s 123ms/step - loss: -37.5176 - accuracy: 0.7586 - val_loss: -9.0619 - val_accuracy: 0.7272
Epoch 20/20
50/50 [==============================] - 6s 120ms/step - loss: -40.0017 - accuracy: 0.7611 - val_loss: -7.7479 - val_accuracy: 0.7248'''
深度学习-循环神经网络(RNN)相关推荐
- 深度学习 --- 循环神经网络RNN详解(BPTT)
今天开始深度学习的最后一个重量级的神经网络即RNN,这个网络在自然语言处理中用处很大,因此需要掌握它,同时本人打算在深度学习总结完成以后就开始自然语言处理的总结,至于强化学习呢,目前不打算总结了,因为 ...
- 深度学习 循环神经网络RNN
循环神经网络简介: 循环神经网络(Recurrent Neural Networks)是一种特殊的神经网络结构, 它是根据"人的认知是基于过往的经验和记忆"这一观点提出的. 它与D ...
- 深度学习---循环神经网络RNN详解(LSTM)
上一节我们详细讲解了RNN的其中一个学习算法即BPTT,这个算法是基于BP的,只是和BP不同的是在反向传播时,BPTT的需要追溯上一个时间的权值更新,如下图,当前时刻是s(t),但是反向传播时,他需要 ...
- MLK | 一文理清深度学习循环神经网络
MLK,即Machine Learning Knowledge,本专栏在于对机器学习的重点知识做一次梳理,便于日后温习,内容主要来自于<百面机器学习>一书,结合自己的经验与思考做的一些总结 ...
- 深度学习--循环神经网络(Recurrent Neural Network)
RNN(Recurrent Neural Network)是怎么来的? 一些应用场景,比如说写论文,写诗,翻译等等. 既然已经学习过神经网络,深度神经网络,卷积神经网络,为什么还要学习RNN? 首先我 ...
- 「NLP」 深度学习NLP开篇-循环神经网络(RNN)
https://www.toutiao.com/a6714260714988503564/ 从这篇文章开始,有三AI-NLP专栏就要进入深度学习了.本文会介绍自然语言处理早期标志性的特征提取工具-循环 ...
- 深度学习~循环神经网络RNN, LSTM
目录 1. 循环神经网络RNN 1.1 RNN出现背景 1.2 RNN概念 2. LSTM 2.1 LSTM出现背景 2.2 LSTM结构 参考 1. 循环神经网络RNN 1.1 RNN出现背景 pr ...
- 【NLP】 深度学习NLP开篇-循环神经网络(RNN)
从这篇文章开始,有三AI-NLP专栏就要进入深度学习了.本文会介绍自然语言处理早期标志性的特征提取工具-循环神经网络(RNN).首先,会介绍RNN提出的由来:然后,详细介绍RNN的模型结构,前向传播和 ...
- 花书+吴恩达深度学习(十五)序列模型之循环神经网络 RNN
目录 0. 前言 1. RNN 计算图 2. RNN 前向传播 3. RNN 反向传播 4. 导师驱动过程(teacher forcing) 5. 不同序列长度的 RNN 如果这篇文章对你有一点小小的 ...
最新文章
- java 中线程的创建方式
- 浅谈java中的四个核心概念【转】
- python pandas读取excel-Python用Pandas读写Excel
- 关于在hdfs上对数据创建外部表的原因
- start ssh-agent
- 最新猪肉价格走势分析,2021又会是一个吃不起猪肉的春节吗?
- python 与或非_Python的阶乘求和
- 5G 登上“神坛”,区块链裁员求生!
- 12年高级工程师的“飞升之路”,android嵌入式开发实战pdf
- linux下安装yum步骤
- dpdk X710 VF reset
- 使用element中el-tab如何改变文字样式等
- 问题解决丨对不起,小米路由器出现网络连接问题无法打开网页
- word文档加密保护不能编辑问题
- python中turtle隐藏画笔_python中Turtle的画笔命令有哪些?
- python智力问答游戏_Python语言编写智力问答小游戏功能
- 基于SDK的支付接口服务端——支付宝,微信
- 使用face-api实现html前端摄像头人脸检测
- python processpoolexecutor_理解Python的PoolExecutor
- python学习:函数(一)
热门文章
- OSError: Cannot initialize new instance of inotify, Errno=Too many open files (EMFILE) 问题解决
- linux内核创建ubi,UBI文件系统制作和挂载
- linux扫描hba卡命令,如何在redhat中查看HBA卡的信息
- linux系统主机信任,Linux信任主机(SSH)
- php listview,ListView Item多布局的实现
- pandas读取剪切板
- Android中的表格布局
- 客户端升级为select模型
- 计算机英语关于网络的演讲稿,关于网络好处的英语短文(我要像演讲稿的)
- android 之自定义适配器(重写的getView()方法中convertView元素的妙用)一个实例