liveBook · Manning

PyTorch

As explained in Fight Fraud with Machine Learning

PyTorch is an open-source machine-learning framework providing tensor computation and automatic differentiation for building and training neural networks.

PyTorch is a deep-learning library for building, training, evaluating, monitoring, and deploying neural-network models. In fraud detection, it complements classical machine-learning libraries and competes with TensorFlow across the deep-learning workflow. section 4.2.2

A PyTorch neural network typically involves:

  • Defining a Python class that inherits from nn.Module. section 4.2.2
  • Creating layers in the constructor, including Linear transformations and activation functions such as Sigmoid. section 4.2.2
  • Implementing forward to pass input data through the ordered layer collection. section 4.2.2

A compact fraud-model architecture

An example defines eight input features, three hidden layers of nine units each, and one sigmoid output. The layers are stored in an nn.ModuleList, alternating Linear and Sigmoid components. section 4.2.2

import torch
import torch.nn as nn
 
# Define the architecture of the neural network
input_size = 8
hidden_layer_sizes = [9, 9, 9]
output_size = 1
 
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.layers = nn.ModuleList()
        
        # Add the input layer
        self.layers.append(nn.Linear(input_size, hidden_layer_sizes[0]))
        self.layers.append(nn.Sigmoid())
        
        # Add three hidden layers with sigmoid activation
        for i in range(len(hidden_layer_sizes) - 1):
            self.layers.append(nn.Linear(hidden_layer_sizes[i], hidden_layer_sizes[i+1]))
            self.layers.append(nn.Sigmoid())
        
        # Add the output layer with sigmoid activation
        self.layers.append(nn.Linear(hidden_layer_sizes[-1], output_size))
        self.layers.append(nn.Sigmoid())
        
    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x
 
# Instantiate the neural network model
model = NeuralNetwork()
 
# Print the model architecture
print(model)

Inspect the PyTorch listing for the class-based architecture, layer construction, and forward-pass structure. section 4.2.2

How Fight Fraud with Machine Learning covers it

How deep it goes
Defines class-based architectures, layers, and forward execution, but supplied evidence does not show complete training or evaluation code.
Examples
Uses an eight-input, three-hidden-layer sigmoid network represented through Linear and Sigmoid layers.
What you can do after
Build it from scratch as a PyTorch neural-network class using nn.Module, ModuleList, Linear, Sigmoid, and forward.
How it connects
Connects neural networks with TensorFlow, Keras, ONNX, and classical machine-learning libraries.

Also explained in

Compare how 14 books explain PyTorch →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fight Fraud with Machine Learning. The section links point to the parts of the book it draws on.