- Create custom callbacks to execute functions during training at specific events like the end of each epoch.pythonCopy code
from tensorflow.keras.callbacks import Callback class CustomCallback(Callback): def on_epoch_end(self, epoch, logs=None): print(f"End of epoch {epoch}. Validation Loss: {logs['val_loss']}") model.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val), callbacks=[CustomCallback()]) - This is useful for tracking custom metrics or making decisions during training.
Author: saqibkhan
-
Custom Callbacks
-
Regularization Techniques (L1/L2/Dropout)
- Prevent overfitting by adding L1/L2 regularization or dropout.pythonCopy code
from tensorflow.keras.layers import Dropout from tensorflow.keras.regularizers import l2 model = Sequential([ Dense(128, activation='relu', kernel_regularizer=l2(0.001), input_shape=(20,)), Dropout(0.5), Dense(64, activation='relu'), Dropout(0.5), Dense(1) ]) model.compile(optimizer='adam', loss='mse') - Dropout randomly drops a fraction of the neurons, making the model less dependent on specific paths and helping to generalize better.
- Prevent overfitting by adding L1/L2 regularization or dropout.pythonCopy code
-
Weight Initialization
- Use custom weight initializers for better convergence, especially in deep networks.pythonCopy code
from tensorflow.keras.initializers import HeNormal model = Sequential([ Dense(128, activation='relu', kernel_initializer=HeNormal(), input_shape=(20,)), Dense(64, activation='relu', kernel_initializer=HeNormal()), Dense(1) ]) model.compile(optimizer='adam', loss='mse') - For ReLU-based networks, He initialization can lead to faster convergence.
- Use custom weight initializers for better convergence, especially in deep networks.pythonCopy code
-
Learning Rate Scheduling
- Dynamically adjust the learning rate during training to improve model convergence.pythonCopy code
from tensorflow.keras.callbacks import LearningRateScheduler def scheduler(epoch, lr): if epoch < 10: return lr else: return lr * 0.99 lr_scheduler = LearningRateScheduler(scheduler) model.fit(X_train, y_train, epochs=50, callbacks=[lr_scheduler])
- Dynamically adjust the learning rate during training to improve model convergence.pythonCopy code
-
Gradient Clipping
- Gradient clipping can prevent exploding gradients in recurrent neural networks (RNNs) and deep models by capping the gradients during backpropagation.pythonCopy code
from tensorflow.keras.optimizers import Adam model.compile(optimizer=Adam(clipvalue=1.0), loss='categorical_crossentropy', metrics=
- Gradient clipping can prevent exploding gradients in recurrent neural networks (RNNs) and deep models by capping the gradients during backpropagation.pythonCopy code
-
Batch Normalization
- Batch normalization can help to stabilize and speed up training, especially in deep networks.pythonCopy code
from tensorflow.keras.layers import BatchNormalization model = Sequential([ Dense(64, input_shape=(20,)), BatchNormalization(), Activation('relu'), Dense(32), BatchNormalization(), Activation('relu'), Dense(1) ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- Batch normalization can help to stabilize and speed up training, especially in deep networks.pythonCopy code
-
MongoDB Remove
In MongoDB, you can delete records or documents by using the remove() method. The first parameter of the remove() method is a query object which specifies the document to delete.
Example
Remove the record of employee whose address is Ghaziabad.
Create a js file named “remove.js”, having the following code:
var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var myquery = { address: 'Ghaziabad' }; db.collection("employees").remove(myquery, function(err, obj) { if (err) throw err; console.log(obj.result.n + " record(s) deleted"); db.close(); }); });Open the command terminal and run the following command:
Node remove.js
Verification
You can check that the record having address “Ghaziabad” is deleted and only following records are available now:

-
Fine-tuning Pretrained Models
- When using pretrained models, fine-tuning specific layers can help adapt the model to your dataset.
- Freeze some layers and only train others.pythonCopy code
for layer in base_model.layers[:100]: layer.trainable = False for layer in base_model.layers[100:]: layer.trainable = True
-
MongoDB Sorting
In MongoDB, the sort() method is used for sorting the results in ascending or descending order. The sort() method uses a parameter to define the object sorting order.
Value used for sorting in ascending order: { name: 1 } Value used for sorting in descending order: { name: -1 }Sort in Ascending Order
Example
Sort the records in ascending order by the name.
Create a js file named “sortasc.js”, having the following code:
var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var mysort = { name: 1 }; db.collection("employees").find().sort(mysort).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });Open the command terminal and run the following command:
Node sortasc.js
Sort in Descending Order
Example
Sort the records in descending order according to name:
Create a js file named “sortdsc.js”, having the following code:
var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var mysort = { name: -1 }; db.collection("employees").find().sort(mysort).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });Open the command terminal and run the following command:
Node sortdsc.js
-
MongoDB Filter Query
The find() method is also used to filter the result on a specific parameter. You can filter the result by using a query object.
Example
Filter the records to retrieve the specific employee whose address is “Delhi”.
Create a js file named “query1.js”, having the following code:
var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var query = { address: "Delhi" }; db.collection("employees").find(query).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });Open the command terminal and run the following command:
Node query1.js
Node.js MongoDB Filter With Regular Expression
You can also use regular expression to find exactly what you want to search. Regular expressions can be used only to query strings.
Example
Retrieve the record from the collection where address start with letter “L”.
Create a js file named “query2”, having the following code:
var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var query = { address: /^L/ }; db.collection("employees").find(query).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });Open the command terminal and run the following command:
Node query2.js