Commit Graph

12 Commits

Author SHA1 Message Date
Scott Zhu
cf599cade1 Move the serialization_test to keras/tests
PiperOrigin-RevId: 316697481
Change-Id: I918b29a976b166662acf9045f87512aef485441b
2020-06-16 09:35:18 -07:00
Francois Chollet
1227a9446d Unifies the behaviors of "regular Sequential" (starts with an Input, wraps a Functional model) and "deferred Sequential" models (only gets built when it sees its input data for the first time).
Before, the following two models had the following behavior differences:

```
regular_sequential = Sequential([
  Dense(2, activation='relu', input_shape=(3,)),
  Dense(4),
]
deferred_sequential = Sequential([
  Dense(2, activation='relu'),
  Dense(4),
])
deferred_sequential(tf.zeros((1, 3)))  # Builds the deferred sequential
```

1) The regular sequential is inspectable: its `.summary()` displays intermediate output shapes. The deferred sequential does not display output shapes.
2) The regular sequential has `inputs` and `outputs` attributes, and its intermediate layers can be used to do feature extraction (see example below), etc. The deferred sequential can't do this.

Feature extraction example:

```
model = Sequential(...)
extractor = keras.Model(inputs=model.inputs,
                        outputs=[layer.output for layer in model.layers])
features = extractor(data)
```

After this CL, the two models behave exactly the same once the deferred sequential has been built (whether by `__call__`ing it, by calling `fit`/`evaluate`/`predict`, or by calling `build` directly). The input shape used is the most restrictive shape compatible with all shapes previously seen by the model (i.e. the set of invariants among all shapes).

The behavior unification is not applied for TF V1, since we don't want to disrupt legacy behaviors and don't want to add new features in V1.

Note that the deferred Sequential remain different in the following cases:
- When a deferred Sequential is called with inputs of different ranks. This is impossible to express in the Functional API (of which "regular Sequential" is a wrapper). However, this is almost certainly not something that anyone is doing.
- When a deferred Sequential starts with a layer that takes multiple inputs. At this time this is something that regular Sequential models do not support. This is an invalid use case of Sequential (which should be single-input and single-output), which unfortunately some users have come to rely on. We may choose to enable it in the future (since it can be expressed with the Functional API). When we enable it we could unify the deferred Sequential behavior in this case.
- When a deferred Sequential contains a non-autographable layer that isn't marked as dynamic, or that is marked as dynamic but does not support shape inference. In that case no Functional model can be built.

PiperOrigin-RevId: 306151882
Change-Id: I4aa881af254ee845f771e375933deae664c80354
2020-04-12 16:04:20 -07:00
Scott Zhu
39b77be0ed Added reverse dependency back to Keras.
Also the original code also passes since the saving code has been updated recently.

PiperOrigin-RevId: 303782079
Change-Id: I0ec9ca84d6584aa30bb5fc3c51906afc7de4320d
2020-03-30 11:31:51 -07:00
TensorFlower Gardener
f955c9e7b2 Merge pull request from howl-anderson:bugfix/keras_custom_train_config_serialize
PiperOrigin-RevId: 298371731
Change-Id: Icf37b89f25da6cfb60a0533f6d66c2228b699549
2020-03-02 09:35:56 -08:00
Xiaoquan Kong
f56acee10b style: fix pylint error 2020-02-28 10:09:40 +08:00
Xiaoquan Kong
e4ea3977e3 fix: code style 2020-02-24 21:00:07 +08:00
Xiaoquan Kong
6da8ce0d1f fix: TF API changes 2020-02-24 20:54:07 +08:00
Xiaoquan Kong
ed85a1efe3 fix(keras): unable serialize custom train config 2020-02-24 14:37:22 +08:00
Francois Chollet
42b61fbd38 Refactor Sequential model to make it behave exactly like a subclassed network unless it receives an input_shape argument with its first layer. In particular in graph mode no placeholder gets created by a Sequential model (unless an input shape is provided).
PiperOrigin-RevId: 207921752
2018-08-08 11:53:01 -07:00
Tom Hennigan
945d1a77ae Replace unnecessary () in run_in_graph_and_eager_modes().
PiperOrigin-RevId: 201652888
2018-06-22 01:49:29 -07:00
Pavithra Vijay
609b2ce3fe Move Keras code out of _impl folder and remove API files.
PiperOrigin-RevId: 197097430
2018-05-17 21:40:10 -07:00
Allen Lavoie
ef58a46b73 Support saving Python state with object-based checkpoints
Allows SaveableObjects to specify feed dict addition callbacks for object-based saving.

For now just saves get_config() with Layers. Doesn't do any loading, and there isn't quite enough information to reconstruct a Model yet (needs topology).

My plan is to get Models to the point where they can be reconstructed from object-based checkpoints (probably one more change), add in SavedModel export (assuming no dynamic control flow for now), then add this "SavedModel+Python" format to Model.save / load_model.

PiperOrigin-RevId: 196043183
2018-05-09 15:59:21 -07:00