Model Creation
From the previous chapter, we've seen how to create individual layers. It's important to understand that these layers become effective only when they are integrated into a cohesive model.
Keras offers two main ways to build models: the sequential API, which is simple and ideal for linear stacks of layers, and the functional API, which is more flexible and suited for complex architectures, such as models with multiple inputs, outputs, or shared layers.
With the sequential API, the code is quite straightforward:
from keras.models import Sequential
from keras.layers import Input, Dense
model = Sequential([
Input(shape=(32,)),
Dense(64, activation='relu'),
Dense(1, activation='sigmoid')
])
With the functional API, the code is a bit more explicit and flexible:
from keras.models import Model
from keras.layers import Input, Dense
inputs = Input(shape=(32,))
x = Dense(64, activation='relu')(inputs)
outputs = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=outputs)
Sequential Model
In this course, we will mostly use the Sequential API, which involves using the Sequential
class. It's essentially a linear stack of layers. You can also view the resulting model's structure by calling the summary()
method.
There are two ways to create such a model, but both come down to simply specifying the layers of the model and yield the same model. Below is the model we will create:
Method 1
The first approach is to create a Sequential
model by passing a list of layers to its constructor:
1234567891011from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Activation, Input # Creating a model with layers model = Sequential([ Input(shape=(2,)), Dense(3), Activation('relu'), ]) model.summary()
Method 2
Alternatively, you can start with an empty Sequential
model and add layers afterward using the model's add()
method.
123456789101112from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Activation, Input # Creating a model model = Sequential() # Adding layers model.add(Input(shape=(2,))) model.add(Dense(3)) model.add(Activation('relu')) model.summary()
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Spørg mig spørgsmål om dette emne
Opsummér dette kapitel
Vis virkelige eksempler
Awesome!
Completion rate improved to 3.45
Model Creation
Stryg for at vise menuen
From the previous chapter, we've seen how to create individual layers. It's important to understand that these layers become effective only when they are integrated into a cohesive model.
Keras offers two main ways to build models: the sequential API, which is simple and ideal for linear stacks of layers, and the functional API, which is more flexible and suited for complex architectures, such as models with multiple inputs, outputs, or shared layers.
With the sequential API, the code is quite straightforward:
from keras.models import Sequential
from keras.layers import Input, Dense
model = Sequential([
Input(shape=(32,)),
Dense(64, activation='relu'),
Dense(1, activation='sigmoid')
])
With the functional API, the code is a bit more explicit and flexible:
from keras.models import Model
from keras.layers import Input, Dense
inputs = Input(shape=(32,))
x = Dense(64, activation='relu')(inputs)
outputs = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=outputs)
Sequential Model
In this course, we will mostly use the Sequential API, which involves using the Sequential
class. It's essentially a linear stack of layers. You can also view the resulting model's structure by calling the summary()
method.
There are two ways to create such a model, but both come down to simply specifying the layers of the model and yield the same model. Below is the model we will create:
Method 1
The first approach is to create a Sequential
model by passing a list of layers to its constructor:
1234567891011from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Activation, Input # Creating a model with layers model = Sequential([ Input(shape=(2,)), Dense(3), Activation('relu'), ]) model.summary()
Method 2
Alternatively, you can start with an empty Sequential
model and add layers afterward using the model's add()
method.
123456789101112from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Activation, Input # Creating a model model = Sequential() # Adding layers model.add(Input(shape=(2,))) model.add(Dense(3)) model.add(Activation('relu')) model.summary()
Tak for dine kommentarer!