4

I've have a basic question about updating the values of tensors via the tensorflow python api.

Consider the code snippet:

x = tf.placeholder(shape=(None,10), ... )
y = tf.placeholder(shape=(None,), ... )
W = tf.Variable( randn(10,10), dtype=tf.float32 )
yhat = tf.matmul(x, W)

Now let's assume I want to implement some sort of algorithm that iteratively updates the value of W (e.g. some optimization algo). This will involve steps like:

for i in range(max_its):
     resid = y_hat - y
     W = f(W , resid) # some update 

the problem here is that W on the LHS is a new tensor, not the W that is used in yhat = tf.matmul(x, W)! That is, a new variable is created and the value of W used in my "model" doesn't update.

Now one way around this would be

 for i in range(max_its):
     resid = y_hat - y
     W = f(W , resid) # some update 
     yhat = tf.matmul( x, W)

which results in the creation of a new "model" for each iteration of my loop !

Is there a better way to implement this (in python) without creating a whole bunch of new models for each iteration of the loop - but instead updating the original tensor W "in-place" so to speak?

3 Answers 3

8

Variables have an assign method. Try:W.assign(f(W,resid))

Sign up to request clarification or add additional context in comments.

Comments

2

@aarbelle's terse answer is correct, I'll expand it a bit in case someone needs more info. The last 2 lines below is used for updating W.

x = tf.placeholder(shape=(None,10), ... )
y = tf.placeholder(shape=(None,), ... )
W = tf.Variable(randn(10,10), dtype=tf.float32 )
yhat = tf.matmul(x, W)

...

for i in range(max_its):
    resid = y_hat - y
    update = W.assign(f(W , resid)) # do not forget to initialize tf variables. 
    # "update" above is just a tf op, you need to run the op to update W.
    sess.run(update)

Comments

0

Precisely, the answer should be sess.run(W.assign(f(W,resid))). Then use sess.run(W) to show the change.

1 Comment

You forgot to define sess.

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.