资源简介
本代码是在参考了别人代码的基础上进一步修改的,该代码的功能是用Bi-LSTM+CRF进行NER任务,仅供大家参考!
代码片段和文件信息
import tensorflow as tf
from Parameters import Parameters as pm
from tensorflow.contrib.crf import crf_log_likelihood
from tensorflow.contrib.crf import viterbi_decode
from Data_process import batch_iter process_seq
class LSTM_CRF(object):
def __init__(self):
self.input_x = tf.placeholder(tf.int32 shape=[None None] name=‘input_x‘)
self.input_y = tf.placeholder(tf.int32 shape=[None None] name=‘input_y‘)
self.seq_length = tf.placeholder(tf.int32 shape=[None] name=‘sequence_length‘)
self.keep_pro = tf.placeholder(tf.float32 name=‘drop_out‘)
self.global_step = tf.Variable(0 trainable=False name=‘global_step‘)
self.Model()
def Model(self):
with tf.device(‘/cpu:0‘) tf.name_scope(‘embedding‘):
#从截断的正态分布中输出随机值。 pm.vocab_size pm.embedding_size表示生成张量的维度, -0.25是均值,0.25是标准差
embedding_ = tf.Variable(tf.truncated_normal([pm.vocab_size pm.embedding_size] -0.25 0.25) name=‘w‘)
print(pm.vocab_size)
# 查找embedding_中的序号为input_x的元素
embedding = tf.nn.embedding_lookup(embedding_ self.input_x)
self.embedding = tf.nn.dropout(embedding pm.keep_pro)
with tf.name_scope(‘biLSTM‘):
cell_fw = tf.nn.rnn_cell.LSTMCell(pm.hidden_dim)
cell_bw = tf.nn.rnn_cell.LSTMCell(pm.hidden_dim)
outputs outstates = tf.nn.bidirectional_dynamic_rnn(cell_fw=cell_fw cell_bw=cell_bwinputs=self.embedding
sequence_length=self.seq_length dtype=tf.float32)
outputs = tf.concat(outputs 2)#将双向RNN的结果进行拼接
#outputs三维张量,[batchsizeseq_length2*hidden_dim]
with tf.name_scope(‘output‘):
s = tf.shape(outputs)
output = tf.reshape(outputs [-1 2*pm.hidden_dim])
#将输出转变成one hot编码
output = tf.layers.dense(output pm.num_tags)
output = tf.contrib.layers.dropout(output pm.keep_pro)
self.logits = tf.reshape(output [-1 s[1] pm.num_tags])
with tf.name_scope(‘crf‘):
log_likelihood self.transition_params = crf_log_likelihood(inputs=self.logits tag_indices=self.input_y sequence_lengths=self.seq_length)
# log_likelihood是对数似然函数,transition_params是转移概率矩阵
#crf_log_likelihood{inputs:[batch_size(64)max_seq_lengthnum_tags]
#tag_indices:[batch_size(64)max_seq_length]
#sequence_lengths:[real_seq_length]
#transition_params: A [num_tags(7) num_tags(7)] transition matrix
#log_likelihood: A scalar containing the log-likelihood of the given sequence of tag indices.
with tf.name_scope(‘loss‘):
self.loss = tf.reduce_mean(-log_likelihood) #最大似然取负,使用梯度下降
with tf.name_scope(‘optimizer‘):
optimizer = tf.train.AdamOptimizer(pm.learn
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1203 2019-01-31 17:08 Bi-LSTM_CRF_NER\.gitignore
文件 5171 2019-05-28 16:48 Bi-LSTM_CRF_NER\biLstm_Crf.py
文件 8636940 2019-05-17 17:05 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-3168.data-00000-of-00001
文件 1015 2019-05-17 17:05 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-3168.index
文件 688284 2019-05-17 17:05 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-3168.me
文件 8636940 2019-05-17 17:16 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-6336.data-00000-of-00001
文件 1015 2019-05-17 17:16 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-6336.index
文件 688284 2019-05-17 17:16 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-6336.me
文件 8636940 2019-05-17 17:28 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-9504.data-00000-of-00001
文件 1015 2019-05-17 17:28 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-9504.index
文件 688284 2019-05-17 17:28 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\best_validation-9504.me
文件 199 2019-05-17 17:28 Bi-LSTM_CRF_NER\checkpoints\biLSTM_Crf\checkpoint
文件 36 2019-05-28 17:11 Bi-LSTM_CRF_NER\data\eva_data
文件 3 2019-05-17 18:00 Bi-LSTM_CRF_NER\data\input_data
文件 1291634 2019-01-31 17:08 Bi-LSTM_CRF_NER\data\test_data
文件 16129425 2019-01-31 17:08 Bi-LSTM_CRF_NER\data\train_data
文件 76143 2019-05-17 16:52 Bi-LSTM_CRF_NER\data\word2id.pkl
文件 4416 2019-05-28 17:43 Bi-LSTM_CRF_NER\Data_process.py
文件 404 2019-05-17 09:19 Bi-LSTM_CRF_NER\Parameters.py
文件 4165 2019-05-28 17:42 Bi-LSTM_CRF_NER\serve.py
文件 586513 2019-01-31 17:08 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1548919100.JTYSL-8304YVB
文件 587478 2019-05-13 16:42 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557736977.BY
文件 587478 2019-05-14 16:54 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557824045.BY
文件 587478 2019-05-15 10:12 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557886360.BY
文件 587478 2019-05-15 10:12 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557886379.BY
文件 587478 2019-05-15 10:43 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557888198.BY
文件 587478 2019-05-15 10:43 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557888221.BY
文件 587478 2019-05-15 10:44 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557888294.BY
文件 587478 2019-05-15 10:55 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557888907.BY
文件 587478 2019-05-15 10:58 Bi-LSTM_CRF_NER\tensorboard\biLSTM_Crf\events.out.tfevents.1557889089.BY
............此处省略25个文件信息
- 上一篇:效能评估理论、方法及应用
- 下一篇:视差图转换物方点云DSM
相关资源
- CRF,LSTM,最大后向匹配法实现中文分
- 水晶报表CRforVS_13_0_5.exe
- CRforVS_redist_install_64bit_13_0_1.zip
- CRforVS_13_0_22.exe ;Crystal;
- BiLSTM-CRF解决序列标注问题
- CRF++0.58 windows 二进制版
- CRF++ 0.58 windows版
- 条件随机场史上最牛条件随机场教程
- CRF++-0.58.tar.gz
- LSTM+CRF模型项包含完整代码
- CRF++工具包使用介绍.ppt
- 条件随机场代码图像分割
- CRF 分词算法
- CoNLL 2000
- CRF++windows版本
- windows版CRF++-0.58.zip;linux版CRF++-0.54.
- 基于crf的中文命名实体识别完整代码
- 条件随机场CRF图像处理工具
- 爱立信PCRF配置说明-全
- LSTM+CRF模型项目完整代码
评论
共有 条评论