1

I'm new to Python & Deep Learning.

I'm trying to make a simple learning, but I got an error:

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

t_label example: ['circle', 'circle', 'rectangle', 'triangle', ..., 'pentagon']

batch_mask example: [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]

I loaded images and labels using following codes. Thanks for your help.

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
0

1 Answer 1

1

Assuming t_label is created using _load_label() it is a list and this type does not support indexing with another list (your batch_mask). Hence your error.

If you want to use this type of indexing you need to create t_label as a NumPy array. More specifiaclly:

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The error is solved, and now I'm heading another error..` UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32') ` Maybe I need to study more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.