Jorge Moraleda
Jorge Moraleda

Reputation: 381

How to reuse some parameters of a Variable in Tensorflow

I know how to reuse complete variables Tensorflow in two different operations as explained in the documentation: https://www.tensorflow.org/programmers_guide/variable_scope

But, is it possible to reuse parameters in more complex ways? In particular, is it possible to specify that two variables of different shapes share some parameters in common?

A small example of what I am trying to do: I would like to have a model with 25 parameters. I would like to use these parameters in three variables X, Y, and Z so that X uses all parameters in a 5x5 tensor: X = w11, w12, ... , w15 w21, w22, ... , w25 ... w51, w52, ... , w55

while Y uses 9 of the parameters in a 3x3 tensor, for example the middle 3x3 block of X. That is: Y = w22, w23, w24 w32, w33, w34 w42, w43, w44

while Z uses the same nine weights as Y and also in a 3x3 tensor, but transposed with respect to Y, that is: Z = w22, w32, w42 w23, w33, w43 w24, w34, w44

If this is not possible, are there plans in the Tensorflow development community to support this capability?

I posted this question as a feature request in github https://github.com/tensorflow/tensorflow/issues/8368 but was referred to post here instead. Also, this question is related to Reusing layer weights in Tensorflow but this is a more general question.

Upvotes: 0

Views: 434

Answers (1)

Steven
Steven

Reputation: 5162

This posts assumes you are using tensorflow 1.0

# 5x5 kernel
X = tf.random_normal(shape=(5,5))
Y = tf.identity(X[2:4,2:4]) #this creates a copy
Z = tf.transpose(Y)

# do whatever you want with X, Y, and Z below

Upvotes: 1

Related Questions