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".
Try tf.compat.v1.assign
instead.
It worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With