Skip to content

Commit e64efb8

Browse files
committed
Support new versions of tensorflow and numpy
1 parent 32ed29b commit e64efb8

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

hanlp/components/parsers/biaffine_parser_tf.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ def build_optimizer(self, optimizer='adam', lr=2e-3, mu=.9, nu=.9, epsilon=1e-12
200200
scheduler = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=lr,
201201
decay_steps=decay_steps,
202202
decay_rate=decay)
203-
optimizer = tf.keras.optimizers.Adam(learning_rate=scheduler,
204-
beta_1=mu,
205-
beta_2=nu,
206-
epsilon=epsilon,
207-
clipnorm=clip)
203+
from hanlp.optimizers.adamw.optimization import AdamTF
204+
optimizer = AdamTF(learning_rate=scheduler,
205+
beta_1=mu,
206+
beta_2=nu,
207+
epsilon=epsilon,
208+
clipnorm=clip)
208209
return optimizer
209210
return super().build_optimizer(optimizer, **kwargs)
210211

hanlp/optimizers/adamw/optimization.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ def create_optimizer(init_lr, num_train_steps, num_warmup_steps):
9595
return optimizer
9696

9797

98-
class AdamWeightDecay(tf.keras.optimizers.Adam):
98+
try:
99+
AdamTF = tf.keras.optimizers.legacy.Adam # avoid slowdown when using v2.11+ Keras optimizers on M1/M2 Macs
100+
except:
101+
AdamTF = tf.keras.optimizers.Adam
102+
103+
104+
class AdamWeightDecay(AdamTF):
99105
"""Adam enables L2 weight decay and clip_by_global_norm on gradients.
100106
101107
Just adding the square of the weights to the loss function is *not* the

hanlp/transform/conll_tf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def batched_inputs_to_batches(self, corpus, indices, shuffle):
736736
raw_batch = [[], [], [], []] if use_pos else [[], [], []]
737737
max_len = len(max([corpus[i] for i in indices], key=len))
738738
for idx in indices:
739-
arc = np.zeros((max_len, max_len), dtype=np.bool)
739+
arc = np.zeros((max_len, max_len), dtype=bool)
740740
rel = np.zeros((max_len, max_len), dtype=np.int64)
741741
for b in raw_batch[:2]:
742742
b.append([])

hanlp/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Author: hankcs
33
# Date: 2019-12-28 19:26
44

5-
__version__ = '2.1.0-beta.59'
5+
__version__ = '2.1.0-beta.60'
66
"""HanLP version"""
77

88

0 commit comments

Comments
 (0)