SEQUENCE MODEL EX-13
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Define Sequential model with 3 layers
model = keras.Sequential([
layers.Dense(2, activation="relu",
name="layer1"),
layers.Dense(3, activation="relu",
name="layer2"),
layers.Dense(4, name="layer3"),
])
# Call model on a test input
x = tf.ones((3, 3))
y = model(x)
# Create 3 layers
layer1 = layers.Dense(2, activation="relu",
name="layer1")
layer2 = layers.Dense(3, activation="relu",
name="layer2")
layer3 = layers.Dense(4, name="layer3")
# Call layers on a test input
x = tf.ones((3, 3))
y = layer3(layer2(layer1(x)))
model = keras.Sequential(
[
layers.Dense(2, activation="relu"),
layers.Dense(3, activation="relu"),
layers.Dense(4),
]
)
model.layers
model = keras.Sequential()
model.add(layers.Dense(2, activation="relu"))
model.add(layers.Dense(3, activation="relu"))
model.add(layers.Dense(4))
model.pop()
print(len(model.layers)) # 2
model = keras.Sequential(name="my_sequential")
model.add(layers.Dense(2, activation="relu",
name="layer1"))
model.add(layers.Dense(3, activation="relu",
name="layer2"))
model.add(layers.Dense(4, name="layer3"))
layer = layers.Dense(3)
layer.weights # Empty
# Call layer on a test input
x = tf.ones((1, 4))
y = layer(x)
layer.weights # Now it has weights, of shape (4, 3) and (3,)
model = keras.Sequential(
[
layers.Dense(2, activation="relu"),
layers.Dense(3, activation="relu"),
layers.Dense(4),
]
) # No weights at this stage!
# At this point, you can't do this:
# model.weights
# You also can't do this:
# model.summary()
# Call the model on a test input
x = tf.ones((1, 4))
y = model(x)
print("Number of weights after calling the model:",
len(model.weights)) # 6
model.summary()
model = keras.Sequential()
model.add(keras.Input(shape=(4,)))
model.add(layers.Dense(2, activation="relu"))
model.summary()
Comments
Post a Comment