Week 3 Tensorflow - cannot replicate the code

This refers to week 3, Tensorflow module.

I cannot replicate the example provided in triple quotes:

“”"
import numpy as np
import tensorflow as tf

w = tf.Variable(0,dtype=tf.float32)
optimizer = tf.keras.optimizers.Adam(0.01)

def train_step():
with tf.GradientTape() as tape:
cost = w**2 - 10 * w + 25
trainable_variables = {w}
grads = tape.gradient(cost, trainable_variables)
optimizer.apply_gradients(zip(grads, trainable_variables)) # zip takes pairs of the lists grads and trainable_variables

print(w) # it has value = 0

train_step()
print(w)
“”"
The train_step() gives me the error: TypeError: Cannot convert value {<tf.Variable ‘Variable:0’ shape=() dtype=float32>} to a TensorFlow DType.

Can it be a problem of versions? Which version of tf is used in class?

Yes, it is probably a version incompatibility issue.

Note: If you’re goin to post code on the forum, pleae use the “preformatted text” tag. This will prevent your code from being interepted as Markdown - and it will preserve the indentation.

1 Like

They include the code in the notebook as written to show you the TF version:

So you can see that it is TF version 2.3.0.

BTW the code you show is not anything that is part of that assignment. Are you sure you are talking about DLS Course 2 Week 3?

If you are running code locally on your own system, note that the DLS courses were most recently updated in a major way in 2021, so they use versions that are quite a bit behind what is now current. So as Tom says, “versionitis” issues are always a concern when you run a different environment than the course website.

1 Like

Thank you for your reply.

The code I provided is not part of the assignment, it comes from the lecture of week 3 on Tensorflow.

I tried again with tensorflow==2.3.0 and I got:

---> 8 trainable_variables = {w}
TypeError: Variable is unhashable. Instead, use tensor.ref() as the key.

Then I tried again by runnig this before:

import numpy
import tensorflow.compat.v1.keras.backend as K
import tensorflow as tf
tf.compat.v1.disable_eager_execution()

And I got

----> 9 grads = tape.gradient(cost, trainable_variables)
TypeError: Cannot convert value {<tf.Variable 'Variable:0' shape=() dtype=float32>} to a TensorFlow DType.

Thanks

Frankly that’s a fairly useless example, as it is really old. It has two weight values but no data set, and gradient tape is no longer used in practice.

Personally I would not spend any time on duplicating the example from the lecture, since the assignment notebooks are much more useful.

2 Likes