前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NLP基础:NNLM模型代码示例

NLP基础:NNLM模型代码示例

作者头像
三猫
发布2022-11-25 20:34:42
3060
发布2022-11-25 20:34:42
举报

导读:在NLP基础:NNLM模型介绍中,已经介绍了NNLM模型原理,通过对网上已发布的代码进行完善并标注,进行模型代码示例展示。

1、Keras实现

代码主要部分如下:

代码语言:javascript
复制
from keras.models import Sequential

import numpy as np
import tensorflow as tf
import re
sentences = [ "我渴了", "你真好", "他的错", "对不起" , "他走了"]

# NNLM Parameter
n_step = len(sentences[0])-1 # number of steps ['我 渴', '你 真', '他 的', '对 不', '他 走']

#分字
def seg_char(sent):
    pattern = re.compile(r'([\u4e00-\u9fa5])')
    chars = pattern.split(sent)
    chars =[w for w in chars if len(w.strip()) > 0]
    return chars

#得到每个句子前n-1个词和目标词
chars=np.array([seg_char(i)for i in sentences])
chars=chars.reshape(1,-1)
word_list=np.squeeze(chars)
#['我' '渴' '了' '你' '真' '好' '他' '的' '错' '对' '不' '起' '他' '走' '了']
word_list = list(set(word_list))
word_dict = {w: i for i, w in enumerate(word_list)}
#{'渴': 0, '错': 1, '不': 2, '好': 3, '起': 4, '他': 5, '对': 6, '你': 7, '走': 8, '我': 9, '了': 10, '的': 11, '真': 12}
number_dict = {i: w for i, w in enumerate(word_list)}
#{0: '渴', 1: '错', 2: '不', 3: '好', 4: '起', 5: '他', 6: '对', 7: '你', 8: '走', 9: '我', 10: '了', 11: '的', 12: '真'}
n_class = len(word_dict) # number of Vocabulary

#这里通过one-hot进行词向量生成
#one-hot转换
def make_batch(sentences):
    input_batch = []
    target_batch = []

    for sen in sentences:
        #将每个句子中的字转化为下标表示
        word = seg_char(sen)
        input = [word_dict[n] for n in word[:-1]]
        target = word_dict[word[-1]]
        
        #one-hot转换
        input_batch.append(np.eye(n_class)[input])
        target_batch.append(np.eye(n_class)[target])

    return input_batch, target_batch

input_batch, target_batch=make_batch(sentences)
input_batch=np.array(input_batch)
#将输入词向量进行首尾拼接
input_batch=input_batch.reshape(-1,n_step*n_class)
target_batch=np.array(target_batch)
target_batch=target_batch.reshape(-1,n_class)

from keras.layers import Dense
import keras

#建立模型,本模型暂不包含直连边
def define_model():
    model = Sequential()

    #Dense为全连接网络
    model.add(Dense(2,activation='tanh',input_shape=(n_step*n_class,))) # 输入层
    model.add(Dense(n_class, activation='softmax'))  # 输出层
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    model.summary()
    return model

#训练模型
model=define_model()
model.fit(input_batch, target_batch, epochs=2000)#训练2000轮,数据少啦,一两轮没效果

#预测测试
predict=model.predict(input_batch)

得到结果如下:

参考文章:https://blog.csdn.net/kobeyu652453/article/details/108238642

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习养成记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档