"For multi-output layers, "
"use the functional API.")
self.outputs = [layer._inbound_nodes[-1].output_tensors[0]]
self.inputs = network.get_source_inputs(self.outputs[0])
// We create an input node, which we will keep updated
// as we add more layers
base_layer.Node(outbound_layer=self,
inbound_layers=[],
node_indices=[],
tensor_indices=[],
input_tensors=self.inputs,
output_tensors=self.outputs,
// no model-level masking for now
input_masks=[None for _ in self.inputs],
output_masks=[None],
input_shapes=[x._keras_shape for x in self.inputs],
output_shapes=[self.outputs[0]._keras_shape])
else:
output_tensor = layer(self.outputs[0])
if isinstance(output_tensor, list):
raise TypeError("All layers in a Sequential model "
"should have a single output tensor. "
"For multi-output layers, "
"use the functional API.")
self.outputs = [output_tensor]
// update self._inbound_nodes
self._inbound_nodes[0].output_tensors = self.outputs
self._inbound_nodes[0].output_shapes = [
self.outputs[0]._keras_shape]
self.layers.append(layer)
self.built = False
After Change
"just use your `Sequential` instance directly.")
return self
def add(self, layer):
Adds a layer instance on top of the layer stack.
// Arguments
layer: layer instance.
// Raises
TypeError: If `layer` is not a layer instance.
ValueError: In case the `layer` argument does not
know its input shape.
ValueError: In case the `layer` argument has
multiple output tensors, or is already connected
somewhere else (forbidden in `Sequential` models).
if not isinstance(layer, Layer):
raise TypeError("The added layer must be "
"an instance of class Layer. "
"Found: " + str(layer))
self.built = False
if not self._layers:
set_inputs = False
// First layer in model: check that it is an input layer.
if not isinstance(layer, InputLayer):
// Create an input tensor and call `layer` on the input tensor.
// First, we need to infer the expected input shape and dtype.
first_layer = layer
if isinstance(layer, (Model, Sequential)):
// We were passed a model as first layer.
// This requires a specific way to figure out the
// input shape and dtype.
if not layer.layers:
raise ValueError("Cannot add an empty model "
"to a `Sequential` model.")
// In case of nested models: recover the first layer
// of the deepest model to infer input shape and dtype.
first_layer = layer.layers[0]
while isinstance(first_layer, (Model, Sequential)):
first_layer = first_layer.layers[0]
batch_shape = first_layer.batch_input_shape
dtype = first_layer.dtype
if hasattr(first_layer, "batch_input_shape"):
batch_shape = first_layer.batch_input_shape
dtype = first_layer.dtype
// Instantiate the input layer.
x = Input(
batch_shape=batch_shape,
dtype=dtype,
name=layer.name + "_input")
// This will build the current layer
// and create the node connecting the current layer
// to the input layer we just created.
layer(x)
set_inputs = True
else:
// The layer doesn"t know about its expected shape.
// We will have to
// build the model lazily on `fit`/etc.
batch_shape = None
else:
// Corner case where the user passes an InputLayer via `add`.
assert len(layer._inbound_nodes[-1].output_tensors) == 1
set_inputs = True
if set_inputs:
if len(layer._inbound_nodes[-1].output_tensors) != 1:
raise ValueError("All layers in a Sequential model "
"should have a single output tensor. "
"For multi-output layers, "
"use the functional API.")
self.outputs = [layer._inbound_nodes[-1].output_tensors[0]]
self.inputs = network.get_source_inputs(self.outputs[0])
elif self.outputs:
output_tensor = layer(self.outputs[0])
if isinstance(output_tensor, list):
raise TypeError("All layers in a Sequential model "
"should have a single output tensor. "
"For multi-output layers, "
"use the functional API.")
self.outputs = [output_tensor]
if self.inputs:
self.build()
else:
self._layers.append(layer)
def pop(self):
Removes the last layer in the model.
// Raises