1 Star 1 Fork 0

jecosine/BiLSTM-CRF-tensorflow

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Bilstm.py 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
silver 提交于 2019-11-26 17:13 . UPDATE:Update
import tensorflow as tf
import jieba
import numpy as np
from tensorflow.contrib.crf import crf_log_likehood
from tensorflow.contrib.crf import viterbi_decode
from tensorflow.contrib.rnn import LSTMCell
import Parameters as pm
class BiLSTM_CRF():
def __init__(self, ):
"""Init paramaters"""
def build_model(self):
# placeholder
# setting up chars ids and words ids
self.word_ids = tf.placeholder(tf.int32, shape = [None, None], name = "word_ids")
self.sequence_lengths = tf.placeholder(tf.int32, shape = [None], name = "sequence_lengths")
self.labels = tf.placeholder(tf.int32, shape = [None, None], name = "labels")
self.dropout = tf.placeholder(tf.int32, shape = [None], name = "dropout")
# build look up layer
with tf.variable_scope("lookup"):
embedding = tf.Variable(self.embeddings, dtype = tf.float32, traina)
word_embeddings = tf.nn.embedding_lookup(params = embeddings,
ids=self.word_ids,
name="word_embeddings")
self.word_embeddings = tf.nn.dropout(word_embeddings, self.dropout)
# build bilstm
with tf.variable_scope("bilstm"):
cell_fw = LSTMCell(self.hidden_dim)
cell_bw = LSTMCell(self.hidden_dim)
(output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn(
cell_bw = cell_bw,
cell_fw = cell_fw,
inputs = self.word_embeddings,
sequence_length = self.sequence_lengths,
dtype = tf.float32
)
output = tf.concat([output_fw, output_bw, axis = -1])
output = tf.nn.dropout(output, self.dropout)
# build project layer
with tf.variable_scope("project"):
W = tf.get_variable(
name = "W",
shape = [2 * self.hidden_dim, self.num_tags],
initializer = tf.contrib.layers.xavier_initializer()
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jecosine/BiLSTM-CRF-tensorflow.git
[email protected]:jecosine/BiLSTM-CRF-tensorflow.git
jecosine
BiLSTM-CRF-tensorflow
BiLSTM-CRF-tensorflow
master

搜索帮助