I am trying to fit an entire training loop including the epochs into graph mode and I keep getting this error:
in user code:
File "C:\Users\naray\AppData\Roaming\JetBrains\PyCharm2022.3\scratches\scratch_10.py", line 69, in train *
for epoch in tf.range(0, epochs, dtype=tf.int8):
TypeError: Failed to convert elements of SparseCategoricalAccuracy(name=sparse_categorical_accuracy,dtype=float32) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.
This is my own exercise, so I am pasting the code here:
import os
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import time
start = time.time()
os.environ['TF_MIN_GPU_MULTIPROCESSOR_COUNT'] = '1'
batch_size = tf.constant(64)
epochs = tf.constant(10)
train_data, info = tfds.load('fashion_mnist', split='train', with_info=True)
test_data = tfds.load('fashion_mnist', split='test')
def prepare_data(data):
image = data['image']
image = tf.reshape(image, (-1,))
image = tf.cast(image, tf.float32)
image = image / 255.0
return image, data['label']
train_data = train_data.map(prepare_data)
train_data = train_data.shuffle(buffer_size=1024).batch(64)
test_data = test_data.map(prepare_data)
test_data = test_data.shuffle(buffer_size=1024).batch(64)
def basic_model():
inputs = tf.keras.layers.Input(shape=(784,))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
x = tf.keras.layers.Dense(64, activation='relu')(x)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
return tf.keras.Model(inputs=inputs, outputs=outputs)
model = basic_model()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()
train_acc_object = tf.keras.metrics.SparseCategoricalAccuracy()
val_acc_object = tf.keras.metrics.SparseCategoricalAccuracy()
train_losses = tf.TensorArray(dtype=tf.float32, size=batch_size, dynamic_size=True)
val_losses = tf.TensorArray(dtype=tf.float32, size=batch_size, dynamic_size=True)
def validate(test_data, model, val_losses, val_acc_object, loss_object):
print('validation trace')
for x, ytrue in test_data:
ypred = model(x)
loss_value = loss_object(ytrue, ypred)
val_losses.write(val_losses.size() - 1, loss_value).mark_used()
val_acc_object.update_state(ytrue, ypred)
return val_losses, val_acc_object
@tf.function
def train(train_data, test_data, model, loss_object, optimizer,
train_acc_object, val_acc_object, epochs,
train_losses,
val_losses):
print('train trace', tf.range(0, epochs, dtype=tf.int8))
for epoch in tf.range(0, epochs, dtype=tf.int8):
tf.print('epoch: ', epoch)
for x, ytrue in train_data:
with tf.GradientTape() as tape:
ypred = model(x)
loss_value = loss_object(ytrue, ypred)
grads = tape.gradient(loss_value, model.variables)
train_losses.write(train_losses.size() - 1, loss_value).mark_used()
# tf.print(tf.convert_to_tensor(grads).shape, ' ', tf.convert_to_tensor(model.variables).shape)
optimizer.apply_gradients(zip(grads, model.variables))
train_acc_object.update_state(ytrue, ypred)
val_losses, val_acc_object = validate(test_data, model, val_losses, val_acc_object, loss_object)
tf.print('train_loss: ', tf.math.reduce_mean(train_losses.stack()), ' train_acc: ', train_acc_object.result(),
' val_loss: ', tf.math.reduce_mean(val_losses.stack()), ' val_accuracy: ', val_acc_object.result())
train_acc_object.reset_state()
val_acc_object.reset_state()
try:
train(train_data=train_data, test_data=test_data, model=model, loss_object=loss_object, optimizer=optimizer, train_acc_object=train_acc_object, val_acc_object=val_acc_object, epochs=epochs, train_losses=train_losses, val_losses=val_losses)
except TypeError as e:
print(tf.autograph.to_code(train.python_function))
print(e)
print('time: ', time.time() - start)
Here is the graph generator:
def tf__train(train_data, test_data, model, loss_object, optimizer, train_acc_object, val_acc_object, epochs, train_losses, val_losses):
with ag__.FunctionScope('train', 'fscope', ag__.ConversionOptions(recursive=True, user_requested=True, optional_features=(), internal_convert_user_code=True)) as fscope:
ag__.ld(print)('train trace', ag__.converted_call(ag__.ld(tf).range, (0, ag__.ld(epochs)), dict(dtype=ag__.ld(tf).int8), fscope))
def get_state_1():
return (val_acc_object, val_losses)
def set_state_1(vars_):
nonlocal val_acc_object, val_losses
(val_acc_object, val_losses) = vars_
def loop_body_1(itr_1):
nonlocal val_acc_object, val_losses
epoch = itr_1
ag__.converted_call(ag__.ld(tf).print, ('epoch: ', ag__.ld(epoch)), None, fscope)
def get_state():
return ()
def set_state(block_vars):
pass
def loop_body(itr):
(x, ytrue) = itr
with ag__.ld(tf).GradientTape() as tape:
ypred = ag__.converted_call(ag__.ld(model), (ag__.ld(x),), None, fscope)
loss_value = ag__.converted_call(ag__.ld(loss_object), (ag__.ld(ytrue), ag__.ld(ypred)), None, fscope)
grads = ag__.converted_call(ag__.ld(tape).gradient, (ag__.ld(loss_value), ag__.ld(model).variables), None, fscope)
ag__.converted_call(ag__.converted_call(ag__.ld(train_losses).write, (ag__.converted_call(ag__.ld(train_losses).size, (), None, fscope) - 1, ag__.ld(loss_value)), None, fscope).mark_used, (), None, fscope)
ag__.converted_call(ag__.ld(optimizer).apply_gradients, (ag__.converted_call(ag__.ld(zip), (ag__.ld(grads), ag__.ld(model).variables), None, fscope),), None, fscope)
ag__.converted_call(ag__.ld(train_acc_object).update_state, (ag__.ld(ytrue), ag__.ld(ypred)), None, fscope)
ag__.for_stmt(ag__.ld(train_data), None, loop_body, get_state, set_state, (), {'iterate_names': '(x, ytrue)'})
(val_losses, val_acc_object) = ag__.converted_call(ag__.ld(validate), (ag__.ld(test_data), ag__.ld(model), ag__.ld(val_losses), ag__.ld(val_acc_object), ag__.ld(loss_object)), None, fscope)
ag__.converted_call(ag__.ld(tf).print, ('train_loss: ', ag__.converted_call(ag__.ld(tf).math.reduce_mean, (ag__.converted_call(ag__.ld(train_losses).stack, (), None, fscope),), None, fscope), ' train_acc: ', ag__.converted_call(ag__.ld(train_acc_object).result, (), None, fscope), ' val_loss: ', ag__.converted_call(ag__.ld(tf).math.reduce_mean, (ag__.converted_call(ag__.ld(val_losses).stack, (), None, fscope),), None, fscope), ' val_accuracy: ', ag__.converted_call(ag__.ld(val_acc_object).result, (), None, fscope)), None, fscope)
ypred = ag__.Undefined('ypred')
tape = ag__.Undefined('tape')
ytrue = ag__.Undefined('ytrue')
grads = ag__.Undefined('grads')
x = ag__.Undefined('x')
epoch = ag__.Undefined('epoch')
loss_value = ag__.Undefined('loss_value')
ag__.for_stmt(ag__.converted_call(ag__.ld(tf).range, (0, ag__.ld(epochs)), dict(dtype=ag__.ld(tf).int8), fscope), None, loop_body_1, get_state_1, set_state_1, ('val_acc_object', 'val_losses'), {'iterate_names': 'epoch'})
The error occurs at the epoch loop but says it is about SparseCategoricalCrossentropy, which is what I don’t understand. Please help me with this.