I use the following code to fit a model via MLPClassifier given my dataset:
tr_X, ts_X, tr_y, ts_y = train_test_split(X, y, train_size=.8)
model = MLPClassifier(hidden_layer_sizes=(32, 32),
activation='relu',
solver=adam,
learning_rate='adaptive',
early_stopping=True)
model.fit(tr_X, tr_y)
prd_r = model.predict(ts_X)
test_acc = accuracy_score(ts_y, prd_r) * 100.
loss_values = model.estimator.loss_curve_
print (loss_values)
As seen above, the loss value from each batch can be acquired by calling loss_curve_
to return a list of losses. I got this:
[0.69411586222116872, 0.6923803442491846, 0.66657293575365906, 0.43212054205535255, 0.23119813830216157, 0.15497928755966919, 0.11799652235604828, 0.095235784011297939, 0.079951427356068624, 0.069012741113626194, 0.061282868601098078, 0.054871864138797251, 0.049835046972801049, 0.046056362860260207, 0.042823979794540182, 0.040681220899240651, 0.038262366774481374, 0.036256840660697079, 0.034418333946277503, 0.033547227978657508, 0.03285581956914093, 0.031671266419493666, 0.030941451221456757]
I want to plot these results to represent the loss curve
from this model. The problem is that I don't know what the x-axis
and y-axis
would be in this case. If I make y-axis
to be these losses values, what should be the x-axis
here to show the loss curve either decreasing or increasing?
Any hint or idea is appreciated.
Therefore, it uses the square error as the loss function, and the output is a set of continuous values. MLPRegressor also supports multi-output regression, in which a sample can have more than one target.
max_iterint, default=200. Maximum number of iterations. The solver iterates until convergence (determined by 'tol') or this number of iterations. For stochastic solvers ('sgd', 'adam'), note that this determines the number of epochs (how many times each data point will be used), not the number of gradient steps.
Building the MLPClassifier Thus the length of tuple denotes the total number of hidden layers in the network. max_iter: It denotes the number of epochs.
The plot()
command is overloaded and doesn't require an x-axis. If you just pass in loss_curve_
, the default x-axis will be the respective indices in the list of the plotted y values. For example, if we run
import matplotlib.pyplot as plt
plt.plot(loss_values)
plt.show()
We then get the following chart:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With