2

My cost function involves the matrix

T=[[1.0-a,b],[a,1.0-b]] 

I can define

import numpy as np
import tensorflow as tf
a=0.3
b=0.4
T = tf.Variable([[1.0-a,b],[a,1.0-b]]

and this works well in the optimization, but then I am saying that I have four variables: 1-a,b,a,1-b (the gradient has four elements). On the other hand, I would like my variables to be two: a and b (the gradient has two elements).

I thought of doing something like

var = tf.Variable([a,b])
T = tf.constant([[1.0-var[0],var[1]],[var[0],1.0-var[1]]])

but this does not work, outputing the following:

TypeError: List of Tensors when single Tensor expected

So how can I construct a tensor made of tf.Variable objects?

Thank you.

2
  • 1
    What kind of cost function is this T=[[1.0-a,b],[a,1.0-b]]? In what sense is this a cost function? Commented Jun 7, 2018 at 15:24
  • I said my cost function involves T. To be specific, my cost function will depend on vector elements of r = T^N v, where v is just [1,1], and N is some large number. Actually, as you might infer, r is just the dominant eigenvector of T, when N is large enough. Commented Jun 7, 2018 at 15:27

2 Answers 2

2

I think what you need is:

import tensorflow as tf

a = tf.Variable(0.3)
b = tf.Variable(0.4)
T = tf.convert_to_tensor([[1.0 - a, b], [a, 1.0 - b]]
Sign up to request clarification or add additional context in comments.

Comments

1

Initialise T as a placeholder.

T = tf.placeholder(tf.float32, [2, 2])

When starting your session, compute T and pass it in through a feed_dict:

with tf.Session() as sess:
    a, b = .3, .4
    inp = np.array([[1 - a, b], [a, 1 - b]])
    sess.run(optimizer, feed_dict={T : inp})

Where optimizer is the node that minimizes your cost function.

1 Comment

Thank you for your answer. @jdehesa answer was more helpful because I need to use tf.Variable since I am using pymanopt (pymanopt.github.io), see section c in Cost Functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.