Output:
Package library: TensorFlow Example 2: In this example, we will retrieve the last layer of a pre-trained convolutional neural network, and use its output as the input to a new fully-connected layer:python import tensorflow as tf from tensorflow.keras.applications import VGG16 from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense # Load pre-trained VGG16 model base_model = VGG16(weights='imagenet') # Retrieve the last layer last_layer = base_model.get_layer('block5_pool') # Create a new model using the last layer as input x = last_layer.output x = Dense(512, activation='relu')(x) output = Dense(10, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=output) # Freeze all layers except the last one for layer in model.layers[:-2]: layer.trainable = False # Compile and fit the model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test)) ``` Package library: TensorFlow