資源簡介
本代碼是在參考了別人代碼的基礎上進一步修改的,該代碼的功能是用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
評論
共有 條評論