Table of Contents

Class OccupancyNeuralNetwork<T>

Namespace
AiDotNet.NeuralNetworks
Assembly
AiDotNet.dll

Represents a Neural Network specialized for occupancy detection and prediction in spaces.

public class OccupancyNeuralNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations (typically float or double).

Inheritance
OccupancyNeuralNetwork<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

An Occupancy Neural Network processes sensor data to detect and predict the presence and number of people in a given space. It can handle various types of sensor inputs including temperature, humidity, CO2 levels, motion detection, and other environmental factors.

For Beginners: Think of this network as a smart system that can "understand" when people are present in a room or building by analyzing data from various sensors. Just like you might determine if someone is in a room by noticing changes in temperature, sounds, or movement, this neural network learns patterns in sensor data that indicate human presence. It's particularly useful for smart buildings, energy management, security systems, and space utilization analysis.

Constructors

OccupancyNeuralNetwork(NeuralNetworkArchitecture<T>, bool, int, ILossFunction<T>?)

Initializes a new instance of the OccupancyNeuralNetwork class.

public OccupancyNeuralNetwork(NeuralNetworkArchitecture<T> architecture, bool includeTemporalData = false, int historyWindowSize = 5, ILossFunction<T>? lossFunction = null)

Parameters

architecture NeuralNetworkArchitecture<T>

The architecture defining the structure of the neural network.

includeTemporalData bool

Whether to include temporal data processing capabilities.

historyWindowSize int

The number of previous time steps to include when using temporal data.

lossFunction ILossFunction<T>

Remarks

Creates an occupancy neural network with the specified architecture and temporal settings. The input type is validated based on whether temporal data is included.

For Beginners: This constructor sets up your occupancy detection network based on your specifications. You provide the network's blueprint (architecture), tell it whether to analyze patterns over time, and if so, how many previous readings to consider. The constructor makes sure your input data format matches what the network expects. For example, if you want to include time patterns but provide only single-moment data, it will alert you that there's a mismatch.

Exceptions

InvalidInputTypeException

Thrown when the input type does not match the required type based on configuration.

Properties

HistoryWindowSize

Gets the size of the time window used for temporal data.

public int HistoryWindowSize { get; }

Property Value

int

IncludesTemporalData

Gets a value indicating whether this network processes temporal data.

public bool IncludesTemporalData { get; }

Property Value

bool

Methods

CreateNewInstance()

Creates a new instance of the OccupancyNeuralNetwork with the same architecture and temporal configuration.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance of the occupancy neural network.

Remarks

This method creates a new occupancy neural network with the same architecture and temporal data processing configuration as the current instance. The new instance starts with fresh layers and an empty sensor history buffer, making it useful for creating multiple networks with identical configurations or for resetting a network while preserving its structure.

For Beginners: This creates a brand new occupancy detection network with the same settings.

Think of it like creating a copy of your current network's blueprint:

  • It has the same structure (layers and neurons)
  • It uses the same approach to time-based analysis (if enabled)
  • It looks at the same number of past readings when analyzing patterns

However, the new network starts fresh with:

  • Newly initialized weights and parameters
  • An empty history buffer (no past sensor readings)

This is useful when you want to start over with a clean network that has the same design but hasn't learned anything yet, or when you need multiple identical networks for different spaces or comparison purposes.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes network-specific data from a binary reader.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

The binary reader to read from.

Remarks

This method loads occupancy network-specific data from the binary stream, such as temporal configuration settings.

For Beginners: This method loads the previously saved special information about your occupancy detection network from a file. It restores settings like whether the network analyzes patterns over time and how many past readings it considers. This allows you to continue using a network exactly where you left off, with all its settings and internal state intact.

ForwardTemporal(Tensor<T>)

Performs a forward pass through the network for temporal data.

public Tensor<T> ForwardTemporal(Tensor<T> input)

Parameters

input Tensor<T>

The input tensor to process with shape [batchSize, timeSteps, features].

Returns

Tensor<T>

The output tensor after processing through all layers.

Exceptions

TensorShapeMismatchException

Thrown when the input shape doesn't match the expected shape.

GetModelMetadata()

Gets metadata about the occupancy neural network.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the network.

Remarks

This method returns comprehensive metadata about the occupancy neural network, including its architecture, temporal configuration, and other relevant parameters.

For Beginners: This provides detailed information about your occupancy detection network. It includes data like: - The network's structure (layers, neurons, etc.) - Whether it analyzes patterns over time - How many past readings it considers for temporal analysis - The total number of internal parameters

This information is useful for documentation, comparing different network configurations, or debugging issues with the network's performance.

InitializeLayers()

Initializes the layers of the neural network based on the provided architecture.

protected override void InitializeLayers()

Remarks

This method either uses custom layers provided in the architecture or creates default layers appropriate for occupancy detection. If temporal data is included, the default configuration will include recurrent layers to capture time-series patterns.

For Beginners: This method sets up the different processing stages (layers) of your occupancy detection network. If you've specified custom layers in your architecture, it uses those. If not, it creates a standard set of layers that work well for occupancy prediction.

When temporal data is included, it adds special layers (called recurrent layers) that can "remember" past data and detect patterns over time - like noticing gradual changes in CO2 levels when people enter a room. Think of this as either using your specific recipe for the network or falling back to a proven recipe that works well for most occupancy detection tasks.

Predict(Tensor<T>)

Makes a prediction using the occupancy neural network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input tensor to process.

Returns

Tensor<T>

The output tensor after processing through all layers.

Remarks

This method processes input data through the network to predict occupancy. If temporal data is enabled, it will ensure the input has the appropriate format before processing.

For Beginners: This method takes your sensor data and produces an occupancy prediction. Depending on how your network is configured, it will either:

  1. Process a single moment's sensor readings (like current temperature, CO2, etc.)
  2. Process a sequence of readings over time to detect temporal patterns

The output typically indicates how many people are in the space or the probability of the space being occupied. This method handles all the complex mathematical transformations needed to convert raw sensor data into meaningful occupancy information.

SerializeNetworkSpecificData(BinaryWriter)

Serializes network-specific data to a binary writer.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

The binary writer to write to.

Remarks

This method saves occupancy network-specific data to the binary stream, such as temporal configuration settings.

For Beginners: This method saves special information about your occupancy detection network to a file. This includes settings like whether it analyzes patterns over time and how many past readings it considers. Saving this information allows you to later reload the network exactly as it was configured.

Train(Tensor<T>, Tensor<T>)

Trains the neural network on sensor data and occupancy labels.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input tensor containing sensor data.

expectedOutput Tensor<T>

The expected output tensor containing occupancy information.

Remarks

This method trains the occupancy neural network by comparing its predictions with expected occupancy values. It handles both temporal and non-temporal training data formats.

For Beginners: This method teaches the network to accurately detect occupancy by showing it examples of sensor readings and the correct occupancy levels.

The training process follows these steps:

  1. The network processes the sensor readings
  2. Its prediction is compared to the actual occupancy value
  3. The difference (error) is used to adjust the network's internal settings
  4. This process repeats with many examples until the network becomes accurate

The network automatically handles different data formats depending on whether it's configured to analyze patterns over time (temporal) or just current readings. Over time, the network becomes better at recognizing the subtle patterns in sensor data that indicate human presence.

UpdateParameters(Vector<T>)

Updates the parameters of the neural network based on computed gradients.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

UpdatePrediction(Vector<T>, Queue<Vector<T>>?)

Processes a new sensor reading and updates the prediction for real-time occupancy detection.

public Vector<T> UpdatePrediction(Vector<T> currentReading, Queue<Vector<T>>? sensorHistory = null)

Parameters

currentReading Vector<T>

The latest sensor reading as a vector.

sensorHistory Queue<Vector<T>>

Optional buffer of previous readings for temporal processing.

Returns

Vector<T>

The updated occupancy prediction.

Exceptions

InvalidOperationException

Thrown when temporal data is required but no history is provided.