Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neural network gives different results for each execution

This is the exact code I'm running with Keras and TensorFlow as a back end. For each run with the same program, the training results are different. Some times it gets 100% accuracy in 400th iteration and some times in the 200th.

training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")
target_data = np.array([[0],[1],[1],[0]], "float32")

model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='mean_squared_error',
              optimizer='adam',
              metrics=['binary_accuracy'])

model.fit(training_data, target_data, epochs=500, verbose=2)


Epoch 403/500
0s - loss: 0.2256 - binary_accuracy: 0.7500

So why does the result change in each execution as the train data is fixed ? Would greatly appreciate some explanation.

like image 276
Nirojan Selvanathan Avatar asked Aug 30 '17 06:08

Nirojan Selvanathan


People also ask

Why do neural networks give different results?

Neural network algorithms are stochastic. This means they make use of randomness, such as initializing to random weights, and in turn the same network trained on the same data can produce different results.

Why do I get different results each time in machine learning?

You will get different results when you run the same algorithm on different data. This is referred to as the variance of the machine learning algorithm.

Can neural network predict multiple outputs?

Multi-output regression is a predictive modeling task that involves two or more numerical output variables. Neural network models can be configured for multi-output regression tasks.

What is the biggest problem with neural networks?

The very most disadvantage of a neural network is its black box nature. Because it has the ability to approximate any function, study its structure but don't give any insights on the structure of the function being approximated.


1 Answers

The training set is fixed, but we set the initial weights of the neural network to a random value in a small range, so each time you train the network you get slightly different results.

If you want reproducible results you can set the numpy random seed with numpy.random.seed to a fixed value, so the same weights will be used, but beware that this can bias your network.

like image 98
Dr. Snoopy Avatar answered Oct 23 '22 16:10

Dr. Snoopy