-
Notifications
You must be signed in to change notification settings - Fork 2
Linear Regression Tool for Autodesk Maya
Welcome to the Linear Regression Tool! This tool allows you to train a Linear Regression model with Elastic Net regularization directly in Autodesk Maya and generate a custom node that embeds the trained model for real-time inference.
-
Attribute-Based Training:
- Select any attributes in your Maya scene as inputs and targets.
- The tool will train a model to predict the target attributes based on the input attributes.
-
Random or Current Animation Data:
- Use existing animation curves in your Maya scene.
- Generate random animation data to expand your dataset.
-
Elastic Net Regularization:
- Combines L1 and L2 penalties during training.
- Parameters like
alpha
,L1 ratio
, andtolerance
are configurable.
-
Normalization:
- Automatically normalizes input and output data.
- Stores the mean and standard deviation in the custom node for consistent runtime normalization.
-
Custom Node Creation:
- Generates a lightweight, custom linear regression node.
- The trained weights, bias, and normalization parameters are automatically set in the node.
Linear Regression is a simple and widely used machine learning algorithm for modeling the relationship between a dependent variable (target) and one or more independent variables (inputs). It works by fitting a linear equation to the observed data:
Here:
-
$y$ : Predicted output (target attribute). -
$x_1, x_2, \dots, x_n$ : Input attributes. -
$w_1, w_2, \dots, w_n$ : Weights (learned parameters). -
$b$ : Bias term (intercept).
The algorithm minimizes the error between predicted and actual values, usually measured using Mean Squared Error (MSE).
Regularization is a technique used to prevent overfitting by adding a penalty to the model's complexity. Elastic Net combines two types of regularization:
L1 adds the absolute value of weights as a penalty term to the loss function:
- Encourages sparsity by setting some weights to zero.
- Useful for feature selection.
L2 adds the square of the weights as a penalty term to the loss function:
- Encourages smaller weights but does not eliminate them.
- Helps distribute the influence among features.
Elastic Net combines L1 and L2 regularization:
- Balances feature selection (L1) and weight distribution (L2).
- Controlled by the
L1 Ratio
parameter.
By using Elastic Net, this tool achieves a balance between simplicity (sparse weights) and stability (reduced overfitting).
Normalization scales input data to have a mean of 0 and a standard deviation of 1. This process ensures that all features contribute equally to the training process, regardless of their original scale. The formula for normalization is:
Here:
-
$x$ : Original data value. -
$\mu$ : Mean of the data. -
$\sigma$ : Standard deviation of the data.
For denormalizing the output, the process is reversed:
- Improved Convergence: Many optimization algorithms, like gradient descent, converge faster when features are on similar scales.
- Equal Contribution: Prevents features with larger scales from dominating the training process.
- Numerical Stability: Reduces the risk of numerical issues caused by very large or small feature values.
By storing the mean and standard deviation in the custom node, the tool ensures that data is consistently normalized during inference, leading to accurate predictions.
- Clone the repository:
git clone https://github.com/yourusername/ml-example-nodes.git
- Copy the Python files to your Maya scripts directory, or Add Repository to System Path using
sys.path.append
.import sys sys.path.append('path/to/your/repo')
- Open Maya and load the script editor.
- To open the Regression UI execute:
import nodes_ui
nodes_ui.regrssion_ui()
- Select the input attributes, set the output attribute, and create the Regression node.
- Add attributes from your scene to serve as inputs.
- Multiple attributes can be selected.
- Define the attributes you want to predict using the model.
- Use Current Animation: Utilize existing animation curves in the scene.
-
Generate Random Animation: Automatically create randomized animation data.
- Specify the number of frames and value range.
- Alpha: Controls the strength of regularization.
- L1 Ratio: Determines the balance between L1 and L2 penalties.
- Tolerance: Sets the convergence threshold.
- Max Iterations: Specifies the maximum number of iterations for training.
- Enable output node duplication for debugging purposes.
- Start the training process.
- Progress and results are displayed in the output window.
- Launch the tool and open the UI.
- Select the input and target attributes.
- Choose whether to use current animation data or generate random data.
- Set the training parameters (optional).
- Click "Train" to train the model.
- A custom linear regression node is created and linked to your scene.
The generated node contains:
- Input connections: The input attributes are connected as source for the node features.
- Output connections: The target attributes are connected as destination for the node output values.
- Weights and Bias: Stored learned parameters from the trained model.
- Normalization Parameters: Mean and standard deviation for input normalization and output denormalization.
- Select
pSphere1.translateY
andpSphere1.translateZ
as input attributes. - Select
locator1.translateZ
as the target attribute. - Train the model and observe how the locator's Z-translation is predicted based on the sphere's translations.
- Generate random animation data with a range of -50 to 50.
- Train the model and apply it to custom attributes.
-
Model Not Converging: Increase
Max Iterations
or adjustTolerance
. -
Overfitting: Increase the
Alpha
value or adjust theL1 Ratio
for stronger regularization.
This tool is open-source and available under the MIT License. Contributions are welcome!
If you encounter issues or have suggestions, feel free to create an issue on GitHub or contact me directly on Blue Sky