- https://github.com/udacity/deep-learning-v2-pytorch
- https://github.com/udacity/deep-learning-v2-pytorch/tree/master/sentiment-rnn/data # 数据
- PyTorch 实现双向LSTM 情感分析
文章目录
网络结构
模型训练与预测
1、Data Preprocessing
我们要去除标点符号。 同时,去除不同文本之间有分隔符号 \n
,我们先把\n
当成分隔符号,分割所有评论。 然后在将所有评论再次连接成为一个大的文本。
import numpy as np
# read data from text files
with open('./data/reviews.txt', 'r') as f:
reviews = f.read()
with open('./data/labels.txt', 'r') as f:
labels = f.read()
print(reviews[:1000])
print()
print(labels[:20])
from string import punctuation
# punctuation:'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
# get rid of punctuation
reviews = reviews.lower() # lowercase, standardize
all_text = ''.join([c for c in reviews if c not in punctuation])
# split by new lines and spaces
reviews_split = all_text.split('\n')
all_text = ' '.join(reviews_split)
# create a list of words
words = all_text.split()
2、Encoding the words
embedding lookup要求输入的网络数据是整数。最简单的方法就是创建数据字典:{单词:整数}。然后将评论全部一一对应转换成整数,传入网络。
# feel free to use this import
from collections import Counter
## Build a dictionary that maps words to integers
counts = Counter(words)
vocab = sorted(counts, key=counts.get, reverse=True)
vocab_to_int = {word: ii for ii, word in enumerate(vocab, 1)}
## use the dict to tokenize each review in reviews_split
## store the tokenized reviews in reviews_ints
reviews_ints = []
for review in reviews_split:
reviews_ints.append([vocab_to_int[word] for word in review.split()])
# stats about vocabulary
print('Unique words: ', len((vocab_to_int))) # should ~ 74000+
print()
# print tokens in first review
print('Tokenized review: \n', reviews_ints[:1])
3、Encoding the labels
将标签 “positive” or "negative"转换为数值。
# 1=positive, 0=negative label conversion
labels_split = labels.split('\n')
encoded_labels = np.array([1 if label == 'positive' else 0 for label in labels_split])
# outlier review stats
review_lens = Counter([len(x) for x in reviews_ints])
print("Zero-length reviews: {}".format(review_lens[0]))
print("Maximum review length: {}".format(max(review_lens)))
消除长度为0的行
print('Number of reviews before removing outliers: ', len(reviews_ints))
## remove any reviews/labels with zero length from the reviews_ints list.
# get indices of any reviews with length 0
non_zero_idx = [ii for ii, review in enumerate(reviews_ints) if len(review) != 0]
# remove 0-length reviews and their labels
reviews_ints = [reviews_ints[ii] for ii in non_zero_idx]
encoded_labels = np.array([encoded_labels[ii] for ii in non_zero_idx])
print('Number of reviews after removing outliers: ', len(reviews_ints))
4、Padding sequences
将所以句子统一长度为200个单词:
1、评论长度小于200的,我们对其左边填充0
2、对于大于200的,我们只截取其前200个单词
#选择每个句子长为200
seq_len = 200
from keras import preprocessing
features = np.zeros((len(reviews_ints),seq_len),dtype=int)
#将reviews_ints值逐行 赋值给features
features = preprocessing.sequence.pad_sequences(reviews_ints,200)
features.shape
or
def pad_features(reviews_ints, seq_length):
''' Return features of review_ints, where each review is padded with 0's
or truncated to the input seq_length.
'''
# getting the correct rows x cols shape
features = np.zeros((len(reviews_ints), seq_length), dtype=int)
# for each review, I grab that review and
for i, row in enumerate(reviews_ints):
features[i, -len(row):] = np.array(row)[:seq_length]
return features
# Test your implementation!
seq_length = 200
features = pad_features(reviews_ints, seq_length=seq_length)
## test statements - do not change - ##
assert len(features)==len(reviews_ints), "Your features should have as many rows as reviews."
assert len(features[0])==seq_length, "Each feature row should contain seq_length values."
# print first 10 values of the first 30 batches
print(features[:30,:10])
5、Training, Test划分
split_frac = 0.8
## split data into training, validation, and test data (features and labels, x and y)
split_idx = int(len(features)*split_frac)
train_x, remaining_x = features[:split_idx], features[split_idx:]
train_y, remaining_y = encoded_labels[:split_idx], encoded_labels[split_idx:]
test_idx = int(len(remaining_x)*0.5)
val_x, test_x = remaining_x[:test_idx], remaining_x[test_idx:]
val_y, test_y = remaining_y[:test_idx], remaining_y[test_idx:]
## print out the shapes of your resultant feature data
print("\t\t\tFeature Shapes:")
print("Train set: \t\t{}".format(train_x.shape),
"\nValidation set: \t{}".format(val_x.shape),
"\nTest set: \t\t{}".format(test_x.shape))
or
from sklearn.model_selection import ShuffleSplit
ss = ShuffleSplit(n_splits=1,test_size=0.2,random_state=0)
for train_index,test_index in ss.split(np.array(reviews_ints)):
train_x = features[train_index]
train_y = encoded_labels[train_index]
test_x = features[test_index]
test_y = encoded_labels[test_index]
print("\t\t\tFeature Shapes:")
print("Train set: \t\t{}".format(train_x.shape),
"\nTrain_Y set: \t{}".format(train_y.shape),
"\nTest set: \t\t{}".format(test_x.shape))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
features, encoded_labels, test_size=0.2, random_state=42)
6. DataLoaders and Batching
import torch
from torch.utils.data import TensorDataset, DataLoader
# create Tensor datasets
train_data = TensorDataset(torch.from_numpy(train_x), torch.from_numpy(train_y))
valid_data = TensorDataset(torch.from_numpy(val_x), torch.from_numpy(val_y))
test_data = TensorDataset(torch.from_numpy(test_x), torch.from_numpy(test_y))
# dataloaders
batch_size = 50
# make sure the SHUFFLE your training data
train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size)
valid_loader = DataLoader(valid_data, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_data, shuffle=True, batch_size=batch_size)
# obtain one batch of training data
dataiter = iter(train_loader)
sample_x, sample_y = dataiter.next()
print('Sample input size: ', sample_x.size()) # batch_size, seq_length
print('Sample input: \n', sample_x)
print()
print('Sample label size: ', sample_y.size()) # batch_size
print('Sample label: \n', sample_y)
7. 双向LSTM模型
- 判断是否有GPU
# First checking if GPU is available
train_on_gpu=torch.cuda.is_available()
if(train_on_gpu):
print('Training on GPU.')
else:
print('No GPU available, training on CPU.')
import torch.nn as nn
class SentimentRNN(nn.Module):
"""
The RNN model that will be used to perform Sentiment analysis.
"""
def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, bidirectional=True, drop_prob=0.5):
"""
Initialize the model by setting up the layers.
"""
super(SentimentRNN, self).__init__()
self.output_size = output_size
self.n_layers = n_layers
self.hidden_dim = hidden_dim
self.bidirectional = bidirectional
# embedding and LSTM layers
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers,
dropout=drop_prob, batch_first=True,
bidirectional=bidirectional)
# dropout layer
self.dropout = nn.Dropout(0.3)
# linear and sigmoid layers
if bidirectional:
self.fc = nn.Linear(hidden_dim*2, output_size)
else:
self.fc = nn.Linear(hidden_dim, output_size)
self.sig = nn.Sigmoid()
def forward(self, x, hidden):
"""
Perform a forward pass of our model on some input and hidden state.
"""
batch_size = x.size(0)
# embeddings and lstm_out
x = x.long()
embeds = self.embedding(x)
lstm_out, hidden = self.lstm(embeds, hidden)
# if bidirectional:
# lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim*2)
# else:
# lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)
# dropout and fully-connected layer
out = self.dropout(lstm_out)
out = self.fc(out)
# sigmoid function
sig_out = self.sig(out)
# reshape to be batch_size first
sig_out = sig_out.view(batch_size, -1)
sig_out = sig_out[:, -1] # get last batch of labels
# return last sigmoid output and hidden state
return sig_out, hidden
def init_hidden(self, batch_size):
''' Initializes hidden state '''
# Create two new tensors with sizes n_layers x batch_size x hidden_dim,
# initialized to zero, for hidden state and cell state of LSTM
weight = next(self.parameters()).data
number = 1
if self.bidirectional:
number = 2
if (train_on_gpu):
hidden = (weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().cuda(),
weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().cuda()
)
else:
hidden = (weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_(),
weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_()
)
return hidden
是否使用双向LSTM(在测试集上效果更好一些)
# Instantiate the model w/ hyperparams
vocab_size = len(vocab_to_int)+1 # +1 for the 0 padding + our word tokens
output_size = 1
embedding_dim = 400
hidden_dim = 256
n_layers = 2
bidirectional = False #这里为True,为双向LSTM
net = SentimentRNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers, bidirectional)
print(net)
8 Train
# loss and optimization functions
lr=0.001
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
# training params
epochs = 4 # 3-4 is approx where I noticed the validation loss stop decreasing
print_every = 100
clip=5 # gradient clipping
# move model to GPU, if available
if(train_on_gpu):
net.cuda()
net.train()
# train for some number of epochs
for e in range(epochs):
# initialize hidden state
h = net.init_hidden(batch_size)
counter = 0
# batch loop
for inputs, labels in train_loader:
counter += 1
if(train_on_gpu):
inputs, labels = inputs.cuda(), labels.cuda()
# Creating new variables for the hidden state, otherwise
# we'd backprop through the entire training history
h = tuple([each.data for each in h])
# zero accumulated gradients
net.zero_grad()
# get the output from the model
output, h = net(inputs, h)
# calculate the loss and perform backprop
loss = criterion(output.squeeze(), labels.float())
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
nn.utils.clip_grad_norm_(net.parameters(), clip)
optimizer.step()
# loss stats
if counter % print_every == 0:
# Get validation loss
val_h = net.init_hidden(batch_size)
val_losses = []
net.eval()
for inputs, labels in valid_loader:
# Creating new variables for the hidden state, otherwise
# we'd backprop through the entire training history
val_h = tuple([each.data for each in val_h])
if(train_on_gpu):
inputs, labels = inputs.cuda(), labels.cuda()
output, val_h = net(inputs, val_h)
val_loss = criterion(output.squeeze(), labels.float())
val_losses.append(val_loss.item())
net.train()
print("Epoch: {}/{}...".format(e+1, epochs),
"Step: {}...".format(counter),
"Loss: {:.6f}...".format(loss.item()),
"Val Loss: {:.6f}".format(np.mean(val_losses)))
9 Test
# Get test data loss and accuracy
test_losses = [] # track loss
num_correct = 0
# init hidden state
h = net.init_hidden(batch_size)
net.eval()
# iterate over test data
for inputs, labels in test_loader:
# Creating new variables for the hidden state, otherwise
# we'd backprop through the entire training history
h = tuple([each.data for each in h])
if(train_on_gpu):
inputs, labels = inputs.cuda(), labels.cuda()
# get predicted outputs
output, h = net(inputs, h)
# calculate loss
test_loss = criterion(output.squeeze(), labels.float())
test_losses.append(test_loss.item())
# convert output probabilities to predicted class (0 or 1)
pred = torch.round(output.squeeze()) # rounds to the nearest integer
# compare predictions to true label
correct_tensor = pred.eq(labels.float().view_as(pred))
correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(correct_tensor.cpu().numpy())
num_correct += np.sum(correct)
# -- stats! -- ##
# avg test loss
print("Test loss: {:.3f}".format(np.mean(test_losses)))
# accuracy over all test data
test_acc = num_correct/len(test_loader.dataset)
print("Test accuracy: {:.3f}".format(test_acc))
模型Inference
# negative test review
test_review_neg = 'The worst movie I have seen; acting was terrible and I want my money back. This movie had bad acting and the dialogue was slow.'
from string import punctuation
def tokenize_review(test_review):
test_review = test_review.lower() # lowercase
# get rid of punctuation
test_text = ''.join([c for c in test_review if c not in punctuation])
# splitting by spaces
test_words = test_text.split()
# tokens
test_ints = []
test_ints.append([vocab_to_int[word] for word in test_words])
return test_ints
# test code and generate tokenized review
test_ints = tokenize_review(test_review_neg)
print(test_ints)
# test sequence padding
seq_length=200
features = pad_features(test_ints, seq_length)
print(features)
# test conversion to tensor and pass into your model
feature_tensor = torch.from_numpy(features)
print(feature_tensor.size())
def predict(net, test_review, sequence_length=200):
net.eval()
# tokenize review
test_ints = tokenize_review(test_review)
# pad tokenized sequence
seq_length=sequence_length
features = pad_features(test_ints, seq_length)
# convert to tensor to pass into your model
feature_tensor = torch.from_numpy(features)
batch_size = feature_tensor.size(0)
# initialize hidden state
h = net.init_hidden(batch_size)
if(train_on_gpu):
feature_tensor = feature_tensor.cuda()
# get the output from the model
output, h = net(feature_tensor, h)
# convert output probabilities to predicted class (0 or 1)
pred = torch.round(output.squeeze())
# printing output value, before rounding
print('Prediction value, pre-rounding: {:.6f}'.format(output.item()))
# print custom response
if(pred.item()==1):
print("Positive review detected!")
else:
print("Negative review detected.")
# positive test review
test_review_pos = 'This movie had the best acting and the dialogue was so good. I loved it.'
# call function
seq_length=200 # good to use the length that was trained on
predict(net, test_review_neg, seq_length)
完整代码
import torch
from torch import nn
import torch.nn.functional as F
from torch.nn.utils import clip_grad_norm_
# from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader,Dataset,TensorDataset
from collections import Counter
from sklearn.model_selection import train_test_split
from string import punctuation
import os
from tqdm import tqdm
import re
import numpy as np
import time
import pickle
if os.path.exists('data.pkl'):
data = pickle.load(open('data.pkl', 'rb'))
X_train = data['X_train']
X_test = data['X_test']
y_train = data['y_train']
y_test = data['y_test']
vocabulary = data['vocabulary']
seq_len = data['seq_len']
del data
else:
# 1、数据预处理
with open('./data/reviews.txt','r') as fp:
reviews = fp.readlines()
with open('./data/labels.txt','r') as fp:
labels = fp.readlines()
labels_ = []
for label in labels:
labels_.append(1 if label.strip()=='positive' else 0)
labels = np.array(labels_)
del labels_
counter=Counter()
datas= []
for review in tqdm(reviews):
# review = re.split(r'\W+',review.strip())
review = "".join([c for c in review.strip().lower() if c not in punctuation]) # 去除特殊字符
tmp = [item for item in review.split(" ") if len(item)>0]
# 去掉停用词(Word2vec 可不做)
datas.append(tmp)
counter.update(tmp)
# 2、构建词汇表
pad = '<pad>'
vocabulary = sorted(list(counter.keys()))
vocabulary.insert(0,pad) # 0作为填充
word_to_ix = dict(zip(vocabulary,np.arange(len(vocabulary))))
del counter
del reviews
# 3、序列量化
# 将所以句子统一长度为200个单词:
# 1、评论长度小于200的,我们对其左边填充0
# 2、对于大于200的,我们只截取其前200个单词
seq_len = 200
new_datas=[]
for data in tqdm(datas):
if len(data) >= seq_len:
data = data[:seq_len]
else:
data = [pad]*(seq_len-len(data))+data
new_datas.append([word_to_ix[word] for word in data])
datas = np.array(new_datas)
del new_datas
# Training, Test划分
X_train, X_test, y_train, y_test = train_test_split(datas,labels,test_size=0.2,random_state=30)
del datas
del labels
pickle.dump({'X_train':X_train,'X_test':X_test,'y_train':y_train,'y_test':y_test,
'vocabulary':vocabulary,'seq_len':seq_len
},open('data.pkl','wb'))
# 4、构建模型
class SentimentRNN(nn.Module):
"""
The RNN model that will be used to perform Sentiment analysis.
"""
def __init__(self, vocab_size, output_size,sent_dim,embedding_dim,
hidden_dim, n_layers, bidirectional=True, drop_prob=0.5,device='cpu'):
"""
Initialize the model by setting up the layers.
"""
super().__init__()
self.hidden_dim = hidden_dim
self.sent_dim = sent_dim
self.device = device
self.n_layers = n_layers
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers,
dropout=drop_prob, batch_first=True,
bidirectional=bidirectional)
# dropout layer
self.dropout = nn.Dropout(0.3)
# linear and sigmoid layers
if bidirectional:
self.number = 2
self.fc = nn.Linear(hidden_dim * 2, output_size)
else:
self.number = 1
self.fc = nn.Linear(hidden_dim, output_size)
self.sig = nn.Sigmoid()
self.hidden = None
def forward(self,x,hidden):
x = x.long()
embedded = self.embedding(x)
lstm_out, hidden = self.lstm(embedded,hidden)
# if bidirectional:
# lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim*2)
# else:
# lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)
# dropout and fully-connected layer
out = self.dropout(lstm_out)
out = self.fc(out)
# sigmoid function
sig_out = self.sig(out)
# reshape to be batch_size first
# sig_out = sig_out.view(batch_size, -1)
# sig_out = sig_out[:, -1] # get last batch of labels
sig_out = sig_out[:,-1,:].squeeze() # 取最后一个序列的输出作为输出
return sig_out,hidden
def init_hidden(self,batch_size):
# the axes semantics are (bn,sent_dim,hidden_size)
return (torch.zeros(self.n_layers*self.number, batch_size, self.hidden_dim, device=self.device),
(torch.zeros(self.n_layers*self.number, batch_size, self.hidden_dim, device=self.device)))
class SentimentRNNV2(nn.Module):
"""
The RNN model that will be used to perform Sentiment analysis.
"""
def __init__(self, vocab_size, output_size,sent_dim,embedding_dim,
hidden_dim, n_layers, bidirectional=True, drop_prob=0.5,device='cpu'):
"""
Initialize the model by setting up the layers.
"""
super().__init__()
self.hidden_dim = hidden_dim
self.sent_dim = sent_dim
self.device = device
self.n_layers = n_layers
self.bidirectional = bidirectional
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers,
dropout=drop_prob, batch_first=True,
bidirectional=bidirectional)
# dropout layer
self.dropout = nn.Dropout(0.3)
# linear and sigmoid layers
if bidirectional:
self.number = 2
self.fc = nn.Linear(sent_dim*hidden_dim * 2, output_size)
else:
self.number = 1
self.fc = nn.Linear(sent_dim*hidden_dim, output_size)
self.sig = nn.Sigmoid()
self.hidden = None
def forward(self,x,hidden):
x = x.long()
embedded = self.embedding(x)
lstm_out, hidden = self.lstm(embedded,hidden)
if self.bidirectional:
lstm_out = lstm_out.contiguous().view(-1, self.sent_dim*self.hidden_dim*2)
else:
lstm_out = lstm_out.contiguous().view(-1, self.sent_dim*self.hidden_dim)
# dropout and fully-connected layer
out = self.dropout(lstm_out)
out = self.fc(out)
# sigmoid function
sig_out = self.sig(out).squeeze(1)
return sig_out,hidden
def init_hidden(self,batch_size):
# the axes semantics are (bn,sent_dim,hidden_size)
return (torch.zeros(self.n_layers*self.number, batch_size, self.hidden_dim, device=self.device),
(torch.zeros(self.n_layers*self.number, batch_size, self.hidden_dim, device=self.device)))
def train(model,optimizer,dataloader,criterion,device,epoch):
model.train()
total_acc, total_count = 0, 0
log_interval = 500
start_time = time.time()
total_loss = 0
for idx, (data, label) in enumerate(dataloader):
label = label.to(device)
data = data.to(device)
optimizer.zero_grad()
# model.zero_grad()
if idx==0:hidden = model.init_hidden(data.size(0))
# Creating new variables for the hidden state, otherwise
# we'd backprop through the entire training history
hidden = tuple([each.data for each in hidden]) # 必须加上这句否则报错 hidden不做梯度更新
predited_label,hidden = model(data,hidden)
loss = criterion(predited_label, label)
total_loss += loss.item()
loss = loss/len(predited_label)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.1)
optimizer.step()
# total_acc += (predited_label.argmax(1) == label).sum().item()
total_acc += (predited_label.round() == label).sum().item()
total_count += label.size(0)
if idx % log_interval == 0 and idx > 0:
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches '
'| accuracy {:8.3f}'.format(epoch, idx, len(dataloader),
total_acc/total_count))
total_acc, total_count = 0, 0
start_time = time.time()
return total_acc/total_count,total_count/total_count
def evaluate(model,dataloader,criterion,device):
model.eval()
total_acc, total_count = 0, 0
total_loss = 0
with torch.no_grad():
for idx, (data, label) in enumerate(dataloader):
label = label.to(device)
data = data.to(device)
if idx==0:hidden = model.init_hidden(data.size(0))
predited_label,hidden = model(data,hidden)
loss = criterion(predited_label, label)
total_loss += loss.item()
# total_acc += (predited_label.argmax(1) == label).sum().item()
total_acc += (predited_label.round() == label).sum().item()
total_count += label.size(0)
return total_acc/total_count,total_loss/total_count
def main():
batch_size = 32
train_data = TensorDataset(torch.from_numpy(X_train),torch.from_numpy(y_train).float())
test_data = TensorDataset(torch.from_numpy(X_test),torch.from_numpy(y_test).float())
train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True,drop_last=True)
valid_dataloader = DataLoader(test_data, batch_size=batch_size, shuffle=True,drop_last=True)
# ,drop_last=True 不足一个batch 丢弃
EPOCHS = 10
LR = 5 # learning rate
vocab_size = len(vocabulary)
sent_dim = seq_len
embedding_dim = 64#400
hidden_dim = 32#256
output_size = 1
device = "cuda:0"
n_layers = 2
bidirectional = False # 这里为True,为双向LSTM
model = SentimentRNN(vocab_size, output_size,sent_dim, embedding_dim, hidden_dim,
n_layers, bidirectional,device=device).to(device)
# model = SentimentRNNV2(vocab_size, output_size, sent_dim, embedding_dim, hidden_dim,
# n_layers, bidirectional, device=device).to(device)
criterion = torch.nn.BCELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=LR, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1, gamma=0.5)
total_accu = None
for epoch in range(1, EPOCHS + 1):
epoch_start_time = time.time()
accu_train,loss_train = train(model, optimizer, train_dataloader, criterion, device,epoch)
accu_val,loss_val = evaluate(model, valid_dataloader, criterion, device)
if total_accu is not None and total_accu > accu_val:
scheduler.step()
else:
total_accu = accu_val
print('-' * 59)
print('| end of epoch {:3d} | time: {:5.2f}s | '
'valid accuracy {:8.3f} '.format(epoch,
time.time() - epoch_start_time,
accu_val))
print('-' * 59)
if __name__ == "__main__":
main()
sklearn方法
from collections import Counter
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer,TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from string import punctuation
import os
from tqdm import tqdm
import re
import numpy as np
import time
import pickle
from spacy.lang.en.stop_words import STOP_WORDS
if os.path.exists('data.pkl'):
data = pickle.load(open('data.pkl', 'rb'))
X_train = data['X_train']
X_test = data['X_test']
y_train = data['y_train']
y_test = data['y_test']
del data
else:
# 1、数据预处理
with open('./data/reviews.txt','r') as fp:
reviews = fp.readlines()
with open('./data/labels.txt','r') as fp:
labels = fp.readlines()
labels_ = []
for label in labels:
labels_.append(1 if label.strip()=='positive' else 0)
labels = np.array(labels_)
del labels_
counter=Counter()
datas= []
for review in tqdm(reviews):
# review = re.split(r'\W+',review.strip())
review = "".join([c for c in review.strip().lower() if c not in punctuation]) # 去除特殊字符
tmp = [item for item in review.split(" ") if len(item)>0 and item not in STOP_WORDS] # 去掉停用词
datas.append(tmp)
counter.update(tmp)
# 2、构建词汇表
# 去除掉高频词和低频词
mean_value = np.mean(list(counter.values()))
min_value = 10 # mean_value/2
max_value = 20000 # mean_value*8
counter = {k: v for k, v in counter.items() if v < max_value and v > min_value}
vocabulary = sorted(list(counter.keys()))
# 3、序列量化
new_datas=[]
tv = TfidfVectorizer(vocabulary=vocabulary)
for data in tqdm(datas):
new_datas.append(tv.fit_transform([" ".join(data)]).toarray())
datas = np.concatenate(new_datas,0)
del new_datas
# Training, Test划分
X_train, X_test, y_train, y_test = train_test_split(datas,labels,test_size=0.2,random_state=30)
del datas
del labels
pickle.dump({'X_train':X_train,'X_test':X_test,'y_train':y_train,'y_test':y_test},open('data.pkl','wb'))
# 4、构建模型
lr = LogisticRegression()
lr.fit(X_train,y_train)
print('score:%.5f'%lr.score(X_test,y_test))