-
大小: 11KB文件类型: .py金币: 1下载: 0 次发布日期: 2021-05-19
- 语言: Python
- 标签: Tensorflow python BiLSTM
资源简介
该段Tensorflow代码可用于文本分类,和情感分类。其主要特点是,在同一份代码中,同时实现两张张量图,一张用于训练,另一张用于测试。并做交叉验证。
代码片段和文件信息
#! usr/bin/env python3
# -*- coding:utf-8 -*-
“““
@Author:zhoukaiyin
@Time:2017/7/22
“““
import glob
import tensorflow as tf
import gensim
import numpy as np
import pickle
import os
from sklearn.metrics import precision_score recall_score f1_score
from sklearn.model_selection import KFold
def data2index():
words = list(get_word())
w2label = {w:i for iw in enumerate(words1)}
label2w = {i:w for iw in enumerate(words1)}
return w2labellabel2w
def get_word():
words=[]
files = glob.glob(“../BIO/*“)
for file in files:
with open(file‘r‘) as rf:
for line in rf:
line = line.strip()
if len(line)!=0:
w = line.split(‘\t‘)[0]
words.append(w)
return set(words)
def get_phara():
pharas = []
labels = []
human_feature = []
files = glob.glob(“../BIO/*“)
with open(“h_features.pkl“‘rb‘) as hf:
features = pickle.load(hfencoding=“bytes“)
m_features = eval(str(features))
with open(‘../label/result.txt‘‘r‘) as pf:
dom = pf.read()
labe_dir = eval(dom)
for file in files:
phara = []
name = os.path.basename(file)
label = labe_dir[name]
if label!=‘unknown‘:
human_feature.append(m_features[name])
label = int(label)
with open(file‘r‘) as rf:
for line in rf:
line = line.strip()
if len(line)!=0:
w = line.split(‘\t‘)[0]
phara.append(w)
labels.append(label)
pharas.append(phara)
return pharaslabelshuman_feature
def w2embed():
w2labellabel2w = data2index()
model = gensim.models.KeyedVectors.load_word2vec_format(“glove200.txt“ binary=False)
embeding=[]
dim = model.vector_size
add = np.random.randn(dim)
embeding.append(add)
count = 0
ncount = 0
for i in range(1 len(w2label) + 1):
word = label2w[i].lower()
try:
embed = model[word]
except KeyError:
count += 1
embed = np.zeros((dim))
word_list = word.split(‘-‘)
try:
for i w in enumerate(word_list):
embed += model[word_list[i]]
except KeyError:
ncount+=1
embed = embeding[0]
embeding.append(embed)
print(“一共有{}个单词没有被找到有{}个重新被找到!“.format(countcount-ncount))
wf=open(“embed.pkl“‘wb‘)
wwf = open(“w2label.pkl“‘wb‘)
pickle.dump(w2labelwwf)
pickle.dump(embedingwf)
wf.close()
wwf.close()
def load_data():
rrf = open(“w2label.pkl“‘rb‘)
w2label = pickle.load(rrfencoding=“bytes“)
index_pharas = []
labels = []
pharas clsshuman_feature= get_phara()
for iphara in enumerate(pharas):
ph
- 上一篇:感知机算法Python实现
- 下一篇:最好中国大学近几年排名及python爬虫代码
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- DeepLabV3-Tensorflow-master
- Python Machine Learning Case Studies
评论
共有 条评论