文章目录

  • ML、DL、CNN学习记录4
    • RNN
      • RNN类别
      • Word 表示
      • 编码
      • 10000dim - 100dim
      • RNN基本运算
      • imdb
    • 文本生成模型
      • 模型示例


ML、DL、CNN学习记录4

Time/Spatial:
SimpleRNN
LSTM
GRU

RNN <—> Attention/Transformers

RNN

RNN类别

many to one:文本分类、股价预测
many to mary:语音识别(语音->文字)、语音合成(文字->语音)、翻译

Word 表示

  1. 分词(jieba、spacy、NLTK、HanLP)
  2. word -> token
  3. one-hot编码(并不是很好用,相似度度量的时候不好处理,过于稀疏)
  4. 相似度度量(欧式距离、夹角余弦…)

编码

one-hot编码

词嵌入(word Embedding):将词的向量 映射到 一个n维空间

Morkov model:
一阶Morlov:wiw_iwi只与wi−1w_{i-1}wi1有关
二阶Morlov:wiw_iwi只与wi−1wi−2w_{i-1}w_{i-2}wi1wi2有关

x阶Morlov:wiw_iwi只与wi−1wi−2...wi−x+1w_{i-1}w_{i-2}...w_{i-x+1}wi1wi2...wix+1有关
w1w2w2w3w4...wmw_1w_2w_2w_3w_4...w_mw1w2w2w3w4...wm

skip-gram:
环境决定我:

wi−x+1wi−x+2...wi−1+wi+1wi+2...wi+x⟶wiw_{i-x+1}w_{i-x+2}...w_{i-1} + w_{i+1}w_{i+2}...w_{i+x} \longrightarrow w_iwix+1wix+2...wi1+wi+1wi+2...wi+xwi

CBOW(连续词带模型)

我决定环境(当前词预测周边词):

wi⟶wi−x+1wi−x+2...wi−1+wi+1wi+2...wi+xw_i \longrightarrow w_{i-x+1}w_{i-x+2}...w_{i-1} + w_{i+1}w_{i+2}...w_{i+x}wiwix+1wix+2...wi1+wi+1wi+2...wi+x

10000dim - 100dim

通过神经网络学习全连接矩阵W,可将word2vec添加到模型的某层

tf.nn.embedding_lookup(Params, ids, pertition_strategy='mod', max_norm=None)
# params:词向量矩阵;
# 按照找ids获取params中的行,ids为矩阵行号(index)
# max_norm允许使用L2范式缩放params中原向量:x*max_norm/norm(x)

note:一般使用缩放one-hot编码到 64-256的范围里

RNN基本运算

RNN基本运算:

Input/Output:

LSTM:



imdb

文本处理的重要数据集

流程:

# coding: utf-8import keras
from keras.models import Sequential
from keras.layers import Embedding, SimpleRNN, LSTM, Dense
from keras.datasets import imdb
# 辅助函数(帮助)
from keras.preprocessing import sequence
import matplotlib.pyplot as plt
from time import timet_start = time()
max_features = 10000  # number of words to consider as features
maxlen = 500  # cut texts after this number of words
batch_size = 32# 获取imdb文本数据
# 限定文本的维度 为: max_features
(input_train, y_train), (input_test, y_test) = imdb.load_data(num_words=max_features)
print(len(input_train), 'train sequences')
print(len(input_test), 'test sequences')print(y_train.shape)
print(y_train[:100])
# 输出文本
# 首先需要构建文本的一个词典
# 每个词都有一个编号
word_index = imdb.get_word_index()
# 根据词来查找编号
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
# note that our indices were offset by 3 because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown".
decoded_text = ' '.join([reverse_word_index.get(i - 3, '?') for i in input_train[0]])
print('第一个文本:\n', decoded_text)print('Pad sequences (samples x time)')
# 做一个padding,进行数据的修正(多了删掉,少了填充)
# 文本长度可能不一样,所以需要这一步操作
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)# 建立模型 使用Embedding,LSTM层
model = Sequential()
# Embedding,将max_features维度降到 32 维
model.add(Embedding(max_features, 32))
# LSTM
model.add(LSTM(64, return_sequences=True))
model.add(LSTM(32))  # SimpleRNN(32)
# 全连接层
# 多分类使用 softmax
# model.add(Dense(10, activation='softmax'))
# 单分类使用 sigmoid
model.add(Dense(1, activation='sigmoid'))
# 编译模型
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(input_train, y_train, epochs=10, batch_size=128, validation_split=0.2)
t_end = time()
print('耗时:%.3f秒。', (t_end - t_start))acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']epochs = range(len(acc))
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.savefig('acc.png')plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.savefig('loss.png')
plt.show()

模型一般就2、3、4层,并不需要太深。

文本生成模型


汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字


不同的文字集合生成字典
前面n个词(词:向量)预测第n+1个词

话说天下大势,分久必合,合久必分。周末七国分争,并入于秦。及秦灭之后,楚、汉分争,又并入于汉。汉朝自高祖斩白蛇而起义,一统天下,后来光武中兴,传至献帝,遂__
已知之前的一句话(n个词),预测第n+1个词。

模型示例

W_hh,W_hy,W_xh 是被复用的。

ML、DL、CNN学习记录5相关推荐

  1. ML、DL、CNN学习记录8

    文章目录 ML.DL.CNN学习记录7 强化学习 Makov 贝尔曼方程 Value-Based 知识图谱 图神经网络 ML.DL.CNN学习记录7 强化学习(Reinforcement Learni ...

  2. ML、DL、CNN学习记录7

    文章目录 ML.DL.CNN学习记录7 GAN(Generative Adversarial Network) GAN的学习 GAN的损失函数 GAN 训练 GAN的扩展 DCGAN CGAN Sta ...

  3. ML、DL、CNN学习记录6

    文章目录 ML.DL.CNN学习记录5 VAE VAE 工作流程 VAE's Detail code+explain Model Output GAN GAN原理 CRNN(多用于文字识别) ML.D ...

  4. ML、DL、CNN学习记录3

    文章目录 ML.DL.CNN学习记录3 Transfer Learning ML.DL.CNN学习记录3 # coding: utf-8 # Date:2020/8/15 19:16 # @Autho ...

  5. ML、DL、CNN学习记录2

    文章目录 ML.DL.CNN学习记录2 图片通道 CNN 卷积 卷积如何操作 卷积核大小 卷积的意义 卷积后大小 卷积后大小计算公式(占的内存) 卷积后大小计算公式(运算时间) 1x1的卷积核 激活函 ...

  6. ml dl el学习_DeepChem —在生命科学和化学信息学中使用ML和DL的框架

    ml dl el学习 Application of Machine Learning and Deep Learning for Drug Discovery, Genomics, Microsoco ...

  7. ML、DL、CNN学习记录1

    文章目录 ML.DL学习记录1 ML .sklearn Tensorflow2.2.0安装问题 ML.DL学习记录1 ML .sklearn # coding: utf-8 # Date:2020/8 ...

  8. DL/T645-2007通信协议指令学习记录

    DL/T645-2007通信协议指令学习记录 DL/T645协议版本 DL/T645通信链路 DL/T645-2007数据格式 地址域 控制码C 数据长度L 数据域 DATA 校验码 CS 数据标识 ...

  9. python/ML/DL学习目录

    第一部分:python语法学习 . /*******环境搭建************/ 1. Anaconda安装 2. Anaconda的使用 3. Spyder的使用中遇到的问题 4. Jupyt ...

最新文章

  1. python两个集合的交集 合集 差集
  2. kettle将多个文件压缩_如何使用WinRAR将一个大文件压缩成多个小的压缩包
  3. axis使用wsdl生成客户端
  4. Redis操作ZSet类型
  5. 破解系统设计访谈:Twitter软件工程师的提示
  6. Jpa的@Id和@GeneratedValue的使用
  7. 如何让手机快速运行AI应用?这有份TVM优化教程
  8. oracle的nvl和nvl2是什么函数,两者区别
  9. MultiActionController
  10. Python之十点半小游戏
  11. 智能水电表远程管理系统
  12. JVM 字节码 栈图(Stack Map Table) 学习笔记
  13. linux用屏幕录制软件有哪些,Linux系统下推荐使用的5个屏幕录像软件是什么?
  14. jd-gui - 打开jar出现中文乱码问题
  15. 使用函数调用 输出三个数中的最大值,最小值
  16. git撤销commit
  17. iOS 玩转微信——通讯录
  18. html文字自动放大缩小单位,如何css控制字体按百分比放大缩小
  19. Python 入门 26 —— ASCII 编码、Unicode 编码、 UTF-32、 UTF-16、 UTF-8、 GB2312 编码、 GBK 编码
  20. java编程定义狗_用Java创建一条自己diy的狗狗类(6)

热门文章

  1. python 识别 None,NaN,null,‘‘,‘ ‘ 等无意义的值
  2. linux sar监控脚本,linux-利用sar进行性能监控
  3. jenkins 集成java搅拌_java-Jenkins中的集成测试
  4. java数据结栈空的条件表达式_数据结构——栈和队列例题
  5. ASP.NET Core2.2 和2.1 版本中对cookie的设置和存储
  6. Netty实战一之异步和事件驱动
  7. rsync实时同步服务部署
  8. 公开课 之 心蓝 计算器 (课堂笔记)
  9. 博客园 文章和随笔区别
  10. SQl---基础整理6--数据库的创建