Skip to content

Commit df810dd

Browse files
csferngtensorflow-copybara
authored andcommitted
Update package path from collections to collections.abc for Python 3.10 support.
Fixes #120 PiperOrigin-RevId: 456644510
1 parent ff15f75 commit df810dd

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

neural_structured_learning/keras/adversarial_regularization.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def resolve_metric(self, metric):
235235
def _prepare_loss_fns(loss, output_names):
236236
"""Converts `loss` to a list of per-output loss functions or objects."""
237237
# losses for multiple outputs indexed by name
238-
if isinstance(loss, collections.Mapping):
238+
if isinstance(loss, collections.abc.Mapping):
239239
for name in output_names:
240240
if name not in loss:
241241
raise ValueError(
@@ -247,7 +247,7 @@ def _prepare_loss_fns(loss, output_names):
247247
return [tf.keras.losses.get(loss) for _ in output_names]
248248

249249
# losses for multiple outputs indexed by position
250-
if isinstance(loss, collections.Sequence):
250+
if isinstance(loss, collections.abc.Sequence):
251251
if len(loss) != len(output_names):
252252
raise ValueError('`loss` should have the same number of elements as '
253253
'model output')
@@ -262,13 +262,13 @@ def _prepare_loss_weights(loss_weights, output_names):
262262
if loss_weights is None:
263263
return [1.0] * len(output_names)
264264

265-
if isinstance(loss_weights, collections.Sequence):
265+
if isinstance(loss_weights, collections.abc.Sequence):
266266
if len(loss_weights) != len(output_names):
267267
raise ValueError('`loss_weights` should have the same number of elements '
268268
'as model output')
269269
return list(map(float, loss_weights))
270270

271-
if isinstance(loss_weights, collections.Mapping):
271+
if isinstance(loss_weights, collections.abc.Mapping):
272272
for name in output_names:
273273
if name not in loss_weights:
274274
raise ValueError('Loss weight for {} not found in `loss_weights` '
@@ -326,13 +326,13 @@ def _prepare_metric_fns(metrics, output_names, loss_wrappers):
326326
if metrics is None:
327327
return [[] for _ in output_names]
328328

329-
if not isinstance(metrics, (list, collections.Mapping)):
329+
if not isinstance(metrics, (list, collections.abc.Mapping)):
330330
raise TypeError('`metrics` must be a list or a dict, got {}'.format(
331331
str(metrics)))
332332

333333
to_list = lambda x: x if isinstance(x, list) else [x]
334334

335-
if isinstance(metrics, collections.Mapping):
335+
if isinstance(metrics, collections.abc.Mapping):
336336
# Converts `metrics` from a dictionary to a list of lists using the order
337337
# specified in `output_names`.
338338
metrics = [to_list(metrics.get(name, [])) for name in output_names]

neural_structured_learning/lib/adversarial_neighbor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ def _compute_gradient(self, loss, dense_features, gradient_tape=None):
149149
# features and feature masks) with possibly missing values (e.g. no mask for
150150
# some features).
151151
def _compose_as_dict(self, inputs):
152-
if isinstance(inputs, collections.Mapping):
152+
if isinstance(inputs, collections.abc.Mapping):
153153
return inputs
154154
elif isinstance(inputs, (tuple, list)):
155155
return dict(enumerate(inputs)) # index -> value
156156
else:
157157
return {'': inputs} if inputs is not None else {}
158158

159159
def _decompose_as(self, structure, values):
160-
if isinstance(structure, collections.Mapping):
160+
if isinstance(structure, collections.abc.Mapping):
161161
return values
162162
elif isinstance(structure, (tuple, list)):
163163
return [values[index] for index in range(len(structure))]

0 commit comments

Comments
 (0)