Skip to content

Commit 04f92e1

Browse files
committed
Merge branch 'master' into keras.datasets-defaults-to
# Conflicts: # keras/datasets/imdb.py # keras/datasets/reuters.py
2 parents 6d271fd + 602cdf1 commit 04f92e1

File tree

113 files changed

+1499
-710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1499
-710
lines changed

keras/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ tf_py_test(
291291
python_version = "PY3",
292292
shard_count = 6,
293293
tags = [
294+
"no_pip", # TODO(b/276923757)
294295
"no_tfrt", # TODO(b/179690526)
295296
"notsan",
296297
],

keras/activations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ def serialize(activation, use_legacy_format=False):
573573
f"Unknown activation function '{activation}' cannot be "
574574
"serialized due to invalid function name. Make sure to use "
575575
"an activation name that matches the references defined in "
576-
"activations.py or use `@keras.utils.register_keras_serializable` "
577-
"for any custom activations. "
576+
"activations.py or use `@keras.utils.register_keras_serializable()`"
577+
"to register any custom activations. "
578578
f"config={fn_config}"
579579
)
580580
if not isinstance(activation, types.FunctionType):
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
path: "tensorflow.keras.utils.TimedThread"
2+
tf_class {
3+
is_instance: "<class \'keras.utils.timed_threads.TimedThread\'>"
4+
is_instance: "<type \'object\'>"
5+
member_method {
6+
name: "__init__"
7+
argspec: "args=[\'self\', \'interval\'], varargs=None, keywords=kwargs, defaults=None"
8+
}
9+
member_method {
10+
name: "is_alive"
11+
argspec: "args=[\'self\'], varargs=None, keywords=None, defaults=None"
12+
}
13+
member_method {
14+
name: "on_interval"
15+
argspec: "args=[\'self\'], varargs=None, keywords=None, defaults=None"
16+
}
17+
member_method {
18+
name: "start"
19+
argspec: "args=[\'self\'], varargs=None, keywords=None, defaults=None"
20+
}
21+
member_method {
22+
name: "stop"
23+
argspec: "args=[\'self\'], varargs=None, keywords=None, defaults=None"
24+
}
25+
}

keras/api/golden/v2/tensorflow.keras.utils.pbtxt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ tf_module {
3232
name: "SidecarEvaluator"
3333
mtype: "<type \'type\'>"
3434
}
35+
member {
36+
name: "TimedThread"
37+
mtype: "<type \'type\'>"
38+
}
3539
member {
3640
name: "custom_object_scope"
3741
mtype: "<type \'type\'>"

keras/api/tests/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ tf_py_test(
3333
"//:expect_six_installed",
3434
"//third_party/py/tensorflow",
3535
"//third_party/tensorflow/python:lib",
36-
"//third_party/tensorflow/python:platform",
3736
"//third_party/tensorflow/tools/api/lib:python_object_to_proto_visitor",
3837
"//third_party/tensorflow/tools/common:public_api",
3938
"//third_party/tensorflow/tools/common:traverse",

keras/api/tests/api_compatibility_test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
from google.protobuf import message
4040
from google.protobuf import text_format
4141
from tensorflow.python.lib.io import file_io
42-
from tensorflow.python.platform import tf_logging as logging
4342
from tensorflow.tools.api.lib import api_objects_pb2
4443
from tensorflow.tools.api.lib import (
4544
python_object_to_proto_visitor,
@@ -262,14 +261,14 @@ def _AssertProtoDictEquals(
262261
# If diffs are found, handle them based on flags.
263262
if diffs:
264263
diff_count = len(diffs)
265-
logging.error(self._test_readme_message)
266-
logging.error(
264+
tf.compat.v1.logging.error(self._test_readme_message)
265+
tf.compat.v1.logging.error(
267266
"%d differences found between API and golden.", diff_count
268267
)
269268

270269
if update_goldens:
271270
# Write files if requested.
272-
logging.warning(self._update_golden_warning)
271+
tf.compat.v1.logging.warning(self._update_golden_warning)
273272

274273
# If the keys are only in expected, some objects are deleted.
275274
# Remove files.
@@ -288,15 +287,17 @@ def _AssertProtoDictEquals(
288287
else:
289288
# Include the actual differences to help debugging.
290289
for d, verbose_d in zip(diffs, verbose_diffs):
291-
logging.error(" %s", d)
292-
logging.error(" %s", verbose_d)
290+
tf.compat.v1.logging.error(" %s", d)
291+
tf.compat.v1.logging.error(" %s", verbose_d)
293292
# Fail if we cannot fix the test by updating goldens.
294293
self.fail(
295294
"%d differences found between API and golden." % diff_count
296295
)
297296

298297
else:
299-
logging.info("No differences found between API and golden.")
298+
tf.compat.v1.logging.info(
299+
"No differences found between API and golden."
300+
)
300301

301302
def _checkBackwardsCompatibility(
302303
self,

keras/applications/imagenet_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
The preprocessed data are written over the input data
5757
if the data types are compatible. To avoid this
5858
behaviour, `numpy.copy(x)` can be used.
59-
data_format: Optional data format of the image tensor/array. Defaults to
60-
None, in which case the global setting
61-
`tf.keras.backend.image_data_format()` is used (unless you changed it,
62-
it defaults to "channels_last").{mode}
59+
data_format: Optional data format of the image tensor/array. None, means
60+
the global setting `tf.keras.backend.image_data_format()` is used
61+
(unless you changed it, it uses "channels_last").{mode}
62+
Defaults to `None`.
6363
6464
Returns:
6565
Preprocessed `numpy.array` or a `tf.Tensor` with type `float32`.
@@ -70,7 +70,7 @@
7070
"""
7171

7272
PREPROCESS_INPUT_MODE_DOC = """
73-
mode: One of "caffe", "tf" or "torch". Defaults to "caffe".
73+
mode: One of "caffe", "tf" or "torch".
7474
- caffe: will convert the images from RGB to BGR,
7575
then will zero-center each color channel with
7676
respect to the ImageNet dataset,
@@ -80,6 +80,7 @@
8080
- torch: will scale pixels between 0 and 1 and then
8181
will normalize each channel with respect to the
8282
ImageNet dataset.
83+
Defaults to "caffe".
8384
"""
8485

8586
PREPROCESS_INPUT_DEFAULT_ERROR_DOC = """

keras/applications/inception_v3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ def InceptionV3(
8282
8383
Args:
8484
include_top: Boolean, whether to include the fully-connected
85-
layer at the top, as the last layer of the network. Default to `True`.
85+
layer at the top, as the last layer of the network. Defaults to `True`.
8686
weights: One of `None` (random initialization),
8787
`imagenet` (pre-training on ImageNet),
88-
or the path to the weights file to be loaded. Default to `imagenet`.
88+
or the path to the weights file to be loaded. Defaults to `imagenet`.
8989
input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`)
9090
to use as image input for the model. `input_tensor` is useful for
91-
sharing inputs between multiple different networks. Default to None.
91+
sharing inputs between multiple different networks. Defaults to `None`.
9292
input_shape: Optional shape tuple, only to be specified
9393
if `include_top` is False (otherwise the input shape
9494
has to be `(299, 299, 3)` (with `channels_last` data format)
@@ -108,7 +108,7 @@ def InceptionV3(
108108
- `max` means that global max pooling will be applied.
109109
classes: optional number of classes to classify images
110110
into, only to be specified if `include_top` is True, and
111-
if no `weights` argument is specified. Default to 1000.
111+
if no `weights` argument is specified. Defaults to 1000.
112112
classifier_activation: A `str` or callable. The activation function to use
113113
on the "top" layer. Ignored unless `include_top=True`. Set
114114
`classifier_activation=None` to return the logits of the "top" layer.

keras/applications/mobilenet.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,26 @@ def MobileNet(
124124
`channels_last` data format) or (3, 224, 224) (with `channels_first`
125125
data format). It should have exactly 3 inputs channels, and width and
126126
height should be no smaller than 32. E.g. `(200, 200, 3)` would be one
127-
valid value. Default to `None`.
127+
valid value. Defaults to `None`.
128128
`input_shape` will be ignored if the `input_tensor` is provided.
129129
alpha: Controls the width of the network. This is known as the width
130130
multiplier in the MobileNet paper. - If `alpha` < 1.0, proportionally
131131
decreases the number of filters in each layer. - If `alpha` > 1.0,
132132
proportionally increases the number of filters in each layer. - If
133133
`alpha` = 1, default number of filters from the paper are used at each
134-
layer. Default to 1.0.
134+
layer. Defaults to `1.0`.
135135
depth_multiplier: Depth multiplier for depthwise convolution. This is
136-
called the resolution multiplier in the MobileNet paper. Default to 1.0.
137-
dropout: Dropout rate. Default to 0.001.
136+
called the resolution multiplier in the MobileNet paper.
137+
Defaults to `1.0`.
138+
dropout: Dropout rate. Defaults to `0.001`.
138139
include_top: Boolean, whether to include the fully-connected layer at the
139-
top of the network. Default to `True`.
140+
top of the network. Defaults to `True`.
140141
weights: One of `None` (random initialization), 'imagenet' (pre-training
141-
on ImageNet), or the path to the weights file to be loaded. Default to
142+
on ImageNet), or the path to the weights file to be loaded. Defaults to
142143
`imagenet`.
143144
input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`) to
144145
use as image input for the model. `input_tensor` is useful for sharing
145-
inputs between multiple different networks. Default to None.
146+
inputs between multiple different networks. Defaults to `None`.
146147
pooling: Optional pooling mode for feature extraction when `include_top`
147148
is `False`.
148149
- `None` (default) means that the output of the model will be
@@ -154,7 +155,7 @@ def MobileNet(
154155
- `max` means that global max pooling will be applied.
155156
classes: Optional number of classes to classify images into, only to be
156157
specified if `include_top` is True, and if no `weights` argument is
157-
specified. Defaults to 1000.
158+
specified. Defaults to `1000`.
158159
classifier_activation: A `str` or callable. The activation function to use
159160
on the "top" layer. Ignored unless `include_top=True`. Set
160161
`classifier_activation=None` to return the logits of the "top" layer.

keras/applications/mobilenet_v3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,10 @@ def preprocess_input(x, data_format=None):
679679
680680
Args:
681681
x: A floating point `numpy.array` or a `tf.Tensor`.
682-
data_format: Optional data format of the image tensor/array. Defaults to
683-
None, in which case the global setting
684-
`tf.keras.backend.image_data_format()` is used (unless you changed it,
685-
it defaults to "channels_last").{mode}
682+
data_format: Optional data format of the image tensor/array. `None` means
683+
the global setting `tf.keras.backend.image_data_format()` is used
684+
(unless you changed it, it uses "channels_last").
685+
Defaults to `None`.
686686
687687
Returns:
688688
Unchanged `numpy.array` or `tf.Tensor`.

keras/backend.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,8 +1901,8 @@ class RandomGenerator(tf.__internal__.tracking.AutoTrackable):
19011901
When `rng_type` is "legacy_stateful", the seed will be passed down to
19021902
stateful random ops.
19031903
rng_type: Type of RNG to use, one of "stateful", "stateless",
1904-
"legacy_stateful". It defaults to "stateful" if
1905-
`enable_tf_random_generator` has been activated, or to
1904+
"legacy_stateful". When `None` it uses "stateful" if
1905+
`enable_tf_random_generator` has been activated, or
19061906
"legacy_stateful" otherwise.
19071907
- When using "stateless", the random ops outputs are constant (the same
19081908
inputs result in the same outputs).
@@ -1913,6 +1913,7 @@ class RandomGenerator(tf.__internal__.tracking.AutoTrackable):
19131913
- "legacy_stateful" is backed by TF1 stateful RNG ops
19141914
(e.g. `tf.random.uniform`), while "stateful"
19151915
is backed by TF2 APIs (e.g. `tf.random.Generator.uniform`).
1916+
Defaults to `None`.
19161917
"""
19171918

19181919
RNG_STATELESS = "stateless"
@@ -5566,8 +5567,12 @@ def categorical_crossentropy(target, output, from_logits=False, axis=-1):
55665567
labels=target, logits=output, axis=axis
55675568
)
55685569

5569-
# scale preds so that the class probas of each sample sum to 1
5570+
# Adjust the predictions so that the probability of
5571+
# each class for every sample adds up to 1
5572+
# This is needed to ensure that the cross entropy is
5573+
# computed correctly.
55705574
output = output / tf.reduce_sum(output, axis, True)
5575+
55715576
# Compute cross entropy from probabilities.
55725577
epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
55735578
output = tf.clip_by_value(output, epsilon_, 1.0 - epsilon_)
@@ -5844,28 +5849,29 @@ def binary_focal_crossentropy(
58445849
where `alpha` is a float in the range of `[0, 1]`.
58455850
58465851
Args:
5847-
target: A tensor with the same shape as `output`.
5848-
output: A tensor.
5849-
apply_class_balancing: A bool, whether to apply weight balancing on the
5850-
binary classes 0 and 1.
5851-
alpha: A weight balancing factor for class 1, default is `0.25` as
5852-
mentioned in the reference. The weight for class 0 is `1.0 - alpha`.
5853-
gamma: A focusing parameter, default is `2.0` as mentioned in the
5854-
reference.
5855-
from_logits: Whether `output` is expected to be a logits tensor. By
5856-
default, we consider that `output` encodes a probability distribution.
5852+
target: A tensor with the same shape as `output`.
5853+
output: A tensor.
5854+
apply_class_balancing: A bool, whether to apply weight balancing on the
5855+
binary classes 0 and 1.
5856+
alpha: A weight balancing factor for class 1, default is `0.25` as
5857+
mentioned in the reference. The weight for class 0 is `1.0 - alpha`.
5858+
gamma: A focusing parameter, default is `2.0` as mentioned in the
5859+
reference.
5860+
from_logits: Whether `output` is expected to be a logits tensor. By
5861+
default, we consider that `output` encodes a probability
5862+
distribution.
58575863
58585864
Returns:
5859-
A tensor.
5865+
A tensor.
58605866
"""
5861-
sigmoidal = tf.__internal__.smart_cond.smart_cond(
5862-
from_logits,
5863-
lambda: sigmoid(output),
5864-
lambda: output,
5865-
)
5867+
5868+
sigmoidal = sigmoid(output) if from_logits else output
5869+
58665870
p_t = target * sigmoidal + (1 - target) * (1 - sigmoidal)
5871+
58675872
# Calculate focal factor
58685873
focal_factor = tf.pow(1.0 - p_t, gamma)
5874+
58695875
# Binary crossentropy
58705876
bce = binary_crossentropy(
58715877
target=target,
@@ -5893,7 +5899,7 @@ def sigmoid(x):
58935899
Returns:
58945900
A tensor.
58955901
"""
5896-
return tf.sigmoid(x)
5902+
return tf.math.sigmoid(x)
58975903

58985904

58995905
@keras_export("keras.backend.hard_sigmoid")
@@ -6318,13 +6324,13 @@ def separable_conv1d(
63186324
pointwise_kernel = tf.expand_dims(pointwise_kernel, 0)
63196325
dilation_rate = (1,) + dilation_rate
63206326

6321-
x = tf.compat.v1.nn.separable_conv2d(
6327+
x = tf.nn.separable_conv2d(
63226328
x,
63236329
depthwise_kernel,
63246330
pointwise_kernel,
63256331
strides=strides,
63266332
padding=padding,
6327-
rate=dilation_rate,
6333+
dilations=dilation_rate,
63286334
data_format=tf_data_format,
63296335
)
63306336

@@ -6384,13 +6390,13 @@ def separable_conv2d(
63846390
else:
63856391
strides = (1, 1) + strides
63866392

6387-
x = tf.compat.v1.nn.separable_conv2d(
6393+
x = tf.nn.separable_conv2d(
63886394
x,
63896395
depthwise_kernel,
63906396
pointwise_kernel,
63916397
strides=strides,
63926398
padding=padding,
6393-
rate=dilation_rate,
6399+
dilations=dilation_rate,
63946400
data_format=tf_data_format,
63956401
)
63966402
if data_format == "channels_first" and tf_data_format == "NHWC":
@@ -6439,12 +6445,12 @@ def depthwise_conv2d(
64396445
else:
64406446
strides = (1, 1) + strides
64416447

6442-
x = tf.compat.v1.nn.depthwise_conv2d(
6448+
x = tf.nn.depthwise_conv2d(
64436449
x,
64446450
depthwise_kernel,
64456451
strides=strides,
64466452
padding=padding,
6447-
rate=dilation_rate,
6453+
dilations=dilation_rate,
64486454
data_format=tf_data_format,
64496455
)
64506456
if data_format == "channels_first" and tf_data_format == "NHWC":
@@ -6898,11 +6904,11 @@ def random_normal(shape, mean=0.0, stddev=1.0, dtype=None, seed=None):
68986904
Args:
68996905
shape: A tuple of integers, the shape of tensor to create.
69006906
mean: A float, the mean value of the normal distribution to draw
6901-
samples. Default to 0.0.
6907+
samples. Defaults to `0.0`.
69026908
stddev: A float, the standard deviation of the normal distribution
6903-
to draw samples. Default to 1.0.
6904-
dtype: `tf.dtypes.DType`, dtype of returned tensor. Default to use Keras
6905-
backend dtype which is float32.
6909+
to draw samples. Defaults to `1.0`.
6910+
dtype: `tf.dtypes.DType`, dtype of returned tensor. None uses Keras
6911+
backend dtype which is float32. Defaults to `None`.
69066912
seed: Integer, random seed. Will use a random numpy integer when not
69076913
specified.
69086914

keras/callbacks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,9 +1889,21 @@ def on_train_begin(self, logs=None):
18891889
self._training_state.restore()
18901890

18911891
def on_train_batch_begin(self, batch, logs=None):
1892+
# Skip batch update for PSS Strategy
1893+
if isinstance(
1894+
self.model.distribute_strategy,
1895+
tf.distribute.ParameterServerStrategy,
1896+
):
1897+
return
18921898
self._training_state._ckpt_saved_batch.assign(batch)
18931899

18941900
def on_train_batch_end(self, batch, logs=None):
1901+
# Skip batch update for PSS Strategy
1902+
if isinstance(
1903+
self.model.distribute_strategy,
1904+
tf.distribute.ParameterServerStrategy,
1905+
):
1906+
return
18951907
self._training_state.backup_if_preempted()
18961908
if self.save_freq and self.save_freq != "epoch":
18971909
self._batches_count += 1

0 commit comments

Comments
 (0)