Added Transformer support

This commit is contained in:
HowToSD
2025-03-28 19:16:55 -07:00
parent e6462b91f8
commit 312f250672
16 changed files with 826 additions and 9 deletions
+2
View File
@@ -157,6 +157,7 @@
| [Ptn Conv 2d](ptn_conv2d.md) | A convolutional model consisting of a single conv2d layer. |
| [Ptn Conv Model](ptn_conv_model.md) | A convolutional model consisting of multiple convolutional layers. |
| [Ptn Embedding](ptn_embedding.md) | Constructs an embedding layer. |
| [Ptn Embedding Transformer Linear](ptn_embedding_transformer_linear.md) | A Transformer encoder model with a linear head. |
| [Ptn GRU](ptn_gru.md) | A gated recurrent unit (GRU) model consisting of one or more of a recurrent layer. |
| [Ptn GRU Linear](ptn_gru_linear.md) | A recurrent neural network (GRU) model with a linear head. |
| [Ptn Instance Norm 2d](ptn_instance_norm2d.md) | A normalization model to normalize elements over spatial axes within each channel for each sample. |
@@ -234,6 +235,7 @@
| [Pt Predict Regression Model](pt_predict_regression_model.md) | Performs inference on input data. |
| [Pt Train Classification Model](pt_train_classification_model.md) | Trains a classification model using a given dataset, optimizer, and number of epochs. |
| [Pt Train Classification Model Lr](pt_train_classification_model_lr.md) | Trains a classification model using a given dataset, optimizer, and number of epochs with learning rate decay. |
| [Pt Train Classification Transformer Model](pt_train_classification_transformer_model.md) | Trains a classification Transformer model using a given dataset, loss function, optimizer, and number of epochs with learning rate decay. |
| [Pt Train Model](pt_train_model.md) | Trains a model using a given dataset, loss function, optimizer, and number of epochs with learning rate decay. |
| [Pt Train RNN Model](pt_train_rnn_model.md) | Trains an RNN model using a given dataset, loss function, optimizer, and number of epochs with learning rate decay. |
| [Pt Train Regression Model](pt_train_regression_model.md) | Trains a regression model using a given dataset, optimizer, and number of epochs. |
@@ -0,0 +1,44 @@
# Pt Train Classification Transformer Model
Trains a classification Transformer model using a given dataset, loss function, optimizer, and number of epochs with learning rate decay.
Args:
model (PTMODEL): The PyTorch model to be trained.
train_loader (torch.utils.data.DataLoader): DataLoader containing the training dataset.
optimizer (PTOPTIMIZER): Optimizer used for training.
loss (PTLOSS): The loss class.
epochs (int): Number of training epochs.
use_gpu (bool): Whether to use GPU for training.
early_stopping (bool): Whether to turn on early stopping
early_stopping_rounds (int): Specifies the number of rounds to monitor the evaluation loss when early_stopping is enabled and validation data is specified. If the loss does not decrease compared to the current best loss within this period, training will be stopped.
output_best_val_model (bool): Whether to outputs the model with the best val loss or the model from the last epoch.
classification_metrics (bool): If True and if val_loader is set, print out classification metrics. This is only valid for classification model training.
val_loader (PTDATALOADER): DataLoader for validation dataset (optional).
scheduler (PTLRSCHEDULER): Learning rate scheduler.
## Input
| Name | Data type |
|---|---|
| model | Ptmodel |
| train_loader | Ptdataloader |
| optimizer | Ptoptimizer |
| loss_function | Ptloss |
| epochs | Int |
| use_gpu | Boolean |
| early_stopping | Boolean |
| early_stopping_rounds | Int |
| output_best_val_model | Boolean |
| classification_metrics | Boolean |
| scheduler | Ptlrscheduler |
| val_loader | Ptdataloader |
## Output
| Data type |
|---|
| Ptmodel |
| Tensor |
| Tensor |
<HR>
Category: PyTorch wrapper - Training
ComfyUI Pt Wrapper Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved.
@@ -0,0 +1,46 @@
# Ptn Embedding Transformer Linear
A Transformer encoder model with a linear head.
Args:
num_layers (int): Number of encoder layers in Transformer.
vocabulary_size (int): The vocabulary size of the tokens.
hidden_size (int): Hidden dimension (d_model) for each token of the Transformer.
nhead (int): Number of attention heads.
dim_feedforward (int): Feedforward dimension.
dropout (float): Dropout rate for Transformer layers.
nonlinearity (str): Activation function ('relu' or 'gelu') used in Transformer layers.
layer_norm_eps (float): Epsilon value for layer normalization.
batch_first (bool): Input shape format: True for (B, Seq, Feature), False for (Seq, B, Feature).
norm_first (bool): Whether to apply normalization before attention/feedforward.
bias (bool): Use bias in the Transformer layers.
max_length (int): Maximum input sequence length (for positional embedding).
linear_output_size (int): Output size of the final linear layer.
linear_bias (bool): Use bias in the linear layer.
## Input
| Name | Data type |
|---|---|
| num_layers | Int |
| vocabulary_size | Int |
| hidden_size | Int |
| nhead | Int |
| dim_feedforward | Int |
| dropout | Float |
| nonlinearity | |
| layer_norm_eps | Float |
| batch_first | Boolean |
| norm_first | Boolean |
| bias | Boolean |
| max_length | Int |
| linear_output_size | Int |
| linear_bias | Boolean |
## Output
| Data type |
|---|
| Ptmodel |
<HR>
Category: PyTorch wrapper - Model
ComfyUI Pt Wrapper Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved.
@@ -7,6 +7,7 @@ A PyTorch Dataset class wrapper for a Hugging Face dataset that converts text to
sample_field_name (str): Field name for text samples.
label_field_name (str): Field name for labels.
encode (PTCALLABLE): The reference to a token encoder function.
remove_html_tags (bool): Remove html tags in text if True.
## Input
| Name | Data type |
@@ -16,6 +17,7 @@ A PyTorch Dataset class wrapper for a Hugging Face dataset that converts text to
| sample_field_name | String |
| label_field_name | String |
| encode | Ptcallable |
| remove_html_tags | Boolean |
## Output
| Data type |