如何在Google Colab上修复错误的语法错误

时间:2019-06-28 16:30:44

标签: python google-colaboratory

我正在使用Google Colab上的TensorFlow使用Fashion Mnist库。我在第62行出现错误,说它的语法无效,即使没有。我想知道我的代码中是否有错误,或者Google Colab是否存在故障或故障。

我尝试注释掉代码,然后仅用简单的打印行替换它,这是行不通的。但是,当我使笔记本的行数少于62行时,我没有收到该错误。在删除之后,我还尝试注释掉所有代码,并且只会说所有其他代码都具有无效的语法。

代码如下:

from __future__ import absolute_import, division, print_function, 
unicode_literals

# Import TensorFlow & Keras
import tensorflow as tf
from tensorflow import keras

# Import helper libraries
import numpy as np
import matplotlib.pyplot as plt

print("TensorFlow is currently on version "+tf.__version__)

# Fashion Mnist setup
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# Train neuron
train_images.shape
len(train_labels)
train_labels
test_images.shape
len(test_labels)

# PREPROCESS THE DATA
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show

train_images = train_images / 255.0
test_images = test_images / 255.0

plt.figure(figsize=(10,10))
for i in range(25):
  plt.subplot(5,5,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  plt.imshow(train_images[i], cmap=plt.cm.binary)
  #plt.xlabel(class_names[train_labels[[i]]])
plt.show)

我收到的错误消息是:

File "<ipython-input-5-a9c182238576>", line 62
  test_loss = model.evaluate(test_images, test_labels)
          ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

您可能缺少前一行的右括号。这是显示行为的简短示例:

x = (1, 2, 3
test_loss = model.evaluate(test_images, test_labels)

执行后:

  File "<ipython-input-14-2b487d3dfc3a>", line 2
    test_loss = model.evaluate(test_images, test_labels)
            ^
SyntaxError: invalid syntax

在错误上方的行中查找缺少的右括号。