什么是RNN?








代码
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
import os
import re
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu,True)
gpu_ok = tf.test.is_gpu_available()
print("tf version:", tf.__version__)
print("use GPU", gpu_ok)

读取数据
data = pd.read_csv ("F:/py/ziliao/数据集/Tweets.csv")

data = data[["airline_sentiment","text"]]


data_p = data[data.airline_sentiment=="positive"]
data_n = data[data.airline_sentiment=="negative"]
data_n = data_n.iloc[:len(data_p)]

数据预处理
data = pd.concat([data_n,data_p])
len(data)



data["review"] = (data.airline_sentiment=="positive").astype("int")


处理评论文本
token = re.compile("[A-Za-z]+|[!?,.()]")
def reg_text(text):
new_text = token.findall(text)
new_text = [word.lower()for word in new_text]
return new_text
data["text"] = data.text.apply(reg_text)

word_set = set()
for text in data.text:
for word in text:
word_set.add(word)


word_list = list(word_set)

word_index = dict((word,word_list.index(word)+1) for word in word_list)

data_ok = data.text.apply(lambda x:[word_index.get(word,0)for word in x])

max(len(x)for x in data_ok),min(len(x)for x in data_ok)

maxlen = max(len(x)for x in data_ok)
data_ok = tf.keras.preprocessing.sequence.pad_sequences(data_ok.values,
maxlen=maxlen)


搭建循环神经网络
model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(max_word,50,input_length=maxlen))
model.add(tf.keras.layers.LSTM(64))
model.add(tf.keras.layers.Dense(1,activation="sigmoid"))

model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["acc"]
)
model.fit(data_ok,label,
epochs=10,
batch_size=128,
validation_split=0.2
)
