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.
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.
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.