我从他们网站上的tensorflow教程复制粘贴的完全有效的代码行中得到了无效的语法。
我尝试搜索问题,但由于某些原因,并不是每个人都面临着同样的问题。
包含的软件包是
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
以下行出现错误(语法无效):
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
我需要帮助调试此错误,否则我的代码将无法运行。
完整代码:
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
class_names = ['0','1','2','3','4','5','6','7','8','9']
print(train_images.shape)
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()
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.sigmoid),
keras.layers.Dense(10, activation=tf.nn.sigmoid)
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
"""
# Load dataset
mndata = MNIST('')
images, labels = mndata.load_training()
# Pick the fifth image from the dataset (it's a 9)
i = 4
image, label = images[i], labels[i]
# Print the image
output = Image.new("L", (28, 28))
output.putdata(image)
output.save("output.png")
# Print label
print(label)
"""
答案 0 :(得分:1)
最后必须使用])正确关闭模型。
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.sigmoid),
keras.layers.Dense(10, activation=tf.nn.sigmoid)])
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])