How can Tensorflow be used to get the variables in a layer?


Tensorflow can be used to get the variables in a layer by displaying the variables in the layer using ‘layer.Variables’, and then using ‘layer.kernel’, and ‘layer.bias’ to access these variables.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

The intuition behind transfer learning for image classification is, if a model is trained on a large and general dataset, this model can be used to effectively serve as a generic model for the visual world. It would have learned the feature maps, which means the user won’t have to start from scratch by training a large model on a large dataset.

TensorFlow Hub is a repository that contains pre-trained TensorFlow models. TensorFlow can be used to fine-tune learning models.

We will understand how to use models from TensorFlow Hub with tf.keras, use an image classification model from TensorFlow Hub.  Once this is done, transfer learning can be performed to fine-tune a model for customized image classes. This is done by using a pretrained classifier model to take an image and predict what it is. This can be done without needing any training.  

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Example

print("The variables in a layer")
print(layer.variables)
print("Variables can be accessed through nice accessors")
print(layer.kernel, layer.bias)

Code credit −https://www.tensorflow.org/tutorials/customization/custom_layers

Output

The variables in a layer
[<tf.Variable 'dense_5/kernel:0' shape=(5, 10) dtype=float32, numpy=
array([[-0.05103415, -0.6048388 , -0.09445673, 0.00147319, 0.5958504 ,
0.03832638, -0.4617405 , 0.22488767, -0.521616 , -0.4939174 ],
[ 0.2143982 , -0.42745167, -0.19899327, 0.011585 , 0.39148778,
-0.5193864 , 0.11133379, 0.07244939, -0.42842603, -0.07152319],
[ 0.09674573, 0.08572572, -0.44079286, -0.10669523, 0.1977045 ,
0.04525411, 0.41716915, 0.2611121 , -0.1167441 , -0.26781783],
[ 0.38834113, -0.5396006 , -0.33349523, -0.5882891 , -0.2575687 ,
-0.33869067, 0.56990904, 0.3368895 , 0.6290985 , -0.31278375],
[-0.40759754, 0.18778783, 0.11296159, 0.35683256, -0.16895893,
-0.55790913, 0.5088188 , -0.06520861, -0.24566567, -0.15854272]],
dtype=float32)>, <tf.Variable 'dense_5/bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>]
Variables can be accessed through nice accessors
<tf.Variable 'dense_5/kernel:0' shape=(5, 10) dtype=float32, numpy=
array([[-0.05103415, -0.6048388 , -0.09445673, 0.00147319, 0.5958504 ,
0.03832638, -0.4617405 , 0.22488767, -0.521616 , -0.4939174 ],
[ 0.2143982 , -0.42745167, -0.19899327, 0.011585 , 0.39148778,
-0.5193864 , 0.11133379, 0.07244939, -0.42842603, -0.07152319],
[ 0.09674573, 0.08572572, -0.44079286, -0.10669523, 0.1977045 ,
0.04525411, 0.41716915, 0.2611121 , -0.1167441 , -0.26781783],
[ 0.38834113, -0.5396006 , -0.33349523, -0.5882891 , -0.2575687 ,
-0.33869067, 0.56990904, 0.3368895 , 0.6290985 , -0.31278375],
[-0.40759754, 0.18778783, 0.11296159, 0.35683256, -0.16895893,
-0.55790913, 0.5088188 , -0.06520861, -0.24566567, -0.15854272]],
dtype=float32)> <tf.Variable 'dense_5/bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>

Explanation

  • Layers have many methods.
  • All variables can be inspected using methods in a layer.
  • fully-connected layer would have variables for weights and biases.

Updated on: 25-Feb-2021

392 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements