Skip to main content

Posts

Showing posts from January, 2024

Tensorflow Keras Freeze Layers / Unfreeze Layers (Python Example)

Tensorflow keras freeze / unfreeze a specific layer or multiple layers based on layer name. Freezing a layer retains current weights of a layer and does not alter when the model is trained / during model fitting. Simply put the the frozen layers are not trainable. Freezing layer is a technique predominantly used for transfer learning and fine-tuning.These are cases in which we wish to retain layer weights; as this could be an already trained model like resnet , mobilenet,etc. Or we just want to train only certain layers in the current model and not the whole model- we can freeze and unfreeze layers according to our needs and then start the fitting processing. Creating a new tensorflow model using functional api - execute the given code to generate the following model. Generated model is stored in a variable called "model". # viewing the model info # pass "show_trainable=True" # to see whether a layer is trainable model.summary(show_trainable=True) Model: &

Tensorflow Keras Create Model Using Functional API - Single Output Regression With Multiple Dense Layers (Python Example)

Trying out tensorflow by creating simple neural network models is a good starting point to understand and to get used to various features of tensorflow and its api. Keras is used here - which sits on top of tensorflow and provides a good api layer with really good documentation.  Functional API will be demonstrated; instead of sequential API. Functional API offers more control over the model architecture, also allows creation of multi-output models, flexibility and much more. Create regression output model using Tensorflow Keras (functional api) Importing tensorflow # importing tensorflow import tensorflow as tf from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense, Input Defining a function to create dense layers for convenience. Functions like these will help with creating complex layers structure and reduce confusion. # function to create dense layers def dense_layer_creator(units, name_for_layer, last_layer, activation=""): if

Topics

Show more