Open hgaur0007 opened 5 months ago
Hi @hgaur0007 -
This issue is related to tensorflow_probability. Can you please create new issue in this repo ?
This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.
Previously ran code suddenly has ValueError:
ValueError Traceback (most recent call last)
2 frames
/tmp/__autograph_generated_filezxcji763.py in tfget_grad_of_loss(model, t, t_0, y_0, yt0) 9 retval = ag.UndefinedReturnValue() 10 with ag.ld(tf).GradientTape(persistent=True) as tape: ---> 11 ag__.converted_call(ag.ld(tape).watch, (ag.ld(model).trainable_variables,), None, fscope) 12 loss = ag__.converted_call(ag.ld(compute_loss), (ag.ld(model), ag.ld(t), ag.ld(t_0), ag__.ld(y_0), ag.ld(yt_0)), None, fscope) 13 g = ag__.converted_call(ag.ld(tape).gradient, (ag.ld(loss), ag__.ld(model).trainable_variables), None, fscope)
ValueError: in user code:
File "<ipython-input-61-da64e963dba0>", line 7, in train_step *
loss, grad_theta = get_grad_of_loss(model, t, t_0, y_0, yt_0)
File "<ipython-input-59-59c4b4ca0f9a>", line 6, in get_grad_of_loss *
tape.watch(model.trainable_variables)
ValueError: Passed in object <KerasVariable shape=(1, 10), dtype=float32, path=sequential_13/dense_81/kernel> of type 'Variable', not tf.Tensor or tf.Variable or ExtensionType.
Hello Everyone,
Could you pleas help me out with the error display above while running the following code: My Keras version is '3.3.3.dev2024060803'
import tensorflow as tf import numpy as np import time from tensorflow.keras.layers import Dense import tensorflow_probability as tfp
tf.random.set_seed(42)
class model (tf.keras.Model): def init(self, layers, train_op, num_epoch, print_epoch): super(model, self).init() self.model_layers = layers self.train_op = train_op self.num_epoch = num_epoch self.print_epoch = print_epoch self.adam_loss_hist = []
rmin = -1 rmax = -0.7 numPts = 25 data_type = "float64" a = tf.constant(-1, dtype=tf.float64) rint = np.linspace(rmin, rmax, numPts).astype(data_type) rint = np.array(rint)[np.newaxis].T
define the model
tf.keras.backend.set_floatx(data_type) l1 = tf.keras.layers.Dense(25, "tanh") l2 = tf.keras.layers.Dense(25, "tanh") l3 = tf.keras.layers.Dense(1, None) train_op = tf.keras.optimizers.Adam() num_epoch = 200 print_epoch = 10 pred_model = model([l1, l2, l3], train_op, num_epoch, print_epoch)
convert the training data to tensors
rint_tf = tf.convert_to_tensor(rint)
training
print("Training (ADAM)...") t0 = time.time() pred_model.network_learn(rint_tf) t1 = time.time() print("Time taken (ADAM)", t1-t0, "seconds")
Following Error Appears:
ValueError Traceback (most recent call last) Cell In[46], line 4 2 print("Training (ADAM)...") 3 t0 = time.time() ----> 4 pred_model.network_learn(rint_tf) 5 t1 = time.time() 6 print("Time taken (ADAM)", t1-t0, "seconds")
Cell In[42], line 83, in model.network_learn(self, r) 80 self.bounds = {"lb" : tf.math.reduce_min(r), 81 "ub" : tf.math.reduce_max(r)} 82 for i in range(self.num_epoch): ---> 83 L, g = self.get_grad(r) 84 self.train_op.apply_gradients(zip(g, self.trainable_variables)) 85 self.adam_loss_hist.append(L)
Cell In[42], line 73, in model.get_grad(self, r) 71 def get_grad(self, r): 72 with tf.GradientTape() as tape: ---> 73 tape.watch(self.trainable_variables) 74 L = self.get_loss(r) 75 g = tape.gradient(L, self.trainable_variables)
File ~\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\eager\backprop.py:873, in GradientTape.watch(self, tensor) 864 def watch(self, tensor): 865 """Ensures that
tensor
is being traced by this tape. 866 867 Args: (...) 871 ValueError: if it encounters something that is not a tensor. 872 """ --> 873 for t in _extract_tensors_and_variables(tensor): 874 if not backprop_util.IsTrainable(t): 875 logging.log_first_n( 876 logging.WARN, "The dtype of the watched tensor must be " 877 "floating (e.g. tf.float32), got %r", 5, t.dtype)File ~\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\eager\backprop.py:700, in _extract_tensors_and_variables(tensor) 698 yield from _extract_tensors_and_variables(components) 699 else: --> 700 raise ValueError(f"Passed in object {obj} of type {type(obj).name!r}" 701 f", not tf.Tensor or tf.Variable or ExtensionType.")
ValueError: Passed in object <KerasVariable shape=(1, 64), dtype=float64, path=dense_15/kernel> of type 'Variable', not tf.Tensor or tf.Variable or ExtensionType.