Author: saqibkhan

  • Custom Callbacks

    • Create custom callbacks to execute functions during training at specific events like the end of each epoch.pythonCopy codefrom 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.
  • Regularization Techniques (L1/L2/Dropout)

    • Prevent overfitting by adding L1/L2 regularization or dropout.pythonCopy codefrom 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.
  • Weight Initialization

    • Use custom weight initializers for better convergence, especially in deep networks.pythonCopy codefrom 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.
  • Learning Rate Scheduling

    • Dynamically adjust the learning rate during training to improve model convergence.pythonCopy codefrom 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])
  • Gradient Clipping

    • Gradient clipping can prevent exploding gradients in recurrent neural networks (RNNs) and deep models by capping the gradients during backpropagation.pythonCopy codefrom tensorflow.keras.optimizers import Adam model.compile(optimizer=Adam(clipvalue=1.0), loss='categorical_crossentropy', metrics=
  • Batch Normalization

    • Batch normalization can help to stabilize and speed up training, especially in deep networks.pythonCopy codefrom 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'])
  • 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  
      Node.js Remove 1

      Verification

      You can check that the record having address “Ghaziabad” is deleted and only following records are available now:

      Node.js Remove 2
    1. 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 codefor layer in base_model.layers[:100]: layer.trainable = False for layer in base_model.layers[100:]: layer.trainable = True
    2. 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  
        Node.js Sorting 1

        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  
          Node.js Sorting 2
        1. 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 Filter query 1

            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  
            Node.js Filter query 2