How can I visualize a neural network?

166    Asked by DipikaAgarwal in Data Science , Asked on Jul 17, 2024

 I am currently working on a particular task that is related to analyzing q Neural network behavior for anomaly detection in financial transactions. How should I visualize the network’s decision-making process to identify the key features contributing to classifying transactions as normal or anomalous? 

Answered by David Piper

In the context of data science, You can visualize the neural networks' decision-making process for the classification of financial transactions as normal or anomalous by using the Gradient Weighted Class Activation Mapping. It can provide a heat map that would highlight the region in the input which is very important for the network decision. Here are the steps given:-

Forward pass

You can pass the input transaction by the network so that you can obtain the output classification score.

Gradient calculation

You can compute the gradient of the classification score to the activation of the fine Convolutions layer.

Weight generation

You can compute the weighted sum of the feature maps and can apply a ReLU activation to generate the heat map.

Overlay heat map

Overlay the heat map on the input data so that you can visualize which features would contribute to the decision making by the network.

Here is a Python based code given by using PyTorch:-

Import torch
Import torch.nn.functional as F
From torchvision import models
Import cv2
Import numpy as np
# Load pre-trained model
Model = models.resnet50(pretrained=True)
Model.eval()
# Define Grad-CAM class
Class GradCAM:
    Def __init__(self, model, target_layer):
        Self.model = model
        Self.target_layer = target_layer
        Self.gradients = None
        Self.activations = None
        Self.hook_layers()
    Def hook_layers(self):
        Def forward_hook(module, input, output):
            Self.activations = output.detach()
        Def backward_hook(module, grad_in, grad_out):
            Self.gradients = grad_out[0].detach()
        Self.target_layer.register_forward_hook(forward_hook)
        Self.target_layer.register_backward_hook(backward_hook)
    Def generate_heatmap(self, input_data, target_class):
        Output = self.model(input_data)
        Self.model.zero_grad()
        Target = output[0][target_class]
        Target.backward()
        Gradients = self.gradients.mean(dim=[2, 3])
        Activations = self.activations
        Weights = gradients.unsqueeze(-1).unsqueeze(-1)
        Weighted_sum = (weights * activations).sum(dim=1)
        Heatmap = F.relu(weighted_sum).squeeze().cpu().numpy()
        Heatmap = cv2.resize(heatmap, (input_data.size(-1), input_data.size(-2)))
        Heatmap = (heatmap – heatmap.min()) / (heatmap.max() – heatmap.min())
        Return heatmap
# Example usage
Input_data = torch.rand(1, 3, 224, 224) # Example input
Target_layer = model.layer4[2].conv3 # Target layer
Grad_cam = GradCAM(model, target_layer)
# Assume class 0 is the target for normal transactions
Heatmap = grad_cam.generate_heatmap(input_data, target_class=0)
# Visualization
Heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)
Input_image = input_data.squeeze().permute(1, 2, 0).cpu().numpy()
Overlayed_img = cv2.addWeighted(input_image, 0.6, heatmap, 0.4, 0)
Cv2.imshow(“Grad-CAM Heatmap”, overlayed_img)
Cv2.waitKey(0)
Here is the java based approach given:-
Import org.deeplearning4j.nn.api.Layer;
Import org.deeplearning4j.nn.graph.ComputationGraph;
Import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
Import org.nd4j.linalg.api.ndarray.INDArray;
Import org.nd4j.linalg.dataset.DataSet;
Import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
Import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
Import org.nd4j.linalg.factory.Nd4j;
Import java.io.File;
Import java.io.IOException;
Public class GradCAMExample {
    Public static void main(String[] args) throws IOException {
        // Load pre-trained model
        String modelPath = “path/to/model.h5”;
        ComputationGraph model = KerasModelImport.importKerasModelAndWeights(modelPath);
        // Load input data
        DataSetIterator dataSetIterator = getDataSetIterator(“path/to/images”);
        DataSet dataSet = dataSetIterator.next();
        INDArray input = dataSet.getFeatures();
        // Preprocess input
        ImagePreProcessingScaler preProcessor = new ImagePreProcessingScaler(0, 1);
        preProcessor.transform(input);
        // Forward pass
        INDArray[] activations = model.feedForward(input, false).values().toArray(new INDArray[0]);
        INDArray targetActivations = activations[activations.length – 1];
        INDArray gradients = model.gradient().gradient();
        // Generate Grad-CAM heatmap
        INDArray weights = gradients.mean(0);
        INDArray cam = targetActivations.mmul(weights);
        Cam = Nd4j.relu(cam);
        // Visualize heatmap (use visualization library here)
    }
    Private static DataSetIterator getDataSetIterator(String path) {
        // Implement data loading logic
        Return null;
    }
}

Your Answer

Interviews

Parent Categories