-
Notifications
You must be signed in to change notification settings - Fork 387
/
modeling.py
116 lines (97 loc) · 3.75 KB
/
modeling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding:utf-8 -*-
"""
Author: BigCat
"""
import tensorflow as tf
from tensorflow_addons.text.crf import crf_decode, crf_log_likelihood
# 关闭eager模式
tf.compat.v1.disable_eager_execution()
tf.compat.v1.experimental.output_all_intermediates(True)
class LstmWithCRFModel(object):
""" lstm + crf解码模型
"""
def __init__(self, batch_size, n_class, ball_num, w_size, embedding_size, words_size, hidden_size, layer_size):
self._inputs = tf.keras.layers.Input(
shape=(w_size, ball_num), batch_size=batch_size, name="inputs"
)
self._tag_indices = tf.keras.layers.Input(
shape=(ball_num, ), batch_size=batch_size, dtype=tf.int32, name="tag_indices"
)
self._sequence_length = tf.keras.layers.Input(
shape=(), batch_size=batch_size, dtype=tf.int32, name="sequence_length"
)
# 构建特征抽取
embedding = tf.keras.layers.Embedding(words_size, embedding_size)(self._inputs)
first_lstm = tf.convert_to_tensor(
[tf.keras.layers.LSTM(hidden_size)(embedding[:, :, i, :]) for i in range(ball_num)]
)
first_lstm = tf.transpose(first_lstm, perm=[1, 0, 2])
second_lstm = None
for _ in range(layer_size):
second_lstm = tf.keras.layers.LSTM(hidden_size, return_sequences=True)(first_lstm)
self._outputs = tf.keras.layers.Dense(n_class)(second_lstm)
# 构建损失函数
self._log_likelihood, self._transition_params = crf_log_likelihood(
self._outputs, self._tag_indices, self._sequence_length
)
self._loss = tf.reduce_sum(-self._log_likelihood)
# 构建预测
self._pred_sequence, self._viterbi_score = crf_decode(
self._outputs, self._transition_params, self._sequence_length
)
@property
def inputs(self):
return self._inputs
@property
def tag_indices(self):
return self._tag_indices
@property
def sequence_length(self):