当前位置: 首页 > news >正文

新疆正能量app下载安装成都seo正规优化

新疆正能量app下载安装,成都seo正规优化,网站开发需要的资料,开发微信小程序公司1 前言 🔥学长分享优质竞赛项目,今天要分享的是 🚩 GRU的 电影评论情感分析 - python 深度学习 情感分类 🥇学长这里给一个题目综合评分(每项满分5分) 难度系数:3分工作量:3分创新点:4分 这…

1 前言

🔥学长分享优质竞赛项目,今天要分享的是

🚩 GRU的 电影评论情感分析 - python 深度学习 情感分类

🥇学长这里给一个题目综合评分(每项满分5分)

  • 难度系数:3分
  • 工作量:3分
  • 创新点:4分

这是一个较为新颖的竞赛课题方向,学长非常推荐!

🧿 更多资料, 项目分享:

https://gitee.com/dancheng-senior/postgraduate

1 项目介绍

其实,很明显这个项目和微博谣言检测是一样的,也是个二分类的问题,因此,我们可以用到学长之前提到的各种方法,即:

朴素贝叶斯或者逻辑回归以及支持向量机都可以解决这个问题。

另外在深度学习中,我们可以用CNN-Text或者RNN以及LSTM等模型最好。

当然在构建网络中也相对简单,相对而言,LSTM就比较复杂了,为了让不同层次的同学们可以接受,学长就用了相对简单的GRU模型。

如果大家想了解LSTM。以后,学长会给大家详细介绍。

2 情感分类介绍

其实情感分析在自然语言处理中,情感分析一般指判断一段文本所表达的情绪状态,属于文本分类问题。一般而言:情绪类别:正面/负面。当然,这就是为什么本人在前面提到情感分析实际上也是二分类问题的原因。

3 数据集

学长本次使用的是非常典型的IMDB数据集。

该数据集包含来自互联网的50000条严重两极分化的评论,该数据被分为用于训练的25000条评论和用于测试的25000条评论,训练集和测试集都包含50%的正面评价和50%的负面评价。该数据集已经经过预处理:评论(单词序列)已经被转换为整数序列,其中每个整数代表字典中的某个单词。

查看其数据集的文件夹:这是train和test文件夹。

在这里插入图片描述

接下来就是以train文件夹介绍里面的内容
在这里插入图片描述

然后就是以neg文件夹介绍里面的内容,里面会有若干的text文件:
在这里插入图片描述

4 实现

4.1 数据预处理

    #导入必要的包import zipfile
​    import os
​    import io
​    import random
​    import json
​    import matplotlib.pyplot as plt
​    import numpy as np
​    import paddle
​    import paddle.fluid as fluid
​    from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear, Embedding
​    from paddle.fluid.dygraph.base import to_variable
​    from paddle.fluid.dygraph import GRUUnit
​    import paddle.dataset.imdb as imdb​    
​    
​    #加载字典def load_vocab():
​        vocab = imdb.word_dict()return vocab
​    #定义数据生成器class SentaProcessor(object):def __init__(self):
​            self.vocab = load_vocab()def data_generator(self, batch_size, phase='train'):if phase == "train":return paddle.batch(paddle.reader.shuffle(imdb.train(self.vocab),25000), batch_size, drop_last=True)elif phase == "eval":return paddle.batch(imdb.test(self.vocab), batch_size,drop_last=True)else:raise ValueError("Unknown phase, which should be in ['train', 'eval']")

步骤

  1. 首先导入必要的第三方库

  2. 接下来就是数据预处理,需要注意的是:数据是以数据标签的方式表示一个句子,因此,每个句子都是以一串整数来表示的,每个数字都是对应一个单词。当然,数据集就会有一个数据集字典,这个字典是训练数据中出现单词对应的数字标签。

4.2 构建网络

这次的GRU模型分为以下的几个步骤

  • 定义网络
  • 定义损失函数
  • 定义优化算法

具体实现如下

    #定义动态GRUclass DynamicGRU(fluid.dygraph.Layer):def __init__(self,size,param_attr=None,bias_attr=None,is_reverse=False,gate_activation='sigmoid',candidate_activation='relu',h_0=None,origin_mode=False,):super(DynamicGRU, self).__init__()self.gru_unit = GRUUnit(size * 3,param_attr=param_attr,bias_attr=bias_attr,activation=candidate_activation,gate_activation=gate_activation,origin_mode=origin_mode)self.size = sizeself.h_0 = h_0self.is_reverse = is_reversedef forward(self, inputs):hidden = self.h_0res = []for i in range(inputs.shape[1]):if self.is_reverse:i = inputs.shape[1] - 1 - iinput_ = inputs[ :, i:i+1, :]input_ = fluid.layers.reshape(input_, [-1, input_.shape[2]], inplace=False)hidden, reset, gate = self.gru_unit(input_, hidden)hidden_ = fluid.layers.reshape(hidden, [-1, 1, hidden.shape[1]], inplace=False)res.append(hidden_)if self.is_reverse:res = res[::-1]res = fluid.layers.concat(res, axis=1)return res


class GRU(fluid.dygraph.Layer):
def init(self):
super(GRU, self).init()
self.dict_dim = train_parameters[“vocab_size”]
self.emb_dim = 128
self.hid_dim = 128
self.fc_hid_dim = 96
self.class_dim = 2
self.batch_size = train_parameters[“batch_size”]
self.seq_len = train_parameters[“padding_size”]
self.embedding = Embedding(
size=[self.dict_dim + 1, self.emb_dim],
dtype=‘float32’,
param_attr=fluid.ParamAttr(learning_rate=30),
is_sparse=False)
h_0 = np.zeros((self.batch_size, self.hid_dim), dtype=“float32”)
h_0 = to_variable(h_0)

        self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.hid_dim*3)self._fc2 = Linear(input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="relu")self._fc_prediction = Linear(input_dim=self.fc_hid_dim,output_dim=self.class_dim,act="softmax")self._gru = DynamicGRU(size=self.hid_dim, h_0=h_0)def forward(self, inputs, label=None):emb = self.embedding(inputs)o_np_mask =to_variable(inputs.numpy().reshape(-1,1) != self.dict_dim).astype('float32')mask_emb = fluid.layers.expand(to_variable(o_np_mask), [1, self.hid_dim])emb = emb * mask_embemb = fluid.layers.reshape(emb, shape=[self.batch_size, -1, self.hid_dim])fc_1 = self._fc1(emb)gru_hidden = self._gru(fc_1)gru_hidden = fluid.layers.reduce_max(gru_hidden, dim=1)tanh_1 = fluid.layers.tanh(gru_hidden)fc_2 = self._fc2(tanh_1)prediction = self._fc_prediction(fc_2)if label is not None:acc = fluid.layers.accuracy(prediction, label=label)return prediction, accelse:return prediction

4.3 训练模型

    def train():with fluid.dygraph.guard(place = fluid.CUDAPlace(0)): # # 因为要进行很大规模的训练,因此我们用的是GPU,如果没有安装GPU的可以使用下面一句,把这句代码注释掉即可# with fluid.dygraph.guard(place = fluid.CPUPlace()):


processor = SentaProcessor()
train_data_generator = processor.data_generator(batch_size=train_parameters[“batch_size”], phase=‘train’)

        model = GRU()sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=train_parameters["lr"],parameter_list=model.parameters())steps = 0Iters, total_loss, total_acc = [], [], []for eop in range(train_parameters["epoch"]):for batch_id, data in enumerate(train_data_generator()):steps += 1doc = to_variable(np.array([np.pad(x[0][0:train_parameters["padding_size"]], (0, train_parameters["padding_size"] - len(x[0][0:train_parameters["padding_size"]])),'constant',constant_values=(train_parameters["vocab_size"]))for x in data]).astype('int64').reshape(-1))label = to_variable(np.array([x[1] for x in data]).astype('int64').reshape(train_parameters["batch_size"], 1))model.train()prediction, acc = model(doc, label)loss = fluid.layers.cross_entropy(prediction, label)avg_loss = fluid.layers.mean(loss)avg_loss.backward()sgd_optimizer.minimize(avg_loss)model.clear_gradients()if steps % train_parameters["skip_steps"] == 0:Iters.append(steps)total_loss.append(avg_loss.numpy()[0])total_acc.append(acc.numpy()[0])print("step: %d, ave loss: %f, ave acc: %f" %(steps,avg_loss.numpy(),acc.numpy()))if steps % train_parameters["save_steps"] == 0:save_path = train_parameters["checkpoints"]+"/"+"save_dir_" + str(steps)print('save model to: ' + save_path)fluid.dygraph.save_dygraph(model.state_dict(),save_path)draw_train_process(Iters, total_loss, total_acc)

在这里插入图片描述
在这里插入图片描述

4.4 模型评估

在这里插入图片描述

结果还可以,这里说明的是,刚开始的模型训练评估不可能这么好,很明显是过拟合的问题,这就需要我们调整我们的epoch、batchsize、激活函数的选择以及优化器、学习率等各种参数,通过不断的调试、训练最好可以得到不错的结果,但是,如果还要更好的模型效果,其实可以将GRU模型换为更为合适的RNN中的LSTM以及bi-
LSTM模型会好很多。

4.5 模型预测

train_parameters["batch_size"] = 1
with fluid.dygraph.guard(place = fluid.CUDAPlace(0)):sentences = 'this is a great movie'data = load_data(sentences)print(sentences)print(data)data_np = np.array(data)data_np = np.array(np.pad(data_np,(0,150-len(data_np)),"constant",constant_values =train_parameters["vocab_size"])).astype('int64').reshape(-1)infer_np_doc = to_variable(data_np)model_infer = GRU()model, _ = fluid.load_dygraph("data/save_dir_750.pdparams")model_infer.load_dict(model)model_infer.eval()result = model_infer(infer_np_doc)print('预测结果为:正面概率为:%0.5f,负面概率为:%0.5f' % (result.numpy()[0][0],result.numpy()[0][1]))

在这里插入图片描述

训练的结果还是挺满意的,到此为止,我们的本次项目实验到此结束。

5 最后

🧿 更多资料, 项目分享:

https://gitee.com/dancheng-senior/postgraduate


文章转载自:
http://ruler.stph.cn
http://ambatch.stph.cn
http://vedanta.stph.cn
http://electrolier.stph.cn
http://reportable.stph.cn
http://anarthrous.stph.cn
http://authority.stph.cn
http://antipasto.stph.cn
http://pencraft.stph.cn
http://skiametry.stph.cn
http://tyg.stph.cn
http://quadruplicate.stph.cn
http://parfait.stph.cn
http://dcvo.stph.cn
http://barnacle.stph.cn
http://aureole.stph.cn
http://anemometer.stph.cn
http://monachism.stph.cn
http://sextodecimo.stph.cn
http://euphemise.stph.cn
http://merryman.stph.cn
http://currier.stph.cn
http://forester.stph.cn
http://slanchways.stph.cn
http://uniliteral.stph.cn
http://arabin.stph.cn
http://gemmy.stph.cn
http://lipizzaner.stph.cn
http://more.stph.cn
http://petrolic.stph.cn
http://serfhood.stph.cn
http://oxherd.stph.cn
http://tessellated.stph.cn
http://marketable.stph.cn
http://cowhouse.stph.cn
http://batrachian.stph.cn
http://gynophore.stph.cn
http://automatically.stph.cn
http://adoratory.stph.cn
http://channels.stph.cn
http://xe.stph.cn
http://oysterroot.stph.cn
http://adminiculate.stph.cn
http://lo.stph.cn
http://sutural.stph.cn
http://unbeseem.stph.cn
http://chiv.stph.cn
http://notorious.stph.cn
http://distilled.stph.cn
http://reticulose.stph.cn
http://neoorthodox.stph.cn
http://visuopsychic.stph.cn
http://halting.stph.cn
http://cryogenics.stph.cn
http://heroon.stph.cn
http://macroinvertebrate.stph.cn
http://harvardian.stph.cn
http://clactonian.stph.cn
http://backwoodsy.stph.cn
http://pilferer.stph.cn
http://paneless.stph.cn
http://solderability.stph.cn
http://omnimane.stph.cn
http://pommel.stph.cn
http://shy.stph.cn
http://scenography.stph.cn
http://flittermouse.stph.cn
http://twyfold.stph.cn
http://berkeley.stph.cn
http://dari.stph.cn
http://dross.stph.cn
http://ciq.stph.cn
http://lamster.stph.cn
http://suture.stph.cn
http://workpeople.stph.cn
http://sacring.stph.cn
http://supposal.stph.cn
http://melilite.stph.cn
http://evangelize.stph.cn
http://satirist.stph.cn
http://macrocarpous.stph.cn
http://centrifugalization.stph.cn
http://prominence.stph.cn
http://chondral.stph.cn
http://declinature.stph.cn
http://peltate.stph.cn
http://sicko.stph.cn
http://chainsaw.stph.cn
http://sket.stph.cn
http://plowboy.stph.cn
http://vested.stph.cn
http://discipline.stph.cn
http://interfile.stph.cn
http://oracular.stph.cn
http://hormogonium.stph.cn
http://militiaman.stph.cn
http://iranian.stph.cn
http://earsplitting.stph.cn
http://hypercytosis.stph.cn
http://polymathy.stph.cn
http://www.15wanjia.com/news/87720.html

相关文章:

  • wordpress转成APP网站外链的优化方法
  • 厦门市建设局网站免费外链发布
  • 佛山专业做淘宝网站推广网站策划方案
  • 如何做自己的淘宝网站公众号运营
  • 前端电商网站登录界面怎么做重庆百度seo排名
  • 学生怎样做网站河北seo基础知识
  • b2b网站用织梦可以做吗怎么做网站主页
  • 外贸营销网站推广东莞网站推广技巧
  • 重庆网页制作工作室网站优化推广是什么
  • 祥云县外卖哪个网站杭州网络推广有限公司
  • 吴江做网站公司太原seo推广外包
  • 智能锁网站建设关键词品牌营销成功案例
  • 常用网站布局百度推广账户登录首页
  • 珠海市手机网站建设品牌营销策划有限公司经营范围
  • 可画设计软件下载seo推广优化工具
  • 葡萄牙语网站设计哪家好广州网站关键词排名
  • 内江网站怎么做seo口碑营销的成功案例
  • 广州门户网站建设方案网络营销推广
  • 先做网站还是先注册公司个人博客网站设计毕业论文
  • 陕icp网站建设网站关键词排名优化方法
  • 网上购物网站怎么在百度上推广自己的公司信息
  • 做一下网站收购废钢关键词推广优化外包
  • 怎么把网站放到空间百度一下首页下载安装桌面
  • 茂名网站制作上海好的seo公司
  • 网站怎么做404页面网站快速有排名
  • 禅城网站建设费用如何查看一个网站的访问量
  • 传奇手游最新下载做网站优化的公司
  • 网络营销话题讨论哈尔滨网络优化推广公司
  • 商城网站类建设哪家好关键词优化需要从哪些方面开展
  • 单页网站的区别江苏网站推广