- Check the architecture and layer-wise summary of your model with
model.summary().pythonCopy codefrom tensorflow.keras.utils import plot_model plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
Author: saqibkhan
-
Use Model.summary() and plot_model for Insights
-
Save and Load Models
- You can save your model during and after training:pythonCopy code
model.save('model.h5') # Loading the model from tensorflow.keras.models import load_model model = load_model('model.h5')
- You can save your model during and after training:pythonCopy code
-
Custom Loss Functions and Metrics
- You can define your own loss function or metrics to suit specific tasks:pythonCopy code
import tensorflow.keras.backend as K def custom_loss(y_true, y_pred): return K.mean(K.square(y_pred - y_true)) model.compile(optimizer='adam', loss=custom_loss, metrics=['accuracy'])
- You can define your own loss function or metrics to suit specific tasks:pythonCopy code
-
MongoDB Select Record
The findOne() method is used to select a single data from a collection in MongoDB. This method returns the first record of the collection.
Example
(Select Single Record)
Select the first record from the ?employees? collection.
Create a js file named “select.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; db.collection("employees").findOne({}, function(err, result) { if (err) throw err; console.log(result.name); db.close(); }); });Open the command terminal and run the following command:
Node select.js
Select Multiple Records
The find() method is used to select all the records from collection in MongoDB.
Example
Select all the records from “employees” collection.
Create a js file named “selectall.js”, having the following code:
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; db.collection("employees").find({}).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });Open the command terminal and run the following command:
Node selectall.js
-
Transfer Learning
- If you don’t have enough data, consider using transfer learning with a pretrained model.
- Freeze the layers of the base model:pythonCopy code
for layer in base_model.layers: layer.trainable = False
- Freeze the layers of the base model:pythonCopy code
- If you don’t have enough data, consider using transfer learning with a pretrained model.
-
MongoDB Insert Record
The insertOne method is used to insert record in MongoDB’s collection. The first argument of the insertOne method is an object which contains the name and value of each field in the record you want to insert.
Example
(Insert Single record)
Insert a record in “employees” collection.
Create a js file named “insert.js”, having the following code:
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var myobj = { name: "Ajeet Kumar", age: "28", address: "Delhi" }; db.collection("employees").insertOne(myobj, function(err, res) { if (err) throw err; console.log("1 record inserted"); db.close(); }); });Open the command terminal and run the following command:
Node insert.js
Now a record is inserted in the collection.
Insert Multiple Records
You can insert multiple records in a collection by using insert() method. The insert() method uses array of objects which contain the data you want to insert.
Example
Insert multiple records in the collection named “employees”.
Create a js file name insertall.js, having the following code:
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var myobj = [ { name: "Mahesh Sharma", age: "25", address: "Ghaziabad"}, { name: "Tom Moody", age: "31", address: "CA"}, { name: "Zahira Wasim", age: "19", address: "Islamabad"}, { name: "Juck Ross", age: "45", address: "London"} ]; db.collection("customers").insert(myobj, function(err, res) { if (err) throw err; console.log("Number of records inserted: " + res.insertedCount); db.close(); }); });Open the command terminal and run the following command:
Node insertall.js
-
Experiment with Optimizers
- The choice of optimizer can impact your model’s performance. Besides
Adam, try usingRMSprop,SGD, orNadam.pythonCopy codefrom tensorflow.keras.optimizers import RMSprop model.compile(optimizer=RMSprop(learning_rate=0.001), loss='categorical_crossentropy',
- The choice of optimizer can impact your model’s performance. Besides
-
Use TensorBoard for Visualization
- TensorBoard helps you visualize the training process, track loss, and monitor metrics.pythonCopy code
from tensorflow.keras.callbacks import TensorBoard tensorboard = TensorBoard(log_dir='./logs') model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=50, callbacks=[tenso
- TensorBoard helps you visualize the training process, track loss, and monitor metrics.pythonCopy code
-
MongoDB Create Collection
MongoDB is a NoSQL database so data is stored in collection instead of table. createCollection method is used to create a collection in MongoDB.
Example
Create a collection named “employees”.
Create a js file named “employees.js”, having the following data:
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; db.createCollection("employees", function(err, res) { if (err) throw err; console.log("Collection is created!"); db.close(); }); });Open the command terminal and run the following command:
Node employees.js
-
Optimize Batch Size and Learning Rate
- Batch size and learning rate significantly affect model performance. Tune these parameters carefully.
- Start with a small learning rate and experiment with different batch sizes (commonly 32, 64, or 128).