糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > ML DL CNN学习记录5

ML DL CNN学习记录5

时间:2020-12-14 16:10:48

相关推荐

ML DL CNN学习记录5

文章目录

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

ML、DL、CNN学习记录4

Time/Spatial:

SimpleRNN

LSTM

GRU

RNN <—> Attention/Transformers

RNN

RNN类别

many to one:文本分类、股价预测

many to mary:语音识别(语音->文字)、语音合成(文字->语音)、翻译

Word 表示

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

编码

one-hot编码

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

Morkov model:

一阶Morlov:wiw_iwi​只与wi−1w_{i-1}wi−1​有关

二阶Morlov:wiw_iwi​只与wi−1wi−2w_{i-1}w_{i-2}wi−1​wi−2​有关

x阶Morlov:wiw_iwi​只与wi−1wi−2...wi−x+1w_{i-1}w_{i-2}...w_{i-x+1}wi−1​wi−2​...wi−x+1​有关

w1w2w2w3w4...wmw_1w_2w_2w_3w_4...w_mw1​w2​w2​w3​w4​...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_iwi−x+1​wi−x+2​...wi−1​+wi+1​wi+2​...wi+x​⟶wi​

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}wi​⟶wi−x+1​wi−x+2​...wi−1​+wi+1​wi+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 kerasfrom keras.models import Sequentialfrom keras.layers import Embedding, SimpleRNN, LSTM, Densefrom keras.datasets import imdb# 辅助函数(帮助)from keras.preprocessing import sequenceimport matplotlib.pyplot as pltfrom time import timet_start = time()max_features = 10000 # number of words to consider as featuresmaxlen = 500 # cut texts after this number of wordsbatch_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))# LSTMmodel.add(LSTM(64, return_sequences=True))model.add(LSTM(32)) # SimpleRNN(32)# 全连接层# 多分类使用 softmax# model.add(Dense(10, activation='softmax'))# 单分类使用 sigmoidmodel.add(Dense(1, activation='sigmoid'))# 编译模型pile(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》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。