I have been working on a model whose training loop uses a tf.function wrapper (I get OOM errors when running eagerly), and training seems to be running fine. However, I am not able to access the tensor values returned by my custom training function (below)
def train_step(inputs, target):
with tf.GradientTape() as tape:
predictions = model(inputs, training=True)
curr_loss = lovasz_softmax_flat(predictions, target)
gradients = tape.gradient(curr_loss, model.trainable_variables)
opt.apply_gradients(zip(gradients, model.trainable_variables))
# Need to access this value
return curr_loss
A simplified version of my 'umbrella' training loop is as follows:
@tf.function
def train_loop():
for epoch in range(EPOCHS):
for tr_file in train_files:
tr_inputs = preprocess(tr_file)
tr_loss = train_step(tr_inputs, target)
print(tr_loss.numpy())
When I do try to print out the loss value, I end up with the following error:
AttributeError: 'Tensor' object has no attribute 'numpy'
I also tried using tf.print() as follows:
tf.print("Loss: ", tr_loss, output_stream=sys.stdout)
But nothing seems to appear on the terminal. Any suggestions?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…