TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense

2024/10/6 4:05:40

I have been trying to get the trainable variables from my layers and can't figure out a way to make it work. So here is what I have tried:

I have tried accessing the kernel and bias attribute of the Dense or Conv2D object directly, but to no avail. The type of result that I get is "Dense object has no attribute 'kernel'".

trainable_variables.append(conv_layer.kernel)
trainable_variables.append(conv_layer.bias)

Similarly, I have tried using the attribute "trainable_variables" in the following way:

trainable_variables.extend(conv_layer.trainable_variables)

From what I know this is supposed to return a list of two variables, the weight and the bias variables. However, what I get is an empty list.

Any idea of how to get the variables from labels in TensorFlow 2.0? I want to be able to later feed those variables to an optimizer, in a way similar to the following:

gradients = tape.gradient(loss, trainable_variables)
optimizer.apply_gradients(zip(gradients, trainable_variables))

Edit: Here is part of my current code to serve as an example and help answering the question (Hope it is readable)

from tensorflow.keras.layers import Dense, Conv2D, Conv2DTranspose, Reshape, Flatten... class Network:def __init__(self, params):weights_initializer = tf.initializers.GlorotUniform(seed=params["seed"])bias_initializer = tf.initializers.Constant(0.0)self.trainable_variables = []self.conv_layers = []self.conv_activations = []self.create_conv_layers(params, weights_initializer, bias_initializer)self.flatten_layer = Flatten()self.dense_layers = []self.dense_activations = []self.create_dense_layers(params, weights_initializer, bias_initializer)self.output_layer = Dense(1, kernel_initializer=weights_initializer, bias_initializer=bias_initializer)self.trainable_variables.append(self.output_layer.kernel)self.trainable_variables.append(self.output_layer.bias)def create_conv_layers(self, params, weight_init, bias_init):nconv = len(params['stride'])for i in range(nconv):conv_layer = Conv2D(filters=params["nfilter"][i],kernel_size=params["shape"][i], kernel_initializer=weight_init,kernel_regularizer=spectral_norm,use_bias=True, bias_initializer=bias_init,strides=params["stride"][i],padding="same", )self.conv_layers.append(conv_layer)self.trainable_variables.append(conv_layer.kernel)self.trainable_variables.append(conv_layer.bias)self.conv_activations.append(params["activation"])def create_conv_layers(self, params, weight_init, bias_init):nconv = len(params['stride'])for i in range(nconv):conv_layer = Conv2D(filters=params["nfilter"][i],kernel_size=params["shape"][i], kernel_initializer=weight_init,kernel_regularizer=spectral_norm,use_bias=True, bias_initializer=bias_init,strides=params["stride"][i],padding="same", )self.conv_layers.append(conv_layer)self.trainable_variables.append(conv_layer.kernel)self.trainable_variables.append(conv_layer.bias)self.conv_activations.append(params["activation"])

As you can see I am trying to gather all my trainable variables into a list attribute called trainable_variables. However as I mentioned this code does not work because I get an error for trying to acquire the kernel and bias attributes of those layer objects.

Answer

Ok, so I think I found the problem.

The trainable variables were not available until I used the given layer object. After I run my forward pass I could retrieve attributes of the tf.keras.layers.Layer object like trainable_variables and weights.

However, before my forward pass I received an empty list. To make things a little bit more clear:

with tf.GradientTape() as tape:print(dense_layers[0].trainable_variables)self.forward_pass(X)self.compute_loss()print(dense_layers[0].trainable_variables)

On the code above, the attribute trainable_variables is an empty list before executing self.forward_pass. However, right after it, I could retrieve the kernel and bias numpy arrays.

https://en.xdnf.cn/q/70405.html

Related Q&A

Convert Excel row,column indices to alphanumeric cell reference in python/openpyxl

I want to convert the row and column indices into an Excel alphanumeric cell reference like A1. Im using python and openpyxl, and I suspect theres a utility somewhere in that package that does this, bu…

Flask-admin - how to change formatting of columns - get URLs to display

Question on flask-admin. I setup flask-admin and one of the models i created is pulling urls and url titles from a mysql database. Using flask-admin, how to i get flask-admin to render the urls instea…

Stream audio from pyaudio with Flask to HTML5

I want to stream the audio of my microphone (that is being recorded via pyaudio) via Flask to any client that connects.This is where the audio comes from:def getSound(self):# Current chunk of audio dat…

Adding into Path var while silent installation of Python - possible bug?

I need to passively install Python in my applications package installation so i use the following:python-3.5.4-amd64.exe /passive PrependPath=1according this: 3.1.4. Installing Without UI I use the Pre…

Pandas add new columns based on splitting another column

I have a pandas dataframe like the following:A B US,65,AMAZON 2016 US,65,EBAY 2016My goal is to get to look like this:A B country code com US.65.AMAZON 2016…

Proper way to insert HTML into Flask

I know how to send some plain text from Python with render_template when a URL is visited:@app.route(/success.html") ... return render_template("index.html", text="This text goes in…

How to add a new class to an existing classifier in deep learning?

I trained a deep learning model to classify the given images into three classes. Now I want to add one more class to my model. I tried to check out "Online learning", but it seems to train on…

Count unique elements along an axis of a NumPy array

I have a three-dimensional array likeA=np.array([[[1,1], [1,0]],[[1,2], [1,0]],[[1,0], [0,0]]])Now I would like to obtain an array that has a nonzero value in a given position if only a unique nonzero …

influxdb python: 404 page not found

I am trying to use the influxdb-python lib which I found here. But I cant even get the tutorial programm to work. When I run the following example code:$ python>>> from influxdb import InfluxD…

Django Table already exist

Here is my Django Migration file. When I run python manage.py makemigrations/migrate I get this error.Error:-django.db.utils.OperationalError: (1050, "Table tickets_duration already exists")I…