2

I want to construct a weight whose certain elements are zero and never change, and other elements are the variables.For example:

[[0,0,a,0],[0,0,b,0],[0,0,0,c],[0,0,0,d]]

This is a tf variable, and all zeros stay unchanged. Only a, b, c, d are tuned using gradient descent.

Are there anyone who knows how to define such a matrix?

ZHANG Juenjie
  • 501
  • 5
  • 20

1 Answers1

1

You should look into SparseTensor. It is highly optimised for operations where tensor consists of many zeros.

So, in your case, to initialise SparseTensor:

a,b,c,d = 10,20,30,40
sparse = tf.SparseTensor([[0,2], [1,2], [2,3], [3,3]], [a,b,c,d], [4,4])
Prasad
  • 5,946
  • 3
  • 30
  • 36
  • However, what I need is a sparse tf.Variable. Now it seems this is a costant tensor? – ZHANG Juenjie Dec 08 '17 at 11:24
  • [This question](https://stackoverflow.com/questions/37001686/using-sparsetensor-as-a-trainable-variable/45779695) looks relevant. Also [this feature request](https://github.com/tensorflow/tensorflow/issues/3907) on the tensorflow github. – scrpy Dec 08 '17 at 13:14