Cursusinhoud
Neural Networks with TensorFlow
Neural Networks with TensorFlow
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:
python
With the functional API, the code is a bit more explicit and flexible:
python
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:
from 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.
from 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()
Bedankt voor je feedback!