Hamza
Hamza

Reputation: 560

Why accuracy got changed everytime i rerun the program

I am training the model but I am facing one issue that every time I run the program means if model trained on 10 epochs its accuracy like 80% but after 10 epochs I re run the program, accuracy got changed like 95% and so on, every time I got changed accuracy.Should i stick with very first time or try it multiple times Is this a sign of overfitting or something else?

import numpy as np
import tensorflow as tf
from imgaug.augmenters import Dropout
from sklearn.model_selection import cross_val_score
from keras.wrappers.scikit_learn import KerasClassifier
data = np.load('subclass_features.npy', allow_pickle=True)

from sklearn.model_selection import train_test_split
training_data = np.asarray([i[0] for i in data])  # select upto second last
train_labels = data[:, -1]  # gives the last column vector
print("Shape of training data", training_data.shape)
print("Labels of training data", train_labels.shape)
data = training_data.astype('float32')
data = data / 255
from tensorflow.keras import utils as np_utils

one_hot_train_labels = np_utils.to_categorical(train_labels)


# train_data1, test_data1, train_labels1, test_labels1 = train_test_split(train_data, one_hot_train_labels,random_state=0, test_size=0.3)
def built_classifier():
    classifier = tf.keras.models.Sequential()
    classifier.add(tf.keras.layers.Dense(64, input_shape=(128,), activation='relu'))
    classifier.add(tf.keras.layers.Dense(4, activation='softmax'))
    classifier.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
    classifier.save('SubClassPredictions.h5')
    return classifier


classifier = KerasClassifier(build_fn=built_classifier, epochs=50, batch_size=32, shuffle=True)
accuracies = cross_val_score(classifier, data, one_hot_train_labels, cv=10)
print("Data Accuracy", accuracies)
print("Mean of accuracy is", accuracies.mean())

Upvotes: 0

Views: 4384

Answers (2)

Palguna Gopireddy
Palguna Gopireddy

Reputation: 81

You have to set the seed for different things like numpy, cuda etc.. Then your program gives same result. Use below function

def set_random_seed(random_seed):
    torch.manual_seed(random_seed)
    torch.cuda.manual_seed(random_seed)
    torch.cuda.manual_seed_all(random_seed)  # if use multi-GPU

    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    np.random.seed(random_seed)
    random.seed(random_seed)

Then call with a seed set_random_seed(1234) This will give you same result no matter on which machine you run. You can change the seed if you want. Different seed results in different results.

Upvotes: 0

sri dhurkesh
sri dhurkesh

Reputation: 11

It may be due to initializing the random weights during forward propagation or else due to splitting of your data randomly. It gives different training and testing datasets at every run. This error can be resolved by providing seed values for the random number generation.

Read this below link you will get some intuition about seeding

Random seed in python

Upvotes: 1

Related Questions