簡體   English   中英

重塑錯誤/ ValueError:新數組的總大小必須保持不變

[英]reshape Error/ValueError: total size of new array must be unchanged

我有一個使用CNN進行圖像分類的代碼,因此有訓練數據集和測試數據集。 當我執行系統時,出現以下錯誤:

ValueError                                Traceback (most recent call last)
<ipython-input-44-cb7ec1a13881> in <module>()
      1 optimize(num_iterations=1)
      2 
----> 3 print_validation_accuracy()

<ipython-input-43-7f1a17e48e41> in print_validation_accuracy(show_example_errors, show_confusion_matrix)
     21 
     22         # Get the images from the test-set between index i and j.
---> 23         images = data.valid.images[i:j, :].reshape(batch_size, img_size_flat)
     24         #images = data.valid.images[i:j, :].reshape(1, 128)
     25 

ValueError: total size of new array must be unchanged

和處理此錯誤的代碼步驟如下:

def print_validation_accuracy(show_example_errors=False,
                        show_confusion_matrix=False):

    # Number of images in the test-set.
    num_test = len(data.valid.images)

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + batch_size, num_test)

        # Get the images from the test-set between index i and j.
        images = data.valid.images[i:j, :].reshape(batch_size, img_size_flat)

        # Get the associated labels.
        labels = data.valid.labels[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    cls_true = np.array(data.valid.cls)
    cls_pred = np.array([classes[x] for x in cls_pred]) 

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))

    # Plot some examples of mis-classifications, if desired.
    if show_example_errors:
        print("Example errors:")
        plot_example_errors(cls_pred=cls_pred, correct=correct)

    # Plot the confusion matrix, if desired.
    if show_confusion_matrix:
        print("Confusion Matrix:")
        plot_confusion_matrix(cls_pred=cls_pred)

誰能幫我嗎?

如錯誤消息所示,此語句中的重塑不匹配。

images = data.valid.images[i:j, :].reshape(batch_size, img_size_flat)

發生的情況是該方程式不相等,即(j-i)*(data.valid.images的column_size)不等於batch_size * img_size_flat。 使它相等,問題將得到解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM