【问题标题】:Python TypeError: only integer scalar arrays can be converted to a scalar indexPython TypeError:只能将整数标量数组转换为标量索引
【发布时间】:2020-04-30 22:44:49
【问题描述】:

我是 Python 和深度学习的新手。

我正在尝试做一个简单的学习,但是我得到了一个错误:

only integer scalar arrays can be converted to a scalar index. on t_batch = t_label[batch_mask]

t_label 示例:['circle', 'circle', 'rectangle', 'triangle', ..., 'pentagon']

batch_mask 示例:[2 0 2 1 2 2 1 0 0 2 2 1 2 0 0 1 0 0 1 0 1 0].

train_size = 3 # x_train.shape[0]
batch_size = 22
for i in range(242): # iters_num = 242
   batch_mask = np.random.choice(train_size, batch_size)
   print( t_train, batch_mask )
   x_batch = x_train[batch_mask]
   t_batch = t_label[batch_mask]

我使用以下代码加载了图像和标签。 感谢您的帮助。

data_list = glob('dataset\\training\\*\\*.jpg')
def _load_img():

    for v in data_list:
   #     print("Converting " + v + " to NumPy Array ...")       
        data = np.array(Image.open(v))
        data = data.reshape(-1, img_size)

    return data

def _load_label():
    labels = []
    for path in data_list:
        labels.append(get_label_from_path(path))

    return labels

【问题讨论】:

    标签: python-3.x deep-learning


    【解决方案1】:

    假设t_label 是使用_load_label() 创建的,它是list,并且此类型不支持使用另一个列表(您的batch_mask)进行索引。因此你的错误。

    如果您想使用这种类型的索引,您需要将t_label 创建为 NumPy 数组。更具体:

    def load_label(data_list):
        labels = []
        for path in data_list:
            labels.append(get_label_from_path(path))
        return np.array(labels)
    
    def load_label_variation(data_list):
        # Use a list comprehension to build list of labels
        labels = [get_label_from_path(path) for path in data_list]
        return np.array(labels)
    

    【讨论】:

    • 谢谢。错误已解决,现在我正在处理另一个错误..` UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype(' dtype('
    猜你喜欢
    • 2020-07-23
    • 2019-03-15
    • 2019-04-26
    • 1970-01-01
    • 2021-02-09
    • 2020-04-20
    • 2021-01-17
    • 2020-03-12
    • 2020-09-24
    相关资源
    最近更新 更多