4

I am trying to migrate my previous tf1 code to tf2. Unfortunately my code was not on eager mode so I am having more difficulties. I did the following code (not yet training) and I got into the error message:

Traceback (most recent call last):
    training_op = tf.assign(W, W - learning_rate * gradients)
AttributeError: module 'tensorflow' has no attribute 'assign'

This is my minimum code example PS: it has to work with complex numbers!

# Data pre-processing
    m = 50
    n = 20
    x_train, y_train, x_test, y_test = get_my_data(x, y, m, n) # data x of size mxn

    # Network Declaration
    input_size = n
    output_size = 1
    learning_rate = 0.001  # The optimization learning rate
    # Create weight matrix initialized randomely from N~(0, 0.01)
    W = tf.Variable(tf.complex(np.random.rand(input_size, output_size),
                               np.random.rand(input_size, output_size)), name="weights")

    with tf.GradientTape() as gtape:
        y_out = tf.matmul(x_train, W, name="out")
        error = y - y_out
        loss = tf.reduce_mean(tf.square(tf.abs(error)), name="mse")
        gradients = gtape.gradient(loss, [W])[0]
        training_op = tf.assign(W, W - learning_rate * gradients)

I do this manually because unless they changed that, optimizers are not supported for complex numbers so I do it "by hand".

1
  • 1
    In TensorFlow 2.0 assign is no more a function of the TensorFlow package but it is a method of the tf.Variable objects. So instead of tf.assign(x, new_value), you can use x.assign(new_value). Commented Sep 6, 2020 at 5:04

2 Answers 2

8

Try tf.compat.v1.assign instead. It worked for me.

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

Comments

5

tf.assign* functions are available as methods on tf.Variable in TF 2.0. So, your example could be rewritten as

with tf.GradientTape() as gtape:
    ...
    W.assign_sub(learning_rate * gradients)

Note that unlike tf.assign in TF 1.X, tf.Variable.assing_sub will execute the assignment eagerly.

1 Comment

Ok, so this gradient and assign I should do it in the training iteration then right?

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.