np 数组是不可变的 - “数组是只读的”

问题描述 投票:0回答:1

我试图加载

fashion_mnist
数据集形式
tf.keras
。但是当我尝试加载和编辑标签时,它说是只读的。我检查并尝试了几个 Stack Overflow 帖子,但问题仍然存在。

我试图加载

fashion_mnist
,而
train_labels
是不可变的

import tensorflow as tf
from tensorflow import keras
# import tensorflow_federated as tff

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import functools
import glob
import os
import PIL
import time

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.fashion_mnist.load_data()
# (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5   # Normalization
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1).astype('float32')
test_images = (test_images - 127.5) / 127.5   # Normalization

state = np.random.get_state()

# train_labels.setflags(write = 1)

# Change the order
np.random.shuffle(train_images)
np.random.set_state(state)
np.random.shuffle(train_labels)

这是错误:

ValueError: array is read-only

当我使用 mnist dataset 和 setflag 不起作用时,不会出现同样的问题

ValueError: cannot set WRITEABLE flag to True of this array

我还检查了

train_label

的旗帜
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
python numpy tensorflow numpy-ndarray
1个回答
0
投票

我已经通过复制 train_labels 解决了这个问题

train_labels_copy = np.copy(train_labels)
© www.soinside.com 2019 - 2024. All rights reserved.