Can someone spot the error please?

x_train, x_test, y_train, y_test = load_data(test_size=0.25)
n_epoch=50
model = MLPClassifier(alpha=0.01, batch_size=128, epsilon=1e-08, hidden_layer_sizes=(300,), learning_rate='adaptive',max_iter=500,early_stopping=True)
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
print(classification_report(y_pred, y_test))

As you can see code is able to find accuracy, classification report, and confusion matrix.
But don’t know in this situation how to plot.
Tried the code below using some online resource. But getting a small error here.

scores_train = []
scores_test = []


epoch = 0
while epoch < n_epoch:
    print('epoch: ', epoch)
    # SHUFFLING
    random_perm = np.random.permutation(x_train.shape[0])
    mini_batch_index = 0
    while True:
        # MINI-BATCH
        indices = random_perm[mini_batch_index:mini_batch_index + 128]
        model.partial_fit(x_train[indices], y_train[indices], classes=7)
        mini_batch_index += 128

        if mini_batch_index >= x_train.shape[0]:
            break

    # SCORE TRAIN
    scores_train.append(model.score(x_train, y_train))

    # SCORE TEST
    scores_test.append(model.score(x_test, y_test))

    epoch += 1


plt.plot(scores_train, color='green', alpha=0.8, label='Train')
plt.plot(scores_test, color='magenta', alpha=0.8, label='Test')
plt.title("Accuracy over epochs", fontsize=14)
plt.xlabel('Epochs')
plt.legend(loc='upper left')
plt.show()

The error is :TypeError: only integer scalar arrays can be converted to a scalar index at line

  model.partial_fit(x_train[indices], y_train[indices], classes=7)

I know it’s not directly related to Pyorch but I hope if someone can guide me.

Can you print types and shapes of x_train, y_train? type(x_train), x_train.shape, ...

1 Like

I would recommend providing a minimum working example (MWE)
as a Colab Notebook in a Github Gist. Then people can quickly reproduce the problem.

Also,

You don’t have to do this manually. You could use from torch.utils.data import DataLoader and drop the last batch. Something like this:

train_set = VisionDataset(root=path, train=True, download=True, transform=transform)
train_loader = DataLoader(train_set, batch_size, drop_last=True, shuffle=True, num_workers=num_workers, pin_memory=True)

Then, for training a epoch, you can do something like:

for inputs, labels in train_loader:
    inputs, labels = inputs.to(self._device), labels.to(self._device)
    ...
1 Like

Thank you @m3tobom_M for reply :slight_smile: The shapes are :

print(x_train.shape)
(360, 180)
type(x_train)
Out[8]: numpy.ndarray
len(y_train)
Out[7]: 360
 type(y_train)
Out[9]: list

Can you try converting y_train to numpy.array? y_train = np.array(y_train)

Tried that sir @m3tobom_M , not working :frowning:
Actually y_train is a string, y_train[1]=‘class_label’

Probably your class labels shouldn’t be a string and should be represented with numbers. I think you should do some research about your task. You can start with reading word embeddings. (I guess).

@m3tobom_M sir My task is classification sir. and MLPClassifier can deal with string type targets.
But this loss and accuracy plot is problem for me now.

@m3tobom_M Sir I am following this link. Changed my x_train, y_train type exactly same they both are

type(x_train)
Out[10]: numpy.ndarray

type(y_train)
Out[11]: numpy.ndarray

as in given example.
Now when I m trying its giving error
ValueError: Expected array-like (array or non-string sequence), got 7

The complete traceback is

 model.partial_fit(x_train[indices], y_train[indices], classes=7)

  File "C:\Users\krishna\Anaconda3\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 1061, in _partial_fit
    if _check_partial_fit_first_call(self, classes):

  File "C:\Users\krishna\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 316, in _check_partial_fit_first_call
    if not np.array_equal(clf.classes_, unique_labels(classes)):

  File "C:\Users\krishna\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 77, in unique_labels
    ys_types = set(type_of_target(x) for x in ys)

  File "C:\Users\krishna\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 77, in <genexpr>
    ys_types = set(type_of_target(x) for x in ys)

  File "C:\Users\krishna\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 243, in type_of_target
raise ValueError('Expected array-like (array or non-string sequence), '

ValueError: Expected array-like (array or non-string sequence), got 7

@ptrblck sir you are my last hope :slight_smile:

Got the result by just putting

early_stopping=False,warm_start=True

in MLPClassifier. Don’t know much about it, but solved the purpose.
Got the result by just putting

early_stopping=False,warm_start=True

in MLPClassifier. Don’t know much about it, but solved the purpose.
image
Thanks