“语法错误:无效语法”,用于从tensorflow急切执行教程中复制的代码

2024-04-26 14:54:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从https://www.tensorflow.org/get_started/eager运行程序,但遇到语法错误。代码如下。在

## Note: Rerunning this cell uses the same model variables
# keep results for plotting
train_loss_results = []
train_accuracy_results = []
num_epochs = 201
for epoch in range(num_epochs):
  epoch_loss_avg = tfe.metrics.Mean()
  epoch_accuracy = tfe.metrics.Accuracy()  
# Training loop - using batches of 32
  for x, y in train_dataset:
  # Optimize the model
    grads = grad(model, x, y)
    optimizer.apply_gradients(zip(grads,model.variables),global_step=tf.train.get_or_create_global_step())
    # Track progress
    epoch_loss_avg(loss(model, x, y))  # add current batch loss
    # compare predicted label to actual label
    epoch_accuracy(tf.argmax(model(x), axis=1, output_type=tf.int32), y)
  # end epoch
  train_loss_results.append(epoch_loss_avg.result())
  train_accuracy_results.append(epoch_accuracy.result())

这是错误。在

^{pr2}$

@


Tags: theinforgetmodeltftrainvariables