资源简介

本代码是在参考了别人代码的基础上进一步修改的,该代码的功能是用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.meta

     文件    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.meta

     文件    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.meta

     文件        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个文件信息

评论

共有 条评论