### 文章目录

 Hi，大家好，这里是丹成学长的毕设系列文章！

 对毕设有任何疑问都可以问学长哦!

####  基于GRU的 电影评论情感分析

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

#### 刺 选题指导, 项目分享：

https://blog.csdn.net/Mr_DC_IT/article/details/126460477

## 4.1 数据预处理

```#导入必要的包
import zipfile
import os
import io
import random
import json
import matplotlib.pyplot as plt
import numpy as np
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear, Embedding

```#加载字典
vocab = imdb.word_dict()
return vocab
#定义数据生成器
class SentaProcessor(object):
def __init__(self):
def data_generator(self, batch_size, phase='train'):
if phase == "train":
elif phase == "eval":
else:
raise ValueError(
"Unknown phase, which should be in ['train', 'eval']")```

## 4.2 构建网络

```#定义动态GRU
class 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 = size
self.h_0 = h_0
self.is_reverse = is_reverse
def forward(self, inputs):
hidden = self.h_0
res = []
for i in range(inputs.shape[1]):
if self.is_reverse:
i = inputs.shape[1] - 1 - i
input_ = 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.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)
emb = 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, acc
else:
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()
steps = 0
Iters, total_loss, total_acc = [], [], []
for eop in range(train_parameters["epoch"]):
for batch_id, data in enumerate(train_data_generator()):
steps += 1
doc = to_variable(
np.array([
'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)

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.5 模型预测

```train_parameters["batch_size"] = 1
with fluid.dygraph.guard(place = fluid.CUDAPlace(0)):
sentences = 'this is a great movie'
print(sentences)
print(data)
data_np = np.array(data)
infer_np_doc = to_variable(data_np)
model_infer = GRU()
model_infer.eval()
result = model_infer(infer_np_doc)
print('预测结果为：正面概率为：%0.5f，负面概率为：%0.5f' % (result.numpy()[0][0],result.numpy()[0][1]))```

#### 刺 选题指导, 项目分享：

https://blog.csdn.net/Mr_DC_IT/article/details/126460477