-
大小: 4.79MB文件类型: .zip金币: 1下载: 0 次发布日期: 2023-11-18
- 语言: Python
- 标签:
资源简介
基于tensorflow 实现的用textcnn方法做情感分析的项目,有数据,可以直接跑
代码片段和文件信息
import numpy as np
import re
import word2vec
# import itertools
# from collections import Counter
# import codecs
class w2v_wrapper:
def __init__(selffile_path):
# w2v_file = os.path.join(base_path “vectors_poem.bin“)
self.model = word2vec.load(file_path)
if ‘unknown‘ not in self.model.vocab_hash:
unknown_vec = np.random.uniform(-0.10.1size=128)
self.model.vocab_hash[‘unknown‘] = len(self.model.vocab)
self.model.vectors = np.row_stack((self.model.vectorsunknown_vec))
def clean_str(string):
“““
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
“““
string = re.sub(r“[^A-Za-z0-9()!?\‘\‘]“ “ “ string)
string = re.sub(r“\‘s“ “ \‘s“ string)
string = re.sub(r“\‘ve“ “ \‘ve“ string)
string = re.sub(r“n\‘t“ “ n\‘t“ string)
string = re.sub(r“\‘re“ “ \‘re“ string)
string = re.sub(r“\‘d“ “ \‘d“ string)
string = re.sub(r“\‘ll“ “ \‘ll“ string)
string = re.sub(r““ “ “ string)
string = re.sub(r“!“ “ ! “ string)
string = re.sub(r“\(“ “ \( “ string)
string = re.sub(r“\)“ “ \) “ string)
string = re.sub(r“\?“ “ \? “ string)
string = re.sub(r“\s{2}“ “ “ string)
return string.strip().lower()
def removezero( x y):
nozero = np.nonzero(y)
print(‘removezero‘np.shape(nozero)[-1]len(y))
if(np.shape(nozero)[-1] == len(y)):
return np.array(x)np.array(y)
y = np.array(y)[nozero]
x = np.array(x)
x = x[nozero]
return x y
def read_file_lines(filenamefrom_sizeline_num):
i = 0
text = []
end_num = from_size + line_num
for line in open(filename):
if(i >= from_size):
text.append(line.strip())
i += 1
if i >= end_num:
return text
return text
def load_data_and_labels(filepathmax_size = -1):
“““
Loads MR polarity data from files splits the data into words and generates labels.
Returns split sentences and labels.
“““
# Load data from files
train_datas = []
with open(filepath ‘r‘ encoding=‘utf-8‘errors=‘ignore‘) as f:
train_datas = f.readlines()
one_hot_labels = []
x_datas = []
for line in train_datas:
parts = line.split(‘\t‘1)
if(len(parts[1].strip()) == 0):
continue
x_datas.append(parts[1])
if parts[0].startswith(‘0‘) :
one_hot_labels.append([01])
else:
one_hot_labels.append([10])
print (‘ data size = ‘ len(train_datas))
# Split by words
# x_text = [clean_str(sent) for sent in x_text]
return [x_datas np.array(one_hot_labels)]
def batch_iter(data batch_size num_epochs shuffle=True):
“““
Generates a batch iterator for a dataset.
““
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-08-25 01:45 sentiment_analysis_textcnn-master\
文件 472 2017-08-25 01:45 sentiment_analysis_textcnn-master\README.md
文件 33806 2017-08-25 01:45 sentiment_analysis_textcnn-master\accuracy.png
目录 0 2017-08-25 01:45 sentiment_analysis_textcnn-master\data\
文件 3654737 2017-08-25 01:45 sentiment_analysis_textcnn-master\data\cutclean_label_corpus10000.txt
文件 4131771 2017-08-25 01:45 sentiment_analysis_textcnn-master\data\vectors.bin
目录 0 2017-08-25 01:45 sentiment_analysis_textcnn-master\textcnn\
文件 4330 2017-08-25 01:45 sentiment_analysis_textcnn-master\textcnn\data_input_helper.py
文件 3982 2017-08-25 01:45 sentiment_analysis_textcnn-master\textcnn\eval.py
文件 4131 2017-08-25 01:45 sentiment_analysis_textcnn-master\textcnn\text_cnn.py
文件 10312 2017-08-25 01:45 sentiment_analysis_textcnn-master\textcnn\train.py
相关资源
- Python-TensorFlow弱监督图像分割
- Python-Keras实现Inceptionv4InceptionResnetv1和
- Python-pytorch中文手册
- Python-FastSCNN的PyTorch实现快速语义分割
- Python-滑动窗口高分辨率显微镜图像分
- Python-使用MovieLens数据集训练的电影推
- Python-机器学习驱动的Web应用程序防火
- Python-subpixel利用Tensorflow的一个子像素
-
Python-汉字的神经风格转移Neuralst
y - Python-神经网络模型能够从音频演讲中
- Python-深度增强学习算法的PyTorch实现策
- Python-基于深度学习的语音增强使用
- Python-基于知识图谱的红楼梦人物关系
- Python-STGAN用于图像合成的空间变换生
- Python-利用GAN进行图片填充
- Python-基于50W携程出行攻略的顺承事件
- Python-在TensorFlow中实现实现图像卷积网
- Python-60DaysRLChallenge中文版强化学习6
- Python-一个非常简单的BiLSTMCRF模型用于
- Python-Tensorflow仿AlphaGo框架实现的AI围棋
- Python-我是小诗姬全唐诗作为训练数据
- Python-用于物体跟踪的全卷积连体网络
- Python-数学建模竞赛中所使用的相关算
- Python-MonoDepthPyTorchPyTorch无监督单目深
- Python-用Tensorflowjs实现的可回收非可回
- Python-利用TensorFlow中的深度学习进行图
- Python-TensorFlow快速入门与实战课件与参
- Python-FCN完全卷积网络中最简单最容易
- Python-匈牙利算法卡尔曼滤波器多目标
- Python-mathAI一个拍照做题程序输入一张
评论
共有 条评论