【问题标题】:TypeError: can only concatenate str (not "numpy.uint8") to strTypeError:只能将str(不是“numpy.uint8”)连接到str
【发布时间】:2020-08-19 01:46:15
【问题描述】:

当我运行这段代码时,我得到了错误

    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/neural network 01.py”,第 33 行,在 plt.xlabel("实际:" + test_labels[i]) TypeError:只能将str(不是“numpy.uint8”)连接到str

【问题讨论】:

  • plt.xlabel("实际:" + str(test_labels[i]))

标签: python tensorflow neural-network


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多