Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting "nan" error #28

Open
NourO93 opened this issue Jan 27, 2019 · 1 comment
Open

Getting "nan" error #28

NourO93 opened this issue Jan 27, 2019 · 1 comment

Comments

@NourO93
Copy link

NourO93 commented Jan 27, 2019

I am trying to implement a simple example with scaled conjugate gradient. This is my code

`
dataset = [Instance( [0,0], [0] ), Instance( [1,0], [1] ), Instance( [0,1], [1] ), Instance( [1,1], [0] )]
settings = {
"n_inputs" : 2,
"layers" : [ (2, sigmoid_function), (1, sigmoid_function) ]
}

network        = NeuralNet( settings )
training_set   = dataset
test_set       = dataset
cost_function  = cross_entropy_cost

scaled_conjugate_gradient(
# Required parameters
network,                     # the neural network instance to train
training_set,                # the training dataset
test_set,                    # the test dataset
cost_function,               # the cost function to optimize

# Optional parameters
ERROR_LIMIT          = 1e-3, # Error tolerance when terminating the learning
max_iterations       = (),   # Regardless of the achieved error, terminate after max_iterations epochs. Default: infinite
print_rate           = 1000, # The epoch interval to print progression statistics
save_trained_network = False # Whether to ask the user if they would like to save the network after training
)

`

The output at each epoch looks something like this:

[training] Current error: nan Epoch: 1000

It never changed from nan and I can't figure out why

@mikelhsia
Copy link

mikelhsia commented Oct 21, 2024

I'm recently working on this and found the same issue while working on the scaled conjugate gradient. I have modified the library with Python3 and also made a few updates to the data structure based on my understanding of sklearn library. I've found that it could be because the cost function didn't produce meaningful error according to the outputs and targets in the original binary_cross_entropy_cost. I've added another cost function to use while predicting the stock price as below:

def mean_squared_error(outputs, targets, derivative=False, epsilon=1e-11 ):
    if derivative:
        return 2 * (outputs - targets)
    else:
        return np.mean(np.sum(np.square(outputs - targets), axis=1))

So you might as well check the value calculated in the error function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants