sns.pairplot(data, hue='target') # Good to explore the dataset plt.show() X = data.drop(['target'], axis=1).values n_cols = X.shape[1] # Get the number of features other than target column from tensorflow.keras.utils import to_categorical # For categorical target variable, you need something like one-hot-encoding y = to_categorical(data['target']) # Use this for one-hot-encoding if target is a class and it is a classification problem from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential def create_model(optimizer='adam', activation='relu', nl=1,nn=256): model = Sequential() # for i in range(nl): # you can also automate this process of creating layers and neurons with specified arguments # # Layers have nn neurons # model.add(Dense(nn, activation=activation)) # Start with the first hidden layer, where information comes in shape (dataset column, any number of rows) model.add(Dense(100, activation=activation, input_shape = (n_cols,))) # First hidden layer, where values directly come from dataset of n features, with unknown number of datapoints model.add(Dense(100, activation=activation, kernel_initializer='normal')) # Second hidden layer model.add(Dense(100, activation=activation)) # Third hidden layer from tensorflow.keras.layers import BatchNormalization model.add(BatchNormalization()) # Add batch normalization for the outputs of the layer above # use 'softmax' for multi-class classification, 'sigmoid' for binary or multi-label classification model.add(Dense(3, activation='softmax')) # Output layer, 3 nodes for 3 class prediction my_optimizer = SGD(lr=lr) # # You can also use custom optimizer like this # For loss function, use 'categorical_crossentropy' for multiclass, 'binary_crossentropy' for binary or multi-label, 'mean_squared_error' for regression model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) # optimizer = my_optimizer return model # K-fold is costly for neural network, so use validation data as a wise choice to get best validation score with early stopping from tensorflow.keras.callbacks import EarlyStopping early_stopping_monitor = EarlyStopping(monitor='val_loss', patience=2) # Terminates once validation loss stops improving # Instantiate a model checkpoint callback, this will automatically save the model when best result is produced from keras.callbacks import ModelCheckpoint model_save = ModelCheckpoint('best_model.hdf5', save_best_only=True) model = create_model() model_1_training = model.fit(X_train, y_train, validation_split=0.3, epochs=20, batch_size=128, callbacks = [early_stopping_monitor, model_save], verbose=0) # or use "validation_data=(X_test, y_test)" predictions = model.predict(X_test) probability_true = predictions[:,1] from tensorflow.keras.models import load_model init_weights = model.get_weights() # Save model weights model.save('model_file.h5') # Saving model first_layer = model.layers[0] # Acessing the first layer of a Keras model print(first_layer.input) # Printing the layer input (this is a tensorflow tensor object) print(first_layer.output) # Printing the layer output (this is a tensorflow tensor object) print(first_layer.weights) # Printing the layer weights (this is a tensorflow variable object, which is updatable) my_model = load_model('model_file.h5') # loading model my_model.summary() # See model summary history = model_1_training # Extracting fitting history print(history.params) # See all parameters print(history.history.keys()) # See what you can extract, eg : val_loss = history.history['val_loss'], val_accuracy = history.history['val_accuracy'] accuracy = model.evaluate(X_test, y_test)[1] # Evaluate preds = model.predict(test_set) # Predict preds_chosen = [np.argmax(pred) for pred in preds] # Extract the position of highest probability from each pred vector (For classification of multi-class problem) ### VISUALIZE KERAS LEARNING CURVE with plot_loss(loss,val_loss) and plot_accuracy(acc,val_acc) using fitting/training history # k-fold Cross validation and Fine-tuning with RandomSearchCV from tensorflow.keras.wrappers.scikit_learn import KerasClassifier # Import sklearn wrapper from keras model = KerasClassifier(build_fn=create_model, epochs=6, batch_size=16) # Create a model as a sklearn estimator from sklearn.model_selection import cross_val_score kfold = cross_val_score(model, X, y, cv=5) # Check how your keras model performs with 5 fold crossvalidation kfold.mean() # Print the mean accuracy per fold params = dict(optimizer=['sgd', 'adam'], epochs=3, batch_size=[5, 10, 20], activation=['relu','tanh'], nl=[1, 2, 9], nn=[128,256,1000]) random_search = RandomizedSearchCV(model, params_dist=params, cv=3) random_search_results = random_search.fit(X, y) random_search_results.best_score_ random_search_results.best_params_