Class NeuralNetworkHelper<T>
Provides helper methods for neural network operations including activation functions and loss functions.
public static class NeuralNetworkHelper<T>
Type Parameters
TThe numeric type used in calculations (e.g., float, double).
- Inheritance
-
NeuralNetworkHelper<T>
- Inherited Members
Remarks
For Beginners: Neural networks are computing systems inspired by the human brain. They process information through interconnected nodes (neurons) that transform input data using mathematical functions. This helper class provides those mathematical functions needed to build neural networks.
Methods
ApplyActivation(Tensor<T>, IActivationFunction<T>?, IVectorActivationFunction<T>?)
Applies an activation function to a tensor of values.
public static Tensor<T> ApplyActivation(Tensor<T> input, IActivationFunction<T>? scalarActivation = null, IVectorActivationFunction<T>? vectorActivation = null)
Parameters
inputTensor<T>The input tensor to apply the activation function to.
scalarActivationIActivationFunction<T>An optional scalar activation function.
vectorActivationIVectorActivationFunction<T>An optional vector activation function.
Returns
- Tensor<T>
A new tensor with the activation function applied.
Remarks
This method flattens the input tensor to a vector, applies the specified activation function, and then reconstructs the result as a tensor. It only works with rank-1 tensors (vectors).
For Beginners: This method is similar to the vector version above, but it works with tensors. A tensor is a more general mathematical object that can represent multi-dimensional data. In this case, we're working with a specific type of tensor that's essentially a vector (a list of numbers). The method converts the tensor to a vector, applies the activation function, and then converts it back to a tensor.
Exceptions
- ArgumentException
Thrown when the input tensor is not rank-1 (vector).
ApplyActivation(Vector<T>, IActivationFunction<T>?, IVectorActivationFunction<T>?)
Applies an activation function to a vector of values.
public static Vector<T> ApplyActivation(Vector<T> input, IActivationFunction<T>? scalarActivation = null, IVectorActivationFunction<T>? vectorActivation = null)
Parameters
inputVector<T>The input vector to apply the activation function to.
scalarActivationIActivationFunction<T>An optional scalar activation function.
vectorActivationIVectorActivationFunction<T>An optional vector activation function.
Returns
- Vector<T>
A new vector with the activation function applied.
Remarks
This method applies either a vector activation function, a scalar activation function, or no activation (identity) to the input vector, based on the provided parameters.
For Beginners: Activation functions are mathematical operations applied to the output of a neuron in a neural network. They introduce non-linearity, allowing the network to learn complex patterns. This method lets you apply different types of activation functions to a set of numbers: - Vector activation: applies the function to the whole set at once - Scalar activation: applies the function to each number individually - If no activation is specified, it returns the original numbers unchanged
ApplyOutputActivation(Tensor<T>, NeuralNetworkArchitecture<T>)
Applies the appropriate activation function to the output tensor based on the task type.
public static void ApplyOutputActivation(Tensor<T> output, NeuralNetworkArchitecture<T> architecture)
Parameters
outputTensor<T>The output tensor to apply activation to.
architectureNeuralNetworkArchitecture<T>
EuclideanDistance(Vector<T>, Vector<T>)
Calculates the Euclidean distance between two vectors.
public static T EuclideanDistance(Vector<T> v1, Vector<T> v2)
Parameters
v1Vector<T>The first vector.
v2Vector<T>The second vector.
Returns
- T
A scalar value representing the Euclidean distance.
Remarks
For Beginners: Euclidean distance is the straight-line distance between two points in space. Think of it as measuring the length of a ruler placed between two points. This is used in many machine learning algorithms to measure how different two data points are.
GetDefaultActivationFunction(NeuralNetworkTaskType)
Gets the default activation function based on the task type.
public static IActivationFunction<T> GetDefaultActivationFunction(NeuralNetworkTaskType taskType)
Parameters
taskTypeNeuralNetworkTaskTypeThe neural network task type.
Returns
- IActivationFunction<T>
An appropriate activation function for the task type.
GetDefaultLossFunction(NeuralNetworkTaskType)
Gets the default loss function based on the task type.
public static ILossFunction<T> GetDefaultLossFunction(NeuralNetworkTaskType taskType)
Parameters
taskTypeNeuralNetworkTaskTypeThe neural network task type.
Returns
- ILossFunction<T>
An appropriate loss function for the task type.
GetDefaultVectorActivationFunction(NeuralNetworkTaskType)
Gets the default vector activation function based on the task type.
public static IVectorActivationFunction<T> GetDefaultVectorActivationFunction(NeuralNetworkTaskType taskType)
Parameters
taskTypeNeuralNetworkTaskTypeThe neural network task type.
Returns
- IVectorActivationFunction<T>
An appropriate vector activation function for the task type.