TypeError: only integer scalar arrays can be converted to a scalar index in tensorflow

TypeError: only integer scalar arrays can be converted to a scalar index in tensorflow

我正在处理 CIFAR10 数据集并遇到以下错误 - 类型错误:只有整数标量数组可以转换为标量索引。当我可视化我的预测图像时 给我上述错误。我试过其他人的解决方案,但没有用,我被卡住了。任何帮助将不胜感激

这是我的代码:

class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
class_names = np.array(class_names)

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.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xticks([])
    plt.yticks([])
    plt.xlabel(class_names[train_labels[i]])
plt.show()

cnn = tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters=34, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)),
    tf.keras.layers.MaxPooling2D((2,2)),
    tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2,2)),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

cnn.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])

cnn.fit(train_images, train_labels, epochs=10)

def plot_image(i, predictions_array, true_label, img):
    true_label, img = true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(img, cmap=plt.cm.binary)

    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'

    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                            100*np.max(predictions_array),
                            class_names[true_label]),
                            color=color)

def plot_value_array(i, predictions_array, true_label):
    true_label = true_label[i]
    plt.grid(False)
    plt.xticks(range(10))
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)

    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')

probability_model = tf.keras.Sequential([cnn, tf.keras.layers.Softmax()])

predictions = probability_model.predict(test_images)

num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, predictions[i], test_labels, test_images)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()

回溯:

TypeError                                 Traceback (most recent call last)
<ipython-input-30-f3e05a180ae8> in <module>()
  7   plot_image(i, predictions[i], test_labels, test_images)
  8   plt.subplot(num_rows, 2*num_cols, 2*i+2)
  ----> 9   plot_value_array(i, predictions[i], test_labels)
 10 plt.tight_layout()
 11 plt.show()

<ipython-input-25-ad6b0beba933> in plot_value_array(i, predictions_array, true_label)
 28 
 29   thisplot[predicted_label].set_color('red')
 ---> 30   thisplot[true_label].set_color('blue')

 TypeError: only integer scalar arrays can be converted to a scalar index

发生此错误是因为 train_labelstest_labels2D array

train_labels

输出:

array([[6],
       [9],
       [9],
       ...,
       [9],
       [1],
       [1]], dtype=uint8)

在训练模型之前,您需要将这些数组 flatten 放入 1D array

扁平化train_labels

import numpy as np
train_labels=np.ravel(train_labels)
train_labels

输出:

array([6, 9, 9, ..., 9, 1, 1], dtype=uint8)

扁平化test_labels

test_labels=np.ravel(test_labels)
test_labels

输出:

array([3, 8, 8, ..., 5, 1, 7], dtype=uint8)