类型错误:只能将 str (不是 "numpy.uint8" ) 连接到 str



当我运行此代码时,我得到错误

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
data = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = data.load_data()
train_images = train_images/255.0
test_images = test_images/255.0
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', "Coat", 'Sandal', 'shirt', 'Sneaker', 'Bag', 'Ankle boot']
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy : ', test_acc)
prediction = model.predict(test_images)
for i  in range(5):
plt.grid(False)
plt.imshow(test_images[i], cmap=plt.cm.binary)
plt.xlabel("Actual : " + test_labels[i])
plt.title("Prediction : ",class_names[np.argmax(prediction[i])])
plt.show()

追踪(最近一次通话(:文件"C:\Users/dwark/PycharmProjects/test/nneural network 01.py",第33行,位于plt.xlabel("实际:"+test_labels[i](TypeError:只能将str(而不是"numpy.unt8"(连接到str

TypeError:只能将str(而不是"numpy.uint8"(连接到str

非常不言自明的

test_labels[i] # is not a string so just change it into one
str(test_labels[i]) # should fix it

最新更新