0

So I have a machine learning model which takes in batches of images and corresponding labels. The model should output a prediction for which class (label) it thinks a brand new image its never seen before is. I have a piece of code in my program which shuffles the training images and labels as I manually classified each image. The labels before the shuffling look something like this: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]. This is my current code for the shuffling:

permutation = np.random.permutation(len(train_labels))
train_images = train_images[permutation]
train_labels = train_labels[permutation]

The train_images shape looks like this: (192, 7, 36, 64, 1)
The train_labels shape looks like this: (192, 1)

The error and traceback I am getting for the train_labels shuffling:

Traceback (most recent call last):
  File "C:/Users/Mason Choi/PycharmProjects/Passion_project/main (test).py", line 43, in <module>
    train_labels = train_labels[permutation]
TypeError: only integer scalar arrays can be converted to a scalar index

Process finished with exit code 1
Mason Choi
  • 101
  • 2
  • 12
  • I don't think that slicing in `tf` has as many features as it does in `numpy`. I'm not sure if what you did is legal in `tf`. – Frank Yellin Apr 07 '21 at 19:35
  • 1
    I'm looking at your error message. train_images appears to be a tf array, not a numpy array. Also looking at the error message, it appears that this would work if permutation were a tf array. I don't know enough about tf to know how to create a random permutation using that framework, but there should be one. – Frank Yellin Apr 07 '21 at 23:41
  • Why don't you shuffle your dataset using a builtin function in tf, like [tf.random.shuffle](https://www.tensorflow.org/api_docs/python/tf/random/shuffle)? – Prefect Apr 07 '21 at 23:43
  • Ahh, thank you so much for catching this for me, I did not realize that I had cast the numpy array into float values using Tensorflow. I have updated the post to just include the second error, which I am now stuck on, but thanks again for getting me past the first one! – Mason Choi Apr 08 '21 at 00:31
  • Also, the reason I did not want to use tf.random.shuffle was because I would have to write more code to shuffle the training images and labels by index so that they would still pair up. If anyone wanted to go down this route, however, here is a link to how to do that: https://stackoverflow.com/questions/56575877/shuffling-two-tensors-in-the-same-order – Mason Choi Apr 08 '21 at 00:34
  • I figured it out, I did not change the labels into numpy arrays, rather they were just python lists which was why the error was thrown. Thank you guys for helping me reach this point!! – Mason Choi Apr 08 '21 at 01:08

0 Answers0