-
大小: 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爬虫
- 感知机算法Python实现
- python 实现将TXT文件内容逐行存到EXC
- python 打开并计算两幅dicom图像感兴趣
- python 决策树代码
- 银行ATM系统(Python实现)
- pygame实现的贪吃蛇游戏RetroSnaker.py
- Python文件
- QT文件转换成Python的自动化工具*.ui转
- fcntl模块 win
- python爬虫爬取企业详细信息
- Kruskal算法python实现
- 蚁群算法的python代码
- 最小二乘法python代码,不用库函数
- sm3 python encode
- openopc for python 3.x
- 张正友相机标定Python代码
- Python调用QQ微信截图
- python图像裁剪
- 海明校验 python源代码 海明码
- mod_wsgi.so
- TensorFlow实现人脸识别(3)--------对人
- Python 强大的图论和网络研究工具 ne
- python从入门到精通视频60集全
- wheel 安装包
- Python程序设计与算法基础教程源代码
- python数据处理csv->图表
- 微博评论Python代码实现
- Python最小距离法
- 五子棋AI python实现
评论
共有 条评论