From 912049de390e3b1c5ec2ffcf2c89aa9b0c27c2c1 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 8 Oct 2025 15:08:03 +0100 Subject: [PATCH 01/30] Added autogenerated classes which create observable streams of TorchSharp modules --- src/Bonsai.ML.Torch/NeuralNets/Backward.cs | 28 ++--- .../NeuralNets/CreateSequential.cs | 67 ++++++++++++ src/Bonsai.ML.Torch/NeuralNets/Forward.cs | 2 +- .../NeuralNets/ITorchModule.cs | 22 ---- ...chitecture.cs => LoadModelArchitecture.cs} | 7 +- src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs | 43 ++++++++ .../NeuralNets/LoadScriptModule.cs | 6 +- src/Bonsai.ML.Torch/NeuralNets/Loss.cs | 13 --- .../NeuralNets/Losses/BCELossModule.cs | 46 ++++++++ .../Losses/BCEWithLogitsLossModule.cs | 54 ++++++++++ .../NeuralNets/Losses/CTCLossModule.cs | 45 ++++++++ .../Losses/CosineEmbeddingLossModule.cs | 39 +++++++ .../Losses/CrossEntropyLossModule.cs | 52 +++++++++ .../Losses/GaussianNLLLossModule.cs | 45 ++++++++ .../Losses/HingeEmbeddingLossModule.cs | 39 +++++++ .../NeuralNets/Losses/HuberLossModule.cs | 39 +++++++ .../NeuralNets/Losses/KLDivLossModule.cs | 39 +++++++ .../NeuralNets/Losses/L1LossModule.cs | 33 ++++++ .../NeuralNets/Losses/MSELossModule.cs | 33 ++++++ .../Losses/MarginRankingLossModule.cs | 39 +++++++ .../Losses/MultiLabelMarginLossModule.cs | 33 ++++++ .../Losses/MultiLabelSoftMarginLossModule.cs | 46 ++++++++ .../Losses/MultiMarginLossModule.cs | 58 ++++++++++ .../NeuralNets/Losses/NLLLossModule.cs | 46 ++++++++ .../NeuralNets/Losses/PoissonNLLLossModule.cs | 51 +++++++++ .../NeuralNets/Losses/SmoothL1LossModule.cs | 39 +++++++ .../NeuralNets/Losses/SoftMarginLossModule.cs | 33 ++++++ .../Losses/TripletMarginLossModule.cs | 57 ++++++++++ .../Modules/AdaptiveAvgPool1dModule.cs | 33 ++++++ .../Modules/AdaptiveAvgPool2dModule.cs | 34 ++++++ .../Modules/AdaptiveAvgPool3dModule.cs | 34 ++++++ .../Modules/AdaptiveMaxPool1dModule.cs | 33 ++++++ .../Modules/AdaptiveMaxPool2dModule.cs | 34 ++++++ .../Modules/AdaptiveMaxPool3dModule.cs | 34 ++++++ .../NeuralNets/Modules/AlphaDropoutModule.cs | 39 +++++++ .../NeuralNets/Modules/AvgPool1dModule.cs | 57 ++++++++++ .../NeuralNets/Modules/AvgPool2dModule.cs | 66 ++++++++++++ .../NeuralNets/Modules/AvgPool3dModule.cs | 66 ++++++++++++ .../NeuralNets/Modules/BatchNorm1dModule.cs | 71 ++++++++++++ .../NeuralNets/Modules/BatchNorm2dModule.cs | 71 ++++++++++++ .../NeuralNets/Modules/BatchNorm3dModule.cs | 71 ++++++++++++ .../NeuralNets/Modules/CELUModule.cs | 39 +++++++ .../Modules/ChannelShuffleModule.cs | 33 ++++++ .../NeuralNets/Modules/ConstantPad1dModule.cs | 39 +++++++ .../NeuralNets/Modules/ConstantPad2dModule.cs | 39 +++++++ .../NeuralNets/Modules/ConstantPad3dModule.cs | 39 +++++++ .../NeuralNets/Modules/Conv1dModule.cs | 95 ++++++++++++++++ .../NeuralNets/Modules/Conv2dModule.cs | 95 ++++++++++++++++ .../NeuralNets/Modules/Conv3dModule.cs | 95 ++++++++++++++++ .../Modules/ConvTranspose1dModule.cs | 101 ++++++++++++++++++ .../Modules/ConvTranspose2dModule.cs | 101 ++++++++++++++++++ .../Modules/ConvTranspose3dModule.cs | 101 ++++++++++++++++++ .../NeuralNets/Modules/Dropout1dModule.cs | 39 +++++++ .../NeuralNets/Modules/Dropout2dModule.cs | 39 +++++++ .../NeuralNets/Modules/Dropout3dModule.cs | 39 +++++++ .../NeuralNets/Modules/DropoutModule.cs | 39 +++++++ .../NeuralNets/Modules/ELUModule.cs | 39 +++++++ .../Modules/EmbeddingFromPretrainedModule.cs | 85 +++++++++++++++ .../NeuralNets/Modules/EmbeddingModule.cs | 83 ++++++++++++++ .../Modules/FeatureAlphaDropoutModule.cs | 33 ++++++ .../NeuralNets/Modules/FlattenModule.cs | 39 +++++++ .../NeuralNets/Modules/FoldModule.cs | 57 ++++++++++ .../Modules/FractionalMaxPool2dModule.cs | 45 ++++++++ .../Modules/FractionalMaxPool3dModule.cs | 45 ++++++++ .../NeuralNets/Modules/GELUModule.cs | 27 +++++ .../NeuralNets/Modules/GLUModule.cs | 33 ++++++ .../NeuralNets/Modules/GroupNormModule.cs | 65 +++++++++++ .../NeuralNets/Modules/HardshrinkModule.cs | 33 ++++++ .../NeuralNets/Modules/HardsigmoidModule.cs | 33 ++++++ .../NeuralNets/Modules/HardswishModule.cs | 33 ++++++ .../NeuralNets/Modules/HardtanhModule.cs | 45 ++++++++ .../NeuralNets/Modules/IdentityModule.cs | 27 +++++ .../Modules/InstanceNorm1dModule.cs | 71 ++++++++++++ .../Modules/InstanceNorm2dModule.cs | 71 ++++++++++++ .../Modules/InstanceNorm3dModule.cs | 71 ++++++++++++ .../NeuralNets/Modules/LPPool1dModule.cs | 51 +++++++++ .../NeuralNets/Modules/LPPool2dModule.cs | 53 +++++++++ .../NeuralNets/Modules/LayerNormModule.cs | 66 ++++++++++++ .../NeuralNets/Modules/LeakyReLUModule.cs | 39 +++++++ .../NeuralNets/Modules/LinearModule.cs | 59 ++++++++++ .../Modules/LocalResponseNormModule.cs | 51 +++++++++ .../NeuralNets/Modules/LogSigmoidModule.cs | 27 +++++ .../NeuralNets/Modules/LogSoftmaxModule.cs | 33 ++++++ .../NeuralNets/Modules/MaxPool1dModule.cs | 57 ++++++++++ .../NeuralNets/Modules/MaxPool2dModule.cs | 57 ++++++++++ .../NeuralNets/Modules/MaxPool3dModule.cs | 57 ++++++++++ .../NeuralNets/Modules/MishModule.cs | 27 +++++ .../NeuralNets/Modules/PReLUModule.cs | 53 +++++++++ .../NeuralNets/Modules/PixelShuffleModule.cs | 33 ++++++ .../Modules/PixelUnshuffleModule.cs | 33 ++++++ .../NeuralNets/Modules/RReLUModule.cs | 45 ++++++++ .../NeuralNets/Modules/ReLU6Module.cs | 33 ++++++ .../NeuralNets/Modules/ReLUModule.cs | 33 ++++++ .../Modules/ReflectionPad1dModule.cs | 33 ++++++ .../Modules/ReflectionPad2dModule.cs | 33 ++++++ .../Modules/ReflectionPad3dModule.cs | 33 ++++++ .../Modules/ReplicationPad1dModule.cs | 33 ++++++ .../Modules/ReplicationPad2dModule.cs | 33 ++++++ .../Modules/ReplicationPad3dModule.cs | 33 ++++++ .../NeuralNets/Modules/SELUModule.cs | 33 ++++++ .../NeuralNets/Modules/SiLUModule.cs | 27 +++++ .../NeuralNets/Modules/SigmoidModule.cs | 27 +++++ .../NeuralNets/Modules/Softmax2dModule.cs | 27 +++++ .../NeuralNets/Modules/SoftmaxModule.cs | 33 ++++++ .../NeuralNets/Modules/SoftminModule.cs | 33 ++++++ .../NeuralNets/Modules/SoftplusModule.cs | 39 +++++++ .../NeuralNets/Modules/SoftshrinkModule.cs | 33 ++++++ .../NeuralNets/Modules/SoftsignModule.cs | 27 +++++ .../NeuralNets/Modules/TanhModule.cs | 27 +++++ .../NeuralNets/Modules/TanhshrinkModule.cs | 27 +++++ .../NeuralNets/Modules/ThresholdModule.cs | 45 ++++++++ .../Modules/TransformerEncoderLayerModule.cs | 57 ++++++++++ .../Modules/TransformerEncoderModule.cs | 39 +++++++ .../NeuralNets/Modules/UnflattenModule.cs | 40 +++++++ .../NeuralNets/Modules/UnfoldModule.cs | 51 +++++++++ .../NeuralNets/Modules/UpsampleModule.cs | 53 +++++++++ .../NeuralNets/Modules/ZeroPad2dModule.cs | 33 ++++++ src/Bonsai.ML.Torch/NeuralNets/Optimizer.cs | 13 --- .../Optimizers/ASGDOptimizerModule.cs | 71 ++++++++++++ .../Optimizers/AdadeltaOptimizerModule.cs | 64 +++++++++++ .../Optimizers/AdagradOptimizerModule.cs | 64 +++++++++++ .../Optimizers/AdamOptimizerModule.cs | 77 +++++++++++++ .../Optimizers/AdamWOptimizerModule.cs | 77 +++++++++++++ .../Optimizers/AdamaxOptimizerModule.cs | 64 +++++++++++ .../Optimizers/LBFGSOptimizerModule.cs | 71 ++++++++++++ .../Optimizers/RMSPropOptimizerModule.cs | 77 +++++++++++++ .../Optimizers/RpropOptimizerModule.cs | 71 ++++++++++++ .../Optimizers/SGDOptimizerModule.cs | 71 ++++++++++++ .../NeuralNets/OtherModules/BilinearModule.cs | 65 +++++++++++ .../OtherModules/CosineSimilarityModule.cs | 39 +++++++ .../OtherModules/EmbeddingBagModule.cs | 95 ++++++++++++++++ .../EmbeddingbagFromPretrainedModule.cs | 97 +++++++++++++++++ .../NeuralNets/OtherModules/GRUCellModule.cs | 59 ++++++++++ .../NeuralNets/OtherModules/GRUModule.cs | 83 ++++++++++++++ .../NeuralNets/OtherModules/LSTMCellModule.cs | 59 ++++++++++ .../NeuralNets/OtherModules/LSTMModule.cs | 83 ++++++++++++++ .../OtherModules/MaxUnpool1dModule.cs | 45 ++++++++ .../OtherModules/MaxUnpool2dModule.cs | 45 ++++++++ .../OtherModules/MaxUnpool3dModule.cs | 45 ++++++++ .../OtherModules/MultiheadAttentionModule.cs | 75 +++++++++++++ .../OtherModules/PairwiseDistanceModule.cs | 45 ++++++++ .../NeuralNets/OtherModules/RNNCellModule.cs | 65 +++++++++++ .../NeuralNets/OtherModules/RNNModule.cs | 89 +++++++++++++++ .../TransformerDecoderLayerModule.cs | 57 ++++++++++ .../OtherModules/TransformerDecoderModule.cs | 39 +++++++ .../OtherModules/TransformerModule.cs | 69 ++++++++++++ src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs | 11 +- .../NeuralNets/TorchModuleAdapter.cs | 51 --------- 148 files changed, 7029 insertions(+), 128 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/ITorchModule.cs rename src/Bonsai.ML.Torch/NeuralNets/{LoadModuleFromArchitecture.cs => LoadModelArchitecture.cs} (93%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Loss.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/TorchModuleAdapter.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs index 52257d68..f46425d4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs @@ -26,12 +26,12 @@ public class Backward /// The model to train. /// [XmlIgnore] - public ITorchModule Model { get; set; } + public IModule Model { get; set; } /// /// The loss function to use for training. /// - public Loss Loss { get; set; } + public IModule Loss { get; set; } /// /// Trains the model using backpropagation. @@ -40,33 +40,23 @@ public class Backward /// public IObservable Process(IObservable> source) { - optim.Optimizer optimizer = Optimizer switch - { - Optimizer.Adam => Adam(Model.Module.parameters()), - _ => throw new ArgumentException($"Selected optimizer, {Optimizer} is currently not supported.") - }; - - Module loss = Loss switch - { - Loss.NegativeLogLikelihood => NLLLoss(), - _ => throw new ArgumentException($"Selected loss, {Loss} is currently not supported.") - }; - - var scheduler = lr_scheduler.StepLR(optimizer, 1, 0.7); - Model.Module.train(); + var model = Model as Module; + var loss = Loss as Module; + var scheduler = lr_scheduler.StepLR(Optimizer, 1, 0.7); + model.train(); return source.Select((input) => { var (data, target) = input; using (_ = NewDisposeScope()) { - optimizer.zero_grad(); + Optimizer.zero_grad(); - var prediction = Model.Forward(data); + var prediction = model.forward(data); var output = loss.forward(prediction, target); output.backward(); - optimizer.step(); + Optimizer.step(); return output.MoveToOuterDisposeScope(); } }); diff --git a/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs b/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs new file mode 100644 index 00000000..b689819b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Reactive.Linq; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using System.Xml.Serialization; +using System.Linq; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Creates a sequential model from the specified modules. +/// +[Combinator] +[ResetCombinator] +[Description("Creates a sequential model from the specified modules.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CreateSequential +{ + /// + /// The modules to include in the sequential model. + /// + [XmlIgnore] + public IModule[] Modules { get; set; } + + /// + /// The device on which to create the sequential model. + /// + [XmlIgnore] + public Device? Device { get; set; } = null; + + /// + /// Generates an observable sequence that creates a sequential model from the specified modules. + /// + /// + public IObservable> Process() + { + return Observable.Defer(() => + { + var sequential = Sequential([..Modules.Select(m => (Module)m)]); + if (Device is not null && Device != CPU) + { + sequential.to(Device); + } + return Observable.Return(sequential); + }); + } + + /// + /// Generates an observable sequence of sequential models for each element in the input sequence. + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => + { + var sequential = Sequential([..Modules.Select(m => (Module)m)]); + if (Device is not null && Device != CPU) + { + sequential.to(Device); + } + return sequential; + }); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs index fdab2387..88d89872 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs @@ -19,7 +19,7 @@ public class Forward /// The model to use for inference. /// [XmlIgnore] - public ITorchModule Model { get; set; } + public IModule Model { get; set; } /// /// Runs forward inference on the input tensor using the specified model. diff --git a/src/Bonsai.ML.Torch/NeuralNets/ITorchModule.cs b/src/Bonsai.ML.Torch/NeuralNets/ITorchModule.cs deleted file mode 100644 index 5cde6f73..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/ITorchModule.cs +++ /dev/null @@ -1,22 +0,0 @@ -using static TorchSharp.torch; - -namespace Bonsai.ML.Torch.NeuralNets -{ - /// - /// Represents an interface for a Torch module. - /// - public interface ITorchModule - { - /// - /// The module. - /// - public nn.Module Module { get; } - - /// - /// Runs forward inference on the input tensor using the specified model. - /// - /// - /// - public Tensor Forward(Tensor tensor); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleFromArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModelArchitecture.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/LoadModuleFromArchitecture.cs rename to src/Bonsai.ML.Torch/NeuralNets/LoadModelArchitecture.cs index b2d35488..65902449 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleFromArchitecture.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModelArchitecture.cs @@ -3,6 +3,8 @@ using System.Reactive.Linq; using static TorchSharp.torch; using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets { @@ -61,7 +63,7 @@ public int NumClasses /// /// /// - public IObservable Process() + public IObservable> Process() { var device = Device; nn.Module module = ModelArchitecture switch @@ -74,8 +76,7 @@ public IObservable Process() if (ModelWeightsPath is not null) module.load(ModelWeightsPath); - var torchModule = new TorchModuleAdapter(module); - return Observable.Return((ITorchModule)torchModule); + return Observable.Return(module); } } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs new file mode 100644 index 00000000..4ccc0380 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets +{ + /// + /// Saves the model to a file. + /// + [Combinator] + [ResetCombinator] + [Description("Saves the model to a file.")] + [WorkflowElementCategory(ElementCategory.Sink)] + public class LoadModule + { + /// + /// The model to save. + /// + [Description("The model to save.")] + [XmlIgnore] + public Module Module { get; set; } + + /// + /// The path to save the model. + /// + [Description("The path to save the model.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModelPath { get; set; } + + /// + /// Saves the model to the specified file path. + /// + /// + public IObservable Process() + { + return Observable.Return(Module.load(ModelPath)); + } + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs index 2dafa038..eb8c0c56 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Reactive.Linq; +using TorchSharp; using static TorchSharp.torch; using System.Xml.Serialization; @@ -33,11 +34,10 @@ public class LoadScriptModule /// Loads the TorchScript module from the specified file path. /// /// - public IObservable Process() + public IObservable> Process() { var scriptModule = Device is null ? jit.load(ModelPath) : jit.load(ModelPath, Device); - var torchModule = new TorchModuleAdapter(scriptModule); - return Observable.Return((ITorchModule)torchModule); + return Observable.Return(scriptModule); } } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss.cs deleted file mode 100644 index 5752565e..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Bonsai.ML.Torch.NeuralNets -{ - /// - /// Represents a loss function that computes the loss value for a given input and target tensor. - /// - public enum Loss - { - /// - /// Computes the negative log likelihood loss. - /// - NegativeLogLikelihood, - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs new file mode 100644 index 00000000..c0cfea0b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a BCELoss module module. +/// +[Combinator] +[Description("Creates a BCELoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BCELossModule : IScalarTypeProvider +{ + /// + /// The weight parameter for the BCELoss module. + /// + [Description("The weight parameter for the BCELoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the BCELoss module. + /// + [Description("The reduction parameter for the BCELoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The scalar type for the module. + /// + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a BCELoss module. + /// + public IObservable> Process() + { + return Observable.Return(BCELoss(Weight, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs new file mode 100644 index 00000000..e9c553bc --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs @@ -0,0 +1,54 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a BCEWithLogitsLoss module module. +/// +[Combinator] +[Description("Creates a BCEWithLogitsLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BCEWithLogitsLossModule : IScalarTypeProvider +{ + /// + /// The weight parameter for the BCEWithLogitsLoss module. + /// + [Description("The weight parameter for the BCEWithLogitsLoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the BCEWithLogitsLoss module. + /// + [Description("The reduction parameter for the BCEWithLogitsLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The pos_weights parameter for the BCEWithLogitsLoss module. + /// + [Description("The pos_weights parameter for the BCEWithLogitsLoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor PosWeights { get; set; } = null; + + /// + /// The scalar type for the module. + /// + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a BCEWithLogitsLoss module. + /// + public IObservable> Process() + { + return Observable.Return(BCEWithLogitsLoss(Weight, Reduction, PosWeights)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs new file mode 100644 index 00000000..aec3bdfc --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a CTCLoss module module. +/// +[Combinator] +[Description("Creates a CTCLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CTCLossModule +{ + /// + /// The blank parameter for the CTCLoss module. + /// + [Description("The blank parameter for the CTCLoss module")] + public long Blank { get; set; } = 0; + + /// + /// The zero_infinity parameter for the CTCLoss module. + /// + [Description("The zero_infinity parameter for the CTCLoss module")] + public bool ZeroInfinity { get; set; } = false; + + /// + /// The reduction parameter for the CTCLoss module. + /// + [Description("The reduction parameter for the CTCLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a CTCLoss module. + /// + public IObservable> Process() + { + return Observable.Return(CTCLoss(Blank, ZeroInfinity, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs new file mode 100644 index 00000000..5e30f851 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a CosineEmbeddingLoss module module. +/// +[Combinator] +[Description("Creates a CosineEmbeddingLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CosineEmbeddingLossModule +{ + /// + /// The margin parameter for the CosineEmbeddingLoss module. + /// + [Description("The margin parameter for the CosineEmbeddingLoss module")] + public double Margin { get; set; } = 0; + + /// + /// The reduction parameter for the CosineEmbeddingLoss module. + /// + [Description("The reduction parameter for the CosineEmbeddingLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a CosineEmbeddingLoss module. + /// + public IObservable> Process() + { + return Observable.Return(CosineEmbeddingLoss(Margin, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs new file mode 100644 index 00000000..3f89cef9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs @@ -0,0 +1,52 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a CrossEntropyLoss module module. +/// +[Combinator] +[Description("Creates a CrossEntropyLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CrossEntropyLossModule : IScalarTypeProvider +{ + /// + /// The weight parameter for the CrossEntropyLoss module. + /// + [Description("The weight parameter for the CrossEntropyLoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The ignore_index parameter for the CrossEntropyLoss module. + /// + [Description("The ignore_index parameter for the CrossEntropyLoss module")] + public long? IgnoreIndex { get; set; } = null; + + /// + /// The reduction parameter for the CrossEntropyLoss module. + /// + [Description("The reduction parameter for the CrossEntropyLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The scalar type for the module. + /// + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a CrossEntropyLoss module. + /// + public IObservable> Process() + { + return Observable.Return(CrossEntropyLoss(Weight, IgnoreIndex, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs new file mode 100644 index 00000000..4f1aab07 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a GaussianNLLLoss module module. +/// +[Combinator] +[Description("Creates a GaussianNLLLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class GaussianNLLLossModule +{ + /// + /// The full parameter for the GaussianNLLLoss module. + /// + [Description("The full parameter for the GaussianNLLLoss module")] + public bool Full { get; set; } = false; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public float Eps { get; set; } = 1E-08F; + + /// + /// The reduction parameter for the GaussianNLLLoss module. + /// + [Description("The reduction parameter for the GaussianNLLLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a GaussianNLLLoss module. + /// + public IObservable> Process() + { + return Observable.Return(GaussianNLLLoss(Full, Eps, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs new file mode 100644 index 00000000..f900857e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a HingeEmbeddingLoss module module. +/// +[Combinator] +[Description("Creates a HingeEmbeddingLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class HingeEmbeddingLossModule +{ + /// + /// The margin parameter for the HingeEmbeddingLoss module. + /// + [Description("The margin parameter for the HingeEmbeddingLoss module")] + public double Margin { get; set; } = 1; + + /// + /// The reduction parameter for the HingeEmbeddingLoss module. + /// + [Description("The reduction parameter for the HingeEmbeddingLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a HingeEmbeddingLoss module. + /// + public IObservable> Process() + { + return Observable.Return(HingeEmbeddingLoss(Margin, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs new file mode 100644 index 00000000..5f3a87c2 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a HuberLoss module module. +/// +[Combinator] +[Description("Creates a HuberLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class HuberLossModule +{ + /// + /// The delta parameter for the HuberLoss module. + /// + [Description("The delta parameter for the HuberLoss module")] + public double Delta { get; set; } = 1; + + /// + /// The reduction parameter for the HuberLoss module. + /// + [Description("The reduction parameter for the HuberLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a HuberLoss module. + /// + public IObservable> Process() + { + return Observable.Return(HuberLoss(Delta, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs new file mode 100644 index 00000000..d0e88460 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a KLDivLoss module module. +/// +[Combinator] +[Description("Creates a KLDivLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class KLDivLossModule +{ + /// + /// The log_target parameter for the KLDivLoss module. + /// + [Description("The log_target parameter for the KLDivLoss module")] + public bool LogTarget { get; set; } = true; + + /// + /// The reduction parameter for the KLDivLoss module. + /// + [Description("The reduction parameter for the KLDivLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a KLDivLoss module. + /// + public IObservable> Process() + { + return Observable.Return(KLDivLoss(LogTarget, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs new file mode 100644 index 00000000..1ca1e550 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a L1Loss module module. +/// +[Combinator] +[Description("Creates a L1Loss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class L1LossModule +{ + /// + /// The reduction parameter for the L1Loss module. + /// + [Description("The reduction parameter for the L1Loss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a L1Loss module. + /// + public IObservable> Process() + { + return Observable.Return(L1Loss(Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs new file mode 100644 index 00000000..3cf6a3fd --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a MSELoss module module. +/// +[Combinator] +[Description("Creates a MSELoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MSELossModule +{ + /// + /// The reduction parameter for the MSELoss module. + /// + [Description("The reduction parameter for the MSELoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a MSELoss module. + /// + public IObservable> Process() + { + return Observable.Return(MSELoss(Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs new file mode 100644 index 00000000..a40f8e98 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a MarginRankingLoss module module. +/// +[Combinator] +[Description("Creates a MarginRankingLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MarginRankingLossModule +{ + /// + /// The margin parameter for the MarginRankingLoss module. + /// + [Description("The margin parameter for the MarginRankingLoss module")] + public double Margin { get; set; } = 0; + + /// + /// The reduction parameter for the MarginRankingLoss module. + /// + [Description("The reduction parameter for the MarginRankingLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a MarginRankingLoss module. + /// + public IObservable> Process() + { + return Observable.Return(MarginRankingLoss(Margin, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs new file mode 100644 index 00000000..278ed6cb --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a MultiLabelMarginLoss module module. +/// +[Combinator] +[Description("Creates a MultiLabelMarginLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MultiLabelMarginLossModule +{ + /// + /// The reduction parameter for the MultiLabelMarginLoss module. + /// + [Description("The reduction parameter for the MultiLabelMarginLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a MultiLabelMarginLoss module. + /// + public IObservable> Process() + { + return Observable.Return(MultiLabelMarginLoss(Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs new file mode 100644 index 00000000..ea1dd046 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a MultiLabelSoftMarginLoss module module. +/// +[Combinator] +[Description("Creates a MultiLabelSoftMarginLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MultiLabelSoftMarginLossModule : IScalarTypeProvider +{ + /// + /// The weight parameter for the MultiLabelSoftMarginLoss module. + /// + [Description("The weight parameter for the MultiLabelSoftMarginLoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the MultiLabelSoftMarginLoss module. + /// + [Description("The reduction parameter for the MultiLabelSoftMarginLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The scalar type for the module. + /// + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a MultiLabelSoftMarginLoss module. + /// + public IObservable> Process() + { + return Observable.Return(MultiLabelSoftMarginLoss(Weight, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs new file mode 100644 index 00000000..592d9b4d --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs @@ -0,0 +1,58 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a MultiMarginLoss module module. +/// +[Combinator] +[Description("Creates a MultiMarginLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MultiMarginLossModule : IScalarTypeProvider +{ + /// + /// The p parameter for the MultiMarginLoss module. + /// + [Description("The p parameter for the MultiMarginLoss module")] + public int P { get; set; } = 1; + + /// + /// The margin parameter for the MultiMarginLoss module. + /// + [Description("The margin parameter for the MultiMarginLoss module")] + public double Margin { get; set; } = 1; + + /// + /// The weight parameter for the MultiMarginLoss module. + /// + [Description("The weight parameter for the MultiMarginLoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the MultiMarginLoss module. + /// + [Description("The reduction parameter for the MultiMarginLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The scalar type for the module. + /// + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a MultiMarginLoss module. + /// + public IObservable> Process() + { + return Observable.Return(MultiMarginLoss(P, Margin, Weight, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs new file mode 100644 index 00000000..66d2b1f7 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a NLLLoss module module. +/// +[Combinator] +[Description("Creates a NLLLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class NLLLossModule : IScalarTypeProvider +{ + /// + /// The weight parameter for the NLLLoss module. + /// + [Description("The weight parameter for the NLLLoss module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the NLLLoss module. + /// + [Description("The reduction parameter for the NLLLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The scalar type for the module. + /// + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a NLLLoss module. + /// + public IObservable> Process() + { + return Observable.Return(NLLLoss(Weight, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs new file mode 100644 index 00000000..0a130002 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs @@ -0,0 +1,51 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a PoissonNLLLoss module module. +/// +[Combinator] +[Description("Creates a PoissonNLLLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PoissonNLLLossModule +{ + /// + /// The log_input parameter for the PoissonNLLLoss module. + /// + [Description("The log_input parameter for the PoissonNLLLoss module")] + public bool LogInput { get; set; } = true; + + /// + /// The full parameter for the PoissonNLLLoss module. + /// + [Description("The full parameter for the PoissonNLLLoss module")] + public bool Full { get; set; } = false; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public float Eps { get; set; } = 1E-08F; + + /// + /// The reduction parameter for the PoissonNLLLoss module. + /// + [Description("The reduction parameter for the PoissonNLLLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a PoissonNLLLoss module. + /// + public IObservable> Process() + { + return Observable.Return(PoissonNLLLoss(LogInput, Full, Eps, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs new file mode 100644 index 00000000..fbc3413f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a SmoothL1Loss module module. +/// +[Combinator] +[Description("Creates a SmoothL1Loss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SmoothL1LossModule +{ + /// + /// The reduction parameter for the SmoothL1Loss module. + /// + [Description("The reduction parameter for the SmoothL1Loss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The beta parameter for the SmoothL1Loss module. + /// + [Description("The beta parameter for the SmoothL1Loss module")] + public double Beta { get; set; } = 1; + + /// + /// Generates an observable sequence that creates a SmoothL1Loss module. + /// + public IObservable> Process() + { + return Observable.Return(SmoothL1Loss(Reduction, Beta)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs new file mode 100644 index 00000000..5f50acf4 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a SoftMarginLoss module module. +/// +[Combinator] +[Description("Creates a SoftMarginLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SoftMarginLossModule +{ + /// + /// The reduction parameter for the SoftMarginLoss module. + /// + [Description("The reduction parameter for the SoftMarginLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a SoftMarginLoss module. + /// + public IObservable> Process() + { + return Observable.Return(SoftMarginLoss(Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs new file mode 100644 index 00000000..4365fddf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Losses; + +/// +/// Creates a TripletMarginLoss module module. +/// +[Combinator] +[Description("Creates a TripletMarginLoss module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TripletMarginLossModule +{ + /// + /// The margin parameter for the TripletMarginLoss module. + /// + [Description("The margin parameter for the TripletMarginLoss module")] + public double Margin { get; set; } = 1; + + /// + /// The p parameter for the TripletMarginLoss module. + /// + [Description("The p parameter for the TripletMarginLoss module")] + public long P { get; set; } = 2; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-06; + + /// + /// The swap parameter for the TripletMarginLoss module. + /// + [Description("The swap parameter for the TripletMarginLoss module")] + public bool Swap { get; set; } = false; + + /// + /// The reduction parameter for the TripletMarginLoss module. + /// + [Description("The reduction parameter for the TripletMarginLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// Generates an observable sequence that creates a TripletMarginLoss module. + /// + public IObservable> Process() + { + return Observable.Return(TripletMarginLoss(Margin, P, Eps, Swap, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs new file mode 100644 index 00000000..ea468bf9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D adaptive average pooling layer module. +/// +[Combinator] +[Description("Creates a 1D adaptive average pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveAvgPool1dModule +{ + /// + /// The outputsize parameter for the AdaptiveAvgPool1d module. + /// + [Description("The outputsize parameter for the AdaptiveAvgPool1d module")] + public long OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveAvgPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveAvgPool1d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs new file mode 100644 index 00000000..e2bfc603 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs @@ -0,0 +1,34 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D adaptive average pooling layer module. +/// +[Combinator] +[Description("Creates a 2D adaptive average pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveAvgPool2dModule +{ + /// + /// The outputsize parameter for the AdaptiveAvgPool2d module. + /// + [Description("The outputsize parameter for the AdaptiveAvgPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveAvgPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveAvgPool2d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs new file mode 100644 index 00000000..a6a67a2b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs @@ -0,0 +1,34 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D adaptive average pooling layer module. +/// +[Combinator] +[Description("Creates a 3D adaptive average pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveAvgPool3dModule +{ + /// + /// The outputsize parameter for the AdaptiveAvgPool3d module. + /// + [Description("The outputsize parameter for the AdaptiveAvgPool3d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveAvgPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveAvgPool3d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs new file mode 100644 index 00000000..509e6e07 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D adaptive max pooling layer module. +/// +[Combinator] +[Description("Creates a 1D adaptive max pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveMaxPool1dModule +{ + /// + /// The outputsize parameter for the AdaptiveMaxPool1d module. + /// + [Description("The outputsize parameter for the AdaptiveMaxPool1d module")] + public long OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveMaxPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveMaxPool1d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs new file mode 100644 index 00000000..44fffd34 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs @@ -0,0 +1,34 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D adaptive max pooling layer module. +/// +[Combinator] +[Description("Creates a 2D adaptive max pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveMaxPool2dModule +{ + /// + /// The outputsize parameter for the AdaptiveMaxPool2d module. + /// + [Description("The outputsize parameter for the AdaptiveMaxPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveMaxPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveMaxPool2d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs new file mode 100644 index 00000000..3b12b029 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs @@ -0,0 +1,34 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D adaptive max pooling layer module. +/// +[Combinator] +[Description("Creates a 3D adaptive max pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveMaxPool3dModule +{ + /// + /// The outputsize parameter for the AdaptiveMaxPool3d module. + /// + [Description("The outputsize parameter for the AdaptiveMaxPool3d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveMaxPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveMaxPool3d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs new file mode 100644 index 00000000..9d9c017c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a AlphaDropout module module. +/// +[Combinator] +[Description("Creates a AlphaDropout module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AlphaDropoutModule +{ + /// + /// The p parameter for the AlphaDropout module. + /// + [Description("The p parameter for the AlphaDropout module")] + public double P { get; set; } = 0.5; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a AlphaDropout module. + /// + public IObservable> Process() + { + return Observable.Return(AlphaDropout(P, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs new file mode 100644 index 00000000..1daa14b0 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D average pooling layer module. +/// +[Combinator] +[Description("Creates a 1D average pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AvgPool1dModule +{ + /// + /// The kernel_size parameter for the AvgPool1d module. + /// + [Description("The kernel_size parameter for the AvgPool1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the AvgPool1d module. + /// + [Description("The stride parameter for the AvgPool1d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the AvgPool1d module. + /// + [Description("The padding parameter for the AvgPool1d module")] + public long Padding { get; set; } = 0; + + /// + /// The ceil_mode parameter for the AvgPool1d module. + /// + [Description("The ceil_mode parameter for the AvgPool1d module")] + public bool CeilMode { get; set; } = false; + + /// + /// The count_include_pad parameter for the AvgPool1d module. + /// + [Description("The count_include_pad parameter for the AvgPool1d module")] + public bool CountIncludePad { get; set; } = true; + + /// + /// Generates an observable sequence that creates a AvgPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs new file mode 100644 index 00000000..9d3d7f75 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D average pooling layer module. +/// +[Combinator] +[Description("Creates a 2D average pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AvgPool2dModule +{ + /// + /// The kernel_size parameter for the AvgPool2d module. + /// + [Description("The kernel_size parameter for the AvgPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] KernelSize { get; set; } + + /// + /// The strides parameter for the AvgPool2d module. + /// + [Description("The strides parameter for the AvgPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Strides { get; set; } = null; + + /// + /// The padding parameter for the AvgPool2d module. + /// + [Description("The padding parameter for the AvgPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Padding { get; set; } = null; + + /// + /// The ceil_mode parameter for the AvgPool2d module. + /// + [Description("The ceil_mode parameter for the AvgPool2d module")] + public bool CeilMode { get; set; } = false; + + /// + /// The count_include_pad parameter for the AvgPool2d module. + /// + [Description("The count_include_pad parameter for the AvgPool2d module")] + public bool CountIncludePad { get; set; } = true; + + /// + /// The divisor_override parameter for the AvgPool2d module. + /// + [Description("The divisor_override parameter for the AvgPool2d module")] + public long? DivisorOverride { get; set; } = null; + + /// + /// Generates an observable sequence that creates a AvgPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(AvgPool2d(KernelSize, Strides, Padding, CeilMode, CountIncludePad, DivisorOverride)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs new file mode 100644 index 00000000..416ff5e4 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D average pooling layer module. +/// +[Combinator] +[Description("Creates a 3D average pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AvgPool3dModule +{ + /// + /// The kernel_size parameter for the AvgPool3d module. + /// + [Description("The kernel_size parameter for the AvgPool3d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] KernelSize { get; set; } + + /// + /// The strides parameter for the AvgPool3d module. + /// + [Description("The strides parameter for the AvgPool3d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Strides { get; set; } = null; + + /// + /// The padding parameter for the AvgPool3d module. + /// + [Description("The padding parameter for the AvgPool3d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Padding { get; set; } = null; + + /// + /// The ceil_mode parameter for the AvgPool3d module. + /// + [Description("The ceil_mode parameter for the AvgPool3d module")] + public bool CeilMode { get; set; } = false; + + /// + /// The count_include_pad parameter for the AvgPool3d module. + /// + [Description("The count_include_pad parameter for the AvgPool3d module")] + public bool CountIncludePad { get; set; } = true; + + /// + /// The divisor_override parameter for the AvgPool3d module. + /// + [Description("The divisor_override parameter for the AvgPool3d module")] + public long? DivisorOverride { get; set; } = null; + + /// + /// Generates an observable sequence that creates a AvgPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(AvgPool3d(KernelSize, Strides, Padding, CeilMode, CountIncludePad, DivisorOverride)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs new file mode 100644 index 00000000..a1a299b3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D batch normalization layer module. +/// +[Combinator] +[Description("Creates a 1D batch normalization layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BatchNorm1dModule +{ + /// + /// The features parameter for the BatchNorm1d module. + /// + [Description("The features parameter for the BatchNorm1d module")] + public long Features { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0.1; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = true; + + /// + /// The track_running_stats parameter for the BatchNorm1d module. + /// + [Description("The track_running_stats parameter for the BatchNorm1d module")] + public bool TrackRunningStats { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a BatchNorm1d module. + /// + public IObservable> Process() + { + return Observable.Return(BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs new file mode 100644 index 00000000..7f3698fa --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D batch normalization layer module. +/// +[Combinator] +[Description("Creates a 2D batch normalization layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BatchNorm2dModule +{ + /// + /// The features parameter for the BatchNorm2d module. + /// + [Description("The features parameter for the BatchNorm2d module")] + public long Features { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0.1; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = true; + + /// + /// The track_running_stats parameter for the BatchNorm2d module. + /// + [Description("The track_running_stats parameter for the BatchNorm2d module")] + public bool TrackRunningStats { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a BatchNorm2d module. + /// + public IObservable> Process() + { + return Observable.Return(BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs new file mode 100644 index 00000000..ea18e8bb --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D batch normalization layer module. +/// +[Combinator] +[Description("Creates a 3D batch normalization layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BatchNorm3dModule +{ + /// + /// The features parameter for the BatchNorm3d module. + /// + [Description("The features parameter for the BatchNorm3d module")] + public long Features { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0.1; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = true; + + /// + /// The track_running_stats parameter for the BatchNorm3d module. + /// + [Description("The track_running_stats parameter for the BatchNorm3d module")] + public bool TrackRunningStats { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a BatchNorm3d module. + /// + public IObservable> Process() + { + return Observable.Return(BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs new file mode 100644 index 00000000..b624eeb9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a CELU module module. +/// +[Combinator] +[Description("Creates a CELU module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CELUModule +{ + /// + /// The alpha parameter for the CELU module. + /// + [Description("The alpha parameter for the CELU module")] + public double Alpha { get; set; } = 1; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a CELU module. + /// + public IObservable> Process() + { + return Observable.Return(CELU(Alpha, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs new file mode 100644 index 00000000..48016333 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ChannelShuffle module module. +/// +[Combinator] +[Description("Creates a ChannelShuffle module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ChannelShuffleModule +{ + /// + /// The groups parameter for the ChannelShuffle module. + /// + [Description("The groups parameter for the ChannelShuffle module")] + public long Groups { get; set; } + + /// + /// Generates an observable sequence that creates a ChannelShuffle module. + /// + public IObservable> Process() + { + return Observable.Return(ChannelShuffle(Groups)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs new file mode 100644 index 00000000..bee63d9f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ConstantPad1d module module. +/// +[Combinator] +[Description("Creates a ConstantPad1d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConstantPad1dModule +{ + /// + /// The padding parameter for the ConstantPad1d module. + /// + [Description("The padding parameter for the ConstantPad1d module")] + public long Padding { get; set; } + + /// + /// The value parameter for the ConstantPad1d module. + /// + [Description("The value parameter for the ConstantPad1d module")] + public double Value { get; set; } + + /// + /// Generates an observable sequence that creates a ConstantPad1d module. + /// + public IObservable> Process() + { + return Observable.Return(ConstantPad1d(Padding, Value)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs new file mode 100644 index 00000000..9ab6e151 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ConstantPad2d module module. +/// +[Combinator] +[Description("Creates a ConstantPad2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConstantPad2dModule +{ + /// + /// The padding parameter for the ConstantPad2d module. + /// + [Description("The padding parameter for the ConstantPad2d module")] + public long Padding { get; set; } + + /// + /// The value parameter for the ConstantPad2d module. + /// + [Description("The value parameter for the ConstantPad2d module")] + public double Value { get; set; } + + /// + /// Generates an observable sequence that creates a ConstantPad2d module. + /// + public IObservable> Process() + { + return Observable.Return(ConstantPad2d(Padding, Value)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs new file mode 100644 index 00000000..4697f2ec --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ConstantPad3d module module. +/// +[Combinator] +[Description("Creates a ConstantPad3d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConstantPad3dModule +{ + /// + /// The padding parameter for the ConstantPad3d module. + /// + [Description("The padding parameter for the ConstantPad3d module")] + public long Padding { get; set; } + + /// + /// The value parameter for the ConstantPad3d module. + /// + [Description("The value parameter for the ConstantPad3d module")] + public double Value { get; set; } + + /// + /// Generates an observable sequence that creates a ConstantPad3d module. + /// + public IObservable> Process() + { + return Observable.Return(ConstantPad3d(Padding, Value)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs new file mode 100644 index 00000000..c2e51737 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs @@ -0,0 +1,95 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D convolution layer module. +/// +[Combinator] +[Description("Creates a 1D convolution layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Conv1dModule +{ + /// + /// The in_channels parameter for the Conv1d module. + /// + [Description("The in_channels parameter for the Conv1d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Conv1d module. + /// + public IObservable> Process() + { + return Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs new file mode 100644 index 00000000..80f76f6a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs @@ -0,0 +1,95 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D convolution layer module. +/// +[Combinator] +[Description("Creates a 2D convolution layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Conv2dModule +{ + /// + /// The in_channels parameter for the Conv2d module. + /// + [Description("The in_channels parameter for the Conv2d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv2d module. + /// + [Description("The out_channels parameter for the Conv2d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv2d module. + /// + [Description("The kernelsize parameter for the Conv2d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv2d module. + /// + [Description("The stride parameter for the Conv2d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv2d module. + /// + [Description("The padding parameter for the Conv2d module")] + public long Padding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv2d module. + /// + [Description("The dilation parameter for the Conv2d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv2d module. + /// + [Description("The padding_mode parameter for the Conv2d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv2d module. + /// + [Description("The groups parameter for the Conv2d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Conv2d module. + /// + public IObservable> Process() + { + return Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs new file mode 100644 index 00000000..adb697e2 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs @@ -0,0 +1,95 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D convolution layer module. +/// +[Combinator] +[Description("Creates a 3D convolution layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Conv3dModule +{ + /// + /// The in_channels parameter for the Conv3d module. + /// + [Description("The in_channels parameter for the Conv3d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv3d module. + /// + [Description("The out_channels parameter for the Conv3d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv3d module. + /// + [Description("The kernelsize parameter for the Conv3d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv3d module. + /// + [Description("The stride parameter for the Conv3d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv3d module. + /// + [Description("The padding parameter for the Conv3d module")] + public long Padding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv3d module. + /// + [Description("The dilation parameter for the Conv3d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv3d module. + /// + [Description("The padding_mode parameter for the Conv3d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv3d module. + /// + [Description("The groups parameter for the Conv3d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Conv3d module. + /// + public IObservable> Process() + { + return Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs new file mode 100644 index 00000000..ef240b2a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs @@ -0,0 +1,101 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D transposed convolution layer module. +/// +[Combinator] +[Description("Creates a 1D transposed convolution layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConvTranspose1dModule +{ + /// + /// The in_channels parameter for the ConvTranspose1d module. + /// + [Description("The in_channels parameter for the ConvTranspose1d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the ConvTranspose1d module. + /// + [Description("The out_channels parameter for the ConvTranspose1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the ConvTranspose1d module. + /// + [Description("The kernelsize parameter for the ConvTranspose1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the ConvTranspose1d module. + /// + [Description("The stride parameter for the ConvTranspose1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the ConvTranspose1d module. + /// + [Description("The padding parameter for the ConvTranspose1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the ConvTranspose1d module. + /// + [Description("The output_padding parameter for the ConvTranspose1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the ConvTranspose1d module. + /// + [Description("The dilation parameter for the ConvTranspose1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the ConvTranspose1d module. + /// + [Description("The padding_mode parameter for the ConvTranspose1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the ConvTranspose1d module. + /// + [Description("The groups parameter for the ConvTranspose1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a ConvTranspose1d module. + /// + public IObservable> Process() + { + return Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs new file mode 100644 index 00000000..0c7008c5 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs @@ -0,0 +1,101 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D transposed convolution layer module. +/// +[Combinator] +[Description("Creates a 2D transposed convolution layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConvTranspose2dModule +{ + /// + /// The in_channels parameter for the ConvTranspose2d module. + /// + [Description("The in_channels parameter for the ConvTranspose2d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the ConvTranspose2d module. + /// + [Description("The out_channels parameter for the ConvTranspose2d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the ConvTranspose2d module. + /// + [Description("The kernelsize parameter for the ConvTranspose2d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the ConvTranspose2d module. + /// + [Description("The stride parameter for the ConvTranspose2d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the ConvTranspose2d module. + /// + [Description("The padding parameter for the ConvTranspose2d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the ConvTranspose2d module. + /// + [Description("The output_padding parameter for the ConvTranspose2d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the ConvTranspose2d module. + /// + [Description("The dilation parameter for the ConvTranspose2d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the ConvTranspose2d module. + /// + [Description("The padding_mode parameter for the ConvTranspose2d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the ConvTranspose2d module. + /// + [Description("The groups parameter for the ConvTranspose2d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a ConvTranspose2d module. + /// + public IObservable> Process() + { + return Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs new file mode 100644 index 00000000..969ae883 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs @@ -0,0 +1,101 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D transposed convolution layer module. +/// +[Combinator] +[Description("Creates a 3D transposed convolution layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConvTranspose3dModule +{ + /// + /// The in_channels parameter for the ConvTranspose3d module. + /// + [Description("The in_channels parameter for the ConvTranspose3d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the ConvTranspose3d module. + /// + [Description("The out_channels parameter for the ConvTranspose3d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the ConvTranspose3d module. + /// + [Description("The kernelsize parameter for the ConvTranspose3d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the ConvTranspose3d module. + /// + [Description("The stride parameter for the ConvTranspose3d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the ConvTranspose3d module. + /// + [Description("The padding parameter for the ConvTranspose3d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the ConvTranspose3d module. + /// + [Description("The output_padding parameter for the ConvTranspose3d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the ConvTranspose3d module. + /// + [Description("The dilation parameter for the ConvTranspose3d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the ConvTranspose3d module. + /// + [Description("The padding_mode parameter for the ConvTranspose3d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the ConvTranspose3d module. + /// + [Description("The groups parameter for the ConvTranspose3d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a ConvTranspose3d module. + /// + public IObservable> Process() + { + return Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs new file mode 100644 index 00000000..ff55a0ac --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Dropout1d module module. +/// +[Combinator] +[Description("Creates a Dropout1d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Dropout1dModule +{ + /// + /// The p parameter for the Dropout1d module. + /// + [Description("The p parameter for the Dropout1d module")] + public double P { get; set; } = 0.5; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Dropout1d module. + /// + public IObservable> Process() + { + return Observable.Return(Dropout1d(P, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs new file mode 100644 index 00000000..86ec3711 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D dropout regularization layer module. +/// +[Combinator] +[Description("Creates a 2D dropout regularization layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Dropout2dModule +{ + /// + /// The p parameter for the Dropout2d module. + /// + [Description("The p parameter for the Dropout2d module")] + public double P { get; set; } = 0.5; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Dropout2d module. + /// + public IObservable> Process() + { + return Observable.Return(Dropout2d(P, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs new file mode 100644 index 00000000..b3945138 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D dropout regularization layer module. +/// +[Combinator] +[Description("Creates a 3D dropout regularization layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Dropout3dModule +{ + /// + /// The p parameter for the Dropout3d module. + /// + [Description("The p parameter for the Dropout3d module")] + public double P { get; set; } = 0.5; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Dropout3d module. + /// + public IObservable> Process() + { + return Observable.Return(Dropout3d(P, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs new file mode 100644 index 00000000..4e3cd580 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Dropout regularization layer module. +/// +[Combinator] +[Description("Creates a Dropout regularization layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class DropoutModule +{ + /// + /// The p parameter for the Dropout module. + /// + [Description("The p parameter for the Dropout module")] + public double P { get; set; } = 0.5; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Dropout module. + /// + public IObservable> Process() + { + return Observable.Return(Dropout(P, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs new file mode 100644 index 00000000..fc4042a0 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ELU activation function module. +/// +[Combinator] +[Description("Creates a ELU activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ELUModule +{ + /// + /// The alpha parameter for the ELU module. + /// + [Description("The alpha parameter for the ELU module")] + public double Alpha { get; set; } = 1; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ELU module. + /// + public IObservable> Process() + { + return Observable.Return(ELU(Alpha, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs new file mode 100644 index 00000000..5226994e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs @@ -0,0 +1,85 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Embedding_from_pretrained module module. +/// +[Combinator] +[Description("Creates a Embedding_from_pretrained module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class EmbeddingFromPretrainedModule : IScalarTypeProvider +{ + /// + /// The embeddings parameter for the Embedding_from_pretrained module. + /// + [Description("The embeddings parameter for the Embedding_from_pretrained module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Embeddings { get; set; } + + /// + /// The freeze parameter for the Embedding_from_pretrained module. + /// + [Description("The freeze parameter for the Embedding_from_pretrained module")] + public bool Freeze { get; set; } = true; + + /// + /// The padding_idx parameter for the Embedding_from_pretrained module. + /// + [Description("The padding_idx parameter for the Embedding_from_pretrained module")] + public long? PaddingIdx { get; set; } = null; + + /// + /// The max_norm parameter for the Embedding_from_pretrained module. + /// + [Description("The max_norm parameter for the Embedding_from_pretrained module")] + public double? MaxNorm { get; set; } = null; + + /// + /// The norm_type parameter for the Embedding_from_pretrained module. + /// + [Description("The norm_type parameter for the Embedding_from_pretrained module")] + public double NormType { get; set; } = 2; + + /// + /// The scale_grad_by_freq parameter for the Embedding_from_pretrained module. + /// + [Description("The scale_grad_by_freq parameter for the Embedding_from_pretrained module")] + public bool ScaleGradByFreq { get; set; } = false; + + /// + /// The sparse parameter for the Embedding_from_pretrained module. + /// + [Description("The sparse parameter for the Embedding_from_pretrained module")] + public bool Sparse { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a EmbeddingFromPretrained module. + /// + public IObservable> Process() + { + return Observable.Return(Embedding_from_pretrained(Embeddings, Freeze, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs new file mode 100644 index 00000000..5beb4edf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs @@ -0,0 +1,83 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Embedding layer module. +/// +[Combinator] +[Description("Creates a Embedding layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class EmbeddingModule +{ + /// + /// The num_embeddings parameter for the Embedding module. + /// + [Description("The num_embeddings parameter for the Embedding module")] + public long NumEmbeddings { get; set; } + + /// + /// The embedding_dims parameter for the Embedding module. + /// + [Description("The embedding_dims parameter for the Embedding module")] + public long EmbeddingDims { get; set; } + + /// + /// The padding_idx parameter for the Embedding module. + /// + [Description("The padding_idx parameter for the Embedding module")] + public long? PaddingIdx { get; set; } = null; + + /// + /// The max_norm parameter for the Embedding module. + /// + [Description("The max_norm parameter for the Embedding module")] + public double? MaxNorm { get; set; } = null; + + /// + /// The norm_type parameter for the Embedding module. + /// + [Description("The norm_type parameter for the Embedding module")] + public double NormType { get; set; } = 2; + + /// + /// The scale_grad_by_freq parameter for the Embedding module. + /// + [Description("The scale_grad_by_freq parameter for the Embedding module")] + public bool ScaleGradByFreq { get; set; } = false; + + /// + /// The sparse parameter for the Embedding module. + /// + [Description("The sparse parameter for the Embedding module")] + public bool Sparse { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Embedding module. + /// + public IObservable> Process() + { + return Observable.Return(Embedding(NumEmbeddings, EmbeddingDims, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs new file mode 100644 index 00000000..816bf58a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a FeatureAlphaDropout module module. +/// +[Combinator] +[Description("Creates a FeatureAlphaDropout module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class FeatureAlphaDropoutModule +{ + /// + /// The p parameter for the FeatureAlphaDropout module. + /// + [Description("The p parameter for the FeatureAlphaDropout module")] + public double P { get; set; } = 0.5; + + /// + /// Generates an observable sequence that creates a FeatureAlphaDropout module. + /// + public IObservable> Process() + { + return Observable.Return(FeatureAlphaDropout(P)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs new file mode 100644 index 00000000..80b326df --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Flatten module module. +/// +[Combinator] +[Description("Creates a Flatten module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class FlattenModule +{ + /// + /// The startdim parameter for the Flatten module. + /// + [Description("The startdim parameter for the Flatten module")] + public long StartDim { get; set; } = 1; + + /// + /// The enddim parameter for the Flatten module. + /// + [Description("The enddim parameter for the Flatten module")] + public long EndDim { get; set; } = -1; + + /// + /// Generates an observable sequence that creates a Flatten module. + /// + public IObservable> Process() + { + return Observable.Return(Flatten(StartDim, EndDim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs new file mode 100644 index 00000000..adfdd7fe --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Fold module module. +/// +[Combinator] +[Description("Creates a Fold module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class FoldModule +{ + /// + /// The output_size parameter for the Fold module. + /// + [Description("The output_size parameter for the Fold module")] + public long OutputSize { get; set; } + + /// + /// The kernel_size parameter for the Fold module. + /// + [Description("The kernel_size parameter for the Fold module")] + public long KernelSize { get; set; } + + /// + /// The dilation parameter for the Fold module. + /// + [Description("The dilation parameter for the Fold module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding parameter for the Fold module. + /// + [Description("The padding parameter for the Fold module")] + public long Padding { get; set; } = 0; + + /// + /// The stride parameter for the Fold module. + /// + [Description("The stride parameter for the Fold module")] + public long Stride { get; set; } = 1; + + /// + /// Generates an observable sequence that creates a Fold module. + /// + public IObservable> Process() + { + return Observable.Return(Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs new file mode 100644 index 00000000..35269f97 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a FractionalMaxPool2d module module. +/// +[Combinator] +[Description("Creates a FractionalMaxPool2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class FractionalMaxPool2dModule +{ + /// + /// The kernel_size parameter for the FractionalMaxPool2d module. + /// + [Description("The kernel_size parameter for the FractionalMaxPool2d module")] + public long KernelSize { get; set; } + + /// + /// The output_size parameter for the FractionalMaxPool2d module. + /// + [Description("The output_size parameter for the FractionalMaxPool2d module")] + public long? OutputSize { get; set; } = null; + + /// + /// The output_ratio parameter for the FractionalMaxPool2d module. + /// + [Description("The output_ratio parameter for the FractionalMaxPool2d module")] + public double? OutputRatio { get; set; } = null; + + /// + /// Generates an observable sequence that creates a FractionalMaxPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs new file mode 100644 index 00000000..b2f1142e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a FractionalMaxPool3d module module. +/// +[Combinator] +[Description("Creates a FractionalMaxPool3d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class FractionalMaxPool3dModule +{ + /// + /// The kernel_size parameter for the FractionalMaxPool3d module. + /// + [Description("The kernel_size parameter for the FractionalMaxPool3d module")] + public long KernelSize { get; set; } + + /// + /// The output_size parameter for the FractionalMaxPool3d module. + /// + [Description("The output_size parameter for the FractionalMaxPool3d module")] + public long? OutputSize { get; set; } = null; + + /// + /// The output_ratio parameter for the FractionalMaxPool3d module. + /// + [Description("The output_ratio parameter for the FractionalMaxPool3d module")] + public double? OutputRatio { get; set; } = null; + + /// + /// Generates an observable sequence that creates a FractionalMaxPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs new file mode 100644 index 00000000..9953c02a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a GELU activation function module. +/// +[Combinator] +[Description("Creates a GELU activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class GELUModule +{ + /// + /// Generates an observable sequence that creates a GELU module. + /// + public IObservable> Process() + { + return Observable.Return(GELU()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs new file mode 100644 index 00000000..b756fa10 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a GLU module module. +/// +[Combinator] +[Description("Creates a GLU module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class GLUModule +{ + /// + /// The dim parameter for the GLU module. + /// + [Description("The dim parameter for the GLU module")] + public long Dim { get; set; } = -1; + + /// + /// Generates an observable sequence that creates a GLU module. + /// + public IObservable> Process() + { + return Observable.Return(GLU(Dim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs new file mode 100644 index 00000000..8be12d32 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs @@ -0,0 +1,65 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Group normalization module. +/// +[Combinator] +[Description("Creates a Group normalization module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class GroupNormModule +{ + /// + /// The num_groups parameter for the GroupNorm module. + /// + [Description("The num_groups parameter for the GroupNorm module")] + public long NumGroups { get; set; } + + /// + /// The num_channels parameter for the GroupNorm module. + /// + [Description("The num_channels parameter for the GroupNorm module")] + public long NumChannels { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a GroupNorm module. + /// + public IObservable> Process() + { + return Observable.Return(GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs new file mode 100644 index 00000000..80961ce0 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Hardshrink module module. +/// +[Combinator] +[Description("Creates a Hardshrink module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class HardshrinkModule +{ + /// + /// The lambda parameter for the Hardshrink module. + /// + [Description("The lambda parameter for the Hardshrink module")] + public double Lambda { get; set; } = 0.5; + + /// + /// Generates an observable sequence that creates a Hardshrink module. + /// + public IObservable> Process() + { + return Observable.Return(Hardshrink(Lambda)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs new file mode 100644 index 00000000..3dc1718a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Hardsigmoid module module. +/// +[Combinator] +[Description("Creates a Hardsigmoid module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class HardsigmoidModule +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Hardsigmoid module. + /// + public IObservable> Process() + { + return Observable.Return(Hardsigmoid(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs new file mode 100644 index 00000000..6c95abf1 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Hardswish module module. +/// +[Combinator] +[Description("Creates a Hardswish module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class HardswishModule +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Hardswish module. + /// + public IObservable> Process() + { + return Observable.Return(Hardswish(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs new file mode 100644 index 00000000..8781cb72 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Hardtanh module module. +/// +[Combinator] +[Description("Creates a Hardtanh module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class HardtanhModule +{ + /// + /// The min_val parameter for the Hardtanh module. + /// + [Description("The min_val parameter for the Hardtanh module")] + public double MinVal { get; set; } = -1; + + /// + /// The max_val parameter for the Hardtanh module. + /// + [Description("The max_val parameter for the Hardtanh module")] + public double MaxVal { get; set; } = 1; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Hardtanh module. + /// + public IObservable> Process() + { + return Observable.Return(Hardtanh(MinVal, MaxVal, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs new file mode 100644 index 00000000..a7254084 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Identity module module. +/// +[Combinator] +[Description("Creates a Identity module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class IdentityModule +{ + /// + /// Generates an observable sequence that creates a Identity module. + /// + public IObservable> Process() + { + return Observable.Return(Identity()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs new file mode 100644 index 00000000..ff79d16c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D instance normalization module. +/// +[Combinator] +[Description("Creates a 1D instance normalization module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class InstanceNorm1dModule +{ + /// + /// The features parameter for the InstanceNorm1d module. + /// + [Description("The features parameter for the InstanceNorm1d module")] + public long Features { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0.1; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = false; + + /// + /// The track_running_stats parameter for the InstanceNorm1d module. + /// + [Description("The track_running_stats parameter for the InstanceNorm1d module")] + public bool TrackRunningStats { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a InstanceNorm1d module. + /// + public IObservable> Process() + { + return Observable.Return(InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs new file mode 100644 index 00000000..34fd0607 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D instance normalization module. +/// +[Combinator] +[Description("Creates a 2D instance normalization module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class InstanceNorm2dModule +{ + /// + /// The features parameter for the InstanceNorm2d module. + /// + [Description("The features parameter for the InstanceNorm2d module")] + public long Features { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0.1; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = false; + + /// + /// The track_running_stats parameter for the InstanceNorm2d module. + /// + [Description("The track_running_stats parameter for the InstanceNorm2d module")] + public bool TrackRunningStats { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a InstanceNorm2d module. + /// + public IObservable> Process() + { + return Observable.Return(InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs new file mode 100644 index 00000000..f4a68be8 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D instance normalization module. +/// +[Combinator] +[Description("Creates a 3D instance normalization module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class InstanceNorm3dModule +{ + /// + /// The features parameter for the InstanceNorm3d module. + /// + [Description("The features parameter for the InstanceNorm3d module")] + public long Features { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0.1; + + /// + /// A boolean value that when set to true, this module has learnable affine parameters. + /// + [Description("A boolean value that when set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = false; + + /// + /// The track_running_stats parameter for the InstanceNorm3d module. + /// + [Description("The track_running_stats parameter for the InstanceNorm3d module")] + public bool TrackRunningStats { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a InstanceNorm3d module. + /// + public IObservable> Process() + { + return Observable.Return(InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs new file mode 100644 index 00000000..aebe0902 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs @@ -0,0 +1,51 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a LPPool1d module module. +/// +[Combinator] +[Description("Creates a LPPool1d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LPPool1dModule +{ + /// + /// The norm_type parameter for the LPPool1d module. + /// + [Description("The norm_type parameter for the LPPool1d module")] + public double NormType { get; set; } + + /// + /// The kernelsize parameter for the LPPool1d module. + /// + [Description("The kernelsize parameter for the LPPool1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the LPPool1d module. + /// + [Description("The stride parameter for the LPPool1d module")] + public long? Stride { get; set; } = null; + + /// + /// The ceil_mode parameter for the LPPool1d module. + /// + [Description("The ceil_mode parameter for the LPPool1d module")] + public bool CeilMode { get; set; } = false; + + /// + /// Generates an observable sequence that creates a LPPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(LPPool1d(NormType, KernelSize, Stride, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs new file mode 100644 index 00000000..110b79d9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs @@ -0,0 +1,53 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a LPPool2d module module. +/// +[Combinator] +[Description("Creates a LPPool2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LPPool2dModule +{ + /// + /// The norm_type parameter for the LPPool2d module. + /// + [Description("The norm_type parameter for the LPPool2d module")] + public double NormType { get; set; } + + /// + /// The kernel_size parameter for the LPPool2d module. + /// + [Description("The kernel_size parameter for the LPPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] KernelSize { get; set; } + + /// + /// The strides parameter for the LPPool2d module. + /// + [Description("The strides parameter for the LPPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Strides { get; set; } = null; + + /// + /// The ceil_mode parameter for the LPPool2d module. + /// + [Description("The ceil_mode parameter for the LPPool2d module")] + public bool CeilMode { get; set; } = false; + + /// + /// Generates an observable sequence that creates a LPPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(LPPool2d(NormType, KernelSize, Strides, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs new file mode 100644 index 00000000..2b200a67 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Layer normalization module. +/// +[Combinator] +[Description("Creates a Layer normalization module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LayerNormModule +{ + /// + /// The normalized_shape parameter for the LayerNorm module. + /// + [Description("The normalized_shape parameter for the LayerNorm module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] NormalizedShape { get; set; } + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-05; + + /// + /// The elementwise_affine parameter for the LayerNorm module. + /// + [Description("The elementwise_affine parameter for the LayerNorm module")] + public bool ElementwiseAffine { get; set; } = true; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a LayerNorm module. + /// + public IObservable> Process() + { + return Observable.Return(LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs new file mode 100644 index 00000000..3c4d1d0d --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a LeakyReLU activation function module. +/// +[Combinator] +[Description("Creates a LeakyReLU activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LeakyReLUModule +{ + /// + /// The negative_slope parameter for the LeakyReLU module. + /// + [Description("The negative_slope parameter for the LeakyReLU module")] + public double NegativeSlope { get; set; } = 0.01; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a LeakyReLU module. + /// + public IObservable> Process() + { + return Observable.Return(LeakyReLU(NegativeSlope, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs new file mode 100644 index 00000000..444fa5a6 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs @@ -0,0 +1,59 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Linear transformation layer module. +/// +[Combinator] +[Description("Creates a Linear transformation layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LinearModule +{ + /// + /// The inputsize parameter for the Linear module. + /// + [Description("The inputsize parameter for the Linear module")] + public long InputSize { get; set; } + + /// + /// The outputsize parameter for the Linear module. + /// + [Description("The outputsize parameter for the Linear module")] + public long OutputSize { get; set; } + + /// + /// The hasbias parameter for the Linear module. + /// + [Description("The hasbias parameter for the Linear module")] + public bool HasBias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Linear module. + /// + public IObservable> Process() + { + return Observable.Return(Linear(InputSize, OutputSize, HasBias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs new file mode 100644 index 00000000..8f5a0671 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs @@ -0,0 +1,51 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a LocalResponseNorm module module. +/// +[Combinator] +[Description("Creates a LocalResponseNorm module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LocalResponseNormModule +{ + /// + /// The size parameter for the LocalResponseNorm module. + /// + [Description("The size parameter for the LocalResponseNorm module")] + public long Size { get; set; } + + /// + /// The alpha parameter for the LocalResponseNorm module. + /// + [Description("The alpha parameter for the LocalResponseNorm module")] + public double Alpha { get; set; } = 0.0001; + + /// + /// The beta parameter for the LocalResponseNorm module. + /// + [Description("The beta parameter for the LocalResponseNorm module")] + public double Beta { get; set; } = 0.75; + + /// + /// The k parameter for the LocalResponseNorm module. + /// + [Description("The k parameter for the LocalResponseNorm module")] + public double K { get; set; } = 1; + + /// + /// Generates an observable sequence that creates a LocalResponseNorm module. + /// + public IObservable> Process() + { + return Observable.Return(LocalResponseNorm(Size, Alpha, Beta, K)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs new file mode 100644 index 00000000..24c3d1cf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a LogSigmoid module module. +/// +[Combinator] +[Description("Creates a LogSigmoid module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LogSigmoidModule +{ + /// + /// Generates an observable sequence that creates a LogSigmoid module. + /// + public IObservable> Process() + { + return Observable.Return(LogSigmoid()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs new file mode 100644 index 00000000..78c29005 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a LogSoftmax activation function module. +/// +[Combinator] +[Description("Creates a LogSoftmax activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LogSoftmaxModule +{ + /// + /// The dim parameter for the LogSoftmax module. + /// + [Description("The dim parameter for the LogSoftmax module")] + public long Dim { get; set; } + + /// + /// Generates an observable sequence that creates a LogSoftmax module. + /// + public IObservable> Process() + { + return Observable.Return(LogSoftmax(Dim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs new file mode 100644 index 00000000..4c56bcdf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 1D max pooling layer module. +/// +[Combinator] +[Description("Creates a 1D max pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MaxPool1dModule +{ + /// + /// The kernelsize parameter for the MaxPool1d module. + /// + [Description("The kernelsize parameter for the MaxPool1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the MaxPool1d module. + /// + [Description("The stride parameter for the MaxPool1d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the MaxPool1d module. + /// + [Description("The padding parameter for the MaxPool1d module")] + public long? Padding { get; set; } = null; + + /// + /// The dilation parameter for the MaxPool1d module. + /// + [Description("The dilation parameter for the MaxPool1d module")] + public long? Dilation { get; set; } = null; + + /// + /// The ceilmode parameter for the MaxPool1d module. + /// + [Description("The ceilmode parameter for the MaxPool1d module")] + public bool CeilMode { get; set; } = false; + + /// + /// Generates an observable sequence that creates a MaxPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs new file mode 100644 index 00000000..186aee4e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 2D max pooling layer module. +/// +[Combinator] +[Description("Creates a 2D max pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MaxPool2dModule +{ + /// + /// The kernelsize parameter for the MaxPool2d module. + /// + [Description("The kernelsize parameter for the MaxPool2d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the MaxPool2d module. + /// + [Description("The stride parameter for the MaxPool2d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the MaxPool2d module. + /// + [Description("The padding parameter for the MaxPool2d module")] + public long? Padding { get; set; } = null; + + /// + /// The dilation parameter for the MaxPool2d module. + /// + [Description("The dilation parameter for the MaxPool2d module")] + public long? Dilation { get; set; } = null; + + /// + /// The ceilmode parameter for the MaxPool2d module. + /// + [Description("The ceilmode parameter for the MaxPool2d module")] + public bool CeilMode { get; set; } = false; + + /// + /// Generates an observable sequence that creates a MaxPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs new file mode 100644 index 00000000..3be88386 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a 3D max pooling layer module. +/// +[Combinator] +[Description("Creates a 3D max pooling layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MaxPool3dModule +{ + /// + /// The kernelsize parameter for the MaxPool3d module. + /// + [Description("The kernelsize parameter for the MaxPool3d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the MaxPool3d module. + /// + [Description("The stride parameter for the MaxPool3d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the MaxPool3d module. + /// + [Description("The padding parameter for the MaxPool3d module")] + public long? Padding { get; set; } = null; + + /// + /// The dilation parameter for the MaxPool3d module. + /// + [Description("The dilation parameter for the MaxPool3d module")] + public long? Dilation { get; set; } = null; + + /// + /// The ceilmode parameter for the MaxPool3d module. + /// + [Description("The ceilmode parameter for the MaxPool3d module")] + public bool CeilMode { get; set; } = false; + + /// + /// Generates an observable sequence that creates a MaxPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs new file mode 100644 index 00000000..6c951053 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Mish module module. +/// +[Combinator] +[Description("Creates a Mish module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MishModule +{ + /// + /// Generates an observable sequence that creates a Mish module. + /// + public IObservable> Process() + { + return Observable.Return(Mish()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs new file mode 100644 index 00000000..a45e388c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs @@ -0,0 +1,53 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a PReLU module module. +/// +[Combinator] +[Description("Creates a PReLU module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PReLUModule +{ + /// + /// The num_parameters parameter for the PReLU module. + /// + [Description("The num_parameters parameter for the PReLU module")] + public long NumParameters { get; set; } + + /// + /// The init parameter for the PReLU module. + /// + [Description("The init parameter for the PReLU module")] + public double Init { get; set; } = 0.25; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a PReLU module. + /// + public IObservable> Process() + { + return Observable.Return(PReLU(NumParameters, Init, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs new file mode 100644 index 00000000..853e4685 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a PixelShuffle module module. +/// +[Combinator] +[Description("Creates a PixelShuffle module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PixelShuffleModule +{ + /// + /// The upscalefactor parameter for the PixelShuffle module. + /// + [Description("The upscalefactor parameter for the PixelShuffle module")] + public long UpscaleFactor { get; set; } + + /// + /// Generates an observable sequence that creates a PixelShuffle module. + /// + public IObservable> Process() + { + return Observable.Return(PixelShuffle(UpscaleFactor)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs new file mode 100644 index 00000000..3189f611 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a PixelUnshuffle module module. +/// +[Combinator] +[Description("Creates a PixelUnshuffle module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PixelUnshuffleModule +{ + /// + /// The downscalefactor parameter for the PixelUnshuffle module. + /// + [Description("The downscalefactor parameter for the PixelUnshuffle module")] + public long DownscaleFactor { get; set; } + + /// + /// Generates an observable sequence that creates a PixelUnshuffle module. + /// + public IObservable> Process() + { + return Observable.Return(PixelUnshuffle(DownscaleFactor)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs new file mode 100644 index 00000000..0a87f4ff --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a RReLU module module. +/// +[Combinator] +[Description("Creates a RReLU module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class RReLUModule +{ + /// + /// The lower parameter for the RReLU module. + /// + [Description("The lower parameter for the RReLU module")] + public double Lower { get; set; } = 0.125; + + /// + /// The upper parameter for the RReLU module. + /// + [Description("The upper parameter for the RReLU module")] + public double Upper { get; set; } = 0.3333333333333333; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a RReLU module. + /// + public IObservable> Process() + { + return Observable.Return(RReLU(Lower, Upper, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs new file mode 100644 index 00000000..fd2a691f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReLU6 module module. +/// +[Combinator] +[Description("Creates a ReLU6 module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReLU6Module +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ReLU6 module. + /// + public IObservable> Process() + { + return Observable.Return(ReLU6(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs new file mode 100644 index 00000000..486ea6e8 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReLU activation function module. +/// +[Combinator] +[Description("Creates a ReLU activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReLUModule +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ReLU module. + /// + public IObservable> Process() + { + return Observable.Return(ReLU(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs new file mode 100644 index 00000000..c6e5411a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReflectionPad1d module module. +/// +[Combinator] +[Description("Creates a ReflectionPad1d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReflectionPad1dModule +{ + /// + /// The padding parameter for the ReflectionPad1d module. + /// + [Description("The padding parameter for the ReflectionPad1d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ReflectionPad1d module. + /// + public IObservable> Process() + { + return Observable.Return(ReflectionPad1d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs new file mode 100644 index 00000000..5af55460 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReflectionPad2d module module. +/// +[Combinator] +[Description("Creates a ReflectionPad2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReflectionPad2dModule +{ + /// + /// The padding parameter for the ReflectionPad2d module. + /// + [Description("The padding parameter for the ReflectionPad2d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ReflectionPad2d module. + /// + public IObservable> Process() + { + return Observable.Return(ReflectionPad2d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs new file mode 100644 index 00000000..902b0065 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReflectionPad3d module module. +/// +[Combinator] +[Description("Creates a ReflectionPad3d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReflectionPad3dModule +{ + /// + /// The padding parameter for the ReflectionPad3d module. + /// + [Description("The padding parameter for the ReflectionPad3d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ReflectionPad3d module. + /// + public IObservable> Process() + { + return Observable.Return(ReflectionPad3d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs new file mode 100644 index 00000000..093eee0a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReplicationPad1d module module. +/// +[Combinator] +[Description("Creates a ReplicationPad1d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReplicationPad1dModule +{ + /// + /// The padding parameter for the ReplicationPad1d module. + /// + [Description("The padding parameter for the ReplicationPad1d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ReplicationPad1d module. + /// + public IObservable> Process() + { + return Observable.Return(ReplicationPad1d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs new file mode 100644 index 00000000..32426497 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReplicationPad2d module module. +/// +[Combinator] +[Description("Creates a ReplicationPad2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReplicationPad2dModule +{ + /// + /// The padding parameter for the ReplicationPad2d module. + /// + [Description("The padding parameter for the ReplicationPad2d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ReplicationPad2d module. + /// + public IObservable> Process() + { + return Observable.Return(ReplicationPad2d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs new file mode 100644 index 00000000..45ce42cd --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ReplicationPad3d module module. +/// +[Combinator] +[Description("Creates a ReplicationPad3d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReplicationPad3dModule +{ + /// + /// The padding parameter for the ReplicationPad3d module. + /// + [Description("The padding parameter for the ReplicationPad3d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ReplicationPad3d module. + /// + public IObservable> Process() + { + return Observable.Return(ReplicationPad3d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs new file mode 100644 index 00000000..8fb76c95 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a SELU activation function module. +/// +[Combinator] +[Description("Creates a SELU activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SELUModule +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a SELU module. + /// + public IObservable> Process() + { + return Observable.Return(SELU(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs new file mode 100644 index 00000000..5d8c1699 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a SiLU module module. +/// +[Combinator] +[Description("Creates a SiLU module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SiLUModule +{ + /// + /// Generates an observable sequence that creates a SiLU module. + /// + public IObservable> Process() + { + return Observable.Return(SiLU()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs new file mode 100644 index 00000000..50af7ba1 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Sigmoid activation function module. +/// +[Combinator] +[Description("Creates a Sigmoid activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SigmoidModule +{ + /// + /// Generates an observable sequence that creates a Sigmoid module. + /// + public IObservable> Process() + { + return Observable.Return(Sigmoid()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs new file mode 100644 index 00000000..5dcec56b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Softmax2d module module. +/// +[Combinator] +[Description("Creates a Softmax2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Softmax2dModule +{ + /// + /// Generates an observable sequence that creates a Softmax2d module. + /// + public IObservable> Process() + { + return Observable.Return(Softmax2d()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs new file mode 100644 index 00000000..103eb971 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Softmax activation function module. +/// +[Combinator] +[Description("Creates a Softmax activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SoftmaxModule +{ + /// + /// The dim parameter for the Softmax module. + /// + [Description("The dim parameter for the Softmax module")] + public long Dim { get; set; } + + /// + /// Generates an observable sequence that creates a Softmax module. + /// + public IObservable> Process() + { + return Observable.Return(Softmax(Dim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs new file mode 100644 index 00000000..3c402846 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Softmin module module. +/// +[Combinator] +[Description("Creates a Softmin module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SoftminModule +{ + /// + /// The dim parameter for the Softmin module. + /// + [Description("The dim parameter for the Softmin module")] + public long Dim { get; set; } + + /// + /// Generates an observable sequence that creates a Softmin module. + /// + public IObservable> Process() + { + return Observable.Return(Softmin(Dim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs new file mode 100644 index 00000000..91308d27 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Softplus module module. +/// +[Combinator] +[Description("Creates a Softplus module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SoftplusModule +{ + /// + /// The beta parameter for the Softplus module. + /// + [Description("The beta parameter for the Softplus module")] + public double Beta { get; set; } = 1; + + /// + /// The threshold parameter for the Softplus module. + /// + [Description("The threshold parameter for the Softplus module")] + public double Threshold { get; set; } = 20; + + /// + /// Generates an observable sequence that creates a Softplus module. + /// + public IObservable> Process() + { + return Observable.Return(Softplus(Beta, Threshold)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs new file mode 100644 index 00000000..ceef1718 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Softshrink module module. +/// +[Combinator] +[Description("Creates a Softshrink module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SoftshrinkModule +{ + /// + /// The lambda parameter for the Softshrink module. + /// + [Description("The lambda parameter for the Softshrink module")] + public double Lambda { get; set; } = 0.5; + + /// + /// Generates an observable sequence that creates a Softshrink module. + /// + public IObservable> Process() + { + return Observable.Return(Softshrink(Lambda)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs new file mode 100644 index 00000000..f361f71c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Softsign module module. +/// +[Combinator] +[Description("Creates a Softsign module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SoftsignModule +{ + /// + /// Generates an observable sequence that creates a Softsign module. + /// + public IObservable> Process() + { + return Observable.Return(Softsign()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs new file mode 100644 index 00000000..af4a432a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Tanh activation function module. +/// +[Combinator] +[Description("Creates a Tanh activation function module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TanhModule +{ + /// + /// Generates an observable sequence that creates a Tanh module. + /// + public IObservable> Process() + { + return Observable.Return(Tanh()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs new file mode 100644 index 00000000..e073284d --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Tanhshrink module module. +/// +[Combinator] +[Description("Creates a Tanhshrink module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TanhshrinkModule +{ + /// + /// Generates an observable sequence that creates a Tanhshrink module. + /// + public IObservable> Process() + { + return Observable.Return(Tanhshrink()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs new file mode 100644 index 00000000..9a9edc84 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Threshold module module. +/// +[Combinator] +[Description("Creates a Threshold module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ThresholdModule +{ + /// + /// The threshold parameter for the Threshold module. + /// + [Description("The threshold parameter for the Threshold module")] + public double Threshold { get; set; } + + /// + /// The value parameter for the Threshold module. + /// + [Description("The value parameter for the Threshold module")] + public double Value { get; set; } + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Threshold module. + /// + public IObservable> Process() + { + return Observable.Return(Threshold(Threshold, Value, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs new file mode 100644 index 00000000..aa4a30c5 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a TransformerEncoderLayer module module. +/// +[Combinator] +[Description("Creates a TransformerEncoderLayer module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TransformerEncoderLayerModule +{ + /// + /// The d_model parameter for the TransformerEncoderLayer module. + /// + [Description("The d_model parameter for the TransformerEncoderLayer module")] + public long DModel { get; set; } = 512; + + /// + /// The nhead parameter for the TransformerEncoderLayer module. + /// + [Description("The nhead parameter for the TransformerEncoderLayer module")] + public long Nhead { get; set; } = 8; + + /// + /// The dim_feedforward parameter for the TransformerEncoderLayer module. + /// + [Description("The dim_feedforward parameter for the TransformerEncoderLayer module")] + public long DimFeedforward { get; set; } = 2048; + + /// + /// The dropout parameter for the TransformerEncoderLayer module. + /// + [Description("The dropout parameter for the TransformerEncoderLayer module")] + public double Dropout { get; set; } = 0.1; + + /// + /// The activation parameter for the TransformerEncoderLayer module. + /// + [Description("The activation parameter for the TransformerEncoderLayer module")] + public Activations Activation { get; set; } = Activations.ReLU; + + /// + /// Generates an observable sequence that creates a TransformerEncoderLayer module. + /// + public IObservable> Process() + { + return Observable.Return(TransformerEncoderLayer(DModel, Nhead, DimFeedforward, Dropout, Activation)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs new file mode 100644 index 00000000..36b39dbc --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a TransformerEncoder module module. +/// +[Combinator] +[Description("Creates a TransformerEncoder module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TransformerEncoderModule +{ + /// + /// The encoder_layer parameter for the TransformerEncoder module. + /// + [Description("The encoder_layer parameter for the TransformerEncoder module")] + public TransformerEncoderLayer EncoderLayer { get; set; } + + /// + /// The num_layers parameter for the TransformerEncoder module. + /// + [Description("The num_layers parameter for the TransformerEncoder module")] + public long NumLayers { get; set; } + + /// + /// Generates an observable sequence that creates a TransformerEncoder module. + /// + public IObservable> Process() + { + return Observable.Return(TransformerEncoder(EncoderLayer, NumLayers)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs new file mode 100644 index 00000000..bf76cbfe --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs @@ -0,0 +1,40 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Unflatten module module. +/// +[Combinator] +[Description("Creates a Unflatten module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class UnflattenModule +{ + /// + /// The dim parameter for the Unflatten module. + /// + [Description("The dim parameter for the Unflatten module")] + public long Dim { get; set; } + + /// + /// The unflattenedsize parameter for the Unflatten module. + /// + [Description("The unflattenedsize parameter for the Unflatten module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] UnflattenedSize { get; set; } + + /// + /// Generates an observable sequence that creates a Unflatten module. + /// + public IObservable> Process() + { + return Observable.Return(Unflatten(Dim, UnflattenedSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs new file mode 100644 index 00000000..55151fdf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs @@ -0,0 +1,51 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Unfold module module. +/// +[Combinator] +[Description("Creates a Unfold module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class UnfoldModule +{ + /// + /// The kernel_size parameter for the Unfold module. + /// + [Description("The kernel_size parameter for the Unfold module")] + public long KernelSize { get; set; } + + /// + /// The dilation parameter for the Unfold module. + /// + [Description("The dilation parameter for the Unfold module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding parameter for the Unfold module. + /// + [Description("The padding parameter for the Unfold module")] + public long Padding { get; set; } = 0; + + /// + /// The stride parameter for the Unfold module. + /// + [Description("The stride parameter for the Unfold module")] + public long Stride { get; set; } = 1; + + /// + /// Generates an observable sequence that creates a Unfold module. + /// + public IObservable> Process() + { + return Observable.Return(Unfold(KernelSize, Dilation, Padding, Stride)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs new file mode 100644 index 00000000..071ab813 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs @@ -0,0 +1,53 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Upsample module module. +/// +[Combinator] +[Description("Creates a Upsample module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class UpsampleModule +{ + /// + /// The size parameter for the Upsample module. + /// + [Description("The size parameter for the Upsample module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Size { get; set; } = null; + + /// + /// The scale_factor parameter for the Upsample module. + /// + [Description("The scale_factor parameter for the Upsample module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public double[] ScaleFactor { get; set; } = null; + + /// + /// The mode parameter for the Upsample module. + /// + [Description("The mode parameter for the Upsample module")] + public UpsampleMode Mode { get; set; } = UpsampleMode.Nearest; + + /// + /// The align_corners parameter for the Upsample module. + /// + [Description("The align_corners parameter for the Upsample module")] + public bool? AlignCorners { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Upsample module. + /// + public IObservable> Process() + { + return Observable.Return(Upsample(Size, ScaleFactor, Mode, AlignCorners)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs new file mode 100644 index 00000000..0251ccf0 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a ZeroPad2d module module. +/// +[Combinator] +[Description("Creates a ZeroPad2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ZeroPad2dModule +{ + /// + /// The padding parameter for the ZeroPad2d module. + /// + [Description("The padding parameter for the ZeroPad2d module")] + public long Padding { get; set; } + + /// + /// Generates an observable sequence that creates a ZeroPad2d module. + /// + public IObservable> Process() + { + return Observable.Return(ZeroPad2d(Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer.cs deleted file mode 100644 index 4ab09dbd..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Bonsai.ML.Torch.NeuralNets -{ - /// - /// Represents an optimizer that updates the parameters of a neural network. - /// - public enum Optimizer - { - /// - /// Adam optimizer. - /// - Adam, - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs new file mode 100644 index 00000000..8bc097f9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a ASGD optimizer module. +/// +[Combinator] +[Description("Creates a ASGD optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ASGDOptimizerModule +{ + /// + /// The parameters parameter for the ASGD module. + /// + [Description("The parameters parameter for the ASGD module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the ASGD module. + /// + [Description("The lr parameter for the ASGD module")] + public double Lr { get; set; } = 0.001; + + /// + /// The lambd parameter for the ASGD module. + /// + [Description("The lambd parameter for the ASGD module")] + public double Lambd { get; set; } = 0.0001; + + /// + /// The alpha parameter for the ASGD module. + /// + [Description("The alpha parameter for the ASGD module")] + public double Alpha { get; set; } = 0.75; + + /// + /// The t0 parameter for the ASGD module. + /// + [Description("The t0 parameter for the ASGD module")] + public double T0 { get; set; } = 1000000; + + /// + /// The weight_decay parameter for the ASGD module. + /// + [Description("The weight_decay parameter for the ASGD module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The maximize parameter for the ASGD module. + /// + [Description("The maximize parameter for the ASGD module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ASGDOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(ASGD(Parameters, Lr, Lambd, Alpha, T0, WeightDecay, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs new file mode 100644 index 00000000..bbc21579 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs @@ -0,0 +1,64 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a Adadelta optimizer module. +/// +[Combinator] +[Description("Creates a Adadelta optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdadeltaOptimizerModule +{ + /// + /// The parameters parameter for the Adadelta module. + /// + [Description("The parameters parameter for the Adadelta module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the Adadelta module. + /// + [Description("The lr parameter for the Adadelta module")] + public double Lr { get; set; } = 1; + + /// + /// The rho parameter for the Adadelta module. + /// + [Description("The rho parameter for the Adadelta module")] + public double Rho { get; set; } = 0.9; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-06; + + /// + /// The weight_decay parameter for the Adadelta module. + /// + [Description("The weight_decay parameter for the Adadelta module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The maximize parameter for the Adadelta module. + /// + [Description("The maximize parameter for the Adadelta module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a AdadeltaOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(Adadelta(Parameters, Lr, Rho, Eps, WeightDecay, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs new file mode 100644 index 00000000..ae72d299 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs @@ -0,0 +1,64 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a Adagrad optimizer module. +/// +[Combinator] +[Description("Creates a Adagrad optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdagradOptimizerModule +{ + /// + /// The parameters parameter for the Adagrad module. + /// + [Description("The parameters parameter for the Adagrad module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the Adagrad module. + /// + [Description("The lr parameter for the Adagrad module")] + public double Lr { get; set; } = 0.01; + + /// + /// The lr_decay parameter for the Adagrad module. + /// + [Description("The lr_decay parameter for the Adagrad module")] + public double LrDecay { get; set; } = 0; + + /// + /// The weight_decay parameter for the Adagrad module. + /// + [Description("The weight_decay parameter for the Adagrad module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The initial_accumulator_value parameter for the Adagrad module. + /// + [Description("The initial_accumulator_value parameter for the Adagrad module")] + public double InitialAccumulatorValue { get; set; } = 0; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-10; + + /// + /// Generates an observable sequence that creates a AdagradOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(Adagrad(Parameters, Lr, LrDecay, WeightDecay, InitialAccumulatorValue, Eps)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs new file mode 100644 index 00000000..87106c84 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs @@ -0,0 +1,77 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a Adam optimizer module. +/// +[Combinator] +[Description("Creates a Adam optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdamOptimizerModule +{ + /// + /// The parameters parameter for the Adam module. + /// + [Description("The parameters parameter for the Adam module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the Adam module. + /// + [Description("The lr parameter for the Adam module")] + public double Lr { get; set; } = 0.001; + + /// + /// The beta1 parameter for the Adam module. + /// + [Description("The beta1 parameter for the Adam module")] + public double Beta1 { get; set; } = 0.9; + + /// + /// The beta2 parameter for the Adam module. + /// + [Description("The beta2 parameter for the Adam module")] + public double Beta2 { get; set; } = 0.999; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-08; + + /// + /// The weight_decay parameter for the Adam module. + /// + [Description("The weight_decay parameter for the Adam module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The amsgrad parameter for the Adam module. + /// + [Description("The amsgrad parameter for the Adam module")] + public bool Amsgrad { get; set; } = false; + + /// + /// The maximize parameter for the Adam module. + /// + [Description("The maximize parameter for the Adam module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a AdamOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(Adam(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay, Amsgrad, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs new file mode 100644 index 00000000..350765dc --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs @@ -0,0 +1,77 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a AdamW optimizer module. +/// +[Combinator] +[Description("Creates a AdamW optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdamWOptimizerModule +{ + /// + /// The parameters parameter for the AdamW module. + /// + [Description("The parameters parameter for the AdamW module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the AdamW module. + /// + [Description("The lr parameter for the AdamW module")] + public double Lr { get; set; } = 0.001; + + /// + /// The beta1 parameter for the AdamW module. + /// + [Description("The beta1 parameter for the AdamW module")] + public double Beta1 { get; set; } = 0.9; + + /// + /// The beta2 parameter for the AdamW module. + /// + [Description("The beta2 parameter for the AdamW module")] + public double Beta2 { get; set; } = 0.999; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-08; + + /// + /// The weight_decay parameter for the AdamW module. + /// + [Description("The weight_decay parameter for the AdamW module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The amsgrad parameter for the AdamW module. + /// + [Description("The amsgrad parameter for the AdamW module")] + public bool Amsgrad { get; set; } = false; + + /// + /// The maximize parameter for the AdamW module. + /// + [Description("The maximize parameter for the AdamW module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a AdamWOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(AdamW(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay, Amsgrad, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs new file mode 100644 index 00000000..06ec079c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs @@ -0,0 +1,64 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a Adamax optimizer module. +/// +[Combinator] +[Description("Creates a Adamax optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdamaxOptimizerModule +{ + /// + /// The parameters parameter for the Adamax module. + /// + [Description("The parameters parameter for the Adamax module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the Adamax module. + /// + [Description("The lr parameter for the Adamax module")] + public double Lr { get; set; } = 0.002; + + /// + /// The beta1 parameter for the Adamax module. + /// + [Description("The beta1 parameter for the Adamax module")] + public double Beta1 { get; set; } = 0.9; + + /// + /// The beta2 parameter for the Adamax module. + /// + [Description("The beta2 parameter for the Adamax module")] + public double Beta2 { get; set; } = 0.999; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-08; + + /// + /// The weight_decay parameter for the Adamax module. + /// + [Description("The weight_decay parameter for the Adamax module")] + public double WeightDecay { get; set; } = 0; + + /// + /// Generates an observable sequence that creates a AdamaxOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(Adamax(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs new file mode 100644 index 00000000..eb3ecaaf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a LBFGS optimizer module. +/// +[Combinator] +[Description("Creates a LBFGS optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LBFGSOptimizerModule +{ + /// + /// The parameters parameter for the LBFGS module. + /// + [Description("The parameters parameter for the LBFGS module")] + public IEnumerable> Parameters { get; set; } + + /// + /// The lr parameter for the LBFGS module. + /// + [Description("The lr parameter for the LBFGS module")] + public double Lr { get; set; } = 0.01; + + /// + /// The max_iter parameter for the LBFGS module. + /// + [Description("The max_iter parameter for the LBFGS module")] + public long MaxIter { get; set; } = 20; + + /// + /// The max_eval parameter for the LBFGS module. + /// + [Description("The max_eval parameter for the LBFGS module")] + public long? MaxEval { get; set; } = null; + + /// + /// The tolerange_grad parameter for the LBFGS module. + /// + [Description("The tolerange_grad parameter for the LBFGS module")] + public double TolerangeGrad { get; set; } = 1E-05; + + /// + /// The tolerance_change parameter for the LBFGS module. + /// + [Description("The tolerance_change parameter for the LBFGS module")] + public double ToleranceChange { get; set; } = 1E-09; + + /// + /// The history_size parameter for the LBFGS module. + /// + [Description("The history_size parameter for the LBFGS module")] + public long HistorySize { get; set; } = 100; + + /// + /// Generates an observable sequence that creates a LBFGSOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(LBFGS(Parameters, Lr, MaxIter, MaxEval, TolerangeGrad, ToleranceChange, HistorySize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs new file mode 100644 index 00000000..f8075317 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs @@ -0,0 +1,77 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a RMSProp optimizer module. +/// +[Combinator] +[Description("Creates a RMSProp optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class RMSPropOptimizerModule +{ + /// + /// The parameters parameter for the RMSProp module. + /// + [Description("The parameters parameter for the RMSProp module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the RMSProp module. + /// + [Description("The lr parameter for the RMSProp module")] + public double Lr { get; set; } = 0.01; + + /// + /// The alpha parameter for the RMSProp module. + /// + [Description("The alpha parameter for the RMSProp module")] + public double Alpha { get; set; } = 0.99; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-08; + + /// + /// The weight_decay parameter for the RMSProp module. + /// + [Description("The weight_decay parameter for the RMSProp module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0; + + /// + /// The centered parameter for the RMSProp module. + /// + [Description("The centered parameter for the RMSProp module")] + public bool Centered { get; set; } = false; + + /// + /// The maximize parameter for the RMSProp module. + /// + [Description("The maximize parameter for the RMSProp module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a RMSPropOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(RMSProp(Parameters, Lr, Alpha, Eps, WeightDecay, Momentum, Centered, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs new file mode 100644 index 00000000..ff253126 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a Rprop optimizer module. +/// +[Combinator] +[Description("Creates a Rprop optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class RpropOptimizerModule +{ + /// + /// The parameters parameter for the Rprop module. + /// + [Description("The parameters parameter for the Rprop module")] + public IEnumerable Parameters { get; set; } + + /// + /// The lr parameter for the Rprop module. + /// + [Description("The lr parameter for the Rprop module")] + public double Lr { get; set; } = 0.01; + + /// + /// The etaminus parameter for the Rprop module. + /// + [Description("The etaminus parameter for the Rprop module")] + public double Etaminus { get; set; } = 0.5; + + /// + /// The etaplus parameter for the Rprop module. + /// + [Description("The etaplus parameter for the Rprop module")] + public double Etaplus { get; set; } = 1.2; + + /// + /// The min_step parameter for the Rprop module. + /// + [Description("The min_step parameter for the Rprop module")] + public double MinStep { get; set; } = 1E-06; + + /// + /// The max_step parameter for the Rprop module. + /// + [Description("The max_step parameter for the Rprop module")] + public double MaxStep { get; set; } = 50; + + /// + /// The maximize parameter for the Rprop module. + /// + [Description("The maximize parameter for the Rprop module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a RpropOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(Rprop(Parameters, Lr, Etaminus, Etaplus, MinStep, MaxStep, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs new file mode 100644 index 00000000..a90d6bf3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs @@ -0,0 +1,71 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Collections.Generic; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizers; + +/// +/// Creates a SGD optimizer module. +/// +[Combinator] +[Description("Creates a SGD optimizer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SGDOptimizerModule +{ + /// + /// The parameters parameter for the SGD module. + /// + [Description("The parameters parameter for the SGD module")] + public IEnumerable Parameters { get; set; } + + /// + /// The learningrate parameter for the SGD module. + /// + [Description("The learningrate parameter for the SGD module")] + public double LearningRate { get; set; } + + /// + /// The value used for the running_mean and running_var computation. + /// + [Description("The value used for the running_mean and running_var computation")] + public double Momentum { get; set; } = 0; + + /// + /// The dampening parameter for the SGD module. + /// + [Description("The dampening parameter for the SGD module")] + public double Dampening { get; set; } = 0; + + /// + /// The weight_decay parameter for the SGD module. + /// + [Description("The weight_decay parameter for the SGD module")] + public double WeightDecay { get; set; } = 0; + + /// + /// The nesterov parameter for the SGD module. + /// + [Description("The nesterov parameter for the SGD module")] + public bool Nesterov { get; set; } = false; + + /// + /// The maximize parameter for the SGD module. + /// + [Description("The maximize parameter for the SGD module")] + public bool Maximize { get; set; } = false; + + /// + /// Generates an observable sequence that creates a SGDOptimizer module. + /// + public IObservable Process() + { + return Observable.Return(SGD(Parameters, LearningRate, Momentum, Dampening, WeightDecay, Nesterov, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs new file mode 100644 index 00000000..8ea8aaf2 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs @@ -0,0 +1,65 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a Bilinear module module. +/// +[Combinator] +[Description("Creates a Bilinear module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BilinearModule +{ + /// + /// The in1features parameter for the Bilinear module. + /// + [Description("The in1features parameter for the Bilinear module")] + public long In1Features { get; set; } + + /// + /// The in2features parameter for the Bilinear module. + /// + [Description("The in2features parameter for the Bilinear module")] + public long In2Features { get; set; } + + /// + /// The outputsize parameter for the Bilinear module. + /// + [Description("The outputsize parameter for the Bilinear module")] + public long OutputSize { get; set; } + + /// + /// The hasbias parameter for the Bilinear module. + /// + [Description("The hasbias parameter for the Bilinear module")] + public bool HasBias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a Bilinear module. + /// + public IObservable> Process() + { + return Observable.Return(Bilinear(In1Features, In2Features, OutputSize, HasBias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs new file mode 100644 index 00000000..e05bb999 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a CosineSimilarity module module. +/// +[Combinator] +[Description("Creates a CosineSimilarity module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CosineSimilarityModule +{ + /// + /// The dim parameter for the CosineSimilarity module. + /// + [Description("The dim parameter for the CosineSimilarity module")] + public long Dim { get; set; } = 1; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-08; + + /// + /// Generates an observable sequence that creates a CosineSimilarity module. + /// + public IObservable> Process() + { + return Observable.Return(CosineSimilarity(Dim, Eps)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs new file mode 100644 index 00000000..db7ffb65 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs @@ -0,0 +1,95 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a EmbeddingBag module module. +/// +[Combinator] +[Description("Creates a EmbeddingBag module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class EmbeddingBagModule +{ + /// + /// The num_embeddings parameter for the EmbeddingBag module. + /// + [Description("The num_embeddings parameter for the EmbeddingBag module")] + public long NumEmbeddings { get; set; } + + /// + /// The embedding_dims parameter for the EmbeddingBag module. + /// + [Description("The embedding_dims parameter for the EmbeddingBag module")] + public long EmbeddingDims { get; set; } + + /// + /// The max_norm parameter for the EmbeddingBag module. + /// + [Description("The max_norm parameter for the EmbeddingBag module")] + public double? MaxNorm { get; set; } = null; + + /// + /// The norm_type parameter for the EmbeddingBag module. + /// + [Description("The norm_type parameter for the EmbeddingBag module")] + public double NormType { get; set; } = 2; + + /// + /// The scale_grad_by_freq parameter for the EmbeddingBag module. + /// + [Description("The scale_grad_by_freq parameter for the EmbeddingBag module")] + public bool ScaleGradByFreq { get; set; } = false; + + /// + /// The mode parameter for the EmbeddingBag module. + /// + [Description("The mode parameter for the EmbeddingBag module")] + public EmbeddingBagMode Mode { get; set; } = EmbeddingBagMode.Mean; + + /// + /// The sparse parameter for the EmbeddingBag module. + /// + [Description("The sparse parameter for the EmbeddingBag module")] + public bool Sparse { get; set; } = false; + + /// + /// The include_last_offset parameter for the EmbeddingBag module. + /// + [Description("The include_last_offset parameter for the EmbeddingBag module")] + public bool IncludeLastOffset { get; set; } = false; + + /// + /// The padding_index parameter for the EmbeddingBag module. + /// + [Description("The padding_index parameter for the EmbeddingBag module")] + public long PaddingIndex { get; set; } = -1; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a EmbeddingBag module. + /// + public IObservable> Process() + { + return Observable.Return(EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs new file mode 100644 index 00000000..7ffee83f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs @@ -0,0 +1,97 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a EmbeddingBag_from_pretrained module module. +/// +[Combinator] +[Description("Creates a EmbeddingBag_from_pretrained module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class EmbeddingbagFromPretrainedModule : IScalarTypeProvider +{ + /// + /// The embeddings parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The embeddings parameter for the EmbeddingBag_from_pretrained module")] + [XmlIgnore] + [TypeConverter(typeof(TensorConverter))] + public Tensor Embeddings { get; set; } + + /// + /// The freeze parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The freeze parameter for the EmbeddingBag_from_pretrained module")] + public bool Freeze { get; set; } = true; + + /// + /// The max_norm parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The max_norm parameter for the EmbeddingBag_from_pretrained module")] + public double? MaxNorm { get; set; } = null; + + /// + /// The norm_type parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The norm_type parameter for the EmbeddingBag_from_pretrained module")] + public double NormType { get; set; } = 2; + + /// + /// The scale_grad_by_freq parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The scale_grad_by_freq parameter for the EmbeddingBag_from_pretrained module")] + public bool ScaleGradByFreq { get; set; } = false; + + /// + /// The mode parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The mode parameter for the EmbeddingBag_from_pretrained module")] + public EmbeddingBagMode Mode { get; set; } = EmbeddingBagMode.Mean; + + /// + /// The sparse parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The sparse parameter for the EmbeddingBag_from_pretrained module")] + public bool Sparse { get; set; } = false; + + /// + /// The include_last_offset parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The include_last_offset parameter for the EmbeddingBag_from_pretrained module")] + public bool IncludeLastOffset { get; set; } = false; + + /// + /// The padding_index parameter for the EmbeddingBag_from_pretrained module. + /// + [Description("The padding_index parameter for the EmbeddingBag_from_pretrained module")] + public long PaddingIndex { get; set; } = -1; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a EmbeddingbagFromPretrained module. + /// + public IObservable> Process() + { + return Observable.Return(EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs new file mode 100644 index 00000000..321c8f35 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs @@ -0,0 +1,59 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a GRUCell module module. +/// +[Combinator] +[Description("Creates a GRUCell module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class GRUCellModule +{ + /// + /// The inputsize parameter for the GRUCell module. + /// + [Description("The inputsize parameter for the GRUCell module")] + public long InputSize { get; set; } + + /// + /// The hiddensize parameter for the GRUCell module. + /// + [Description("The hiddensize parameter for the GRUCell module")] + public long HiddenSize { get; set; } + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a GRUCell module. + /// + public IObservable> Process() + { + return Observable.Return(GRUCell(InputSize, HiddenSize, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs new file mode 100644 index 00000000..cc6487d5 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs @@ -0,0 +1,83 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a Gated Recurrent Unit layer module. +/// +[Combinator] +[Description("Creates a Gated Recurrent Unit layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class GRUModule +{ + /// + /// The inputsize parameter for the GRU module. + /// + [Description("The inputsize parameter for the GRU module")] + public long InputSize { get; set; } + + /// + /// The hiddensize parameter for the GRU module. + /// + [Description("The hiddensize parameter for the GRU module")] + public long HiddenSize { get; set; } + + /// + /// The numlayers parameter for the GRU module. + /// + [Description("The numlayers parameter for the GRU module")] + public long NumLayers { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The batchfirst parameter for the GRU module. + /// + [Description("The batchfirst parameter for the GRU module")] + public bool BatchFirst { get; set; } = false; + + /// + /// The dropout parameter for the GRU module. + /// + [Description("The dropout parameter for the GRU module")] + public double Dropout { get; set; } = 0; + + /// + /// The bidirectional parameter for the GRU module. + /// + [Description("The bidirectional parameter for the GRU module")] + public bool Bidirectional { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a GRU module. + /// + public IObservable> Process() + { + return Observable.Return(GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs new file mode 100644 index 00000000..1171376c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs @@ -0,0 +1,59 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a LSTMCell module module. +/// +[Combinator] +[Description("Creates a LSTMCell module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LSTMCellModule +{ + /// + /// The inputsize parameter for the LSTMCell module. + /// + [Description("The inputsize parameter for the LSTMCell module")] + public long InputSize { get; set; } + + /// + /// The hiddensize parameter for the LSTMCell module. + /// + [Description("The hiddensize parameter for the LSTMCell module")] + public long HiddenSize { get; set; } + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a LSTMCell module. + /// + public IObservable> Process() + { + return Observable.Return(LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs new file mode 100644 index 00000000..7af8c5a6 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs @@ -0,0 +1,83 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a Long Short-Term Memory layer module. +/// +[Combinator] +[Description("Creates a Long Short-Term Memory layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LSTMModule +{ + /// + /// The inputsize parameter for the LSTM module. + /// + [Description("The inputsize parameter for the LSTM module")] + public long InputSize { get; set; } + + /// + /// The hiddensize parameter for the LSTM module. + /// + [Description("The hiddensize parameter for the LSTM module")] + public long HiddenSize { get; set; } + + /// + /// The numlayers parameter for the LSTM module. + /// + [Description("The numlayers parameter for the LSTM module")] + public long NumLayers { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The batchfirst parameter for the LSTM module. + /// + [Description("The batchfirst parameter for the LSTM module")] + public bool BatchFirst { get; set; } = false; + + /// + /// The dropout parameter for the LSTM module. + /// + [Description("The dropout parameter for the LSTM module")] + public double Dropout { get; set; } = 0; + + /// + /// The bidirectional parameter for the LSTM module. + /// + [Description("The bidirectional parameter for the LSTM module")] + public bool Bidirectional { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a LSTM module. + /// + public IObservable> Process() + { + return Observable.Return(LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs new file mode 100644 index 00000000..8e3a6336 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a MaxUnpool1d module module. +/// +[Combinator] +[Description("Creates a MaxUnpool1d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MaxUnpool1dModule +{ + /// + /// The kernelsize parameter for the MaxUnpool1d module. + /// + [Description("The kernelsize parameter for the MaxUnpool1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the MaxUnpool1d module. + /// + [Description("The stride parameter for the MaxUnpool1d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the MaxUnpool1d module. + /// + [Description("The padding parameter for the MaxUnpool1d module")] + public long? Padding { get; set; } = null; + + /// + /// Generates an observable sequence that creates a MaxUnpool1d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs new file mode 100644 index 00000000..782828c4 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a MaxUnpool2d module module. +/// +[Combinator] +[Description("Creates a MaxUnpool2d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MaxUnpool2dModule +{ + /// + /// The kernelsize parameter for the MaxUnpool2d module. + /// + [Description("The kernelsize parameter for the MaxUnpool2d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the MaxUnpool2d module. + /// + [Description("The stride parameter for the MaxUnpool2d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the MaxUnpool2d module. + /// + [Description("The padding parameter for the MaxUnpool2d module")] + public long? Padding { get; set; } = null; + + /// + /// Generates an observable sequence that creates a MaxUnpool2d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs new file mode 100644 index 00000000..d6e64d45 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a MaxUnpool3d module module. +/// +[Combinator] +[Description("Creates a MaxUnpool3d module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MaxUnpool3dModule +{ + /// + /// The kernelsize parameter for the MaxUnpool3d module. + /// + [Description("The kernelsize parameter for the MaxUnpool3d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the MaxUnpool3d module. + /// + [Description("The stride parameter for the MaxUnpool3d module")] + public long? Stride { get; set; } = null; + + /// + /// The padding parameter for the MaxUnpool3d module. + /// + [Description("The padding parameter for the MaxUnpool3d module")] + public long? Padding { get; set; } = null; + + /// + /// Generates an observable sequence that creates a MaxUnpool3d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs new file mode 100644 index 00000000..e2192a9e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs @@ -0,0 +1,75 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a Multi-head attention mechanism module. +/// +[Combinator] +[Description("Creates a Multi-head attention mechanism module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MultiheadAttentionModule +{ + /// + /// The embedded_dim parameter for the MultiheadAttention module. + /// + [Description("The embedded_dim parameter for the MultiheadAttention module")] + public long EmbeddedDim { get; set; } + + /// + /// The num_heads parameter for the MultiheadAttention module. + /// + [Description("The num_heads parameter for the MultiheadAttention module")] + public long NumHeads { get; set; } + + /// + /// The dropout parameter for the MultiheadAttention module. + /// + [Description("The dropout parameter for the MultiheadAttention module")] + public double Dropout { get; set; } = 0; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The add_bias_kv parameter for the MultiheadAttention module. + /// + [Description("The add_bias_kv parameter for the MultiheadAttention module")] + public bool AddBiasKv { get; set; } = false; + + /// + /// The add_zero_attn parameter for the MultiheadAttention module. + /// + [Description("The add_zero_attn parameter for the MultiheadAttention module")] + public bool AddZeroAttn { get; set; } = false; + + /// + /// The kdim parameter for the MultiheadAttention module. + /// + [Description("The kdim parameter for the MultiheadAttention module")] + public long? Kdim { get; set; } = null; + + /// + /// The vdim parameter for the MultiheadAttention module. + /// + [Description("The vdim parameter for the MultiheadAttention module")] + public long? Vdim { get; set; } = null; + + /// + /// Generates an observable sequence that creates a MultiheadAttention module. + /// + public IObservable>> Process() + { + return Observable.Return(MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKv, AddZeroAttn, Kdim, Vdim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs new file mode 100644 index 00000000..589b3030 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs @@ -0,0 +1,45 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a PairwiseDistance module module. +/// +[Combinator] +[Description("Creates a PairwiseDistance module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PairwiseDistanceModule +{ + /// + /// The p parameter for the PairwiseDistance module. + /// + [Description("The p parameter for the PairwiseDistance module")] + public double P { get; set; } = 2; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-06; + + /// + /// The keep_dim parameter for the PairwiseDistance module. + /// + [Description("The keep_dim parameter for the PairwiseDistance module")] + public bool KeepDim { get; set; } = false; + + /// + /// Generates an observable sequence that creates a PairwiseDistance module. + /// + public IObservable> Process() + { + return Observable.Return(PairwiseDistance(P, Eps, KeepDim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs new file mode 100644 index 00000000..159111e2 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs @@ -0,0 +1,65 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a RNNCell module module. +/// +[Combinator] +[Description("Creates a RNNCell module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class RNNCellModule +{ + /// + /// The inputsize parameter for the RNNCell module. + /// + [Description("The inputsize parameter for the RNNCell module")] + public long InputSize { get; set; } + + /// + /// The hiddensize parameter for the RNNCell module. + /// + [Description("The hiddensize parameter for the RNNCell module")] + public long HiddenSize { get; set; } + + /// + /// The nonlinearity parameter for the RNNCell module. + /// + [Description("The nonlinearity parameter for the RNNCell module")] + public NonLinearities NonLinearity { get; set; } = NonLinearities.Tanh; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a RNNCell module. + /// + public IObservable> Process() + { + return Observable.Return(RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs new file mode 100644 index 00000000..8488acfe --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs @@ -0,0 +1,89 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a Basic RNN layer module. +/// +[Combinator] +[Description("Creates a Basic RNN layer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class RNNModule +{ + /// + /// The inputsize parameter for the RNN module. + /// + [Description("The inputsize parameter for the RNN module")] + public long InputSize { get; set; } + + /// + /// The hiddensize parameter for the RNN module. + /// + [Description("The hiddensize parameter for the RNN module")] + public long HiddenSize { get; set; } + + /// + /// The numlayers parameter for the RNN module. + /// + [Description("The numlayers parameter for the RNN module")] + public long NumLayers { get; set; } = 1; + + /// + /// The nonlinearity parameter for the RNN module. + /// + [Description("The nonlinearity parameter for the RNN module")] + public NonLinearities NonLinearity { get; set; } = NonLinearities.Tanh; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The batchfirst parameter for the RNN module. + /// + [Description("The batchfirst parameter for the RNN module")] + public bool BatchFirst { get; set; } = false; + + /// + /// The dropout parameter for the RNN module. + /// + [Description("The dropout parameter for the RNN module")] + public double Dropout { get; set; } = 0; + + /// + /// The bidirectional parameter for the RNN module. + /// + [Description("The bidirectional parameter for the RNN module")] + public bool Bidirectional { get; set; } = false; + + /// + /// The desired device of returned tensor. + /// + [Description("The desired device of returned tensor")] + [XmlIgnore] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Generates an observable sequence that creates a RNN module. + /// + public IObservable> Process() + { + return Observable.Return(RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs new file mode 100644 index 00000000..05dcce1b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs @@ -0,0 +1,57 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a TransformerDecoderLayer module module. +/// +[Combinator] +[Description("Creates a TransformerDecoderLayer module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TransformerDecoderLayerModule +{ + /// + /// The d_model parameter for the TransformerDecoderLayer module. + /// + [Description("The d_model parameter for the TransformerDecoderLayer module")] + public long DModel { get; set; } = 512; + + /// + /// The nhead parameter for the TransformerDecoderLayer module. + /// + [Description("The nhead parameter for the TransformerDecoderLayer module")] + public long Nhead { get; set; } = 8; + + /// + /// The dim_feedforward parameter for the TransformerDecoderLayer module. + /// + [Description("The dim_feedforward parameter for the TransformerDecoderLayer module")] + public long DimFeedforward { get; set; } = 2048; + + /// + /// The dropout parameter for the TransformerDecoderLayer module. + /// + [Description("The dropout parameter for the TransformerDecoderLayer module")] + public double Dropout { get; set; } = 0.1; + + /// + /// The activation parameter for the TransformerDecoderLayer module. + /// + [Description("The activation parameter for the TransformerDecoderLayer module")] + public Activations Activation { get; set; } = Activations.ReLU; + + /// + /// Generates an observable sequence that creates a TransformerDecoderLayer module. + /// + public IObservable> Process() + { + return Observable.Return(TransformerDecoderLayer(DModel, Nhead, DimFeedforward, Dropout, Activation)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs new file mode 100644 index 00000000..538cf532 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a TransformerDecoder module module. +/// +[Combinator] +[Description("Creates a TransformerDecoder module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TransformerDecoderModule +{ + /// + /// The decoder_layer parameter for the TransformerDecoder module. + /// + [Description("The decoder_layer parameter for the TransformerDecoder module")] + public TransformerDecoderLayer DecoderLayer { get; set; } + + /// + /// The num_layers parameter for the TransformerDecoder module. + /// + [Description("The num_layers parameter for the TransformerDecoder module")] + public long NumLayers { get; set; } + + /// + /// Generates an observable sequence that creates a TransformerDecoder module. + /// + public IObservable> Process() + { + return Observable.Return(TransformerDecoder(DecoderLayer, NumLayers)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs new file mode 100644 index 00000000..6a93568e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs @@ -0,0 +1,69 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.OtherModules; + +/// +/// Creates a Transformer module module. +/// +[Combinator] +[Description("Creates a Transformer module module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TransformerModule +{ + /// + /// The d_model parameter for the Transformer module. + /// + [Description("The d_model parameter for the Transformer module")] + public long DModel { get; set; } = 512; + + /// + /// The nhead parameter for the Transformer module. + /// + [Description("The nhead parameter for the Transformer module")] + public long Nhead { get; set; } = 8; + + /// + /// The num_encoder_layers parameter for the Transformer module. + /// + [Description("The num_encoder_layers parameter for the Transformer module")] + public long NumEncoderLayers { get; set; } = 6; + + /// + /// The num_decoder_layers parameter for the Transformer module. + /// + [Description("The num_decoder_layers parameter for the Transformer module")] + public long NumDecoderLayers { get; set; } = 6; + + /// + /// The dim_feedforward parameter for the Transformer module. + /// + [Description("The dim_feedforward parameter for the Transformer module")] + public long DimFeedforward { get; set; } = 2048; + + /// + /// The dropout parameter for the Transformer module. + /// + [Description("The dropout parameter for the Transformer module")] + public double Dropout { get; set; } = 0.1; + + /// + /// The activation parameter for the Transformer module. + /// + [Description("The activation parameter for the Transformer module")] + public Activations Activation { get; set; } = Activations.ReLU; + + /// + /// Generates an observable sequence that creates a Transformer module. + /// + public IObservable> Process() + { + return Observable.Return(Transformer(DModel, Nhead, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs index da4a2cd9..bcf6db88 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs @@ -2,6 +2,9 @@ using System.ComponentModel; using System.Reactive.Linq; using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets { @@ -19,7 +22,7 @@ public class SaveModel /// [Description("The model to save.")] [XmlIgnore] - public ITorchModule Model { get; set; } + public IModule Model { get; set; } /// /// The path to save the model. @@ -36,8 +39,10 @@ public class SaveModel /// public IObservable Process(IObservable source) { - return source.Do(input => { - Model.Module.save(ModelPath); + var model = Model as Module; + return source.Do(input => + { + model.save(ModelPath); }); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/TorchModuleAdapter.cs b/src/Bonsai.ML.Torch/NeuralNets/TorchModuleAdapter.cs deleted file mode 100644 index 3ec35071..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/TorchModuleAdapter.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Reflection; -using static TorchSharp.torch; - -namespace Bonsai.ML.Torch.NeuralNets -{ - /// - /// Represents a torch module adapter that wraps a torch module or script module. - /// - public class TorchModuleAdapter : ITorchModule - { - private readonly nn.Module _module = null; - - private readonly jit.ScriptModule _scriptModule = null; - - private readonly Func _forwardFunc; - - /// - /// The module. - /// - public nn.Module Module { get; } - - /// - /// Initializes a new instance of the class. - /// - /// - public TorchModuleAdapter(nn.Module module) - { - _module = module; - _forwardFunc = _module.forward; - Module = _module; - } - - /// - /// Initializes a new instance of the class. - /// - /// - public TorchModuleAdapter(jit.ScriptModule scriptModule) - { - _scriptModule = scriptModule; - _forwardFunc = _scriptModule.forward; - Module = _scriptModule; - } - - /// - public Tensor Forward(Tensor input) - { - return _forwardFunc(input); - } - } -} \ No newline at end of file From 7261825bd0df0443f9c849faa0e4854da78e1680 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 8 Oct 2025 20:39:10 +0100 Subject: [PATCH 02/30] Added learning rate schedulers --- src/Bonsai.ML.Torch/NeuralNets/Backward.cs | 16 ++- .../ConstantLRLRSchedulerModule.cs | 60 +++++++++ .../CosineAnnealingLRLRSchedulerModule.cs | 60 +++++++++ .../ExponentialLRLRSchedulerModule.cs | 54 ++++++++ .../LinearLRLRSchedulerModule.cs | 66 ++++++++++ .../MultiStepLRLRSchedulerModule.cs | 60 +++++++++ .../OneCycleLRLRSchedulerModule.cs | 120 ++++++++++++++++++ .../PolynomialLRLRSchedulerModule.cs | 60 +++++++++ .../ReduceLROnPlateauLRSchedulerModule.cs | 90 +++++++++++++ .../StepLRLRSchedulerModule.cs | 60 +++++++++ 10 files changed, 643 insertions(+), 3 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs index f46425d4..d4b1ffc0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs @@ -5,6 +5,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets { @@ -20,7 +21,12 @@ public class Backward /// /// The optimizer to use for training. /// - public Optimizer Optimizer { get; set; } + public Optimizer Optimizer { get; set; } = null; + + /// + /// The learning rate scheduler to use for training. + /// + public LRScheduler LRScheduler { get; set; } = null; /// /// The model to train. @@ -31,7 +37,7 @@ public class Backward /// /// The loss function to use for training. /// - public IModule Loss { get; set; } + public IModule Loss { get; set; } = null; /// /// Trains the model using backpropagation. @@ -41,8 +47,12 @@ public class Backward public IObservable Process(IObservable> source) { var model = Model as Module; + + Loss ??= NLLLoss(); var loss = Loss as Module; - var scheduler = lr_scheduler.StepLR(Optimizer, 1, 0.7); + + Optimizer ??= SGD(model.parameters(), 0.01); + LRScheduler ??= StepLR(Optimizer, 1, 0.7); model.train(); return source.Select((input) => { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs new file mode 100644 index 00000000..09e58ee3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a ConstantLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a ConstantLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConstantLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the ConstantLR module. + /// + [Description("The optimizer parameter for the ConstantLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The factor parameter for the ConstantLR module. + /// + [Description("The factor parameter for the ConstantLR module")] + public double Factor { get; set; } = 0.3333333333333333; + + /// + /// The total_iters parameter for the ConstantLR module. + /// + [Description("The total_iters parameter for the ConstantLR module")] + public int TotalIters { get; set; } = 5; + + /// + /// The last_epoch parameter for the ConstantLR module. + /// + [Description("The last_epoch parameter for the ConstantLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the ConstantLR module. + /// + [Description("The verbose parameter for the ConstantLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ConstantLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(ConstantLR(Optimizer, Factor, TotalIters, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs new file mode 100644 index 00000000..d9670350 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a CosineAnnealingLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a CosineAnnealingLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class CosineAnnealingLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the CosineAnnealingLR module. + /// + [Description("The optimizer parameter for the CosineAnnealingLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The t_max parameter for the CosineAnnealingLR module. + /// + [Description("The t_max parameter for the CosineAnnealingLR module")] + public double TMax { get; set; } + + /// + /// The eta_min parameter for the CosineAnnealingLR module. + /// + [Description("The eta_min parameter for the CosineAnnealingLR module")] + public double EtaMin { get; set; } = 0; + + /// + /// The last_epoch parameter for the CosineAnnealingLR module. + /// + [Description("The last_epoch parameter for the CosineAnnealingLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the CosineAnnealingLR module. + /// + [Description("The verbose parameter for the CosineAnnealingLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a CosineAnnealingLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(CosineAnnealingLR(Optimizer, TMax, EtaMin, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs new file mode 100644 index 00000000..669bb087 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs @@ -0,0 +1,54 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a ExponentialLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a ExponentialLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ExponentialLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the ExponentialLR module. + /// + [Description("The optimizer parameter for the ExponentialLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The gamma parameter for the ExponentialLR module. + /// + [Description("The gamma parameter for the ExponentialLR module")] + public double Gamma { get; set; } = 0.1; + + /// + /// The last_epoch parameter for the ExponentialLR module. + /// + [Description("The last_epoch parameter for the ExponentialLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the ExponentialLR module. + /// + [Description("The verbose parameter for the ExponentialLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ExponentialLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(ExponentialLR(Optimizer, Gamma, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs new file mode 100644 index 00000000..d537586a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a LinearLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a LinearLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LinearLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the LinearLR module. + /// + [Description("The optimizer parameter for the LinearLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The start_factor parameter for the LinearLR module. + /// + [Description("The start_factor parameter for the LinearLR module")] + public double StartFactor { get; set; } = 0.3333333333333333; + + /// + /// The end_factor parameter for the LinearLR module. + /// + [Description("The end_factor parameter for the LinearLR module")] + public double EndFactor { get; set; } = 5; + + /// + /// The total_iters parameter for the LinearLR module. + /// + [Description("The total_iters parameter for the LinearLR module")] + public int TotalIters { get; set; } = 5; + + /// + /// The last_epoch parameter for the LinearLR module. + /// + [Description("The last_epoch parameter for the LinearLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the LinearLR module. + /// + [Description("The verbose parameter for the LinearLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a LinearLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(LinearLR(Optimizer, StartFactor, EndFactor, TotalIters, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs new file mode 100644 index 00000000..37d0d40c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a MultiStepLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a MultiStepLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class MultiStepLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the MultiStepLR module. + /// + [Description("The optimizer parameter for the MultiStepLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The milestones parameter for the MultiStepLR module. + /// + [Description("The milestones parameter for the MultiStepLR module")] + public IList Milestones { get; set; } + + /// + /// The gamma parameter for the MultiStepLR module. + /// + [Description("The gamma parameter for the MultiStepLR module")] + public double Gamma { get; set; } = 0.1; + + /// + /// The last_epoch parameter for the MultiStepLR module. + /// + [Description("The last_epoch parameter for the MultiStepLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the MultiStepLR module. + /// + [Description("The verbose parameter for the MultiStepLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a MultiStepLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(MultiStepLR(Optimizer, Milestones, Gamma, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs new file mode 100644 index 00000000..017ccb56 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs @@ -0,0 +1,120 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a OneCycleLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a OneCycleLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class OneCycleLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the OneCycleLR module. + /// + [Description("The optimizer parameter for the OneCycleLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The max_lr parameter for the OneCycleLR module. + /// + [Description("The max_lr parameter for the OneCycleLR module")] + public double MaxLr { get; set; } + + /// + /// The total_steps parameter for the OneCycleLR module. + /// + [Description("The total_steps parameter for the OneCycleLR module")] + public int TotalSteps { get; set; } = -1; + + /// + /// The epochs parameter for the OneCycleLR module. + /// + [Description("The epochs parameter for the OneCycleLR module")] + public int Epochs { get; set; } = -1; + + /// + /// The steps_per_epoch parameter for the OneCycleLR module. + /// + [Description("The steps_per_epoch parameter for the OneCycleLR module")] + public int StepsPerEpoch { get; set; } = -1; + + /// + /// The pct_start parameter for the OneCycleLR module. + /// + [Description("The pct_start parameter for the OneCycleLR module")] + public double PctStart { get; set; } = 0.3; + + /// + /// The anneal_strategy parameter for the OneCycleLR module. + /// + [Description("The anneal_strategy parameter for the OneCycleLR module")] + public impl.OneCycleLR.AnnealStrategy AnnealStrategy { get; set; } = impl.OneCycleLR.AnnealStrategy.Cos; + + /// + /// The cycle_momentum parameter for the OneCycleLR module. + /// + [Description("The cycle_momentum parameter for the OneCycleLR module")] + public bool CycleMomentum { get; set; } = true; + + /// + /// The base_momentum parameter for the OneCycleLR module. + /// + [Description("The base_momentum parameter for the OneCycleLR module")] + public double BaseMomentum { get; set; } = 0.85; + + /// + /// The max_momentum parameter for the OneCycleLR module. + /// + [Description("The max_momentum parameter for the OneCycleLR module")] + public double MaxMomentum { get; set; } = 0.95; + + /// + /// The div_factor parameter for the OneCycleLR module. + /// + [Description("The div_factor parameter for the OneCycleLR module")] + public double DivFactor { get; set; } = 25; + + /// + /// The final_div_factor parameter for the OneCycleLR module. + /// + [Description("The final_div_factor parameter for the OneCycleLR module")] + public double FinalDivFactor { get; set; } = 10000; + + /// + /// The three_phase parameter for the OneCycleLR module. + /// + [Description("The three_phase parameter for the OneCycleLR module")] + public bool ThreePhase { get; set; } = false; + + /// + /// The last_epoch parameter for the OneCycleLR module. + /// + [Description("The last_epoch parameter for the OneCycleLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the OneCycleLR module. + /// + [Description("The verbose parameter for the OneCycleLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a OneCycleLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(OneCycleLR(Optimizer, MaxLr, TotalSteps, Epochs, StepsPerEpoch, PctStart, AnnealStrategy, CycleMomentum, BaseMomentum, MaxMomentum, DivFactor, FinalDivFactor, ThreePhase, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs new file mode 100644 index 00000000..fc5b7ccf --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a PolynomialLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a PolynomialLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PolynomialLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the PolynomialLR module. + /// + [Description("The optimizer parameter for the PolynomialLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The total_iters parameter for the PolynomialLR module. + /// + [Description("The total_iters parameter for the PolynomialLR module")] + public int TotalIters { get; set; } = 5; + + /// + /// The power parameter for the PolynomialLR module. + /// + [Description("The power parameter for the PolynomialLR module")] + public int Power { get; set; } = 1; + + /// + /// The last_epoch parameter for the PolynomialLR module. + /// + [Description("The last_epoch parameter for the PolynomialLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the PolynomialLR module. + /// + [Description("The verbose parameter for the PolynomialLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a PolynomialLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(PolynomialLR(Optimizer, TotalIters, Power, LastEpoch, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs new file mode 100644 index 00000000..814ae561 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs @@ -0,0 +1,90 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a ReduceLROnPlateau learning rate scheduler module. +/// +[Combinator] +[Description("Creates a ReduceLROnPlateau learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ReduceLROnPlateauLRSchedulerModule +{ + /// + /// The optimizer parameter for the ReduceLROnPlateau module. + /// + [Description("The optimizer parameter for the ReduceLROnPlateau module")] + public Optimizer Optimizer { get; set; } + + /// + /// The mode parameter for the ReduceLROnPlateau module. + /// + [Description("The mode parameter for the ReduceLROnPlateau module")] + public string Mode { get; set; } = "min"; + + /// + /// The factor parameter for the ReduceLROnPlateau module. + /// + [Description("The factor parameter for the ReduceLROnPlateau module")] + public double Factor { get; set; } = 0.1; + + /// + /// The patience parameter for the ReduceLROnPlateau module. + /// + [Description("The patience parameter for the ReduceLROnPlateau module")] + public int Patience { get; set; } = 10; + + /// + /// The threshold parameter for the ReduceLROnPlateau module. + /// + [Description("The threshold parameter for the ReduceLROnPlateau module")] + public double Threshold { get; set; } = 0.0001; + + /// + /// The threshold_mode parameter for the ReduceLROnPlateau module. + /// + [Description("The threshold_mode parameter for the ReduceLROnPlateau module")] + public string ThresholdMode { get; set; } = "rel"; + + /// + /// The cooldown parameter for the ReduceLROnPlateau module. + /// + [Description("The cooldown parameter for the ReduceLROnPlateau module")] + public int Cooldown { get; set; } = 0; + + /// + /// The min_lr parameter for the ReduceLROnPlateau module. + /// + [Description("The min_lr parameter for the ReduceLROnPlateau module")] + public IList MinLr { get; set; } = null; + + /// + /// A value added to the denominator for numerical stability. + /// + [Description("A value added to the denominator for numerical stability")] + public double Eps { get; set; } = 1E-08; + + /// + /// The verbose parameter for the ReduceLROnPlateau module. + /// + [Description("The verbose parameter for the ReduceLROnPlateau module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a ReduceLROnPlateauLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(ReduceLROnPlateau(Optimizer, Mode, Factor, Patience, Threshold, ThresholdMode, Cooldown, MinLr, Eps, Verbose)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs new file mode 100644 index 00000000..cf0ba974 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; + +namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; + +/// +/// Creates a StepLR learning rate scheduler module. +/// +[Combinator] +[Description("Creates a StepLR learning rate scheduler module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class StepLRLRSchedulerModule +{ + /// + /// The optimizer parameter for the StepLR module. + /// + [Description("The optimizer parameter for the StepLR module")] + public Optimizer Optimizer { get; set; } + + /// + /// The step_size parameter for the StepLR module. + /// + [Description("The step_size parameter for the StepLR module")] + public int StepSize { get; set; } + + /// + /// The gamma parameter for the StepLR module. + /// + [Description("The gamma parameter for the StepLR module")] + public double Gamma { get; set; } = 0.1; + + /// + /// The last_epoch parameter for the StepLR module. + /// + [Description("The last_epoch parameter for the StepLR module")] + public int LastEpoch { get; set; } = -1; + + /// + /// The verbose parameter for the StepLR module. + /// + [Description("The verbose parameter for the StepLR module")] + public bool Verbose { get; set; } = false; + + /// + /// Generates an observable sequence that creates a StepLRLRScheduler module. + /// + public IObservable Process() + { + return Observable.Return(StepLR(Optimizer, StepSize, Gamma, LastEpoch, Verbose)); + } +} From eefefeaeb04ca36099659adf4ca068ce7a6e5437 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 9 Oct 2025 14:09:52 +0100 Subject: [PATCH 03/30] Updated with improved naming --- ...le.cs => ConstantLearningRateScheduler.cs} | 12 ++++---- ...> CosineAnnealingLearningRateScheduler.cs} | 12 ++++---- ...cs => ExponentialLearningRateScheduler.cs} | 12 ++++---- ...dule.cs => LinearLearningRateScheduler.cs} | 14 +++++----- ...e.cs => MultiStepLearningRateScheduler.cs} | 12 ++++---- ...le.cs => OneCycleLearningRateScheduler.cs} | 20 ++++++------- ....cs => PolynomialLearningRateScheduler.cs} | 10 +++---- ...> ReduceLearningRateOnPlateauScheduler.cs} | 16 +++++------ ...Module.cs => StepLearningRateScheduler.cs} | 12 ++++---- .../Losses/{BCELossModule.cs => BCELoss.cs} | 18 ++++-------- ...gitsLossModule.cs => BCEWithLogitsLoss.cs} | 22 +++++---------- .../Losses/{CTCLossModule.cs => CTCLoss.cs} | 9 +++--- ...ngLossModule.cs => CosineEmbeddingLoss.cs} | 11 ++++---- ...tropyLossModule.cs => CrossEntropyLoss.cs} | 18 ++++-------- ...ianNLLLossModule.cs => GaussianNLLLoss.cs} | 9 +++--- ...ingLossModule.cs => HingeEmbeddingLoss.cs} | 11 ++++---- .../{HuberLossModule.cs => HuberLoss.cs} | 11 ++++---- .../{KLDivLossModule.cs => KLDivLoss.cs} | 9 +++--- .../Losses/{L1LossModule.cs => L1Loss.cs} | 9 +++--- .../Losses/{MSELossModule.cs => MSELoss.cs} | 9 +++--- ...kingLossModule.cs => MarginRankingLoss.cs} | 11 ++++---- ...nLossModule.cs => MultiLabelMarginLoss.cs} | 9 +++--- ...sModule.cs => MultiLabelSoftMarginLoss.cs} | 18 ++++-------- ...MarginLossModule.cs => MultiMarginLoss.cs} | 20 +++++-------- .../Losses/{NLLLossModule.cs => NLLLoss.cs} | 18 ++++-------- ...ssonNLLLossModule.cs => PoissonNLLLoss.cs} | 9 +++--- ...{SmoothL1LossModule.cs => SmoothL1Loss.cs} | 11 ++++---- ...tMarginLossModule.cs => SoftMarginLoss.cs} | 9 +++--- ...rginLossModule.cs => TripletMarginLoss.cs} | 13 +++++---- .../Modules/AdaptiveAvgPool1dModule.cs | 7 +++-- .../Modules/AdaptiveAvgPool2dModule.cs | 7 +++-- .../Modules/AdaptiveAvgPool3dModule.cs | 7 +++-- .../Modules/AdaptiveMaxPool1dModule.cs | 7 +++-- .../Modules/AdaptiveMaxPool2dModule.cs | 7 +++-- .../Modules/AdaptiveMaxPool3dModule.cs | 7 +++-- .../NeuralNets/Modules/AlphaDropoutModule.cs | 9 +++--- .../NeuralNets/Modules/AvgPool1dModule.cs | 7 +++-- .../NeuralNets/Modules/AvgPool2dModule.cs | 7 +++-- .../NeuralNets/Modules/AvgPool3dModule.cs | 7 +++-- .../NeuralNets/Modules/BatchNorm1dModule.cs | 14 +++++----- .../NeuralNets/Modules/BatchNorm2dModule.cs | 14 +++++----- .../NeuralNets/Modules/BatchNorm3dModule.cs | 14 +++++----- .../NeuralNets/Modules/CELUModule.cs | 9 +++--- .../Modules/ChannelShuffleModule.cs | 7 +++-- .../NeuralNets/Modules/ConstantPad1dModule.cs | 7 +++-- .../NeuralNets/Modules/ConstantPad2dModule.cs | 7 +++-- .../NeuralNets/Modules/ConstantPad3dModule.cs | 7 +++-- .../NeuralNets/Modules/Conv1dModule.cs | 10 +++---- .../NeuralNets/Modules/Conv2dModule.cs | 10 +++---- .../NeuralNets/Modules/Conv3dModule.cs | 10 +++---- .../Modules/ConvTranspose1dModule.cs | 10 +++---- .../Modules/ConvTranspose2dModule.cs | 10 +++---- .../Modules/ConvTranspose3dModule.cs | 10 +++---- .../NeuralNets/Modules/Dropout1dModule.cs | 9 +++--- .../NeuralNets/Modules/Dropout2dModule.cs | 9 +++--- .../NeuralNets/Modules/Dropout3dModule.cs | 9 +++--- .../NeuralNets/Modules/DropoutModule.cs | 9 +++--- .../NeuralNets/Modules/ELUModule.cs | 9 +++--- .../Modules/EmbeddingFromPretrainedModule.cs | 20 ++++++------- .../NeuralNets/Modules/EmbeddingModule.cs | 12 ++++---- .../Modules/FeatureAlphaDropoutModule.cs | 9 +++--- .../NeuralNets/Modules/FlattenModule.cs | 7 +++-- .../NeuralNets/Modules/FoldModule.cs | 7 +++-- .../Modules/FractionalMaxPool2dModule.cs | 7 +++-- .../Modules/FractionalMaxPool3dModule.cs | 7 +++-- .../NeuralNets/Modules/GELUModule.cs | 7 +++-- .../NeuralNets/Modules/GLUModule.cs | 7 +++-- .../NeuralNets/Modules/GroupNormModule.cs | 12 ++++---- .../NeuralNets/Modules/HardshrinkModule.cs | 9 +++--- .../NeuralNets/Modules/HardsigmoidModule.cs | 7 +++-- .../NeuralNets/Modules/HardswishModule.cs | 7 +++-- .../NeuralNets/Modules/HardtanhModule.cs | 11 ++++---- .../NeuralNets/Modules/IdentityModule.cs | 7 +++-- .../Modules/InstanceNorm1dModule.cs | 14 +++++----- .../Modules/InstanceNorm2dModule.cs | 14 +++++----- .../Modules/InstanceNorm3dModule.cs | 14 +++++----- .../NeuralNets/Modules/LPPool1dModule.cs | 7 +++-- .../NeuralNets/Modules/LPPool2dModule.cs | 7 +++-- .../NeuralNets/Modules/LayerNormModule.cs | 12 ++++---- .../NeuralNets/Modules/LeakyReLUModule.cs | 9 +++--- .../NeuralNets/Modules/LinearModule.cs | 10 +++---- .../Modules/LocalResponseNormModule.cs | 13 +++++---- .../NeuralNets/Modules/LogSigmoidModule.cs | 7 +++-- .../NeuralNets/Modules/LogSoftmaxModule.cs | 7 +++-- .../NeuralNets/Modules/MaxPool1dModule.cs | 7 +++-- .../NeuralNets/Modules/MaxPool2dModule.cs | 7 +++-- .../NeuralNets/Modules/MaxPool3dModule.cs | 7 +++-- .../NeuralNets/Modules/MishModule.cs | 7 +++-- .../NeuralNets/Modules/PReLUModule.cs | 12 ++++---- .../NeuralNets/Modules/PixelShuffleModule.cs | 7 +++-- .../Modules/PixelUnshuffleModule.cs | 7 +++-- .../NeuralNets/Modules/RReLUModule.cs | 11 ++++---- .../NeuralNets/Modules/ReLU6Module.cs | 7 +++-- .../NeuralNets/Modules/ReLUModule.cs | 7 +++-- .../Modules/ReflectionPad1dModule.cs | 7 +++-- .../Modules/ReflectionPad2dModule.cs | 7 +++-- .../Modules/ReflectionPad3dModule.cs | 7 +++-- .../Modules/ReplicationPad1dModule.cs | 7 +++-- .../Modules/ReplicationPad2dModule.cs | 7 +++-- .../Modules/ReplicationPad3dModule.cs | 7 +++-- .../NeuralNets/Modules/SELUModule.cs | 7 +++-- .../NeuralNets/Modules/SequentialModule.cs | 28 +++++++++++++++++++ .../NeuralNets/Modules/SiLUModule.cs | 7 +++-- .../NeuralNets/Modules/SigmoidModule.cs | 7 +++-- .../NeuralNets/Modules/Softmax2dModule.cs | 7 +++-- .../NeuralNets/Modules/SoftmaxModule.cs | 7 +++-- .../NeuralNets/Modules/SoftminModule.cs | 7 +++-- .../NeuralNets/Modules/SoftplusModule.cs | 11 ++++---- .../NeuralNets/Modules/SoftshrinkModule.cs | 9 +++--- .../NeuralNets/Modules/SoftsignModule.cs | 7 +++-- .../NeuralNets/Modules/TanhModule.cs | 7 +++-- .../NeuralNets/Modules/TanhshrinkModule.cs | 7 +++-- .../NeuralNets/Modules/ThresholdModule.cs | 7 +++-- .../Modules/TransformerEncoderLayerModule.cs | 9 +++--- .../Modules/TransformerEncoderModule.cs | 7 +++-- .../NeuralNets/Modules/UnflattenModule.cs | 7 +++-- .../NeuralNets/Modules/UnfoldModule.cs | 7 +++-- .../NeuralNets/Modules/UpsampleModule.cs | 7 +++-- .../NeuralNets/Modules/ZeroPad2dModule.cs | 7 +++-- ...SGDOptimizerModule.cs => ASGDOptimizer.cs} | 20 ++++++------- ...ptimizerModule.cs => AdadeltaOptimizer.cs} | 19 +++++++------ ...OptimizerModule.cs => AdagradOptimizer.cs} | 21 +++++++------- ...damOptimizerModule.cs => AdamOptimizer.cs} | 20 ++++++------- ...mWOptimizerModule.cs => AdamWOptimizer.cs} | 20 ++++++------- ...xOptimizerModule.cs => AdamaxOptimizer.cs} | 21 +++++++------- ...GSOptimizerModule.cs => LBFGSOptimizer.cs} | 18 ++++++------ ...OptimizerModule.cs => RMSPropOptimizer.cs} | 20 ++++++------- ...opOptimizerModule.cs => RpropOptimizer.cs} | 20 ++++++------- ...{SGDOptimizerModule.cs => SGDOptimizer.cs} | 16 +++++------ .../NeuralNets/OtherModules/BilinearModule.cs | 10 +++---- .../OtherModules/CosineSimilarityModule.cs | 9 +++--- .../OtherModules/EmbeddingBagModule.cs | 12 ++++---- .../EmbeddingbagFromPretrainedModule.cs | 20 ++++++------- .../NeuralNets/OtherModules/GRUCellModule.cs | 10 +++---- .../NeuralNets/OtherModules/GRUModule.cs | 12 ++++---- .../NeuralNets/OtherModules/LSTMCellModule.cs | 10 +++---- .../NeuralNets/OtherModules/LSTMModule.cs | 12 ++++---- .../OtherModules/MaxUnpool1dModule.cs | 9 +++--- .../OtherModules/MaxUnpool2dModule.cs | 9 +++--- .../OtherModules/MaxUnpool3dModule.cs | 9 +++--- .../OtherModules/MultiheadAttentionModule.cs | 11 ++++---- .../OtherModules/PairwiseDistanceModule.cs | 11 ++++---- .../NeuralNets/OtherModules/RNNCellModule.cs | 10 +++---- .../NeuralNets/OtherModules/RNNModule.cs | 14 +++++----- .../TransformerDecoderLayerModule.cs | 9 +++--- .../OtherModules/TransformerDecoderModule.cs | 7 +++-- .../OtherModules/TransformerModule.cs | 9 +++--- 147 files changed, 817 insertions(+), 734 deletions(-) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{ConstantLRLRSchedulerModule.cs => ConstantLearningRateScheduler.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{CosineAnnealingLRLRSchedulerModule.cs => CosineAnnealingLearningRateScheduler.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{ExponentialLRLRSchedulerModule.cs => ExponentialLearningRateScheduler.cs} (83%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{LinearLRLRSchedulerModule.cs => LinearLearningRateScheduler.cs} (84%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{MultiStepLRLRSchedulerModule.cs => MultiStepLearningRateScheduler.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{OneCycleLRLRSchedulerModule.cs => OneCycleLearningRateScheduler.cs} (88%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{PolynomialLRLRSchedulerModule.cs => PolynomialLearningRateScheduler.cs} (87%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{ReduceLROnPlateauLRSchedulerModule.cs => ReduceLearningRateOnPlateauScheduler.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/{StepLRLRSchedulerModule.cs => StepLearningRateScheduler.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{BCELossModule.cs => BCELoss.cs} (66%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{BCEWithLogitsLossModule.cs => BCEWithLogitsLoss.cs} (69%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{CTCLossModule.cs => CTCLoss.cs} (86%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{CosineEmbeddingLossModule.cs => CosineEmbeddingLoss.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{CrossEntropyLossModule.cs => CrossEntropyLoss.cs} (74%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{GaussianNLLLossModule.cs => GaussianNLLLoss.cs} (88%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{HingeEmbeddingLossModule.cs => HingeEmbeddingLoss.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{HuberLossModule.cs => HuberLoss.cs} (84%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{KLDivLossModule.cs => KLDivLoss.cs} (88%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{L1LossModule.cs => L1Loss.cs} (79%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{MSELossModule.cs => MSELoss.cs} (79%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{MarginRankingLossModule.cs => MarginRankingLoss.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{MultiLabelMarginLossModule.cs => MultiLabelMarginLoss.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{MultiLabelSoftMarginLossModule.cs => MultiLabelSoftMarginLoss.cs} (69%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{MultiMarginLossModule.cs => MultiMarginLoss.cs} (74%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{NLLLossModule.cs => NLLLoss.cs} (66%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{PoissonNLLLossModule.cs => PoissonNLLLoss.cs} (90%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{SmoothL1LossModule.cs => SmoothL1Loss.cs} (83%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{SoftMarginLossModule.cs => SoftMarginLoss.cs} (84%) rename src/Bonsai.ML.Torch/NeuralNets/Losses/{TripletMarginLossModule.cs => TripletMarginLoss.cs} (86%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{ASGDOptimizerModule.cs => ASGDOptimizer.cs} (83%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{AdadeltaOptimizerModule.cs => AdadeltaOptimizer.cs} (83%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{AdagradOptimizerModule.cs => AdagradOptimizer.cs} (81%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{AdamOptimizerModule.cs => AdamOptimizer.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{AdamWOptimizerModule.cs => AdamWOptimizer.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{AdamaxOptimizerModule.cs => AdamaxOptimizer.cs} (81%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{LBFGSOptimizerModule.cs => LBFGSOptimizer.cs} (84%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{RMSPropOptimizerModule.cs => RMSPropOptimizer.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{RpropOptimizerModule.cs => RpropOptimizer.cs} (83%) rename src/Bonsai.ML.Torch/NeuralNets/Optimizers/{SGDOptimizerModule.cs => SGDOptimizer.cs} (88%) diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLearningRateScheduler.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLearningRateScheduler.cs index 09e58ee3..813aaca9 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLearningRateScheduler.cs @@ -13,24 +13,24 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a ConstantLR learning rate scheduler module. +/// Creates a ConstantLearningRate scheduler. /// [Combinator] -[Description("Creates a ConstantLR learning rate scheduler module.")] +[Description("Creates a ConstantLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ConstantLRLRSchedulerModule +public class ConstantLearningRateScheduler { /// /// The optimizer parameter for the ConstantLR module. /// [Description("The optimizer parameter for the ConstantLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The factor parameter for the ConstantLR module. /// [Description("The factor parameter for the ConstantLR module")] - public double Factor { get; set; } = 0.3333333333333333; + public double Factor { get; set; } = 0.3333333333333333D; /// /// The total_iters parameter for the ConstantLR module. @@ -51,7 +51,7 @@ public class ConstantLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a ConstantLRLRScheduler module. + /// Generates an observable sequence that creates a ConstantLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs index d9670350..09206d8e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs @@ -13,18 +13,18 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a CosineAnnealingLR learning rate scheduler module. +/// Creates a CosineAnnealingLearningRate scheduler. /// [Combinator] -[Description("Creates a CosineAnnealingLR learning rate scheduler module.")] +[Description("Creates a CosineAnnealingLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CosineAnnealingLRLRSchedulerModule +public class CosineAnnealingLearningRateScheduler { /// /// The optimizer parameter for the CosineAnnealingLR module. /// [Description("The optimizer parameter for the CosineAnnealingLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The t_max parameter for the CosineAnnealingLR module. @@ -36,7 +36,7 @@ public class CosineAnnealingLRLRSchedulerModule /// The eta_min parameter for the CosineAnnealingLR module. /// [Description("The eta_min parameter for the CosineAnnealingLR module")] - public double EtaMin { get; set; } = 0; + public double EtaMin { get; set; } = 0D; /// /// The last_epoch parameter for the CosineAnnealingLR module. @@ -51,7 +51,7 @@ public class CosineAnnealingLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a CosineAnnealingLRLRScheduler module. + /// Generates an observable sequence that creates a CosineAnnealingLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLearningRateScheduler.cs similarity index 83% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLearningRateScheduler.cs index 669bb087..cc7ee4c7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLearningRateScheduler.cs @@ -13,24 +13,24 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a ExponentialLR learning rate scheduler module. +/// Creates a ExponentialLearningRate scheduler. /// [Combinator] -[Description("Creates a ExponentialLR learning rate scheduler module.")] +[Description("Creates a ExponentialLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ExponentialLRLRSchedulerModule +public class ExponentialLearningRateScheduler { /// /// The optimizer parameter for the ExponentialLR module. /// [Description("The optimizer parameter for the ExponentialLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The gamma parameter for the ExponentialLR module. /// [Description("The gamma parameter for the ExponentialLR module")] - public double Gamma { get; set; } = 0.1; + public double Gamma { get; set; } = 0.1D; /// /// The last_epoch parameter for the ExponentialLR module. @@ -45,7 +45,7 @@ public class ExponentialLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a ExponentialLRLRScheduler module. + /// Generates an observable sequence that creates a ExponentialLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLearningRateScheduler.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLearningRateScheduler.cs index d537586a..d961f1d6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLearningRateScheduler.cs @@ -13,30 +13,30 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a LinearLR learning rate scheduler module. +/// Creates a LinearLearningRate scheduler. /// [Combinator] -[Description("Creates a LinearLR learning rate scheduler module.")] +[Description("Creates a LinearLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LinearLRLRSchedulerModule +public class LinearLearningRateScheduler { /// /// The optimizer parameter for the LinearLR module. /// [Description("The optimizer parameter for the LinearLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The start_factor parameter for the LinearLR module. /// [Description("The start_factor parameter for the LinearLR module")] - public double StartFactor { get; set; } = 0.3333333333333333; + public double StartFactor { get; set; } = 0.3333333333333333D; /// /// The end_factor parameter for the LinearLR module. /// [Description("The end_factor parameter for the LinearLR module")] - public double EndFactor { get; set; } = 5; + public double EndFactor { get; set; } = 5D; /// /// The total_iters parameter for the LinearLR module. @@ -57,7 +57,7 @@ public class LinearLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a LinearLRLRScheduler module. + /// Generates an observable sequence that creates a LinearLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLearningRateScheduler.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLearningRateScheduler.cs index 37d0d40c..5ccada37 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLearningRateScheduler.cs @@ -13,18 +13,18 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a MultiStepLR learning rate scheduler module. +/// Creates a MultiStepLearningRate scheduler. /// [Combinator] -[Description("Creates a MultiStepLR learning rate scheduler module.")] +[Description("Creates a MultiStepLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiStepLRLRSchedulerModule +public class MultiStepLearningRateScheduler { /// /// The optimizer parameter for the MultiStepLR module. /// [Description("The optimizer parameter for the MultiStepLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The milestones parameter for the MultiStepLR module. @@ -36,7 +36,7 @@ public class MultiStepLRLRSchedulerModule /// The gamma parameter for the MultiStepLR module. /// [Description("The gamma parameter for the MultiStepLR module")] - public double Gamma { get; set; } = 0.1; + public double Gamma { get; set; } = 0.1D; /// /// The last_epoch parameter for the MultiStepLR module. @@ -51,7 +51,7 @@ public class MultiStepLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a MultiStepLRLRScheduler module. + /// Generates an observable sequence that creates a MultiStepLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLearningRateScheduler.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLearningRateScheduler.cs index 017ccb56..541c2006 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLearningRateScheduler.cs @@ -13,18 +13,18 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a OneCycleLR learning rate scheduler module. +/// Creates a OneCycleLearningRate scheduler. /// [Combinator] -[Description("Creates a OneCycleLR learning rate scheduler module.")] +[Description("Creates a OneCycleLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class OneCycleLRLRSchedulerModule +public class OneCycleLearningRateScheduler { /// /// The optimizer parameter for the OneCycleLR module. /// [Description("The optimizer parameter for the OneCycleLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The max_lr parameter for the OneCycleLR module. @@ -54,7 +54,7 @@ public class OneCycleLRLRSchedulerModule /// The pct_start parameter for the OneCycleLR module. /// [Description("The pct_start parameter for the OneCycleLR module")] - public double PctStart { get; set; } = 0.3; + public double PctStart { get; set; } = 0.3D; /// /// The anneal_strategy parameter for the OneCycleLR module. @@ -72,25 +72,25 @@ public class OneCycleLRLRSchedulerModule /// The base_momentum parameter for the OneCycleLR module. /// [Description("The base_momentum parameter for the OneCycleLR module")] - public double BaseMomentum { get; set; } = 0.85; + public double BaseMomentum { get; set; } = 0.85D; /// /// The max_momentum parameter for the OneCycleLR module. /// [Description("The max_momentum parameter for the OneCycleLR module")] - public double MaxMomentum { get; set; } = 0.95; + public double MaxMomentum { get; set; } = 0.95D; /// /// The div_factor parameter for the OneCycleLR module. /// [Description("The div_factor parameter for the OneCycleLR module")] - public double DivFactor { get; set; } = 25; + public double DivFactor { get; set; } = 25D; /// /// The final_div_factor parameter for the OneCycleLR module. /// [Description("The final_div_factor parameter for the OneCycleLR module")] - public double FinalDivFactor { get; set; } = 10000; + public double FinalDivFactor { get; set; } = 10000D; /// /// The three_phase parameter for the OneCycleLR module. @@ -111,7 +111,7 @@ public class OneCycleLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a OneCycleLRLRScheduler module. + /// Generates an observable sequence that creates a OneCycleLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLearningRateScheduler.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLearningRateScheduler.cs index fc5b7ccf..e0114bd8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLearningRateScheduler.cs @@ -13,18 +13,18 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a PolynomialLR learning rate scheduler module. +/// Creates a PolynomialLearningRate scheduler. /// [Combinator] -[Description("Creates a PolynomialLR learning rate scheduler module.")] +[Description("Creates a PolynomialLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PolynomialLRLRSchedulerModule +public class PolynomialLearningRateScheduler { /// /// The optimizer parameter for the PolynomialLR module. /// [Description("The optimizer parameter for the PolynomialLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The total_iters parameter for the PolynomialLR module. @@ -51,7 +51,7 @@ public class PolynomialLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a PolynomialLRLRScheduler module. + /// Generates an observable sequence that creates a PolynomialLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs index 814ae561..6d45b009 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLROnPlateauLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs @@ -13,18 +13,18 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a ReduceLROnPlateau learning rate scheduler module. +/// Creates a ReduceLearningRateOnPlateau scheduler. /// [Combinator] -[Description("Creates a ReduceLROnPlateau learning rate scheduler module.")] +[Description("Creates a ReduceLearningRateOnPlateau scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ReduceLROnPlateauLRSchedulerModule +public class ReduceLearningRateOnPlateauScheduler { /// /// The optimizer parameter for the ReduceLROnPlateau module. /// [Description("The optimizer parameter for the ReduceLROnPlateau module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The mode parameter for the ReduceLROnPlateau module. @@ -36,7 +36,7 @@ public class ReduceLROnPlateauLRSchedulerModule /// The factor parameter for the ReduceLROnPlateau module. /// [Description("The factor parameter for the ReduceLROnPlateau module")] - public double Factor { get; set; } = 0.1; + public double Factor { get; set; } = 0.1D; /// /// The patience parameter for the ReduceLROnPlateau module. @@ -48,7 +48,7 @@ public class ReduceLROnPlateauLRSchedulerModule /// The threshold parameter for the ReduceLROnPlateau module. /// [Description("The threshold parameter for the ReduceLROnPlateau module")] - public double Threshold { get; set; } = 0.0001; + public double Threshold { get; set; } = 0.0001D; /// /// The threshold_mode parameter for the ReduceLROnPlateau module. @@ -72,7 +72,7 @@ public class ReduceLROnPlateauLRSchedulerModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08; + public double Eps { get; set; } = 1E-08D; /// /// The verbose parameter for the ReduceLROnPlateau module. @@ -81,7 +81,7 @@ public class ReduceLROnPlateauLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a ReduceLROnPlateauLRScheduler module. + /// Generates an observable sequence that creates a ReduceLearningRateOnPlateauScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLearningRateScheduler.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLearningRateScheduler.cs index cf0ba974..211f2b5a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLRLRSchedulerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLearningRateScheduler.cs @@ -13,18 +13,18 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; /// -/// Creates a StepLR learning rate scheduler module. +/// Creates a StepLearningRate scheduler. /// [Combinator] -[Description("Creates a StepLR learning rate scheduler module.")] +[Description("Creates a StepLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class StepLRLRSchedulerModule +public class StepLearningRateScheduler { /// /// The optimizer parameter for the StepLR module. /// [Description("The optimizer parameter for the StepLR module")] - public Optimizer Optimizer { get; set; } + public torch.optim.Optimizer Optimizer { get; set; } /// /// The step_size parameter for the StepLR module. @@ -36,7 +36,7 @@ public class StepLRLRSchedulerModule /// The gamma parameter for the StepLR module. /// [Description("The gamma parameter for the StepLR module")] - public double Gamma { get; set; } = 0.1; + public double Gamma { get; set; } = 0.1D; /// /// The last_epoch parameter for the StepLR module. @@ -51,7 +51,7 @@ public class StepLRLRSchedulerModule public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a StepLRLRScheduler module. + /// Generates an observable sequence that creates a StepLearningRateScheduler. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs similarity index 66% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs index c0cfea0b..2e09690a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a BCELoss module module. +/// Creates a BCELoss module. /// [Combinator] -[Description("Creates a BCELoss module module.")] +[Description("Creates a BCELoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class BCELossModule : IScalarTypeProvider +public class BCELoss { /// /// The weight parameter for the BCELoss module. /// [Description("The weight parameter for the BCELoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Weight { get; set; } = null; + public torch.Tensor Weight { get; set; } = null; /// /// The reduction parameter for the BCELoss module. @@ -32,12 +31,7 @@ public class BCELossModule : IScalarTypeProvider public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The scalar type for the module. - /// - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a BCELoss module. + /// Generates an observable sequence that creates a BCELoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs similarity index 69% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs index e9c553bc..579b73ab 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a BCEWithLogitsLoss module module. +/// Creates a BCEWithLogitsLoss module. /// [Combinator] -[Description("Creates a BCEWithLogitsLoss module module.")] +[Description("Creates a BCEWithLogitsLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class BCEWithLogitsLossModule : IScalarTypeProvider +public class BCEWithLogitsLoss { /// /// The weight parameter for the BCEWithLogitsLoss module. /// [Description("The weight parameter for the BCEWithLogitsLoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Weight { get; set; } = null; + public torch.Tensor Weight { get; set; } = null; /// /// The reduction parameter for the BCEWithLogitsLoss module. @@ -35,17 +34,10 @@ public class BCEWithLogitsLossModule : IScalarTypeProvider /// The pos_weights parameter for the BCEWithLogitsLoss module. /// [Description("The pos_weights parameter for the BCEWithLogitsLoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor PosWeights { get; set; } = null; + public torch.Tensor PosWeights { get; set; } = null; /// - /// The scalar type for the module. - /// - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a BCEWithLogitsLoss module. + /// Generates an observable sequence that creates a BCEWithLogitsLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLoss.cs similarity index 86% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLoss.cs index aec3bdfc..262d60cc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a CTCLoss module module. +/// Creates a CTCLoss module. /// [Combinator] -[Description("Creates a CTCLoss module module.")] +[Description("Creates a CTCLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CTCLossModule +public class CTCLoss { /// /// The blank parameter for the CTCLoss module. @@ -36,7 +37,7 @@ public class CTCLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a CTCLoss module. + /// Generates an observable sequence that creates a CTCLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLoss.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLoss.cs index 5e30f851..c7c6c2fd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,18 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a CosineEmbeddingLoss module module. +/// Creates a CosineEmbeddingLoss module. /// [Combinator] -[Description("Creates a CosineEmbeddingLoss module module.")] +[Description("Creates a CosineEmbeddingLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CosineEmbeddingLossModule +public class CosineEmbeddingLoss { /// /// The margin parameter for the CosineEmbeddingLoss module. /// [Description("The margin parameter for the CosineEmbeddingLoss module")] - public double Margin { get; set; } = 0; + public double Margin { get; set; } = 0D; /// /// The reduction parameter for the CosineEmbeddingLoss module. @@ -30,7 +31,7 @@ public class CosineEmbeddingLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a CosineEmbeddingLoss module. + /// Generates an observable sequence that creates a CosineEmbeddingLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLoss.cs similarity index 74% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLoss.cs index 3f89cef9..0711b743 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a CrossEntropyLoss module module. +/// Creates a CrossEntropyLoss module. /// [Combinator] -[Description("Creates a CrossEntropyLoss module module.")] +[Description("Creates a CrossEntropyLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CrossEntropyLossModule : IScalarTypeProvider +public class CrossEntropyLoss { /// /// The weight parameter for the CrossEntropyLoss module. /// [Description("The weight parameter for the CrossEntropyLoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Weight { get; set; } = null; + public torch.Tensor Weight { get; set; } = null; /// /// The ignore_index parameter for the CrossEntropyLoss module. @@ -38,12 +37,7 @@ public class CrossEntropyLossModule : IScalarTypeProvider public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The scalar type for the module. - /// - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a CrossEntropyLoss module. + /// Generates an observable sequence that creates a CrossEntropyLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLoss.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLoss.cs index 4f1aab07..fa8a531a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a GaussianNLLLoss module module. +/// Creates a GaussianNLLLoss module. /// [Combinator] -[Description("Creates a GaussianNLLLoss module module.")] +[Description("Creates a GaussianNLLLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class GaussianNLLLossModule +public class GaussianNLLLoss { /// /// The full parameter for the GaussianNLLLoss module. @@ -36,7 +37,7 @@ public class GaussianNLLLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a GaussianNLLLoss module. + /// Generates an observable sequence that creates a GaussianNLLLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLoss.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLoss.cs index f900857e..a1c11c8f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,18 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a HingeEmbeddingLoss module module. +/// Creates a HingeEmbeddingLoss module. /// [Combinator] -[Description("Creates a HingeEmbeddingLoss module module.")] +[Description("Creates a HingeEmbeddingLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HingeEmbeddingLossModule +public class HingeEmbeddingLoss { /// /// The margin parameter for the HingeEmbeddingLoss module. /// [Description("The margin parameter for the HingeEmbeddingLoss module")] - public double Margin { get; set; } = 1; + public double Margin { get; set; } = 1D; /// /// The reduction parameter for the HingeEmbeddingLoss module. @@ -30,7 +31,7 @@ public class HingeEmbeddingLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a HingeEmbeddingLoss module. + /// Generates an observable sequence that creates a HingeEmbeddingLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLoss.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLoss.cs index 5f3a87c2..6e931c80 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,18 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a HuberLoss module module. +/// Creates a HuberLoss module. /// [Combinator] -[Description("Creates a HuberLoss module module.")] +[Description("Creates a HuberLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HuberLossModule +public class HuberLoss { /// /// The delta parameter for the HuberLoss module. /// [Description("The delta parameter for the HuberLoss module")] - public double Delta { get; set; } = 1; + public double Delta { get; set; } = 1D; /// /// The reduction parameter for the HuberLoss module. @@ -30,7 +31,7 @@ public class HuberLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a HuberLoss module. + /// Generates an observable sequence that creates a HuberLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLoss.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLoss.cs index d0e88460..1092810b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a KLDivLoss module module. +/// Creates a KLDivLoss module. /// [Combinator] -[Description("Creates a KLDivLoss module module.")] +[Description("Creates a KLDivLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class KLDivLossModule +public class KLDivLoss { /// /// The log_target parameter for the KLDivLoss module. @@ -30,7 +31,7 @@ public class KLDivLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a KLDivLoss module. + /// Generates an observable sequence that creates a KLDivLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/L1Loss.cs similarity index 79% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/L1Loss.cs index 1ca1e550..7d89ac75 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/L1LossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/L1Loss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a L1Loss module module. +/// Creates a L1Loss module. /// [Combinator] -[Description("Creates a L1Loss module module.")] +[Description("Creates a L1Loss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class L1LossModule +public class L1Loss { /// /// The reduction parameter for the L1Loss module. @@ -24,7 +25,7 @@ public class L1LossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a L1Loss module. + /// Generates an observable sequence that creates a L1Loss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELoss.cs similarity index 79% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/MSELoss.cs index 3cf6a3fd..0c6fddac 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a MSELoss module module. +/// Creates a MSELoss module. /// [Combinator] -[Description("Creates a MSELoss module module.")] +[Description("Creates a MSELoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MSELossModule +public class MSELoss { /// /// The reduction parameter for the MSELoss module. @@ -24,7 +25,7 @@ public class MSELossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MSELoss module. + /// Generates an observable sequence that creates a MSELoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLoss.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLoss.cs index a40f8e98..f0458172 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,18 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a MarginRankingLoss module module. +/// Creates a MarginRankingLoss module. /// [Combinator] -[Description("Creates a MarginRankingLoss module module.")] +[Description("Creates a MarginRankingLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MarginRankingLossModule +public class MarginRankingLoss { /// /// The margin parameter for the MarginRankingLoss module. /// [Description("The margin parameter for the MarginRankingLoss module")] - public double Margin { get; set; } = 0; + public double Margin { get; set; } = 0D; /// /// The reduction parameter for the MarginRankingLoss module. @@ -30,7 +31,7 @@ public class MarginRankingLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MarginRankingLoss module. + /// Generates an observable sequence that creates a MarginRankingLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLoss.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLoss.cs index 278ed6cb..36cf82a2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a MultiLabelMarginLoss module module. +/// Creates a MultiLabelMarginLoss module. /// [Combinator] -[Description("Creates a MultiLabelMarginLoss module module.")] +[Description("Creates a MultiLabelMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiLabelMarginLossModule +public class MultiLabelMarginLoss { /// /// The reduction parameter for the MultiLabelMarginLoss module. @@ -24,7 +25,7 @@ public class MultiLabelMarginLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MultiLabelMarginLoss module. + /// Generates an observable sequence that creates a MultiLabelMarginLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLoss.cs similarity index 69% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLoss.cs index ea1dd046..f57061c6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a MultiLabelSoftMarginLoss module module. +/// Creates a MultiLabelSoftMarginLoss module. /// [Combinator] -[Description("Creates a MultiLabelSoftMarginLoss module module.")] +[Description("Creates a MultiLabelSoftMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiLabelSoftMarginLossModule : IScalarTypeProvider +public class MultiLabelSoftMarginLoss { /// /// The weight parameter for the MultiLabelSoftMarginLoss module. /// [Description("The weight parameter for the MultiLabelSoftMarginLoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Weight { get; set; } = null; + public torch.Tensor Weight { get; set; } = null; /// /// The reduction parameter for the MultiLabelSoftMarginLoss module. @@ -32,12 +31,7 @@ public class MultiLabelSoftMarginLossModule : IScalarTypeProvider public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The scalar type for the module. - /// - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a MultiLabelSoftMarginLoss module. + /// Generates an observable sequence that creates a MultiLabelSoftMarginLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLoss.cs similarity index 74% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLoss.cs index 592d9b4d..6fe9190b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a MultiMarginLoss module module. +/// Creates a MultiMarginLoss module. /// [Combinator] -[Description("Creates a MultiMarginLoss module module.")] +[Description("Creates a MultiMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiMarginLossModule : IScalarTypeProvider +public class MultiMarginLoss { /// /// The p parameter for the MultiMarginLoss module. @@ -27,15 +28,13 @@ public class MultiMarginLossModule : IScalarTypeProvider /// The margin parameter for the MultiMarginLoss module. /// [Description("The margin parameter for the MultiMarginLoss module")] - public double Margin { get; set; } = 1; + public double Margin { get; set; } = 1D; /// /// The weight parameter for the MultiMarginLoss module. /// [Description("The weight parameter for the MultiMarginLoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Weight { get; set; } = null; + public torch.Tensor Weight { get; set; } = null; /// /// The reduction parameter for the MultiMarginLoss module. @@ -44,12 +43,7 @@ public class MultiMarginLossModule : IScalarTypeProvider public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The scalar type for the module. - /// - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a MultiMarginLoss module. + /// Generates an observable sequence that creates a MultiMarginLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLoss.cs similarity index 66% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLoss.cs index 66d2b1f7..8e585c2b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a NLLLoss module module. +/// Creates a NLLLoss module. /// [Combinator] -[Description("Creates a NLLLoss module module.")] +[Description("Creates a NLLLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class NLLLossModule : IScalarTypeProvider +public class NLLLoss { /// /// The weight parameter for the NLLLoss module. /// [Description("The weight parameter for the NLLLoss module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Weight { get; set; } = null; + public torch.Tensor Weight { get; set; } = null; /// /// The reduction parameter for the NLLLoss module. @@ -32,12 +31,7 @@ public class NLLLossModule : IScalarTypeProvider public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The scalar type for the module. - /// - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a NLLLoss module. + /// Generates an observable sequence that creates a NLLLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLoss.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLoss.cs index 0a130002..382e7702 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a PoissonNLLLoss module module. +/// Creates a PoissonNLLLoss module. /// [Combinator] -[Description("Creates a PoissonNLLLoss module module.")] +[Description("Creates a PoissonNLLLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PoissonNLLLossModule +public class PoissonNLLLoss { /// /// The log_input parameter for the PoissonNLLLoss module. @@ -42,7 +43,7 @@ public class PoissonNLLLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a PoissonNLLLoss module. + /// Generates an observable sequence that creates a PoissonNLLLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1Loss.cs similarity index 83% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1Loss.cs index fbc3413f..ac383424 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1LossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1Loss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a SmoothL1Loss module module. +/// Creates a SmoothL1Loss module. /// [Combinator] -[Description("Creates a SmoothL1Loss module module.")] +[Description("Creates a SmoothL1Loss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SmoothL1LossModule +public class SmoothL1Loss { /// /// The reduction parameter for the SmoothL1Loss module. @@ -27,10 +28,10 @@ public class SmoothL1LossModule /// The beta parameter for the SmoothL1Loss module. /// [Description("The beta parameter for the SmoothL1Loss module")] - public double Beta { get; set; } = 1; + public double Beta { get; set; } = 1D; /// - /// Generates an observable sequence that creates a SmoothL1Loss module. + /// Generates an observable sequence that creates a SmoothL1Loss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLoss.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLoss.cs index 5f50acf4..60532b36 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,12 +11,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a SoftMarginLoss module module. +/// Creates a SoftMarginLoss module. /// [Combinator] -[Description("Creates a SoftMarginLoss module module.")] +[Description("Creates a SoftMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftMarginLossModule +public class SoftMarginLoss { /// /// The reduction parameter for the SoftMarginLoss module. @@ -24,7 +25,7 @@ public class SoftMarginLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a SoftMarginLoss module. + /// Generates an observable sequence that creates a SoftMarginLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLoss.cs similarity index 86% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLoss.cs index 4365fddf..48971eb8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLossModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLoss.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,18 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; /// -/// Creates a TripletMarginLoss module module. +/// Creates a TripletMarginLoss module. /// [Combinator] -[Description("Creates a TripletMarginLoss module module.")] +[Description("Creates a TripletMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TripletMarginLossModule +public class TripletMarginLoss { /// /// The margin parameter for the TripletMarginLoss module. /// [Description("The margin parameter for the TripletMarginLoss module")] - public double Margin { get; set; } = 1; + public double Margin { get; set; } = 1D; /// /// The p parameter for the TripletMarginLoss module. @@ -33,7 +34,7 @@ public class TripletMarginLossModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-06; + public double Eps { get; set; } = 1E-06D; /// /// The swap parameter for the TripletMarginLoss module. @@ -48,7 +49,7 @@ public class TripletMarginLossModule public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a TripletMarginLoss module. + /// Generates an observable sequence that creates a TripletMarginLoss. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs index ea468bf9..e1b2ad60 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D adaptive average pooling layer module. +/// Creates a 1D adaptive average pooling layer. /// [Combinator] -[Description("Creates a 1D adaptive average pooling layer module.")] +[Description("Creates a 1D adaptive average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AdaptiveAvgPool1dModule { @@ -24,7 +25,7 @@ public class AdaptiveAvgPool1dModule public long OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveAvgPool1d module. + /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs index e2bfc603..947d3a25 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D adaptive average pooling layer module. +/// Creates a 2D adaptive average pooling layer. /// [Combinator] -[Description("Creates a 2D adaptive average pooling layer module.")] +[Description("Creates a 2D adaptive average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AdaptiveAvgPool2dModule { @@ -25,7 +26,7 @@ public class AdaptiveAvgPool2dModule public long[] OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveAvgPool2d module. + /// Generates an observable sequence that creates a AdaptiveAvgPool2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs index a6a67a2b..b9b4c665 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D adaptive average pooling layer module. +/// Creates a 3D adaptive average pooling layer. /// [Combinator] -[Description("Creates a 3D adaptive average pooling layer module.")] +[Description("Creates a 3D adaptive average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AdaptiveAvgPool3dModule { @@ -25,7 +26,7 @@ public class AdaptiveAvgPool3dModule public long[] OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveAvgPool3d module. + /// Generates an observable sequence that creates a AdaptiveAvgPool3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs index 509e6e07..2cdd9d09 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D adaptive max pooling layer module. +/// Creates a 1D adaptive max pooling layer. /// [Combinator] -[Description("Creates a 1D adaptive max pooling layer module.")] +[Description("Creates a 1D adaptive max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AdaptiveMaxPool1dModule { @@ -24,7 +25,7 @@ public class AdaptiveMaxPool1dModule public long OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveMaxPool1d module. + /// Generates an observable sequence that creates a AdaptiveMaxPool1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs index 44fffd34..7a6d75f2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D adaptive max pooling layer module. +/// Creates a 2D adaptive max pooling layer. /// [Combinator] -[Description("Creates a 2D adaptive max pooling layer module.")] +[Description("Creates a 2D adaptive max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AdaptiveMaxPool2dModule { @@ -25,7 +26,7 @@ public class AdaptiveMaxPool2dModule public long[] OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveMaxPool2d module. + /// Generates an observable sequence that creates a AdaptiveMaxPool2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs index 3b12b029..0d9a5dcf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D adaptive max pooling layer module. +/// Creates a 3D adaptive max pooling layer. /// [Combinator] -[Description("Creates a 3D adaptive max pooling layer module.")] +[Description("Creates a 3D adaptive max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AdaptiveMaxPool3dModule { @@ -25,7 +26,7 @@ public class AdaptiveMaxPool3dModule public long[] OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveMaxPool3d module. + /// Generates an observable sequence that creates a AdaptiveMaxPool3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs index 9d9c017c..e68697e3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a AlphaDropout module module. +/// Creates a AlphaDropout module. /// [Combinator] -[Description("Creates a AlphaDropout module module.")] +[Description("Creates a AlphaDropout module.")] [WorkflowElementCategory(ElementCategory.Source)] public class AlphaDropoutModule { @@ -21,7 +22,7 @@ public class AlphaDropoutModule /// The p parameter for the AlphaDropout module. /// [Description("The p parameter for the AlphaDropout module")] - public double P { get; set; } = 0.5; + public double P { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class AlphaDropoutModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a AlphaDropout module. + /// Generates an observable sequence that creates a AlphaDropoutModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs index 1daa14b0..38f1b5fc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D average pooling layer module. +/// Creates a 1D average pooling layer. /// [Combinator] -[Description("Creates a 1D average pooling layer module.")] +[Description("Creates a 1D average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AvgPool1dModule { @@ -48,7 +49,7 @@ public class AvgPool1dModule public bool CountIncludePad { get; set; } = true; /// - /// Generates an observable sequence that creates a AvgPool1d module. + /// Generates an observable sequence that creates a AvgPool1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs index 9d3d7f75..7c37acf8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D average pooling layer module. +/// Creates a 2D average pooling layer. /// [Combinator] -[Description("Creates a 2D average pooling layer module.")] +[Description("Creates a 2D average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AvgPool2dModule { @@ -57,7 +58,7 @@ public class AvgPool2dModule public long? DivisorOverride { get; set; } = null; /// - /// Generates an observable sequence that creates a AvgPool2d module. + /// Generates an observable sequence that creates a AvgPool2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs index 416ff5e4..dab8d078 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D average pooling layer module. +/// Creates a 3D average pooling layer. /// [Combinator] -[Description("Creates a 3D average pooling layer module.")] +[Description("Creates a 3D average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class AvgPool3dModule { @@ -57,7 +58,7 @@ public class AvgPool3dModule public long? DivisorOverride { get; set; } = null; /// - /// Generates an observable sequence that creates a AvgPool3d module. + /// Generates an observable sequence that creates a AvgPool3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs index a1a299b3..8e421857 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D batch normalization layer module. +/// Creates a 1D batch normalization layer. /// [Combinator] -[Description("Creates a 1D batch normalization layer module.")] +[Description("Creates a 1D batch normalization layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class BatchNorm1dModule { @@ -27,13 +28,13 @@ public class BatchNorm1dModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1; + public double Momentum { get; set; } = 0.1D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -51,8 +52,7 @@ public class BatchNorm1dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,7 +62,7 @@ public class BatchNorm1dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a BatchNorm1d module. + /// Generates an observable sequence that creates a BatchNorm1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs index 7f3698fa..076df14d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D batch normalization layer module. +/// Creates a 2D batch normalization layer. /// [Combinator] -[Description("Creates a 2D batch normalization layer module.")] +[Description("Creates a 2D batch normalization layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class BatchNorm2dModule { @@ -27,13 +28,13 @@ public class BatchNorm2dModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1; + public double Momentum { get; set; } = 0.1D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -51,8 +52,7 @@ public class BatchNorm2dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,7 +62,7 @@ public class BatchNorm2dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a BatchNorm2d module. + /// Generates an observable sequence that creates a BatchNorm2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs index ea18e8bb..21ff9a0d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D batch normalization layer module. +/// Creates a 3D batch normalization layer. /// [Combinator] -[Description("Creates a 3D batch normalization layer module.")] +[Description("Creates a 3D batch normalization layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class BatchNorm3dModule { @@ -27,13 +28,13 @@ public class BatchNorm3dModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1; + public double Momentum { get; set; } = 0.1D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -51,8 +52,7 @@ public class BatchNorm3dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,7 +62,7 @@ public class BatchNorm3dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a BatchNorm3d module. + /// Generates an observable sequence that creates a BatchNorm3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs index b624eeb9..9ff0a74c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a CELU module module. +/// Creates a CELU module. /// [Combinator] -[Description("Creates a CELU module module.")] +[Description("Creates a CELU module.")] [WorkflowElementCategory(ElementCategory.Source)] public class CELUModule { @@ -21,7 +22,7 @@ public class CELUModule /// The alpha parameter for the CELU module. /// [Description("The alpha parameter for the CELU module")] - public double Alpha { get; set; } = 1; + public double Alpha { get; set; } = 1D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class CELUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a CELU module. + /// Generates an observable sequence that creates a CELUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs index 48016333..b6b800b8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ChannelShuffle module module. +/// Creates a ChannelShuffle module. /// [Combinator] -[Description("Creates a ChannelShuffle module module.")] +[Description("Creates a ChannelShuffle module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ChannelShuffleModule { @@ -24,7 +25,7 @@ public class ChannelShuffleModule public long Groups { get; set; } /// - /// Generates an observable sequence that creates a ChannelShuffle module. + /// Generates an observable sequence that creates a ChannelShuffleModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs index bee63d9f..8aa934c9 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ConstantPad1d module module. +/// Creates a ConstantPad1d module. /// [Combinator] -[Description("Creates a ConstantPad1d module module.")] +[Description("Creates a ConstantPad1d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConstantPad1dModule { @@ -30,7 +31,7 @@ public class ConstantPad1dModule public double Value { get; set; } /// - /// Generates an observable sequence that creates a ConstantPad1d module. + /// Generates an observable sequence that creates a ConstantPad1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs index 9ab6e151..47ff62c0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ConstantPad2d module module. +/// Creates a ConstantPad2d module. /// [Combinator] -[Description("Creates a ConstantPad2d module module.")] +[Description("Creates a ConstantPad2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConstantPad2dModule { @@ -30,7 +31,7 @@ public class ConstantPad2dModule public double Value { get; set; } /// - /// Generates an observable sequence that creates a ConstantPad2d module. + /// Generates an observable sequence that creates a ConstantPad2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs index 4697f2ec..37b9b69f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ConstantPad3d module module. +/// Creates a ConstantPad3d module. /// [Combinator] -[Description("Creates a ConstantPad3d module module.")] +[Description("Creates a ConstantPad3d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConstantPad3dModule { @@ -30,7 +31,7 @@ public class ConstantPad3dModule public double Value { get; set; } /// - /// Generates an observable sequence that creates a ConstantPad3d module. + /// Generates an observable sequence that creates a ConstantPad3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs index c2e51737..fdb0e1a3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D convolution layer module. +/// Creates a 1D convolution layer. /// [Combinator] -[Description("Creates a 1D convolution layer module.")] +[Description("Creates a 1D convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class Conv1dModule { @@ -75,8 +76,7 @@ public class Conv1dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -86,7 +86,7 @@ public class Conv1dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Conv1d module. + /// Generates an observable sequence that creates a Conv1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs index 80f76f6a..a3c1025d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D convolution layer module. +/// Creates a 2D convolution layer. /// [Combinator] -[Description("Creates a 2D convolution layer module.")] +[Description("Creates a 2D convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class Conv2dModule { @@ -75,8 +76,7 @@ public class Conv2dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -86,7 +86,7 @@ public class Conv2dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Conv2d module. + /// Generates an observable sequence that creates a Conv2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs index adb697e2..68f6dd6d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D convolution layer module. +/// Creates a 3D convolution layer. /// [Combinator] -[Description("Creates a 3D convolution layer module.")] +[Description("Creates a 3D convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class Conv3dModule { @@ -75,8 +76,7 @@ public class Conv3dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -86,7 +86,7 @@ public class Conv3dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Conv3d module. + /// Generates an observable sequence that creates a Conv3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs index ef240b2a..88037d99 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D transposed convolution layer module. +/// Creates a 1D transposed convolution layer. /// [Combinator] -[Description("Creates a 1D transposed convolution layer module.")] +[Description("Creates a 1D transposed convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConvTranspose1dModule { @@ -81,8 +82,7 @@ public class ConvTranspose1dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -92,7 +92,7 @@ public class ConvTranspose1dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a ConvTranspose1d module. + /// Generates an observable sequence that creates a ConvTranspose1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs index 0c7008c5..b49ec7d8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D transposed convolution layer module. +/// Creates a 2D transposed convolution layer. /// [Combinator] -[Description("Creates a 2D transposed convolution layer module.")] +[Description("Creates a 2D transposed convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConvTranspose2dModule { @@ -81,8 +82,7 @@ public class ConvTranspose2dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -92,7 +92,7 @@ public class ConvTranspose2dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a ConvTranspose2d module. + /// Generates an observable sequence that creates a ConvTranspose2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs index 969ae883..d8785475 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D transposed convolution layer module. +/// Creates a 3D transposed convolution layer. /// [Combinator] -[Description("Creates a 3D transposed convolution layer module.")] +[Description("Creates a 3D transposed convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConvTranspose3dModule { @@ -81,8 +82,7 @@ public class ConvTranspose3dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -92,7 +92,7 @@ public class ConvTranspose3dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a ConvTranspose3d module. + /// Generates an observable sequence that creates a ConvTranspose3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs index ff55a0ac..207d9bd7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Dropout1d module module. +/// Creates a Dropout1d module. /// [Combinator] -[Description("Creates a Dropout1d module module.")] +[Description("Creates a Dropout1d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class Dropout1dModule { @@ -21,7 +22,7 @@ public class Dropout1dModule /// The p parameter for the Dropout1d module. /// [Description("The p parameter for the Dropout1d module")] - public double P { get; set; } = 0.5; + public double P { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class Dropout1dModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Dropout1d module. + /// Generates an observable sequence that creates a Dropout1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs index 86ec3711..e7b7a90a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D dropout regularization layer module. +/// Creates a 2D dropout regularization layer. /// [Combinator] -[Description("Creates a 2D dropout regularization layer module.")] +[Description("Creates a 2D dropout regularization layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class Dropout2dModule { @@ -21,7 +22,7 @@ public class Dropout2dModule /// The p parameter for the Dropout2d module. /// [Description("The p parameter for the Dropout2d module")] - public double P { get; set; } = 0.5; + public double P { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class Dropout2dModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Dropout2d module. + /// Generates an observable sequence that creates a Dropout2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs index b3945138..45188054 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D dropout regularization layer module. +/// Creates a 3D dropout regularization layer. /// [Combinator] -[Description("Creates a 3D dropout regularization layer module.")] +[Description("Creates a 3D dropout regularization layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class Dropout3dModule { @@ -21,7 +22,7 @@ public class Dropout3dModule /// The p parameter for the Dropout3d module. /// [Description("The p parameter for the Dropout3d module")] - public double P { get; set; } = 0.5; + public double P { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class Dropout3dModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Dropout3d module. + /// Generates an observable sequence that creates a Dropout3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs index 4e3cd580..b25a87d3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Dropout regularization layer module. +/// Creates a Dropout regularization layer. /// [Combinator] -[Description("Creates a Dropout regularization layer module.")] +[Description("Creates a Dropout regularization layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class DropoutModule { @@ -21,7 +22,7 @@ public class DropoutModule /// The p parameter for the Dropout module. /// [Description("The p parameter for the Dropout module")] - public double P { get; set; } = 0.5; + public double P { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class DropoutModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Dropout module. + /// Generates an observable sequence that creates a DropoutModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs index fc4042a0..362ad7e0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ELU activation function module. +/// Creates a ELU activation function. /// [Combinator] -[Description("Creates a ELU activation function module.")] +[Description("Creates a ELU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class ELUModule { @@ -21,7 +22,7 @@ public class ELUModule /// The alpha parameter for the ELU module. /// [Description("The alpha parameter for the ELU module")] - public double Alpha { get; set; } = 1; + public double Alpha { get; set; } = 1D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class ELUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a ELU module. + /// Generates an observable sequence that creates a ELUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs index 5226994e..2f429733 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Embedding_from_pretrained module module. +/// Creates a EmbeddingFromPretrained module. /// [Combinator] -[Description("Creates a Embedding_from_pretrained module module.")] +[Description("Creates a EmbeddingFromPretrained module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingFromPretrainedModule : IScalarTypeProvider +public class EmbeddingFromPretrainedModule { /// /// The embeddings parameter for the Embedding_from_pretrained module. /// [Description("The embeddings parameter for the Embedding_from_pretrained module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Embeddings { get; set; } + public torch.Tensor Embeddings { get; set; } /// /// The freeze parameter for the Embedding_from_pretrained module. @@ -47,7 +46,7 @@ public class EmbeddingFromPretrainedModule : IScalarTypeProvider /// The norm_type parameter for the Embedding_from_pretrained module. /// [Description("The norm_type parameter for the Embedding_from_pretrained module")] - public double NormType { get; set; } = 2; + public double NormType { get; set; } = 2D; /// /// The scale_grad_by_freq parameter for the Embedding_from_pretrained module. @@ -65,18 +64,17 @@ public class EmbeddingFromPretrainedModule : IScalarTypeProvider /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType Type { get; set; } = ScalarType.Float32; + public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a EmbeddingFromPretrained module. + /// Generates an observable sequence that creates a EmbeddingFromPretrainedModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs index 5beb4edf..e10972d5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Embedding layer module. +/// Creates a Embedding layer. /// [Combinator] -[Description("Creates a Embedding layer module.")] +[Description("Creates a Embedding layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class EmbeddingModule { @@ -45,7 +46,7 @@ public class EmbeddingModule /// The norm_type parameter for the Embedding module. /// [Description("The norm_type parameter for the Embedding module")] - public double NormType { get; set; } = 2; + public double NormType { get; set; } = 2D; /// /// The scale_grad_by_freq parameter for the Embedding module. @@ -63,8 +64,7 @@ public class EmbeddingModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -74,7 +74,7 @@ public class EmbeddingModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Embedding module. + /// Generates an observable sequence that creates a EmbeddingModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs index 816bf58a..544e3108 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a FeatureAlphaDropout module module. +/// Creates a FeatureAlphaDropout module. /// [Combinator] -[Description("Creates a FeatureAlphaDropout module module.")] +[Description("Creates a FeatureAlphaDropout module.")] [WorkflowElementCategory(ElementCategory.Source)] public class FeatureAlphaDropoutModule { @@ -21,10 +22,10 @@ public class FeatureAlphaDropoutModule /// The p parameter for the FeatureAlphaDropout module. /// [Description("The p parameter for the FeatureAlphaDropout module")] - public double P { get; set; } = 0.5; + public double P { get; set; } = 0.5D; /// - /// Generates an observable sequence that creates a FeatureAlphaDropout module. + /// Generates an observable sequence that creates a FeatureAlphaDropoutModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs index 80b326df..e51f8189 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Flatten module module. +/// Creates a Flatten module. /// [Combinator] -[Description("Creates a Flatten module module.")] +[Description("Creates a Flatten module.")] [WorkflowElementCategory(ElementCategory.Source)] public class FlattenModule { @@ -30,7 +31,7 @@ public class FlattenModule public long EndDim { get; set; } = -1; /// - /// Generates an observable sequence that creates a Flatten module. + /// Generates an observable sequence that creates a FlattenModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs index adfdd7fe..10310bea 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Fold module module. +/// Creates a Fold module. /// [Combinator] -[Description("Creates a Fold module module.")] +[Description("Creates a Fold module.")] [WorkflowElementCategory(ElementCategory.Source)] public class FoldModule { @@ -48,7 +49,7 @@ public class FoldModule public long Stride { get; set; } = 1; /// - /// Generates an observable sequence that creates a Fold module. + /// Generates an observable sequence that creates a FoldModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs index 35269f97..d075aa8c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a FractionalMaxPool2d module module. +/// Creates a FractionalMaxPool2d module. /// [Combinator] -[Description("Creates a FractionalMaxPool2d module module.")] +[Description("Creates a FractionalMaxPool2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class FractionalMaxPool2dModule { @@ -36,7 +37,7 @@ public class FractionalMaxPool2dModule public double? OutputRatio { get; set; } = null; /// - /// Generates an observable sequence that creates a FractionalMaxPool2d module. + /// Generates an observable sequence that creates a FractionalMaxPool2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs index b2f1142e..f049dc07 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a FractionalMaxPool3d module module. +/// Creates a FractionalMaxPool3d module. /// [Combinator] -[Description("Creates a FractionalMaxPool3d module module.")] +[Description("Creates a FractionalMaxPool3d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class FractionalMaxPool3dModule { @@ -36,7 +37,7 @@ public class FractionalMaxPool3dModule public double? OutputRatio { get; set; } = null; /// - /// Generates an observable sequence that creates a FractionalMaxPool3d module. + /// Generates an observable sequence that creates a FractionalMaxPool3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs index 9953c02a..c8091be2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a GELU activation function module. +/// Creates a GELU activation function. /// [Combinator] -[Description("Creates a GELU activation function module.")] +[Description("Creates a GELU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class GELUModule { /// - /// Generates an observable sequence that creates a GELU module. + /// Generates an observable sequence that creates a GELUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs index b756fa10..590be20b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a GLU module module. +/// Creates a GLU module. /// [Combinator] -[Description("Creates a GLU module module.")] +[Description("Creates a GLU module.")] [WorkflowElementCategory(ElementCategory.Source)] public class GLUModule { @@ -24,7 +25,7 @@ public class GLUModule public long Dim { get; set; } = -1; /// - /// Generates an observable sequence that creates a GLU module. + /// Generates an observable sequence that creates a GLUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs index 8be12d32..db1efbed 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Group normalization module. +/// Creates a Group normalization. /// [Combinator] -[Description("Creates a Group normalization module.")] +[Description("Creates a Group normalization.")] [WorkflowElementCategory(ElementCategory.Source)] public class GroupNormModule { @@ -33,7 +34,7 @@ public class GroupNormModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -45,8 +46,7 @@ public class GroupNormModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -56,7 +56,7 @@ public class GroupNormModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a GroupNorm module. + /// Generates an observable sequence that creates a GroupNormModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs index 80961ce0..cd07c78b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Hardshrink module module. +/// Creates a Hardshrink module. /// [Combinator] -[Description("Creates a Hardshrink module module.")] +[Description("Creates a Hardshrink module.")] [WorkflowElementCategory(ElementCategory.Source)] public class HardshrinkModule { @@ -21,10 +22,10 @@ public class HardshrinkModule /// The lambda parameter for the Hardshrink module. /// [Description("The lambda parameter for the Hardshrink module")] - public double Lambda { get; set; } = 0.5; + public double Lambda { get; set; } = 0.5D; /// - /// Generates an observable sequence that creates a Hardshrink module. + /// Generates an observable sequence that creates a HardshrinkModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs index 3dc1718a..f6d7191c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Hardsigmoid module module. +/// Creates a Hardsigmoid module. /// [Combinator] -[Description("Creates a Hardsigmoid module module.")] +[Description("Creates a Hardsigmoid module.")] [WorkflowElementCategory(ElementCategory.Source)] public class HardsigmoidModule { @@ -24,7 +25,7 @@ public class HardsigmoidModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Hardsigmoid module. + /// Generates an observable sequence that creates a HardsigmoidModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs index 6c95abf1..72c7579c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Hardswish module module. +/// Creates a Hardswish module. /// [Combinator] -[Description("Creates a Hardswish module module.")] +[Description("Creates a Hardswish module.")] [WorkflowElementCategory(ElementCategory.Source)] public class HardswishModule { @@ -24,7 +25,7 @@ public class HardswishModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Hardswish module. + /// Generates an observable sequence that creates a HardswishModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs index 8781cb72..e7598136 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Hardtanh module module. +/// Creates a Hardtanh module. /// [Combinator] -[Description("Creates a Hardtanh module module.")] +[Description("Creates a Hardtanh module.")] [WorkflowElementCategory(ElementCategory.Source)] public class HardtanhModule { @@ -21,13 +22,13 @@ public class HardtanhModule /// The min_val parameter for the Hardtanh module. /// [Description("The min_val parameter for the Hardtanh module")] - public double MinVal { get; set; } = -1; + public double MinVal { get; set; } = -1D; /// /// The max_val parameter for the Hardtanh module. /// [Description("The max_val parameter for the Hardtanh module")] - public double MaxVal { get; set; } = 1; + public double MaxVal { get; set; } = 1D; /// /// If set to true, will do this operation in-place. @@ -36,7 +37,7 @@ public class HardtanhModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Hardtanh module. + /// Generates an observable sequence that creates a HardtanhModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs index a7254084..2cce74ef 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Identity module module. +/// Creates a Identity module. /// [Combinator] -[Description("Creates a Identity module module.")] +[Description("Creates a Identity module.")] [WorkflowElementCategory(ElementCategory.Source)] public class IdentityModule { /// - /// Generates an observable sequence that creates a Identity module. + /// Generates an observable sequence that creates a IdentityModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs index ff79d16c..8079b53f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D instance normalization module. +/// Creates a 1D instance normalization. /// [Combinator] -[Description("Creates a 1D instance normalization module.")] +[Description("Creates a 1D instance normalization.")] [WorkflowElementCategory(ElementCategory.Source)] public class InstanceNorm1dModule { @@ -27,13 +28,13 @@ public class InstanceNorm1dModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1; + public double Momentum { get; set; } = 0.1D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -51,8 +52,7 @@ public class InstanceNorm1dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,7 +62,7 @@ public class InstanceNorm1dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a InstanceNorm1d module. + /// Generates an observable sequence that creates a InstanceNorm1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs index 34fd0607..4c0a4079 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D instance normalization module. +/// Creates a 2D instance normalization. /// [Combinator] -[Description("Creates a 2D instance normalization module.")] +[Description("Creates a 2D instance normalization.")] [WorkflowElementCategory(ElementCategory.Source)] public class InstanceNorm2dModule { @@ -27,13 +28,13 @@ public class InstanceNorm2dModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1; + public double Momentum { get; set; } = 0.1D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -51,8 +52,7 @@ public class InstanceNorm2dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,7 +62,7 @@ public class InstanceNorm2dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a InstanceNorm2d module. + /// Generates an observable sequence that creates a InstanceNorm2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs index f4a68be8..f0a9b572 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D instance normalization module. +/// Creates a 3D instance normalization. /// [Combinator] -[Description("Creates a 3D instance normalization module.")] +[Description("Creates a 3D instance normalization.")] [WorkflowElementCategory(ElementCategory.Source)] public class InstanceNorm3dModule { @@ -27,13 +28,13 @@ public class InstanceNorm3dModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1; + public double Momentum { get; set; } = 0.1D; /// /// A boolean value that when set to true, this module has learnable affine parameters. @@ -51,8 +52,7 @@ public class InstanceNorm3dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,7 +62,7 @@ public class InstanceNorm3dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a InstanceNorm3d module. + /// Generates an observable sequence that creates a InstanceNorm3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs index aebe0902..f19a93a8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a LPPool1d module module. +/// Creates a LPPool1d module. /// [Combinator] -[Description("Creates a LPPool1d module module.")] +[Description("Creates a LPPool1d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class LPPool1dModule { @@ -42,7 +43,7 @@ public class LPPool1dModule public bool CeilMode { get; set; } = false; /// - /// Generates an observable sequence that creates a LPPool1d module. + /// Generates an observable sequence that creates a LPPool1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs index 110b79d9..dc9a5a95 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a LPPool2d module module. +/// Creates a LPPool2d module. /// [Combinator] -[Description("Creates a LPPool2d module module.")] +[Description("Creates a LPPool2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class LPPool2dModule { @@ -44,7 +45,7 @@ public class LPPool2dModule public bool CeilMode { get; set; } = false; /// - /// Generates an observable sequence that creates a LPPool2d module. + /// Generates an observable sequence that creates a LPPool2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs index 2b200a67..192345de 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Layer normalization module. +/// Creates a Layer normalization. /// [Combinator] -[Description("Creates a Layer normalization module.")] +[Description("Creates a Layer normalization.")] [WorkflowElementCategory(ElementCategory.Source)] public class LayerNormModule { @@ -28,7 +29,7 @@ public class LayerNormModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05; + public double Eps { get; set; } = 1E-05D; /// /// The elementwise_affine parameter for the LayerNorm module. @@ -46,8 +47,7 @@ public class LayerNormModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -57,7 +57,7 @@ public class LayerNormModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a LayerNorm module. + /// Generates an observable sequence that creates a LayerNormModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs index 3c4d1d0d..2a9c5bb5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a LeakyReLU activation function module. +/// Creates a LeakyReLU activation function. /// [Combinator] -[Description("Creates a LeakyReLU activation function module.")] +[Description("Creates a LeakyReLU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class LeakyReLUModule { @@ -21,7 +22,7 @@ public class LeakyReLUModule /// The negative_slope parameter for the LeakyReLU module. /// [Description("The negative_slope parameter for the LeakyReLU module")] - public double NegativeSlope { get; set; } = 0.01; + public double NegativeSlope { get; set; } = 0.01D; /// /// If set to true, will do this operation in-place. @@ -30,7 +31,7 @@ public class LeakyReLUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a LeakyReLU module. + /// Generates an observable sequence that creates a LeakyReLUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs index 444fa5a6..031b8634 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Linear transformation layer module. +/// Creates a Linear transformation layer. /// [Combinator] -[Description("Creates a Linear transformation layer module.")] +[Description("Creates a Linear transformation layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class LinearModule { @@ -39,8 +40,7 @@ public class LinearModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -50,7 +50,7 @@ public class LinearModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Linear module. + /// Generates an observable sequence that creates a LinearModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs index 8f5a0671..f2bbd077 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a LocalResponseNorm module module. +/// Creates a LocalResponseNorm module. /// [Combinator] -[Description("Creates a LocalResponseNorm module module.")] +[Description("Creates a LocalResponseNorm module.")] [WorkflowElementCategory(ElementCategory.Source)] public class LocalResponseNormModule { @@ -27,22 +28,22 @@ public class LocalResponseNormModule /// The alpha parameter for the LocalResponseNorm module. /// [Description("The alpha parameter for the LocalResponseNorm module")] - public double Alpha { get; set; } = 0.0001; + public double Alpha { get; set; } = 0.0001D; /// /// The beta parameter for the LocalResponseNorm module. /// [Description("The beta parameter for the LocalResponseNorm module")] - public double Beta { get; set; } = 0.75; + public double Beta { get; set; } = 0.75D; /// /// The k parameter for the LocalResponseNorm module. /// [Description("The k parameter for the LocalResponseNorm module")] - public double K { get; set; } = 1; + public double K { get; set; } = 1D; /// - /// Generates an observable sequence that creates a LocalResponseNorm module. + /// Generates an observable sequence that creates a LocalResponseNormModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs index 24c3d1cf..53cdd8d2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a LogSigmoid module module. +/// Creates a LogSigmoid module. /// [Combinator] -[Description("Creates a LogSigmoid module module.")] +[Description("Creates a LogSigmoid module.")] [WorkflowElementCategory(ElementCategory.Source)] public class LogSigmoidModule { /// - /// Generates an observable sequence that creates a LogSigmoid module. + /// Generates an observable sequence that creates a LogSigmoidModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs index 78c29005..6b10d805 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a LogSoftmax activation function module. +/// Creates a LogSoftmax activation function. /// [Combinator] -[Description("Creates a LogSoftmax activation function module.")] +[Description("Creates a LogSoftmax activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class LogSoftmaxModule { @@ -24,7 +25,7 @@ public class LogSoftmaxModule public long Dim { get; set; } /// - /// Generates an observable sequence that creates a LogSoftmax module. + /// Generates an observable sequence that creates a LogSoftmaxModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs index 4c56bcdf..9ac69236 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 1D max pooling layer module. +/// Creates a 1D max pooling layer. /// [Combinator] -[Description("Creates a 1D max pooling layer module.")] +[Description("Creates a 1D max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class MaxPool1dModule { @@ -48,7 +49,7 @@ public class MaxPool1dModule public bool CeilMode { get; set; } = false; /// - /// Generates an observable sequence that creates a MaxPool1d module. + /// Generates an observable sequence that creates a MaxPool1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs index 186aee4e..3347eb4d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 2D max pooling layer module. +/// Creates a 2D max pooling layer. /// [Combinator] -[Description("Creates a 2D max pooling layer module.")] +[Description("Creates a 2D max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class MaxPool2dModule { @@ -48,7 +49,7 @@ public class MaxPool2dModule public bool CeilMode { get; set; } = false; /// - /// Generates an observable sequence that creates a MaxPool2d module. + /// Generates an observable sequence that creates a MaxPool2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs index 3be88386..d6b4a2f5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a 3D max pooling layer module. +/// Creates a 3D max pooling layer. /// [Combinator] -[Description("Creates a 3D max pooling layer module.")] +[Description("Creates a 3D max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class MaxPool3dModule { @@ -48,7 +49,7 @@ public class MaxPool3dModule public bool CeilMode { get; set; } = false; /// - /// Generates an observable sequence that creates a MaxPool3d module. + /// Generates an observable sequence that creates a MaxPool3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs index 6c951053..b7d3a8b5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Mish module module. +/// Creates a Mish module. /// [Combinator] -[Description("Creates a Mish module module.")] +[Description("Creates a Mish module.")] [WorkflowElementCategory(ElementCategory.Source)] public class MishModule { /// - /// Generates an observable sequence that creates a Mish module. + /// Generates an observable sequence that creates a MishModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs index a45e388c..6f486350 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a PReLU module module. +/// Creates a PReLU module. /// [Combinator] -[Description("Creates a PReLU module module.")] +[Description("Creates a PReLU module.")] [WorkflowElementCategory(ElementCategory.Source)] public class PReLUModule { @@ -27,14 +28,13 @@ public class PReLUModule /// The init parameter for the PReLU module. /// [Description("The init parameter for the PReLU module")] - public double Init { get; set; } = 0.25; + public double Init { get; set; } = 0.25D; /// /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -44,7 +44,7 @@ public class PReLUModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a PReLU module. + /// Generates an observable sequence that creates a PReLUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs index 853e4685..b023d96c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a PixelShuffle module module. +/// Creates a PixelShuffle module. /// [Combinator] -[Description("Creates a PixelShuffle module module.")] +[Description("Creates a PixelShuffle module.")] [WorkflowElementCategory(ElementCategory.Source)] public class PixelShuffleModule { @@ -24,7 +25,7 @@ public class PixelShuffleModule public long UpscaleFactor { get; set; } /// - /// Generates an observable sequence that creates a PixelShuffle module. + /// Generates an observable sequence that creates a PixelShuffleModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs index 3189f611..151030fb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a PixelUnshuffle module module. +/// Creates a PixelUnshuffle module. /// [Combinator] -[Description("Creates a PixelUnshuffle module module.")] +[Description("Creates a PixelUnshuffle module.")] [WorkflowElementCategory(ElementCategory.Source)] public class PixelUnshuffleModule { @@ -24,7 +25,7 @@ public class PixelUnshuffleModule public long DownscaleFactor { get; set; } /// - /// Generates an observable sequence that creates a PixelUnshuffle module. + /// Generates an observable sequence that creates a PixelUnshuffleModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs index 0a87f4ff..85dec289 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a RReLU module module. +/// Creates a RReLU module. /// [Combinator] -[Description("Creates a RReLU module module.")] +[Description("Creates a RReLU module.")] [WorkflowElementCategory(ElementCategory.Source)] public class RReLUModule { @@ -21,13 +22,13 @@ public class RReLUModule /// The lower parameter for the RReLU module. /// [Description("The lower parameter for the RReLU module")] - public double Lower { get; set; } = 0.125; + public double Lower { get; set; } = 0.125D; /// /// The upper parameter for the RReLU module. /// [Description("The upper parameter for the RReLU module")] - public double Upper { get; set; } = 0.3333333333333333; + public double Upper { get; set; } = 0.3333333333333333D; /// /// If set to true, will do this operation in-place. @@ -36,7 +37,7 @@ public class RReLUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a RReLU module. + /// Generates an observable sequence that creates a RReLUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs index fd2a691f..9b019fc0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReLU6 module module. +/// Creates a ReLU6 module. /// [Combinator] -[Description("Creates a ReLU6 module module.")] +[Description("Creates a ReLU6 module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReLU6Module { @@ -24,7 +25,7 @@ public class ReLU6Module public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a ReLU6 module. + /// Generates an observable sequence that creates a ReLU6Module module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs index 486ea6e8..1c69453c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReLU activation function module. +/// Creates a ReLU activation function. /// [Combinator] -[Description("Creates a ReLU activation function module.")] +[Description("Creates a ReLU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReLUModule { @@ -24,7 +25,7 @@ public class ReLUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a ReLU module. + /// Generates an observable sequence that creates a ReLUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs index c6e5411a..2b4be88b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReflectionPad1d module module. +/// Creates a ReflectionPad1d module. /// [Combinator] -[Description("Creates a ReflectionPad1d module module.")] +[Description("Creates a ReflectionPad1d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReflectionPad1dModule { @@ -24,7 +25,7 @@ public class ReflectionPad1dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ReflectionPad1d module. + /// Generates an observable sequence that creates a ReflectionPad1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs index 5af55460..9851b5d8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReflectionPad2d module module. +/// Creates a ReflectionPad2d module. /// [Combinator] -[Description("Creates a ReflectionPad2d module module.")] +[Description("Creates a ReflectionPad2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReflectionPad2dModule { @@ -24,7 +25,7 @@ public class ReflectionPad2dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ReflectionPad2d module. + /// Generates an observable sequence that creates a ReflectionPad2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs index 902b0065..09f98c7c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReflectionPad3d module module. +/// Creates a ReflectionPad3d module. /// [Combinator] -[Description("Creates a ReflectionPad3d module module.")] +[Description("Creates a ReflectionPad3d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReflectionPad3dModule { @@ -24,7 +25,7 @@ public class ReflectionPad3dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ReflectionPad3d module. + /// Generates an observable sequence that creates a ReflectionPad3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs index 093eee0a..74396b14 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReplicationPad1d module module. +/// Creates a ReplicationPad1d module. /// [Combinator] -[Description("Creates a ReplicationPad1d module module.")] +[Description("Creates a ReplicationPad1d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReplicationPad1dModule { @@ -24,7 +25,7 @@ public class ReplicationPad1dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ReplicationPad1d module. + /// Generates an observable sequence that creates a ReplicationPad1dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs index 32426497..f6495564 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReplicationPad2d module module. +/// Creates a ReplicationPad2d module. /// [Combinator] -[Description("Creates a ReplicationPad2d module module.")] +[Description("Creates a ReplicationPad2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReplicationPad2dModule { @@ -24,7 +25,7 @@ public class ReplicationPad2dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ReplicationPad2d module. + /// Generates an observable sequence that creates a ReplicationPad2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs index 45ce42cd..4b20fb36 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ReplicationPad3d module module. +/// Creates a ReplicationPad3d module. /// [Combinator] -[Description("Creates a ReplicationPad3d module module.")] +[Description("Creates a ReplicationPad3d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ReplicationPad3dModule { @@ -24,7 +25,7 @@ public class ReplicationPad3dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ReplicationPad3d module. + /// Generates an observable sequence that creates a ReplicationPad3dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs index 8fb76c95..8446030b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a SELU activation function module. +/// Creates a SELU activation function. /// [Combinator] -[Description("Creates a SELU activation function module.")] +[Description("Creates a SELU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class SELUModule { @@ -24,7 +25,7 @@ public class SELUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a SELU module. + /// Generates an observable sequence that creates a SELUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs new file mode 100644 index 00000000..36aca372 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs @@ -0,0 +1,28 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Modules; + +/// +/// Creates a Sequential module. +/// +[Combinator] +[Description("Creates a Sequential module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SequentialModule +{ + /// + /// Generates an observable sequence that creates a SequentialModule module. + /// + public IObservable> Process() + { + return Observable.Return(Sequential()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs index 5d8c1699..0f816495 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a SiLU module module. +/// Creates a SiLU module. /// [Combinator] -[Description("Creates a SiLU module module.")] +[Description("Creates a SiLU module.")] [WorkflowElementCategory(ElementCategory.Source)] public class SiLUModule { /// - /// Generates an observable sequence that creates a SiLU module. + /// Generates an observable sequence that creates a SiLUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs index 50af7ba1..3dab5921 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Sigmoid activation function module. +/// Creates a Sigmoid activation function. /// [Combinator] -[Description("Creates a Sigmoid activation function module.")] +[Description("Creates a Sigmoid activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class SigmoidModule { /// - /// Generates an observable sequence that creates a Sigmoid module. + /// Generates an observable sequence that creates a SigmoidModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs index 5dcec56b..aa9029a7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Softmax2d module module. +/// Creates a Softmax2d module. /// [Combinator] -[Description("Creates a Softmax2d module module.")] +[Description("Creates a Softmax2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class Softmax2dModule { /// - /// Generates an observable sequence that creates a Softmax2d module. + /// Generates an observable sequence that creates a Softmax2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs index 103eb971..9c577a60 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Softmax activation function module. +/// Creates a Softmax activation function. /// [Combinator] -[Description("Creates a Softmax activation function module.")] +[Description("Creates a Softmax activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class SoftmaxModule { @@ -24,7 +25,7 @@ public class SoftmaxModule public long Dim { get; set; } /// - /// Generates an observable sequence that creates a Softmax module. + /// Generates an observable sequence that creates a SoftmaxModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs index 3c402846..4d2bb4cd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Softmin module module. +/// Creates a Softmin module. /// [Combinator] -[Description("Creates a Softmin module module.")] +[Description("Creates a Softmin module.")] [WorkflowElementCategory(ElementCategory.Source)] public class SoftminModule { @@ -24,7 +25,7 @@ public class SoftminModule public long Dim { get; set; } /// - /// Generates an observable sequence that creates a Softmin module. + /// Generates an observable sequence that creates a SoftminModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs index 91308d27..9d59cbfc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Softplus module module. +/// Creates a Softplus module. /// [Combinator] -[Description("Creates a Softplus module module.")] +[Description("Creates a Softplus module.")] [WorkflowElementCategory(ElementCategory.Source)] public class SoftplusModule { @@ -21,16 +22,16 @@ public class SoftplusModule /// The beta parameter for the Softplus module. /// [Description("The beta parameter for the Softplus module")] - public double Beta { get; set; } = 1; + public double Beta { get; set; } = 1D; /// /// The threshold parameter for the Softplus module. /// [Description("The threshold parameter for the Softplus module")] - public double Threshold { get; set; } = 20; + public double Threshold { get; set; } = 20D; /// - /// Generates an observable sequence that creates a Softplus module. + /// Generates an observable sequence that creates a SoftplusModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs index ceef1718..295649ba 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Softshrink module module. +/// Creates a Softshrink module. /// [Combinator] -[Description("Creates a Softshrink module module.")] +[Description("Creates a Softshrink module.")] [WorkflowElementCategory(ElementCategory.Source)] public class SoftshrinkModule { @@ -21,10 +22,10 @@ public class SoftshrinkModule /// The lambda parameter for the Softshrink module. /// [Description("The lambda parameter for the Softshrink module")] - public double Lambda { get; set; } = 0.5; + public double Lambda { get; set; } = 0.5D; /// - /// Generates an observable sequence that creates a Softshrink module. + /// Generates an observable sequence that creates a SoftshrinkModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs index f361f71c..fba24ebb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Softsign module module. +/// Creates a Softsign module. /// [Combinator] -[Description("Creates a Softsign module module.")] +[Description("Creates a Softsign module.")] [WorkflowElementCategory(ElementCategory.Source)] public class SoftsignModule { /// - /// Generates an observable sequence that creates a Softsign module. + /// Generates an observable sequence that creates a SoftsignModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs index af4a432a..97647a43 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Tanh activation function module. +/// Creates a Tanh activation function. /// [Combinator] -[Description("Creates a Tanh activation function module.")] +[Description("Creates a Tanh activation function.")] [WorkflowElementCategory(ElementCategory.Source)] public class TanhModule { /// - /// Generates an observable sequence that creates a Tanh module. + /// Generates an observable sequence that creates a TanhModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs index e073284d..bea3e2dc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,15 +11,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Tanhshrink module module. +/// Creates a Tanhshrink module. /// [Combinator] -[Description("Creates a Tanhshrink module module.")] +[Description("Creates a Tanhshrink module.")] [WorkflowElementCategory(ElementCategory.Source)] public class TanhshrinkModule { /// - /// Generates an observable sequence that creates a Tanhshrink module. + /// Generates an observable sequence that creates a TanhshrinkModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs index 9a9edc84..b63c9f64 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Threshold module module. +/// Creates a Threshold module. /// [Combinator] -[Description("Creates a Threshold module module.")] +[Description("Creates a Threshold module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ThresholdModule { @@ -36,7 +37,7 @@ public class ThresholdModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Threshold module. + /// Generates an observable sequence that creates a ThresholdModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs index aa4a30c5..06f6fac2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a TransformerEncoderLayer module module. +/// Creates a TransformerEncoderLayer module. /// [Combinator] -[Description("Creates a TransformerEncoderLayer module module.")] +[Description("Creates a TransformerEncoderLayer module.")] [WorkflowElementCategory(ElementCategory.Source)] public class TransformerEncoderLayerModule { @@ -39,7 +40,7 @@ public class TransformerEncoderLayerModule /// The dropout parameter for the TransformerEncoderLayer module. /// [Description("The dropout parameter for the TransformerEncoderLayer module")] - public double Dropout { get; set; } = 0.1; + public double Dropout { get; set; } = 0.1D; /// /// The activation parameter for the TransformerEncoderLayer module. @@ -48,7 +49,7 @@ public class TransformerEncoderLayerModule public Activations Activation { get; set; } = Activations.ReLU; /// - /// Generates an observable sequence that creates a TransformerEncoderLayer module. + /// Generates an observable sequence that creates a TransformerEncoderLayerModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs index 36b39dbc..4a543f31 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a TransformerEncoder module module. +/// Creates a TransformerEncoder module. /// [Combinator] -[Description("Creates a TransformerEncoder module module.")] +[Description("Creates a TransformerEncoder module.")] [WorkflowElementCategory(ElementCategory.Source)] public class TransformerEncoderModule { @@ -30,7 +31,7 @@ public class TransformerEncoderModule public long NumLayers { get; set; } /// - /// Generates an observable sequence that creates a TransformerEncoder module. + /// Generates an observable sequence that creates a TransformerEncoderModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs index bf76cbfe..ed618115 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Unflatten module module. +/// Creates a Unflatten module. /// [Combinator] -[Description("Creates a Unflatten module module.")] +[Description("Creates a Unflatten module.")] [WorkflowElementCategory(ElementCategory.Source)] public class UnflattenModule { @@ -31,7 +32,7 @@ public class UnflattenModule public long[] UnflattenedSize { get; set; } /// - /// Generates an observable sequence that creates a Unflatten module. + /// Generates an observable sequence that creates a UnflattenModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs index 55151fdf..0b2c2774 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Unfold module module. +/// Creates a Unfold module. /// [Combinator] -[Description("Creates a Unfold module module.")] +[Description("Creates a Unfold module.")] [WorkflowElementCategory(ElementCategory.Source)] public class UnfoldModule { @@ -42,7 +43,7 @@ public class UnfoldModule public long Stride { get; set; } = 1; /// - /// Generates an observable sequence that creates a Unfold module. + /// Generates an observable sequence that creates a UnfoldModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs index 071ab813..1c5230ac 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a Upsample module module. +/// Creates a Upsample module. /// [Combinator] -[Description("Creates a Upsample module module.")] +[Description("Creates a Upsample module.")] [WorkflowElementCategory(ElementCategory.Source)] public class UpsampleModule { @@ -44,7 +45,7 @@ public class UpsampleModule public bool? AlignCorners { get; set; } = null; /// - /// Generates an observable sequence that creates a Upsample module. + /// Generates an observable sequence that creates a UpsampleModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs index 0251ccf0..acb90c8d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; /// -/// Creates a ZeroPad2d module module. +/// Creates a ZeroPad2d module. /// [Combinator] -[Description("Creates a ZeroPad2d module module.")] +[Description("Creates a ZeroPad2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class ZeroPad2dModule { @@ -24,7 +25,7 @@ public class ZeroPad2dModule public long Padding { get; set; } /// - /// Generates an observable sequence that creates a ZeroPad2d module. + /// Generates an observable sequence that creates a ZeroPad2dModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizer.cs similarity index 83% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizer.cs index 8bc097f9..9c3a2aa8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,12 +12,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a ASGD optimizer module. +/// Creates a ASGD optimizer. /// [Combinator] -[Description("Creates a ASGD optimizer module.")] +[Description("Creates a ASGD optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ASGDOptimizerModule +public class ASGDOptimizer { /// /// The parameters parameter for the ASGD module. @@ -29,31 +29,31 @@ public class ASGDOptimizerModule /// The lr parameter for the ASGD module. /// [Description("The lr parameter for the ASGD module")] - public double Lr { get; set; } = 0.001; + public double Lr { get; set; } = 0.001D; /// /// The lambd parameter for the ASGD module. /// [Description("The lambd parameter for the ASGD module")] - public double Lambd { get; set; } = 0.0001; + public double Lambd { get; set; } = 0.0001D; /// /// The alpha parameter for the ASGD module. /// [Description("The alpha parameter for the ASGD module")] - public double Alpha { get; set; } = 0.75; + public double Alpha { get; set; } = 0.75D; /// /// The t0 parameter for the ASGD module. /// [Description("The t0 parameter for the ASGD module")] - public double T0 { get; set; } = 1000000; + public double T0 { get; set; } = 1000000D; /// /// The weight_decay parameter for the ASGD module. /// [Description("The weight_decay parameter for the ASGD module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The maximize parameter for the ASGD module. @@ -62,7 +62,7 @@ public class ASGDOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a ASGDOptimizer module. + /// Generates an observable sequence that creates a ASGDOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizer.cs similarity index 83% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizer.cs index bbc21579..b5a49ad3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizer.cs @@ -1,22 +1,23 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; using static TorchSharp.torch; +using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a Adadelta optimizer module. +/// Creates a Adadelta optimizer. /// [Combinator] -[Description("Creates a Adadelta optimizer module.")] +[Description("Creates a Adadelta optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdadeltaOptimizerModule +public class AdadeltaOptimizer { /// /// The parameters parameter for the Adadelta module. @@ -28,25 +29,25 @@ public class AdadeltaOptimizerModule /// The lr parameter for the Adadelta module. /// [Description("The lr parameter for the Adadelta module")] - public double Lr { get; set; } = 1; + public double Lr { get; set; } = 1D; /// /// The rho parameter for the Adadelta module. /// [Description("The rho parameter for the Adadelta module")] - public double Rho { get; set; } = 0.9; + public double Rho { get; set; } = 0.9D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-06; + public double Eps { get; set; } = 1E-06D; /// /// The weight_decay parameter for the Adadelta module. /// [Description("The weight_decay parameter for the Adadelta module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The maximize parameter for the Adadelta module. @@ -55,7 +56,7 @@ public class AdadeltaOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a AdadeltaOptimizer module. + /// Generates an observable sequence that creates a AdadeltaOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizer.cs similarity index 81% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizer.cs index ae72d299..eaefab47 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizer.cs @@ -1,22 +1,23 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; using static TorchSharp.torch; +using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a Adagrad optimizer module. +/// Creates a Adagrad optimizer. /// [Combinator] -[Description("Creates a Adagrad optimizer module.")] +[Description("Creates a Adagrad optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdagradOptimizerModule +public class AdagradOptimizer { /// /// The parameters parameter for the Adagrad module. @@ -28,34 +29,34 @@ public class AdagradOptimizerModule /// The lr parameter for the Adagrad module. /// [Description("The lr parameter for the Adagrad module")] - public double Lr { get; set; } = 0.01; + public double Lr { get; set; } = 0.01D; /// /// The lr_decay parameter for the Adagrad module. /// [Description("The lr_decay parameter for the Adagrad module")] - public double LrDecay { get; set; } = 0; + public double LrDecay { get; set; } = 0D; /// /// The weight_decay parameter for the Adagrad module. /// [Description("The weight_decay parameter for the Adagrad module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The initial_accumulator_value parameter for the Adagrad module. /// [Description("The initial_accumulator_value parameter for the Adagrad module")] - public double InitialAccumulatorValue { get; set; } = 0; + public double InitialAccumulatorValue { get; set; } = 0D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-10; + public double Eps { get; set; } = 1E-10D; /// - /// Generates an observable sequence that creates a AdagradOptimizer module. + /// Generates an observable sequence that creates a AdagradOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizer.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizer.cs index 87106c84..941fd4ea 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,12 +12,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a Adam optimizer module. +/// Creates a Adam optimizer. /// [Combinator] -[Description("Creates a Adam optimizer module.")] +[Description("Creates a Adam optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdamOptimizerModule +public class AdamOptimizer { /// /// The parameters parameter for the Adam module. @@ -29,31 +29,31 @@ public class AdamOptimizerModule /// The lr parameter for the Adam module. /// [Description("The lr parameter for the Adam module")] - public double Lr { get; set; } = 0.001; + public double Lr { get; set; } = 0.001D; /// /// The beta1 parameter for the Adam module. /// [Description("The beta1 parameter for the Adam module")] - public double Beta1 { get; set; } = 0.9; + public double Beta1 { get; set; } = 0.9D; /// /// The beta2 parameter for the Adam module. /// [Description("The beta2 parameter for the Adam module")] - public double Beta2 { get; set; } = 0.999; + public double Beta2 { get; set; } = 0.999D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08; + public double Eps { get; set; } = 1E-08D; /// /// The weight_decay parameter for the Adam module. /// [Description("The weight_decay parameter for the Adam module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The amsgrad parameter for the Adam module. @@ -68,7 +68,7 @@ public class AdamOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a AdamOptimizer module. + /// Generates an observable sequence that creates a AdamOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizer.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizer.cs index 350765dc..df9eab19 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,12 +12,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a AdamW optimizer module. +/// Creates a AdamW optimizer. /// [Combinator] -[Description("Creates a AdamW optimizer module.")] +[Description("Creates a AdamW optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdamWOptimizerModule +public class AdamWOptimizer { /// /// The parameters parameter for the AdamW module. @@ -29,31 +29,31 @@ public class AdamWOptimizerModule /// The lr parameter for the AdamW module. /// [Description("The lr parameter for the AdamW module")] - public double Lr { get; set; } = 0.001; + public double Lr { get; set; } = 0.001D; /// /// The beta1 parameter for the AdamW module. /// [Description("The beta1 parameter for the AdamW module")] - public double Beta1 { get; set; } = 0.9; + public double Beta1 { get; set; } = 0.9D; /// /// The beta2 parameter for the AdamW module. /// [Description("The beta2 parameter for the AdamW module")] - public double Beta2 { get; set; } = 0.999; + public double Beta2 { get; set; } = 0.999D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08; + public double Eps { get; set; } = 1E-08D; /// /// The weight_decay parameter for the AdamW module. /// [Description("The weight_decay parameter for the AdamW module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The amsgrad parameter for the AdamW module. @@ -68,7 +68,7 @@ public class AdamWOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a AdamWOptimizer module. + /// Generates an observable sequence that creates a AdamWOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizer.cs similarity index 81% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizer.cs index 06ec079c..25ed75ac 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizer.cs @@ -1,22 +1,23 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; using static TorchSharp.torch; +using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a Adamax optimizer module. +/// Creates a Adamax optimizer. /// [Combinator] -[Description("Creates a Adamax optimizer module.")] +[Description("Creates a Adamax optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdamaxOptimizerModule +public class AdamaxOptimizer { /// /// The parameters parameter for the Adamax module. @@ -28,34 +29,34 @@ public class AdamaxOptimizerModule /// The lr parameter for the Adamax module. /// [Description("The lr parameter for the Adamax module")] - public double Lr { get; set; } = 0.002; + public double Lr { get; set; } = 0.002D; /// /// The beta1 parameter for the Adamax module. /// [Description("The beta1 parameter for the Adamax module")] - public double Beta1 { get; set; } = 0.9; + public double Beta1 { get; set; } = 0.9D; /// /// The beta2 parameter for the Adamax module. /// [Description("The beta2 parameter for the Adamax module")] - public double Beta2 { get; set; } = 0.999; + public double Beta2 { get; set; } = 0.999D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08; + public double Eps { get; set; } = 1E-08D; /// /// The weight_decay parameter for the Adamax module. /// [Description("The weight_decay parameter for the Adamax module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// - /// Generates an observable sequence that creates a AdamaxOptimizer module. + /// Generates an observable sequence that creates a AdamaxOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizer.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizer.cs index eb3ecaaf..c5aa5fbd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,24 +12,24 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a LBFGS optimizer module. +/// Creates a LBFGS optimizer. /// [Combinator] -[Description("Creates a LBFGS optimizer module.")] +[Description("Creates a LBFGS optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LBFGSOptimizerModule +public class LBFGSOptimizer { /// /// The parameters parameter for the LBFGS module. /// [Description("The parameters parameter for the LBFGS module")] - public IEnumerable> Parameters { get; set; } + public IEnumerable<(string, Parameter)> Parameters { get; set; } /// /// The lr parameter for the LBFGS module. /// [Description("The lr parameter for the LBFGS module")] - public double Lr { get; set; } = 0.01; + public double Lr { get; set; } = 0.01D; /// /// The max_iter parameter for the LBFGS module. @@ -47,13 +47,13 @@ public class LBFGSOptimizerModule /// The tolerange_grad parameter for the LBFGS module. /// [Description("The tolerange_grad parameter for the LBFGS module")] - public double TolerangeGrad { get; set; } = 1E-05; + public double TolerangeGrad { get; set; } = 1E-05D; /// /// The tolerance_change parameter for the LBFGS module. /// [Description("The tolerance_change parameter for the LBFGS module")] - public double ToleranceChange { get; set; } = 1E-09; + public double ToleranceChange { get; set; } = 1E-09D; /// /// The history_size parameter for the LBFGS module. @@ -62,7 +62,7 @@ public class LBFGSOptimizerModule public long HistorySize { get; set; } = 100; /// - /// Generates an observable sequence that creates a LBFGSOptimizer module. + /// Generates an observable sequence that creates a LBFGSOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizer.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizer.cs index f8075317..81283738 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,12 +12,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a RMSProp optimizer module. +/// Creates a RMSProp optimizer. /// [Combinator] -[Description("Creates a RMSProp optimizer module.")] +[Description("Creates a RMSProp optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class RMSPropOptimizerModule +public class RMSPropOptimizer { /// /// The parameters parameter for the RMSProp module. @@ -29,31 +29,31 @@ public class RMSPropOptimizerModule /// The lr parameter for the RMSProp module. /// [Description("The lr parameter for the RMSProp module")] - public double Lr { get; set; } = 0.01; + public double Lr { get; set; } = 0.01D; /// /// The alpha parameter for the RMSProp module. /// [Description("The alpha parameter for the RMSProp module")] - public double Alpha { get; set; } = 0.99; + public double Alpha { get; set; } = 0.99D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08; + public double Eps { get; set; } = 1E-08D; /// /// The weight_decay parameter for the RMSProp module. /// [Description("The weight_decay parameter for the RMSProp module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0; + public double Momentum { get; set; } = 0D; /// /// The centered parameter for the RMSProp module. @@ -68,7 +68,7 @@ public class RMSPropOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a RMSPropOptimizer module. + /// Generates an observable sequence that creates a RMSPropOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizer.cs similarity index 83% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizer.cs index ff253126..39fd0ef6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,12 +12,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a Rprop optimizer module. +/// Creates a Rprop optimizer. /// [Combinator] -[Description("Creates a Rprop optimizer module.")] +[Description("Creates a Rprop optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class RpropOptimizerModule +public class RpropOptimizer { /// /// The parameters parameter for the Rprop module. @@ -29,31 +29,31 @@ public class RpropOptimizerModule /// The lr parameter for the Rprop module. /// [Description("The lr parameter for the Rprop module")] - public double Lr { get; set; } = 0.01; + public double Lr { get; set; } = 0.01D; /// /// The etaminus parameter for the Rprop module. /// [Description("The etaminus parameter for the Rprop module")] - public double Etaminus { get; set; } = 0.5; + public double Etaminus { get; set; } = 0.5D; /// /// The etaplus parameter for the Rprop module. /// [Description("The etaplus parameter for the Rprop module")] - public double Etaplus { get; set; } = 1.2; + public double Etaplus { get; set; } = 1.2D; /// /// The min_step parameter for the Rprop module. /// [Description("The min_step parameter for the Rprop module")] - public double MinStep { get; set; } = 1E-06; + public double MinStep { get; set; } = 1E-06D; /// /// The max_step parameter for the Rprop module. /// [Description("The max_step parameter for the Rprop module")] - public double MaxStep { get; set; } = 50; + public double MaxStep { get; set; } = 50D; /// /// The maximize parameter for the Rprop module. @@ -62,7 +62,7 @@ public class RpropOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a RpropOptimizer module. + /// Generates an observable sequence that creates a RpropOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizer.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizer.cs index a90d6bf3..910ffb03 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizer.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; -using System.Reactive.Linq; using System.Collections.Generic; +using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; using TorchSharp.Modules; @@ -12,12 +12,12 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; /// -/// Creates a SGD optimizer module. +/// Creates a SGD optimizer. /// [Combinator] -[Description("Creates a SGD optimizer module.")] +[Description("Creates a SGD optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SGDOptimizerModule +public class SGDOptimizer { /// /// The parameters parameter for the SGD module. @@ -35,19 +35,19 @@ public class SGDOptimizerModule /// The value used for the running_mean and running_var computation. /// [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0; + public double Momentum { get; set; } = 0D; /// /// The dampening parameter for the SGD module. /// [Description("The dampening parameter for the SGD module")] - public double Dampening { get; set; } = 0; + public double Dampening { get; set; } = 0D; /// /// The weight_decay parameter for the SGD module. /// [Description("The weight_decay parameter for the SGD module")] - public double WeightDecay { get; set; } = 0; + public double WeightDecay { get; set; } = 0D; /// /// The nesterov parameter for the SGD module. @@ -62,7 +62,7 @@ public class SGDOptimizerModule public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a SGDOptimizer module. + /// Generates an observable sequence that creates a SGDOptimizer. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs index 8ea8aaf2..894a3432 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a Bilinear module module. +/// Creates a Bilinear module. /// [Combinator] -[Description("Creates a Bilinear module module.")] +[Description("Creates a Bilinear module.")] [WorkflowElementCategory(ElementCategory.Source)] public class BilinearModule { @@ -45,8 +46,7 @@ public class BilinearModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -56,7 +56,7 @@ public class BilinearModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Bilinear module. + /// Generates an observable sequence that creates a BilinearModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs index e05bb999..7f545536 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a CosineSimilarity module module. +/// Creates a CosineSimilarity module. /// [Combinator] -[Description("Creates a CosineSimilarity module module.")] +[Description("Creates a CosineSimilarity module.")] [WorkflowElementCategory(ElementCategory.Source)] public class CosineSimilarityModule { @@ -27,10 +28,10 @@ public class CosineSimilarityModule /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08; + public double Eps { get; set; } = 1E-08D; /// - /// Generates an observable sequence that creates a CosineSimilarity module. + /// Generates an observable sequence that creates a CosineSimilarityModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs index db7ffb65..8100caa1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a EmbeddingBag module module. +/// Creates a EmbeddingBag module. /// [Combinator] -[Description("Creates a EmbeddingBag module module.")] +[Description("Creates a EmbeddingBag module.")] [WorkflowElementCategory(ElementCategory.Source)] public class EmbeddingBagModule { @@ -39,7 +40,7 @@ public class EmbeddingBagModule /// The norm_type parameter for the EmbeddingBag module. /// [Description("The norm_type parameter for the EmbeddingBag module")] - public double NormType { get; set; } = 2; + public double NormType { get; set; } = 2D; /// /// The scale_grad_by_freq parameter for the EmbeddingBag module. @@ -75,8 +76,7 @@ public class EmbeddingBagModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -86,7 +86,7 @@ public class EmbeddingBagModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a EmbeddingBag module. + /// Generates an observable sequence that creates a EmbeddingBagModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs index 7ffee83f..c9d731fe 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,20 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a EmbeddingBag_from_pretrained module module. +/// Creates a EmbeddingbagFromPretrained module. /// [Combinator] -[Description("Creates a EmbeddingBag_from_pretrained module module.")] +[Description("Creates a EmbeddingbagFromPretrained module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingbagFromPretrainedModule : IScalarTypeProvider +public class EmbeddingbagFromPretrainedModule { /// /// The embeddings parameter for the EmbeddingBag_from_pretrained module. /// [Description("The embeddings parameter for the EmbeddingBag_from_pretrained module")] - [XmlIgnore] - [TypeConverter(typeof(TensorConverter))] - public Tensor Embeddings { get; set; } + public torch.Tensor Embeddings { get; set; } /// /// The freeze parameter for the EmbeddingBag_from_pretrained module. @@ -41,7 +40,7 @@ public class EmbeddingbagFromPretrainedModule : IScalarTypeProvider /// The norm_type parameter for the EmbeddingBag_from_pretrained module. /// [Description("The norm_type parameter for the EmbeddingBag_from_pretrained module")] - public double NormType { get; set; } = 2; + public double NormType { get; set; } = 2D; /// /// The scale_grad_by_freq parameter for the EmbeddingBag_from_pretrained module. @@ -77,18 +76,17 @@ public class EmbeddingbagFromPretrainedModule : IScalarTypeProvider /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType Type { get; set; } = ScalarType.Float32; + public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a EmbeddingbagFromPretrained module. + /// Generates an observable sequence that creates a EmbeddingbagFromPretrainedModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs index 321c8f35..955ab994 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a GRUCell module module. +/// Creates a GRUCell module. /// [Combinator] -[Description("Creates a GRUCell module module.")] +[Description("Creates a GRUCell module.")] [WorkflowElementCategory(ElementCategory.Source)] public class GRUCellModule { @@ -39,8 +40,7 @@ public class GRUCellModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -50,7 +50,7 @@ public class GRUCellModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a GRUCell module. + /// Generates an observable sequence that creates a GRUCellModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs index cc6487d5..4cb86741 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a Gated Recurrent Unit layer module. +/// Creates a Gated Recurrent Unit layer. /// [Combinator] -[Description("Creates a Gated Recurrent Unit layer module.")] +[Description("Creates a Gated Recurrent Unit layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class GRUModule { @@ -51,7 +52,7 @@ public class GRUModule /// The dropout parameter for the GRU module. /// [Description("The dropout parameter for the GRU module")] - public double Dropout { get; set; } = 0; + public double Dropout { get; set; } = 0D; /// /// The bidirectional parameter for the GRU module. @@ -63,8 +64,7 @@ public class GRUModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -74,7 +74,7 @@ public class GRUModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a GRU module. + /// Generates an observable sequence that creates a GRUModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs index 1171376c..8cf160e8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a LSTMCell module module. +/// Creates a LSTMCell module. /// [Combinator] -[Description("Creates a LSTMCell module module.")] +[Description("Creates a LSTMCell module.")] [WorkflowElementCategory(ElementCategory.Source)] public class LSTMCellModule { @@ -39,8 +40,7 @@ public class LSTMCellModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -50,7 +50,7 @@ public class LSTMCellModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a LSTMCell module. + /// Generates an observable sequence that creates a LSTMCellModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs index 7af8c5a6..903db710 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a Long Short-Term Memory layer module. +/// Creates a Long Short-Term Memory layer. /// [Combinator] -[Description("Creates a Long Short-Term Memory layer module.")] +[Description("Creates a Long Short-Term Memory layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class LSTMModule { @@ -51,7 +52,7 @@ public class LSTMModule /// The dropout parameter for the LSTM module. /// [Description("The dropout parameter for the LSTM module")] - public double Dropout { get; set; } = 0; + public double Dropout { get; set; } = 0D; /// /// The bidirectional parameter for the LSTM module. @@ -63,8 +64,7 @@ public class LSTMModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -74,7 +74,7 @@ public class LSTMModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a LSTM module. + /// Generates an observable sequence that creates a LSTMModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs index 8e3a6336..3fd0f4fc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a MaxUnpool1d module module. +/// Creates a MaxUnpool1d module. /// [Combinator] -[Description("Creates a MaxUnpool1d module module.")] +[Description("Creates a MaxUnpool1d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class MaxUnpool1dModule { @@ -36,9 +37,9 @@ public class MaxUnpool1dModule public long? Padding { get; set; } = null; /// - /// Generates an observable sequence that creates a MaxUnpool1d module. + /// Generates an observable sequence that creates a MaxUnpool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs index 782828c4..80499cfb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a MaxUnpool2d module module. +/// Creates a MaxUnpool2d module. /// [Combinator] -[Description("Creates a MaxUnpool2d module module.")] +[Description("Creates a MaxUnpool2d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class MaxUnpool2dModule { @@ -36,9 +37,9 @@ public class MaxUnpool2dModule public long? Padding { get; set; } = null; /// - /// Generates an observable sequence that creates a MaxUnpool2d module. + /// Generates an observable sequence that creates a MaxUnpool2dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs index d6e64d45..6536073b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a MaxUnpool3d module module. +/// Creates a MaxUnpool3d module. /// [Combinator] -[Description("Creates a MaxUnpool3d module module.")] +[Description("Creates a MaxUnpool3d module.")] [WorkflowElementCategory(ElementCategory.Source)] public class MaxUnpool3dModule { @@ -36,9 +37,9 @@ public class MaxUnpool3dModule public long? Padding { get; set; } = null; /// - /// Generates an observable sequence that creates a MaxUnpool3d module. + /// Generates an observable sequence that creates a MaxUnpool3dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs index e2192a9e..d3829b6c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a Multi-head attention mechanism module. +/// Creates a Multi-head attention mechanism. /// [Combinator] -[Description("Creates a Multi-head attention mechanism module.")] +[Description("Creates a Multi-head attention mechanism.")] [WorkflowElementCategory(ElementCategory.Source)] public class MultiheadAttentionModule { @@ -33,7 +34,7 @@ public class MultiheadAttentionModule /// The dropout parameter for the MultiheadAttention module. /// [Description("The dropout parameter for the MultiheadAttention module")] - public double Dropout { get; set; } = 0; + public double Dropout { get; set; } = 0D; /// /// If true, adds a learnable bias to the output. @@ -66,9 +67,9 @@ public class MultiheadAttentionModule public long? Vdim { get; set; } = null; /// - /// Generates an observable sequence that creates a MultiheadAttention module. + /// Generates an observable sequence that creates a MultiheadAttentionModule module. /// - public IObservable>> Process() + public IObservable>> Process() { return Observable.Return(MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKv, AddZeroAttn, Kdim, Vdim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs index 589b3030..51d2f94f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a PairwiseDistance module module. +/// Creates a PairwiseDistance module. /// [Combinator] -[Description("Creates a PairwiseDistance module module.")] +[Description("Creates a PairwiseDistance module.")] [WorkflowElementCategory(ElementCategory.Source)] public class PairwiseDistanceModule { @@ -21,13 +22,13 @@ public class PairwiseDistanceModule /// The p parameter for the PairwiseDistance module. /// [Description("The p parameter for the PairwiseDistance module")] - public double P { get; set; } = 2; + public double P { get; set; } = 2D; /// /// A value added to the denominator for numerical stability. /// [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-06; + public double Eps { get; set; } = 1E-06D; /// /// The keep_dim parameter for the PairwiseDistance module. @@ -36,7 +37,7 @@ public class PairwiseDistanceModule public bool KeepDim { get; set; } = false; /// - /// Generates an observable sequence that creates a PairwiseDistance module. + /// Generates an observable sequence that creates a PairwiseDistanceModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs index 159111e2..b635465e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a RNNCell module module. +/// Creates a RNNCell module. /// [Combinator] -[Description("Creates a RNNCell module module.")] +[Description("Creates a RNNCell module.")] [WorkflowElementCategory(ElementCategory.Source)] public class RNNCellModule { @@ -45,8 +46,7 @@ public class RNNCellModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -56,7 +56,7 @@ public class RNNCellModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a RNNCell module. + /// Generates an observable sequence that creates a RNNCellModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs index 8488acfe..caff68ae 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a Basic RNN layer module. +/// Creates a Basic RNN layer. /// [Combinator] -[Description("Creates a Basic RNN layer module.")] +[Description("Creates a Basic RNN layer.")] [WorkflowElementCategory(ElementCategory.Source)] public class RNNModule { @@ -57,7 +58,7 @@ public class RNNModule /// The dropout parameter for the RNN module. /// [Description("The dropout parameter for the RNN module")] - public double Dropout { get; set; } = 0; + public double Dropout { get; set; } = 0D; /// /// The bidirectional parameter for the RNN module. @@ -69,8 +70,7 @@ public class RNNModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - [XmlIgnore] - public Device Device { get; set; } = null; + public torch.Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -80,9 +80,9 @@ public class RNNModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a RNN module. + /// Generates an observable sequence that creates a RNNModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs index 05dcce1b..0808af53 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a TransformerDecoderLayer module module. +/// Creates a TransformerDecoderLayer module. /// [Combinator] -[Description("Creates a TransformerDecoderLayer module module.")] +[Description("Creates a TransformerDecoderLayer module.")] [WorkflowElementCategory(ElementCategory.Source)] public class TransformerDecoderLayerModule { @@ -39,7 +40,7 @@ public class TransformerDecoderLayerModule /// The dropout parameter for the TransformerDecoderLayer module. /// [Description("The dropout parameter for the TransformerDecoderLayer module")] - public double Dropout { get; set; } = 0.1; + public double Dropout { get; set; } = 0.1D; /// /// The activation parameter for the TransformerDecoderLayer module. @@ -48,7 +49,7 @@ public class TransformerDecoderLayerModule public Activations Activation { get; set; } = Activations.ReLU; /// - /// Generates an observable sequence that creates a TransformerDecoderLayer module. + /// Generates an observable sequence that creates a TransformerDecoderLayerModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs index 538cf532..46571f01 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a TransformerDecoder module module. +/// Creates a TransformerDecoder module. /// [Combinator] -[Description("Creates a TransformerDecoder module module.")] +[Description("Creates a TransformerDecoder module.")] [WorkflowElementCategory(ElementCategory.Source)] public class TransformerDecoderModule { @@ -30,7 +31,7 @@ public class TransformerDecoderModule public long NumLayers { get; set; } /// - /// Generates an observable sequence that creates a TransformerDecoder module. + /// Generates an observable sequence that creates a TransformerDecoderModule module. /// public IObservable> Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs index 6a93568e..80c159c6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; @@ -10,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; /// -/// Creates a Transformer module module. +/// Creates a Transformer module. /// [Combinator] -[Description("Creates a Transformer module module.")] +[Description("Creates a Transformer module.")] [WorkflowElementCategory(ElementCategory.Source)] public class TransformerModule { @@ -51,7 +52,7 @@ public class TransformerModule /// The dropout parameter for the Transformer module. /// [Description("The dropout parameter for the Transformer module")] - public double Dropout { get; set; } = 0.1; + public double Dropout { get; set; } = 0.1D; /// /// The activation parameter for the Transformer module. @@ -60,7 +61,7 @@ public class TransformerModule public Activations Activation { get; set; } = Activations.ReLU; /// - /// Generates an observable sequence that creates a Transformer module. + /// Generates an observable sequence that creates a TransformerModule module. /// public IObservable> Process() { From 7aecc5dea76faa129c184e3ff9ed5a9b26601616 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Tue, 14 Oct 2025 18:15:17 +0100 Subject: [PATCH 04/30] Adding changes which include renaming files and improving doc strings --- .../NeuralNets/Architecture/AlexNet.cs | 72 +++++++++++ .../NeuralNets/Architecture/Mnist.cs | 84 ++++++++++++ .../NeuralNets/Architecture/MobileNet.cs | 76 +++++++++++ .../Architecture/ModelArchitecture.cs | 22 ++++ src/Bonsai.ML.Torch/NeuralNets/Backward.cs | 120 +++++++++++------- .../NeuralNets/CreateSequential.cs | 23 +++- .../Constant.cs} | 28 ++-- .../CosineAnnealing.cs} | 30 ++--- .../Exponential.cs} | 10 +- .../Linear.cs} | 10 +- .../MultiStep.cs} | 8 +- .../OneCycle.cs} | 12 +- .../Polynomial.cs} | 12 +- .../ReduceOnPlateau.cs} | 12 +- .../Step.cs} | 12 +- src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs | 26 +++- ...hitecture.cs => LoadModuleArchitecture.cs} | 12 +- .../NeuralNets/LoadScriptModule.cs | 3 +- .../NeuralNets/Loss/BinaryCrossEntropy.cs | 46 +++++++ .../Loss/BinaryCrossEntropyWithLogits.cs | 46 +++++++ .../ConnectionistTemporalClassification.cs} | 4 +- .../CosineEmbedding.cs} | 8 +- .../CrossEntropy.cs} | 10 +- .../GaussianNegativeLogLikelihood.cs} | 4 +- .../HingeEmbedding.cs} | 2 +- .../{Losses/HuberLoss.cs => Loss/Huber.cs} | 4 +- .../KullbackLeiblerDivergence.cs} | 4 +- .../MarginRanking.cs} | 4 +- .../L1Loss.cs => Loss/MeanAbsoluteError.cs} | 4 +- .../MSELoss.cs => Loss/MeanSquaredError.cs} | 4 +- .../MultiLabelMargin.cs} | 4 +- .../MultiLabelSoftMargin.cs} | 4 +- .../{Losses => Loss}/MultiMarginLoss.cs | 2 +- .../NegativeLogLikelihood.cs} | 6 +- .../PoissonNegativeLogLikelihood.cs} | 4 +- .../SmoothL1Loss.cs => Loss/SmoothL1.cs} | 4 +- .../SoftMarginLoss.cs => Loss/SoftMargin.cs} | 4 +- .../TripletMargin.cs} | 4 +- .../NeuralNets/Losses/BCELoss.cs | 40 ------ .../NeuralNets/Losses/BCEWithLogitsLoss.cs | 46 ------- .../NeuralNets/Models/AlexNet.cs | 74 ----------- .../NeuralNets/Models/Mnist.cs | 86 ------------- .../NeuralNets/Models/MobileNet.cs | 77 ----------- .../NeuralNets/Models/ModelArchitecture.cs | 23 ---- .../AdaptiveAveragePooling.cs} | 24 +++- .../NeuralNets/Module/AdaptiveMaxPooling.cs | 47 +++++++ .../AlphaDropout.cs} | 4 +- .../NeuralNets/Module/AveragePooling.cs | 47 +++++++ .../BatchNormalization.cs} | 26 +++- .../ChannelShuffle.cs} | 4 +- ...slyDifferentiableExponentialLinearUnit.cs} | 4 +- .../Conv1dModule.cs => Module/Convolution.cs} | 38 +++++- .../NeuralNets/Module/Dimensions.cs | 27 ++++ .../NeuralNets/Module/Dropout.cs | 53 ++++++++ .../Embedding.cs} | 4 +- .../EmbeddingFromPretrained.cs} | 4 +- .../ExponentialLinearUnit.cs} | 4 +- .../FeatureAlphaDropout.cs} | 2 +- .../FlattenModule.cs => Module/Flatten.cs} | 4 +- .../{Modules/FoldModule.cs => Module/Fold.cs} | 2 +- .../FractionalMaxPooling.cs} | 23 +++- .../GatedLinearUnit.cs} | 4 +- .../GaussianErrorLinearUnit.cs} | 14 +- .../GroupNormalization.cs} | 6 +- .../HardShrinkage.cs} | 4 +- .../HardSigmoid.cs} | 4 +- .../HardSwish.cs} | 4 +- .../HardtanhModule.cs => Module/HardTanh.cs} | 4 +- .../IdentityModule.cs => Module/Identity.cs} | 4 +- .../InstanceNormalization.cs} | 24 +++- .../LayerNormalization.cs} | 6 +- .../LeakyRectifiedLinearUnit.cs} | 4 +- .../LinearModule.cs => Module/Linear.cs} | 6 +- .../LocalResponseNormalization.cs} | 4 +- .../LogSigmoid.cs} | 4 +- .../LogSoftmax.cs} | 4 +- .../MaxPooling.cs} | 25 +++- .../{Modules/MishModule.cs => Module/Mish.cs} | 4 +- .../NeuralNets/Module/Padding.cs | 82 ++++++++++++ .../NeuralNets/Module/PaddingMode.cs | 22 ++++ .../ParametricRectifiedLinearUnit.cs} | 6 +- .../PixelShuffle.cs} | 4 +- .../PixelUnshuffle.cs} | 4 +- .../PowerAveragePooling.cs} | 17 ++- .../RandomizedLeakyRectifiedLinearUnit.cs} | 4 +- .../RectifiedLinearUnit.cs} | 17 ++- .../ScaledExponentialLinearUnit.cs} | 4 +- .../SigmoidModule.cs => Module/Sigmoid.cs} | 2 +- .../SigmoidWeightedLinearUnit.cs} | 4 +- .../SoftmaxModule.cs => Module/SoftMax.cs} | 4 +- .../SoftMax2D.cs} | 4 +- .../SoftminModule.cs => Module/SoftMin.cs} | 4 +- .../SoftplusModule.cs => Module/SoftPlus.cs} | 4 +- .../SoftShrinkage.cs} | 4 +- .../SoftsignModule.cs => Module/SoftSign.cs} | 4 +- .../{Modules/TanhModule.cs => Module/Tanh.cs} | 4 +- .../TanhShrinkage.cs} | 4 +- .../Threshold.cs} | 10 +- .../TransformerEncoder.cs} | 8 +- .../TransformerEncoderLayer.cs} | 4 +- .../Unflatten.cs} | 4 +- .../UnfoldModule.cs => Module/Unfold.cs} | 4 +- .../UpsampleModule.cs => Module/Upsample.cs} | 2 +- .../{Modules => Module}/ZeroPad2dModule.cs | 2 +- .../Modules/AdaptiveAvgPool1dModule.cs | 34 ----- .../Modules/AdaptiveAvgPool3dModule.cs | 35 ----- .../Modules/AdaptiveMaxPool1dModule.cs | 34 ----- .../Modules/AdaptiveMaxPool2dModule.cs | 35 ----- .../Modules/AdaptiveMaxPool3dModule.cs | 35 ----- .../NeuralNets/Modules/AvgPool1dModule.cs | 58 --------- .../NeuralNets/Modules/AvgPool2dModule.cs | 67 ---------- .../NeuralNets/Modules/AvgPool3dModule.cs | 67 ---------- .../NeuralNets/Modules/BatchNorm2dModule.cs | 71 ----------- .../NeuralNets/Modules/BatchNorm3dModule.cs | 71 ----------- .../NeuralNets/Modules/ConstantPad1dModule.cs | 40 ------ .../NeuralNets/Modules/ConstantPad2dModule.cs | 40 ------ .../NeuralNets/Modules/ConstantPad3dModule.cs | 40 ------ .../NeuralNets/Modules/Conv2dModule.cs | 95 -------------- .../NeuralNets/Modules/Conv3dModule.cs | 95 -------------- .../Modules/ConvTranspose1dModule.cs | 101 --------------- .../Modules/ConvTranspose2dModule.cs | 101 --------------- .../Modules/ConvTranspose3dModule.cs | 101 --------------- .../NeuralNets/Modules/Dropout1dModule.cs | 40 ------ .../NeuralNets/Modules/Dropout2dModule.cs | 40 ------ .../NeuralNets/Modules/Dropout3dModule.cs | 40 ------ .../NeuralNets/Modules/DropoutModule.cs | 40 ------ .../Modules/FractionalMaxPool2dModule.cs | 46 ------- .../Modules/InstanceNorm2dModule.cs | 71 ----------- .../Modules/InstanceNorm3dModule.cs | 71 ----------- .../NeuralNets/Modules/LPPool2dModule.cs | 54 -------- .../NeuralNets/Modules/MaxPool1dModule.cs | 58 --------- .../NeuralNets/Modules/MaxPool2dModule.cs | 58 --------- .../NeuralNets/Modules/ReLU6Module.cs | 34 ----- .../Modules/ReflectionPad1dModule.cs | 34 ----- .../Modules/ReflectionPad2dModule.cs | 34 ----- .../Modules/ReflectionPad3dModule.cs | 34 ----- .../Modules/ReplicationPad1dModule.cs | 34 ----- .../Modules/ReplicationPad2dModule.cs | 34 ----- .../Modules/ReplicationPad3dModule.cs | 34 ----- .../NeuralNets/Modules/SequentialModule.cs | 28 ---- .../AdaDelta.cs} | 6 +- .../AdaGrad.cs} | 6 +- .../AdaMax.cs} | 6 +- .../AdamOptimizer.cs => Optimizer/Adam.cs} | 6 +- .../AdamWOptimizer.cs => Optimizer/AdamW.cs} | 6 +- .../ASGDOptimizer.cs => Optimizer/Asgd.cs} | 6 +- .../LBFGSOptimizer.cs => Optimizer/Lbfgs.cs} | 6 +- .../ResilientBackprop.cs} | 6 +- .../RmsPropogation.cs} | 6 +- .../SGDOptimizer.cs => Optimizer/Sgd.cs} | 6 +- .../{SaveModel.cs => SaveModule.cs} | 32 +++-- 151 files changed, 1158 insertions(+), 2602 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Architecture/ModelArchitecture.cs rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/ConstantLearningRateScheduler.cs => LearningRateScheduler/Constant.cs} (58%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs => LearningRateScheduler/CosineAnnealing.cs} (55%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/ExponentialLearningRateScheduler.cs => LearningRateScheduler/Exponential.cs} (84%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/LinearLearningRateScheduler.cs => LearningRateScheduler/Linear.cs} (88%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/MultiStepLearningRateScheduler.cs => LearningRateScheduler/MultiStep.cs} (90%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/OneCycleLearningRateScheduler.cs => LearningRateScheduler/OneCycle.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/PolynomialLearningRateScheduler.cs => LearningRateScheduler/Polynomial.cs} (85%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs => LearningRateScheduler/ReduceOnPlateau.cs} (87%) rename src/Bonsai.ML.Torch/NeuralNets/{LearningRateSchedulers/StepLearningRateScheduler.cs => LearningRateScheduler/Step.cs} (78%) rename src/Bonsai.ML.Torch/NeuralNets/{LoadModelArchitecture.cs => LoadModuleArchitecture.cs} (81%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs rename src/Bonsai.ML.Torch/NeuralNets/{Losses/CTCLoss.cs => Loss/ConnectionistTemporalClassification.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/CosineEmbeddingLoss.cs => Loss/CosineEmbedding.cs} (81%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/CrossEntropyLoss.cs => Loss/CrossEntropy.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/GaussianNLLLoss.cs => Loss/GaussianNegativeLogLikelihood.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/HingeEmbeddingLoss.cs => Loss/HingeEmbedding.cs} (96%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/HuberLoss.cs => Loss/Huber.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/KLDivLoss.cs => Loss/KullbackLeiblerDivergence.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/MarginRankingLoss.cs => Loss/MarginRanking.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/L1Loss.cs => Loss/MeanAbsoluteError.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/MSELoss.cs => Loss/MeanSquaredError.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/MultiLabelMarginLoss.cs => Loss/MultiLabelMargin.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/MultiLabelSoftMarginLoss.cs => Loss/MultiLabelSoftMargin.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses => Loss}/MultiMarginLoss.cs (97%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/NLLLoss.cs => Loss/NegativeLogLikelihood.cs} (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/PoissonNLLLoss.cs => Loss/PoissonNegativeLogLikelihood.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/SmoothL1Loss.cs => Loss/SmoothL1.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/SoftMarginLoss.cs => Loss/SoftMargin.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Losses/TripletMarginLoss.cs => Loss/TripletMargin.cs} (96%) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Models/AlexNet.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Models/Mnist.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Models/MobileNet.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Models/ModelArchitecture.cs rename src/Bonsai.ML.Torch/NeuralNets/{Modules/AdaptiveAvgPool2dModule.cs => Module/AdaptiveAveragePooling.cs} (50%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs rename src/Bonsai.ML.Torch/NeuralNets/{Modules/AlphaDropoutModule.cs => Module/AlphaDropout.cs} (93%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs rename src/Bonsai.ML.Torch/NeuralNets/{Modules/BatchNorm1dModule.cs => Module/BatchNormalization.cs} (64%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/ChannelShuffleModule.cs => Module/ChannelShuffle.cs} (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/CELUModule.cs => Module/ContinuouslyDifferentiableExponentialLinearUnit.cs} (90%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/Conv1dModule.cs => Module/Convolution.cs} (58%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs rename src/Bonsai.ML.Torch/NeuralNets/{Modules/EmbeddingModule.cs => Module/Embedding.cs} (97%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/EmbeddingFromPretrainedModule.cs => Module/EmbeddingFromPretrained.cs} (97%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/ELUModule.cs => Module/ExponentialLinearUnit.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/FeatureAlphaDropoutModule.cs => Module/FeatureAlphaDropout.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/FlattenModule.cs => Module/Flatten.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/FoldModule.cs => Module/Fold.cs} (97%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/FractionalMaxPool3dModule.cs => Module/FractionalMaxPooling.cs} (57%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/GLUModule.cs => Module/GatedLinearUnit.cs} (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/GELUModule.cs => Module/GaussianErrorLinearUnit.cs} (56%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/GroupNormModule.cs => Module/GroupNormalization.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/HardshrinkModule.cs => Module/HardShrinkage.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/HardsigmoidModule.cs => Module/HardSigmoid.cs} (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/HardswishModule.cs => Module/HardSwish.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/HardtanhModule.cs => Module/HardTanh.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/IdentityModule.cs => Module/Identity.cs} (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/InstanceNorm1dModule.cs => Module/InstanceNormalization.cs} (67%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LayerNormModule.cs => Module/LayerNormalization.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LeakyReLUModule.cs => Module/LeakyRectifiedLinearUnit.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LinearModule.cs => Module/Linear.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LocalResponseNormModule.cs => Module/LocalResponseNormalization.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LogSigmoidModule.cs => Module/LogSigmoid.cs} (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LogSoftmaxModule.cs => Module/LogSoftmax.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/MaxPool3dModule.cs => Module/MaxPooling.cs} (60%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/MishModule.cs => Module/Mish.cs} (90%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs rename src/Bonsai.ML.Torch/NeuralNets/{Modules/PReLUModule.cs => Module/ParametricRectifiedLinearUnit.cs} (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/PixelShuffleModule.cs => Module/PixelShuffle.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/PixelUnshuffleModule.cs => Module/PixelUnshuffle.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/LPPool1dModule.cs => Module/PowerAveragePooling.cs} (67%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/RReLUModule.cs => Module/RandomizedLeakyRectifiedLinearUnit.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/ReLUModule.cs => Module/RectifiedLinearUnit.cs} (56%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SELUModule.cs => Module/ScaledExponentialLinearUnit.cs} (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SigmoidModule.cs => Module/Sigmoid.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SiLUModule.cs => Module/SigmoidWeightedLinearUnit.cs} (88%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SoftmaxModule.cs => Module/SoftMax.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/Softmax2dModule.cs => Module/SoftMax2D.cs} (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SoftminModule.cs => Module/SoftMin.cs} (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SoftplusModule.cs => Module/SoftPlus.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SoftshrinkModule.cs => Module/SoftShrinkage.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/SoftsignModule.cs => Module/SoftSign.cs} (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/TanhModule.cs => Module/Tanh.cs} (90%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/TanhshrinkModule.cs => Module/TanhShrinkage.cs} (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/ThresholdModule.cs => Module/Threshold.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/TransformerEncoderModule.cs => Module/TransformerEncoder.cs} (86%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/TransformerEncoderLayerModule.cs => Module/TransformerEncoderLayer.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/UnflattenModule.cs => Module/Unflatten.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/UnfoldModule.cs => Module/Unfold.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules/UpsampleModule.cs => Module/Upsample.cs} (97%) rename src/Bonsai.ML.Torch/NeuralNets/{Modules => Module}/ZeroPad2dModule.cs (95%) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/AdadeltaOptimizer.cs => Optimizer/AdaDelta.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/AdagradOptimizer.cs => Optimizer/AdaGrad.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/AdamaxOptimizer.cs => Optimizer/AdaMax.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/AdamOptimizer.cs => Optimizer/Adam.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/AdamWOptimizer.cs => Optimizer/AdamW.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/ASGDOptimizer.cs => Optimizer/Asgd.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/LBFGSOptimizer.cs => Optimizer/Lbfgs.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/RpropOptimizer.cs => Optimizer/ResilientBackprop.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/RMSPropOptimizer.cs => Optimizer/RmsPropogation.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Optimizers/SGDOptimizer.cs => Optimizer/Sgd.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{SaveModel.cs => SaveModule.cs} (53%) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs new file mode 100644 index 00000000..189c06d4 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs @@ -0,0 +1,72 @@ +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Architecture; + +/// +/// Modified version of original AlexNet to fix CIFAR10 32x32 images. +/// +internal class AlexNet : Module +{ + private readonly Module features; + private readonly Module avgPool; + private readonly Module classifier; + + /// + /// Constructs a new AlexNet model. + /// + /// + /// + /// + public AlexNet(string name, int numClasses, Device device = null) : base(name) + { + features = Sequential( + ("c1", Conv2d(3, 64, kernel_size: 3, stride: 2, padding: 1)), + ("r1", ReLU(inplace: true)), + ("mp1", MaxPool2d(kernel_size: [ 2, 2 ])), + ("c2", Conv2d(64, 192, kernel_size: 3, padding: 1)), + ("r2", ReLU(inplace: true)), + ("mp2", MaxPool2d(kernel_size: [ 2, 2 ])), + ("c3", Conv2d(192, 384, kernel_size: 3, padding: 1)), + ("r3", ReLU(inplace: true)), + ("c4", Conv2d(384, 256, kernel_size: 3, padding: 1)), + ("r4", ReLU(inplace: true)), + ("c5", Conv2d(256, 256, kernel_size: 3, padding: 1)), + ("r5", ReLU(inplace: true)), + ("mp3", MaxPool2d(kernel_size: [ 2, 2 ]))); + + avgPool = AdaptiveAvgPool2d([ 2, 2 ]); + + classifier = Sequential( + ("d1", Dropout()), + ("l1", Linear(256 * 2 * 2, 4096)), + ("r1", ReLU(inplace: true)), + ("d2", Dropout()), + ("l2", Linear(4096, 4096)), + ("r3", ReLU(inplace: true)), + ("d3", Dropout()), + ("l3", Linear(4096, numClasses)) + ); + + RegisterComponents(); + + if (device != null && device.type != DeviceType.CPU) + this.to(device); + } + + /// + /// Forward pass of the AlexNet model. + /// + /// + /// + public override Tensor forward(Tensor input) + { + var f = features.forward(input); + var avg = avgPool.forward(f); + + var x = avg.view([ avg.shape[0], 256 * 2 * 2 ]); + + return classifier.forward(x); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs new file mode 100644 index 00000000..9bafaa2e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs @@ -0,0 +1,84 @@ +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Architecture; + /// +/// Represents a simple convolutional neural network for the MNIST dataset. +/// +internal class Mnist : Module +{ + private readonly Module conv1; + private readonly Module conv2; + private readonly Module fc1; + private readonly Module fc2; + + private readonly Module pool1; + + private readonly Module relu1; + private readonly Module relu2; + private readonly Module relu3; + + private readonly Module dropout1; + private readonly Module dropout2; + + private readonly Module flatten; + private readonly Module logsm; + + /// + /// Constructs a new Mnist model. + /// + /// + /// + /// + public Mnist(string name, int numClasses, Device device = null) : base(name) + { + conv1 = Conv2d(1, 32, 3); + conv2 = Conv2d(32, 64, 3); + fc1 = Linear(9216, 128); + fc2 = Linear(128, numClasses); + + pool1 = MaxPool2d(kernel_size: [2, 2]); + + relu1 = ReLU(); + relu2 = ReLU(); + relu3 = ReLU(); + + dropout1 = Dropout(0.25); + dropout2 = Dropout(0.5); + + flatten = Flatten(); + logsm = LogSoftmax(1); + + RegisterComponents(); + + if (device != null && device.type != DeviceType.CPU) + this.to(device); + } + + /// + /// Forward pass of the Mnist model. + /// + /// + /// + public override Tensor forward(Tensor input) + { + var l11 = conv1.forward(input); + var l12 = relu1.forward(l11); + + var l21 = conv2.forward(l12); + var l22 = relu2.forward(l21); + var l23 = pool1.forward(l22); + var l24 = dropout1.forward(l23); + + var x = flatten.forward(l24); + + var l31 = fc1.forward(x); + var l32 = relu3.forward(l31); + var l33 = dropout2.forward(l32); + + var l41 = fc2.forward(l33); + + return logsm.forward(l41); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs new file mode 100644 index 00000000..16a77454 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Architecture; + +/// +/// MobileNet model. +/// +internal class MobileNet : Module +{ + private readonly long[] planes = [ 64, 128, 128, 256, 256, 512, 512, 512, 512, 512, 512, 1024, 1024 ]; + private readonly long[] strides = [ 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1 ]; + + private readonly Module layers; + + /// + /// Constructs a new MobileNet model. + /// + /// + /// + /// + /// + public MobileNet(string name, int numClasses, Device device = null) : base(name) + { + if (planes.Length != strides.Length) throw new ArgumentException("'planes' and 'strides' must have the same length."); + + var modules = new List<(string, Module)> + { + ($"conv2d-first", Conv2d(3, 32, kernel_size: 3, stride: 1, padding: 1, bias: false)), + ($"bnrm2d-first", BatchNorm2d(32)), + ($"relu-first", ReLU()) + }; + MakeLayers(modules, 32); + modules.Add(("avgpool", AvgPool2d([2, 2]))); + modules.Add(("flatten", Flatten())); + modules.Add(($"linear", Linear(planes[planes.Length-1], numClasses))); + + layers = Sequential(modules); + + RegisterComponents(); + + if (device != null && device.type != DeviceType.CPU) + this.to(device); + } + + private void MakeLayers(List<(string, Module)> modules, long in_planes) + { + + for (var i = 0; i < strides.Length; i++) { + var out_planes = planes[i]; + var stride = strides[i]; + + modules.Add(($"conv2d-{i}a", Conv2d(in_planes, in_planes, kernel_size: 3, stride: stride, padding: 1, groups: in_planes, bias: false))); + modules.Add(($"bnrm2d-{i}a", BatchNorm2d(in_planes))); + modules.Add(($"relu-{i}a", ReLU())); + modules.Add(($"conv2d-{i}b", Conv2d(in_planes, out_planes, kernel_size: 1L, stride: 1L, padding: 0L, bias: false))); + modules.Add(($"bnrm2d-{i}b", BatchNorm2d(out_planes))); + modules.Add(($"relu-{i}b", ReLU())); + + in_planes = out_planes; + } + } + + /// + /// Forward pass of the MobileNet model. + /// + /// + /// + public override Tensor forward(Tensor input) + { + return layers.forward(input); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/ModelArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/ModelArchitecture.cs new file mode 100644 index 00000000..1a1b2552 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/ModelArchitecture.cs @@ -0,0 +1,22 @@ +namespace Bonsai.ML.Torch.NeuralNets.Architecture; + +/// +/// Represents canonical model architectures. +/// +public enum ModelArchitecture +{ + /// + /// The AlexNet model architecture. + /// + AlexNet, + + /// + /// The MobileNet model architecture. + /// + MobileNet, + + /// + /// The Mnist model architecture. + /// + Mnist +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs index d4b1ffc0..d288d38c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs @@ -1,75 +1,99 @@ using System; +using System.Linq; using System.ComponentModel; +using System.Reactive; +using System.Reactive.Disposables; using System.Reactive.Linq; using System.Xml.Serialization; using static TorchSharp.torch; using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; +using System.Threading.Tasks; -namespace Bonsai.ML.Torch.NeuralNets +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Trains a model using backpropagation. +/// +[Combinator] +[ResetCombinator] +[Description("Trains a model using backpropagation.")] +[WorkflowElementCategory(ElementCategory.Transform)] +public class Backward { /// - /// Trains a model using backpropagation. + /// The optimizer to use for training. /// - [Combinator] - [ResetCombinator] - [Description("Trains a model using backpropagation.")] - [WorkflowElementCategory(ElementCategory.Transform)] - public class Backward - { - /// - /// The optimizer to use for training. - /// - public Optimizer Optimizer { get; set; } = null; + [XmlIgnore] + public optim.Optimizer Optimizer { get; set; } = null; - /// - /// The learning rate scheduler to use for training. - /// - public LRScheduler LRScheduler { get; set; } = null; + /// + /// The module to train. + /// + [XmlIgnore] + public IModule Module { get; set; } - /// - /// The model to train. - /// - [XmlIgnore] - public IModule Model { get; set; } + /// + /// The criterion (loss function) to use for training. + /// + [XmlIgnore] + public IModule Criterion { get; set; } = null; - /// - /// The loss function to use for training. - /// - public IModule Loss { get; set; } = null; + /// + /// The learning rate scheduler to use for training. + /// + [XmlIgnore] + public LRScheduler LRScheduler { get; set; } = null; - /// - /// Trains the model using backpropagation. - /// - /// - /// - public IObservable Process(IObservable> source) + /// + /// The number of epochs to train the model. + /// + public int Epochs { get; set; } = 1; + + /// + /// Trains the model using backpropagation. + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(input => Observable.Create>((observer, cancellationToken) => { - var model = Model as Module; + return Task.Run(() => + { + var module = Module as Module; - Loss ??= NLLLoss(); - var loss = Loss as Module; + Criterion ??= NLLLoss(); + var criterion = Criterion as Module; - Optimizer ??= SGD(model.parameters(), 0.01); - LRScheduler ??= StepLR(Optimizer, 1, 0.7); - model.train(); + Optimizer ??= SGD(module.parameters(), 0.01); + if (!module.training) + module.train(); - return source.Select((input) => { var (data, target) = input; - using (_ = NewDisposeScope()) + + for (int epoch = 0; epoch < Epochs; epoch++) { - Optimizer.zero_grad(); - - var prediction = model.forward(data); - var output = loss.forward(prediction, target); + if (cancellationToken.IsCancellationRequested) break; + using (_ = NewDisposeScope()) + { + Optimizer.zero_grad(); + + var prediction = module.forward(data); + var loss = criterion.forward(prediction, target); + + loss.backward(); - output.backward(); + Optimizer.step(); - Optimizer.step(); - return output.MoveToOuterDisposeScope(); + observer.OnNext(Tuple.Create(prediction, loss)); + } + LRScheduler?.step(); } - }); - } + observer.OnCompleted(); + return Disposable.Empty; + }, cancellationToken); + })).Concat(); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs b/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs index b689819b..01d4bc6b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs @@ -39,7 +39,24 @@ public IObservable> Process() { return Observable.Defer(() => { - var sequential = Sequential([..Modules.Select(m => (Module)m)]); + var sequential = Sequential([.. Modules.Select(m => (Module)m)]); + if (Device is not null && Device != CPU) + { + sequential.to(Device); + } + return Observable.Return(sequential); + }); + } + + /// + /// Generates an observable sequence that creates a sequential model from the input modules. + /// + /// + public IObservable> Process(IObservable[]> source) + { + return source.SelectMany(modules => + { + var sequential = Sequential([.. modules.Select(m => (Module)m)]); if (Device is not null && Device != CPU) { sequential.to(Device); @@ -54,14 +71,14 @@ public IObservable> Process() /// public IObservable> Process(IObservable source) { - return source.Select(_ => + return source.SelectMany(_ => { var sequential = Sequential([..Modules.Select(m => (Module)m)]); if (Device is not null && Device != CPU) { sequential.to(Device); } - return sequential; + return Observable.Return(sequential); }); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs similarity index 58% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs index 813aaca9..ccf8f915 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ConstantLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs @@ -10,44 +10,44 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a ConstantLearningRate scheduler. +/// Creates a constant learning rate scheduler. /// [Combinator] [Description("Creates a ConstantLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ConstantLearningRateScheduler +public class Constant { /// - /// The optimizer parameter for the ConstantLR module. + /// The optimizer parameter for the Constant module. /// - [Description("The optimizer parameter for the ConstantLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + [Description("The optimizer parameter for the Constant module")] + public optim.Optimizer Optimizer { get; set; } /// - /// The factor parameter for the ConstantLR module. + /// The factor parameter for the Constant module. /// - [Description("The factor parameter for the ConstantLR module")] + [Description("The factor parameter for the Constant module")] public double Factor { get; set; } = 0.3333333333333333D; /// - /// The total_iters parameter for the ConstantLR module. + /// The total_iters parameter for the Constant module. /// - [Description("The total_iters parameter for the ConstantLR module")] + [Description("The total_iters parameter for the Constant module")] public int TotalIters { get; set; } = 5; /// - /// The last_epoch parameter for the ConstantLR module. + /// The last_epoch parameter for the Constant module. /// - [Description("The last_epoch parameter for the ConstantLR module")] + [Description("The last_epoch parameter for the Constant module")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the ConstantLR module. + /// The verbose parameter for the Constant module. /// - [Description("The verbose parameter for the ConstantLR module")] + [Description("The verbose parameter for the Constant module")] public bool Verbose { get; set; } = false; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs similarity index 55% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs index 09206d8e..76006e6e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/CosineAnnealingLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs @@ -10,44 +10,44 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a CosineAnnealingLearningRate scheduler. +/// Sets the learning rate using a cosine annealing schedule. /// [Combinator] -[Description("Creates a CosineAnnealingLearningRate scheduler.")] +[Description("Sets the learning rate using a cosine annealing schedule.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CosineAnnealingLearningRateScheduler +public class CosineAnnealing { /// - /// The optimizer parameter for the CosineAnnealingLR module. + /// The wrapped optimizer. /// - [Description("The optimizer parameter for the CosineAnnealingLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + [Description("The wrapped optimizer.")] + public optim.Optimizer Optimizer { get; set; } /// - /// The t_max parameter for the CosineAnnealingLR module. + /// The maximum number of iterations. /// - [Description("The t_max parameter for the CosineAnnealingLR module")] + [Description("The maximum number of iterations.")] public double TMax { get; set; } /// - /// The eta_min parameter for the CosineAnnealingLR module. + /// The minimum learning rate. /// - [Description("The eta_min parameter for the CosineAnnealingLR module")] + [Description("The minimum learning rate.")] public double EtaMin { get; set; } = 0D; /// - /// The last_epoch parameter for the CosineAnnealingLR module. + /// The index /// - [Description("The last_epoch parameter for the CosineAnnealingLR module")] + [Description("The last_epoch parameter for the CosineAnnealing module")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the CosineAnnealingLR module. + /// The verbose parameter for the CosineAnnealing module. /// - [Description("The verbose parameter for the CosineAnnealingLR module")] + [Description("The verbose parameter for the CosineAnnealing module")] public bool Verbose { get; set; } = false; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs index cc7ee4c7..ecdb9af9 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ExponentialLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs @@ -10,21 +10,21 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a ExponentialLearningRate scheduler. +/// Creates an exponential learning rate scheduler. /// [Combinator] -[Description("Creates a ExponentialLearningRate scheduler.")] +[Description("Creates an exponential learning rate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ExponentialLearningRateScheduler +public class Exponential { /// /// The optimizer parameter for the ExponentialLR module. /// [Description("The optimizer parameter for the ExponentialLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The gamma parameter for the ExponentialLR module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs index d961f1d6..c8d512ff 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/LinearLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs @@ -10,21 +10,21 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a LinearLearningRate scheduler. +/// Creates a linear learning rate scheduler. /// [Combinator] -[Description("Creates a LinearLearningRate scheduler.")] +[Description("Creates a linear learning rate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LinearLearningRateScheduler +public class Linear { /// /// The optimizer parameter for the LinearLR module. /// [Description("The optimizer parameter for the LinearLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The start_factor parameter for the LinearLR module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs index 5ccada37..2b3ba8a5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/MultiStepLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs @@ -10,7 +10,7 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// /// Creates a MultiStepLearningRate scheduler. @@ -18,13 +18,13 @@ namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; [Combinator] [Description("Creates a MultiStepLearningRate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiStepLearningRateScheduler +public class MultiStep { /// /// The optimizer parameter for the MultiStepLR module. /// [Description("The optimizer parameter for the MultiStepLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The milestones parameter for the MultiStepLR module. @@ -51,7 +51,7 @@ public class MultiStepLearningRateScheduler public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a MultiStepLearningRateScheduler. + /// Generates an observable sequence that creates a MultiStep. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs index 541c2006..7fab62a4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/OneCycleLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs @@ -10,21 +10,21 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a OneCycleLearningRate scheduler. +/// Creates a one cycle learning rate policy scheduler. /// [Combinator] -[Description("Creates a OneCycleLearningRate scheduler.")] +[Description("Creates a one cycle learning rate policy scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class OneCycleLearningRateScheduler +public class OneCycle { /// /// The optimizer parameter for the OneCycleLR module. /// [Description("The optimizer parameter for the OneCycleLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The max_lr parameter for the OneCycleLR module. @@ -111,7 +111,7 @@ public class OneCycleLearningRateScheduler public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a OneCycleLearningRateScheduler. + /// Generates an observable sequence that creates a OneCycle. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs index e0114bd8..699ce0c6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/PolynomialLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs @@ -10,21 +10,21 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a PolynomialLearningRate scheduler. +/// Creates a polynomial learning rate scheduler. /// [Combinator] -[Description("Creates a PolynomialLearningRate scheduler.")] +[Description("Creates a polynomial learning rate scheduler.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PolynomialLearningRateScheduler +public class Polynomial { /// /// The optimizer parameter for the PolynomialLR module. /// [Description("The optimizer parameter for the PolynomialLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The total_iters parameter for the PolynomialLR module. @@ -51,7 +51,7 @@ public class PolynomialLearningRateScheduler public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a PolynomialLearningRateScheduler. + /// Generates an observable sequence that creates a Polynomial. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs index 6d45b009..ea1dd030 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/ReduceLearningRateOnPlateauScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs @@ -10,21 +10,21 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a ReduceLearningRateOnPlateau scheduler. +/// Creates a scheduler that reduces the learning rate when a metric has stopped improving. /// [Combinator] -[Description("Creates a ReduceLearningRateOnPlateau scheduler.")] +[Description("Creates a scheduler that reduces the learning rate when a metric has stopped improving.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ReduceLearningRateOnPlateauScheduler +public class ReduceOnPlateau { /// /// The optimizer parameter for the ReduceLROnPlateau module. /// [Description("The optimizer parameter for the ReduceLROnPlateau module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The mode parameter for the ReduceLROnPlateau module. @@ -81,7 +81,7 @@ public class ReduceLearningRateOnPlateauScheduler public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a ReduceLearningRateOnPlateauScheduler. + /// Generates an observable sequence that creates a ReduceOnPlateau. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLearningRateScheduler.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs similarity index 78% rename from src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLearningRateScheduler.cs rename to src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs index 211f2b5a..0af7606a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulers/StepLearningRateScheduler.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs @@ -10,21 +10,21 @@ using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LearningRateSchedulers; +namespace Bonsai.ML.Torch.NeuralNets.LinearRateScheduler; /// -/// Creates a StepLearningRate scheduler. +/// Creates a scheduler that decays the learning rate of each parameter group by gamma every step_size epochs. /// [Combinator] -[Description("Creates a StepLearningRate scheduler.")] +[Description("Creates a scheduler that decays the learning rate of each parameter group by gamma every step_size epochs.")] [WorkflowElementCategory(ElementCategory.Source)] -public class StepLearningRateScheduler +public class Step { /// /// The optimizer parameter for the StepLR module. /// [Description("The optimizer parameter for the StepLR module")] - public torch.optim.Optimizer Optimizer { get; set; } + public optim.Optimizer Optimizer { get; set; } /// /// The step_size parameter for the StepLR module. @@ -51,7 +51,7 @@ public class StepLearningRateScheduler public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a StepLearningRateScheduler. + /// Generates an observable sequence that creates a Step. /// public IObservable Process() { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs index 4ccc0380..0006a06d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs @@ -22,22 +22,34 @@ public class LoadModule /// [Description("The model to save.")] [XmlIgnore] - public Module Module { get; set; } + public nn.Module Module { get; set; } /// - /// The path to save the model. + /// The path to the modules state. /// - [Description("The path to save the model.")] + [Description("The path to the modules state.")] [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModelPath { get; set; } + public string ModulePath { get; set; } /// - /// Saves the model to the specified file path. + /// Loads the modules state from the specified file path. /// /// - public IObservable Process() + public IObservable Process() { - return Observable.Return(Module.load(ModelPath)); + return Observable.Return(Module.load(ModulePath)); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + public IObservable Process(IObservable source) + { + return source.Select(module => + { + return module.load(ModulePath); + }); } } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModelArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs similarity index 81% rename from src/Bonsai.ML.Torch/NeuralNets/LoadModelArchitecture.cs rename to src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs index 65902449..70550353 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModelArchitecture.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs @@ -15,13 +15,13 @@ namespace Bonsai.ML.Torch.NeuralNets [ResetCombinator] [Description("Loads a neural network module from a specified architecture.")] [WorkflowElementCategory(ElementCategory.Source)] - public class LoadModuleFromArchitecture + public class LoadModuleArchitecture { /// /// The model architecture to load. /// [Description("The model architecture to load.")] - public Models.ModelArchitecture ModelArchitecture { get; set; } + public Architecture.ModelArchitecture ModelArchitecture { get; set; } /// /// The device on which to load the model. @@ -66,11 +66,11 @@ public int NumClasses public IObservable> Process() { var device = Device; - nn.Module module = ModelArchitecture switch + Module module = ModelArchitecture switch { - Models.ModelArchitecture.AlexNet => new Models.AlexNet("alexnet", numClasses, device), - Models.ModelArchitecture.MobileNet => new Models.MobileNet("mobilenet", numClasses, device), - Models.ModelArchitecture.Mnist => new Models.Mnist("mnist", numClasses, device), + Architecture.ModelArchitecture.AlexNet => new Architecture.AlexNet("alexnet", numClasses, device), + Architecture.ModelArchitecture.MobileNet => new Architecture.MobileNet("mobilenet", numClasses, device), + Architecture.ModelArchitecture.Mnist => new Architecture.Mnist("mnist", numClasses, device), _ => throw new ArgumentException($"Model {ModelArchitecture} not supported.") }; diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs index eb8c0c56..baded318 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs @@ -3,6 +3,7 @@ using System.Reactive.Linq; using TorchSharp; using static TorchSharp.torch; +using static TorchSharp.torch.nn; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets @@ -34,7 +35,7 @@ public class LoadScriptModule /// Loads the TorchScript module from the specified file path. /// /// - public IObservable> Process() + public IObservable> Process() { var scriptModule = Device is null ? jit.load(ModelPath) : jit.load(ModelPath, Device); return Observable.Return(scriptModule); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs new file mode 100644 index 00000000..9c26e358 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Loss; + +/// +/// Creates a binary cross entropy loss module. +/// +[Combinator] +[Description("Creates a binary cross entropy loss module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BinaryCrossEntropy : IScalarTypeProvider +{ + /// + /// The weight parameter for the BinaryCrossEntropy module. + /// + [Description("The weight parameter for the BinaryCrossEntropy module")] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the BinaryCrossEntropy module. + /// + [Description("The reduction parameter for the BinaryCrossEntropy module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The data type of the tensor elements. + /// + [Description("The data type of the tensor elements.")] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Generates an observable sequence that creates a BinaryCrossEntropy. + /// + public IObservable> Process() + { + return Observable.Return(BCELoss(Weight, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs new file mode 100644 index 00000000..d7b8b9a5 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Loss; + +/// +/// Measures the Binary Cross Entropy between the target and the output logits. +/// +[Combinator] +[Description("Measures the Binary Cross Entropy between the target and the output logits.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class BinaryCrossEntropyWithLogits +{ + /// + /// The weight parameter for the BinaryCrossEntropyWithLogitsLoss module. + /// + [Description("The weight parameter for the BinaryCrossEntropyWithLogitsLoss module")] + public Tensor Weight { get; set; } = null; + + /// + /// The reduction parameter for the BinaryCrossEntropyWithLogitsLoss module. + /// + [Description("The reduction parameter for the BinaryCrossEntropyWithLogitsLoss module")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The pos_weights parameter for the BinaryCrossEntropyWithLogitsLoss module. + /// + [Description("The pos_weights parameter for the BinaryCrossEntropyWithLogitsLoss module")] + public Tensor PosWeights { get; set; } = null; + + /// + /// Generates an observable sequence that creates a BinaryCrossEntropyWithLogitsLoss. + /// + public IObservable> Process() + { + return Observable.Return(BCEWithLogitsLoss(Weight, Reduction, PosWeights)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs index 262d60cc..b4958c14 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/CTCLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a CTCLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a CTCLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CTCLoss +public class ConnectionistTemporalClassification { /// /// The blank parameter for the CTCLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs similarity index 81% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs index c7c6c2fd..c363d71a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/CosineEmbeddingLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs @@ -8,15 +8,15 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a CosineEmbeddingLoss module. +/// Measures the loss given two input tensor and a label tensor with values 1 or -1 /// [Combinator] -[Description("Creates a CosineEmbeddingLoss module.")] +[Description("Measures the loss given two input tensor and a label tensor with values 1 or -1")] [WorkflowElementCategory(ElementCategory.Source)] -public class CosineEmbeddingLoss +public class CosineEmbedding { /// /// The margin parameter for the CosineEmbeddingLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs index 0711b743..12d3de41 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/CrossEntropyLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs @@ -8,21 +8,21 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a CrossEntropyLoss module. +/// Computes the cross entropy loss between input logits and target. /// [Combinator] -[Description("Creates a CrossEntropyLoss module.")] +[Description("Computes the cross entropy loss between input logits and target.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CrossEntropyLoss +public class CrossEntropy { /// /// The weight parameter for the CrossEntropyLoss module. /// [Description("The weight parameter for the CrossEntropyLoss module")] - public torch.Tensor Weight { get; set; } = null; + public Tensor Weight { get; set; } = null; /// /// The ignore_index parameter for the CrossEntropyLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs index fa8a531a..2e07867e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/GaussianNLLLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a GaussianNLLLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a GaussianNLLLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class GaussianNLLLoss +public class GaussianNegativeLogLikelihood { /// /// The full parameter for the GaussianNLLLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs index a1c11c8f..570b3d93 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/HingeEmbeddingLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a HingeEmbeddingLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs index 6e931c80..16d81183 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/HuberLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a HuberLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a HuberLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HuberLoss +public class Huber { /// /// The delta parameter for the HuberLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs index 1092810b..21770e36 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/KLDivLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a KLDivLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a KLDivLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class KLDivLoss +public class KullbackLeiblerDivergence { /// /// The log_target parameter for the KLDivLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs index f0458172..0925be56 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MarginRankingLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a MarginRankingLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a MarginRankingLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MarginRankingLoss +public class MarginRanking { /// /// The margin parameter for the MarginRankingLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/L1Loss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/L1Loss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs index 7d89ac75..898c0e6b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/L1Loss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a L1Loss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a L1Loss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class L1Loss +public class MeanAbsoluteError { /// /// The reduction parameter for the L1Loss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MSELoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs index 0c6fddac..b1502da4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MSELoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a MSELoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a MSELoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MSELoss +public class MeanSquaredError { /// /// The reduction parameter for the MSELoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs index 36cf82a2..eb640076 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelMarginLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a MultiLabelMarginLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a MultiLabelMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiLabelMarginLoss +public class MultiLabelMargin { /// /// The reduction parameter for the MultiLabelMarginLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs index f57061c6..a44bca1c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiLabelSoftMarginLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a MultiLabelSoftMarginLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a MultiLabelSoftMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MultiLabelSoftMarginLoss +public class MultiLabelSoftMargin { /// /// The weight parameter for the MultiLabelSoftMarginLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs similarity index 97% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs index 6fe9190b..ec85027e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/MultiMarginLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a MultiMarginLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs index 8e585c2b..d566ec9c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/NLLLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a NLLLoss module. @@ -16,13 +16,13 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a NLLLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class NLLLoss +public class NegativeLogLikelihood { /// /// The weight parameter for the NLLLoss module. /// [Description("The weight parameter for the NLLLoss module")] - public torch.Tensor Weight { get; set; } = null; + public Tensor Weight { get; set; } = null; /// /// The reduction parameter for the NLLLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs index 382e7702..4e966ba4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/PoissonNLLLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a PoissonNLLLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a PoissonNLLLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PoissonNLLLoss +public class PoissonNegativeLogLikelihood { /// /// The log_input parameter for the PoissonNLLLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1Loss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1Loss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs index ac383424..67e15cec 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/SmoothL1Loss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a SmoothL1Loss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a SmoothL1Loss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SmoothL1Loss +public class SmoothL1 { /// /// The reduction parameter for the SmoothL1Loss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs index 60532b36..1f961810 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/SoftMarginLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a SoftMarginLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a SoftMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftMarginLoss +public class SoftMargin { /// /// The reduction parameter for the SoftMarginLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLoss.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs index 48971eb8..128acb55 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/TripletMarginLoss.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Losses; +namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// Creates a TripletMarginLoss module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Losses; [Combinator] [Description("Creates a TripletMarginLoss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TripletMarginLoss +public class TripletMargin { /// /// The margin parameter for the TripletMarginLoss module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs deleted file mode 100644 index 2e09690a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCELoss.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Losses; - -/// -/// Creates a BCELoss module. -/// -[Combinator] -[Description("Creates a BCELoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class BCELoss -{ - /// - /// The weight parameter for the BCELoss module. - /// - [Description("The weight parameter for the BCELoss module")] - public torch.Tensor Weight { get; set; } = null; - - /// - /// The reduction parameter for the BCELoss module. - /// - [Description("The reduction parameter for the BCELoss module")] - public Reduction Reduction { get; set; } = Reduction.Mean; - - /// - /// Generates an observable sequence that creates a BCELoss. - /// - public IObservable> Process() - { - return Observable.Return(BCELoss(Weight, Reduction)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs deleted file mode 100644 index 579b73ab..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Losses/BCEWithLogitsLoss.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Losses; - -/// -/// Creates a BCEWithLogitsLoss module. -/// -[Combinator] -[Description("Creates a BCEWithLogitsLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class BCEWithLogitsLoss -{ - /// - /// The weight parameter for the BCEWithLogitsLoss module. - /// - [Description("The weight parameter for the BCEWithLogitsLoss module")] - public torch.Tensor Weight { get; set; } = null; - - /// - /// The reduction parameter for the BCEWithLogitsLoss module. - /// - [Description("The reduction parameter for the BCEWithLogitsLoss module")] - public Reduction Reduction { get; set; } = Reduction.Mean; - - /// - /// The pos_weights parameter for the BCEWithLogitsLoss module. - /// - [Description("The pos_weights parameter for the BCEWithLogitsLoss module")] - public torch.Tensor PosWeights { get; set; } = null; - - /// - /// Generates an observable sequence that creates a BCEWithLogitsLoss. - /// - public IObservable> Process() - { - return Observable.Return(BCEWithLogitsLoss(Weight, Reduction, PosWeights)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Models/AlexNet.cs b/src/Bonsai.ML.Torch/NeuralNets/Models/AlexNet.cs deleted file mode 100644 index c3d19d55..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Models/AlexNet.cs +++ /dev/null @@ -1,74 +0,0 @@ -using TorchSharp; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Models -{ - /// - /// Modified version of original AlexNet to fix CIFAR10 32x32 images. - /// - internal class AlexNet : Module - { - private readonly Module features; - private readonly Module avgPool; - private readonly Module classifier; - - /// - /// Constructs a new AlexNet model. - /// - /// - /// - /// - public AlexNet(string name, int numClasses, Device device = null) : base(name) - { - features = Sequential( - ("c1", Conv2d(3, 64, kernel_size: 3, stride: 2, padding: 1)), - ("r1", ReLU(inplace: true)), - ("mp1", MaxPool2d(kernel_size: [ 2, 2 ])), - ("c2", Conv2d(64, 192, kernel_size: 3, padding: 1)), - ("r2", ReLU(inplace: true)), - ("mp2", MaxPool2d(kernel_size: [ 2, 2 ])), - ("c3", Conv2d(192, 384, kernel_size: 3, padding: 1)), - ("r3", ReLU(inplace: true)), - ("c4", Conv2d(384, 256, kernel_size: 3, padding: 1)), - ("r4", ReLU(inplace: true)), - ("c5", Conv2d(256, 256, kernel_size: 3, padding: 1)), - ("r5", ReLU(inplace: true)), - ("mp3", MaxPool2d(kernel_size: [ 2, 2 ]))); - - avgPool = AdaptiveAvgPool2d([ 2, 2 ]); - - classifier = Sequential( - ("d1", Dropout()), - ("l1", Linear(256 * 2 * 2, 4096)), - ("r1", ReLU(inplace: true)), - ("d2", Dropout()), - ("l2", Linear(4096, 4096)), - ("r3", ReLU(inplace: true)), - ("d3", Dropout()), - ("l3", Linear(4096, numClasses)) - ); - - RegisterComponents(); - - if (device != null && device.type != DeviceType.CPU) - this.to(device); - } - - /// - /// Forward pass of the AlexNet model. - /// - /// - /// - public override Tensor forward(Tensor input) - { - var f = features.forward(input); - var avg = avgPool.forward(f); - - var x = avg.view([ avg.shape[0], 256 * 2 * 2 ]); - - return classifier.forward(x); - } - } - -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Models/Mnist.cs b/src/Bonsai.ML.Torch/NeuralNets/Models/Mnist.cs deleted file mode 100644 index a1606ddc..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Models/Mnist.cs +++ /dev/null @@ -1,86 +0,0 @@ -using TorchSharp; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Models -{ - /// - /// Represents a simple convolutional neural network for the MNIST dataset. - /// - internal class Mnist : Module - { - private readonly Module conv1; - private readonly Module conv2; - private readonly Module fc1; - private readonly Module fc2; - - private readonly Module pool1; - - private readonly Module relu1; - private readonly Module relu2; - private readonly Module relu3; - - private readonly Module dropout1; - private readonly Module dropout2; - - private readonly Module flatten; - private readonly Module logsm; - - /// - /// Constructs a new Mnist model. - /// - /// - /// - /// - public Mnist(string name, int numClasses, Device device = null) : base(name) - { - conv1 = Conv2d(1, 32, 3); - conv2 = Conv2d(32, 64, 3); - fc1 = Linear(9216, 128); - fc2 = Linear(128, numClasses); - - pool1 = MaxPool2d(kernel_size: [2, 2]); - - relu1 = ReLU(); - relu2 = ReLU(); - relu3 = ReLU(); - - dropout1 = Dropout(0.25); - dropout2 = Dropout(0.5); - - flatten = Flatten(); - logsm = LogSoftmax(1); - - RegisterComponents(); - - if (device != null && device.type != DeviceType.CPU) - this.to(device); - } - - /// - /// Forward pass of the Mnist model. - /// - /// - /// - public override Tensor forward(Tensor input) - { - var l11 = conv1.forward(input); - var l12 = relu1.forward(l11); - - var l21 = conv2.forward(l12); - var l22 = relu2.forward(l21); - var l23 = pool1.forward(l22); - var l24 = dropout1.forward(l23); - - var x = flatten.forward(l24); - - var l31 = fc1.forward(x); - var l32 = relu3.forward(l31); - var l33 = dropout2.forward(l32); - - var l41 = fc2.forward(l33); - - return logsm.forward(l41); - } - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Models/MobileNet.cs b/src/Bonsai.ML.Torch/NeuralNets/Models/MobileNet.cs deleted file mode 100644 index a5f7701a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Models/MobileNet.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.Collections.Generic; -using TorchSharp; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Models -{ - /// - /// MobileNet model. - /// - internal class MobileNet : Module - { - private readonly long[] planes = [ 64, 128, 128, 256, 256, 512, 512, 512, 512, 512, 512, 1024, 1024 ]; - private readonly long[] strides = [ 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1 ]; - - private readonly Module layers; - - /// - /// Constructs a new MobileNet model. - /// - /// - /// - /// - /// - public MobileNet(string name, int numClasses, Device device = null) : base(name) - { - if (planes.Length != strides.Length) throw new ArgumentException("'planes' and 'strides' must have the same length."); - - var modules = new List<(string, Module)> - { - ($"conv2d-first", Conv2d(3, 32, kernel_size: 3, stride: 1, padding: 1, bias: false)), - ($"bnrm2d-first", BatchNorm2d(32)), - ($"relu-first", ReLU()) - }; - MakeLayers(modules, 32); - modules.Add(("avgpool", AvgPool2d([2, 2]))); - modules.Add(("flatten", Flatten())); - modules.Add(($"linear", Linear(planes[planes.Length-1], numClasses))); - - layers = Sequential(modules); - - RegisterComponents(); - - if (device != null && device.type != DeviceType.CPU) - this.to(device); - } - - private void MakeLayers(List<(string, Module)> modules, long in_planes) - { - - for (var i = 0; i < strides.Length; i++) { - var out_planes = planes[i]; - var stride = strides[i]; - - modules.Add(($"conv2d-{i}a", Conv2d(in_planes, in_planes, kernel_size: 3, stride: stride, padding: 1, groups: in_planes, bias: false))); - modules.Add(($"bnrm2d-{i}a", BatchNorm2d(in_planes))); - modules.Add(($"relu-{i}a", ReLU())); - modules.Add(($"conv2d-{i}b", Conv2d(in_planes, out_planes, kernel_size: 1L, stride: 1L, padding: 0L, bias: false))); - modules.Add(($"bnrm2d-{i}b", BatchNorm2d(out_planes))); - modules.Add(($"relu-{i}b", ReLU())); - - in_planes = out_planes; - } - } - - /// - /// Forward pass of the MobileNet model. - /// - /// - /// - public override Tensor forward(Tensor input) - { - return layers.forward(input); - } - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Models/ModelArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/Models/ModelArchitecture.cs deleted file mode 100644 index 9d6fe55b..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Models/ModelArchitecture.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Bonsai.ML.Torch.NeuralNets.Models -{ - /// - /// Represents the architecture of a neural network model. - /// - public enum ModelArchitecture - { - /// - /// The AlexNet model architecture. - /// - AlexNet, - - /// - /// The MobileNet model architecture. - /// - MobileNet, - - /// - /// The Mnist model architecture. - /// - Mnist - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs similarity index 50% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs index 947d3a25..8f1595d3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs @@ -8,16 +8,22 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// -/// Creates a 2D adaptive average pooling layer. +/// Creates a 1D adaptive average pooling layer. /// [Combinator] -[Description("Creates a 2D adaptive average pooling layer.")] +[Description("Creates a 1D adaptive average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveAvgPool2dModule +public class AdaptiveAveragePooling { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + + public Dimensions Dimensions { get; set; } = Dimensions.One; + /// /// The outputsize parameter for the AdaptiveAvgPool2d module. /// @@ -26,10 +32,16 @@ public class AdaptiveAvgPool2dModule public long[] OutputSize { get; set; } /// - /// Generates an observable sequence that creates a AdaptiveAvgPool2dModule module. + /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// public IObservable> Process() { - return Observable.Return(AdaptiveAvgPool2d(OutputSize)); + return Dimensions switch + { + Dimensions.One => Observable.Return(AdaptiveAvgPool1d(OutputSize[0])), + Dimensions.Two => Observable.Return(AdaptiveAvgPool2d(OutputSize)), + Dimensions.Three => Observable.Return(AdaptiveAvgPool3d(OutputSize)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs new file mode 100644 index 00000000..7883762a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs @@ -0,0 +1,47 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Module; + +/// +/// Creates a 1D adaptive max pooling layer. +/// +[Combinator] +[Description("Creates a 1D adaptive max pooling layer.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AdaptiveMaxPooling +{ + /// + /// The number of dimensions for the AdaptiveMaxPool module. + /// + [Description("The number of dimensions for the AdaptiveMaxPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + + /// + /// The outputsize parameter for the AdaptiveMaxPool2d module. + /// + [Description("The outputsize parameter for the AdaptiveMaxPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveMaxPool1dModule module. + /// + public IObservable> Process() + { + return Dimensions switch + { + Dimensions.One => Observable.Return(AdaptiveMaxPool1d(OutputSize[0])), + Dimensions.Two => Observable.Return(AdaptiveMaxPool2d(OutputSize)), + Dimensions.Three => Observable.Return(AdaptiveMaxPool3d(OutputSize)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/AlphaDropout.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/AlphaDropout.cs index e68697e3..4819d72d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AlphaDropoutModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/AlphaDropout.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a AlphaDropout module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a AlphaDropout module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AlphaDropoutModule +public class AlphaDropout { /// /// The p parameter for the AlphaDropout module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs new file mode 100644 index 00000000..c452d0b6 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs @@ -0,0 +1,47 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Module; + +/// +/// Creates a 1D adaptive average pooling layer. +/// +[Combinator] +[Description("Creates a 1D adaptive average pooling layer.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class AveragePooling +{ + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + + /// + /// The outputsize parameter for the AdaptiveAvgPool2d module. + /// + [Description("The outputsize parameter for the AdaptiveAvgPool2d module")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. + /// + public IObservable> Process() + { + return Dimensions switch + { + Dimensions.One => Observable.Return(AvgPool1d(OutputSize[0])), + Dimensions.Two => Observable.Return(AvgPool2d(OutputSize)), + Dimensions.Three => Observable.Return(AvgPool3d(OutputSize)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs similarity index 64% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs index 8e421857..b4a79a38 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs @@ -8,16 +8,22 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// -/// Creates a 1D batch normalization layer. +/// Creates a 1D adaptive average pooling layer. /// [Combinator] -[Description("Creates a 1D batch normalization layer.")] +[Description("Creates a 1D adaptive average pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class BatchNorm1dModule +public class BatchNormalization { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + /// /// The features parameter for the BatchNorm1d module. /// @@ -52,7 +58,7 @@ public class BatchNorm1dModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -62,10 +68,16 @@ public class BatchNorm1dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a BatchNorm1dModule module. + /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// public IObservable> Process() { - return Observable.Return(BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + return Dimensions switch + { + Dimensions.One => Observable.Return(BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), + Dimensions.Two => Observable.Return(BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), + Dimensions.Three => Observable.Return(BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ChannelShuffle.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/ChannelShuffle.cs index b6b800b8..f22ae74a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ChannelShuffleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ChannelShuffle.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a ChannelShuffle module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a ChannelShuffle module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ChannelShuffleModule +public class ChannelShuffle { /// /// The groups parameter for the ChannelShuffle module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ContinuouslyDifferentiableExponentialLinearUnit.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/ContinuouslyDifferentiableExponentialLinearUnit.cs index 9ff0a74c..85bbd7f0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/CELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ContinuouslyDifferentiableExponentialLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a CELU module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a CELU module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CELUModule +public class ContinuouslyDifferentiableExponentialLinearUnit { /// /// The alpha parameter for the CELU module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs similarity index 58% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs index fdb0e1a3..e84ad751 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a 1D convolution layer. @@ -16,8 +16,14 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a 1D convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class Conv1dModule +public class Convolution { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + /// /// The in_channels parameter for the Conv1d module. /// @@ -48,6 +54,12 @@ public class Conv1dModule [Description("The padding parameter for the Conv1d module")] public long Padding { get; set; } = 0; + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + /// /// The dilation parameter for the Conv1d module. /// @@ -72,11 +84,17 @@ public class Conv1dModule [Description("If true, adds a learnable bias to the output")] public bool Bias { get; set; } = true; + /// + /// If true, creates a transposed convolution layer. + /// + [Description("If true, creates a transposed convolution layer")] + public bool Transposed { get; set; } = false; + /// /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -90,6 +108,18 @@ public class Conv1dModule /// public IObservable> Process() { - return Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + return Dimensions switch + { + Dimensions.One => Transposed + ? Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, 0, Dilation, PaddingMode, Groups, Bias, Device, Type)) + : Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)), + Dimensions.Two => Transposed + ? Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)) + : Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)), + Dimensions.Three => Transposed + ? Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)) + : Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs new file mode 100644 index 00000000..fb1d9ab3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs @@ -0,0 +1,27 @@ +namespace Bonsai.ML.Torch.NeuralNets.Module; + +/// +/// Specifies the number of dimensions for a neural network module. +/// +public enum Dimensions +{ + /// + /// No dimensions. + /// + None = 0, + + /// + /// One dimension. + /// + One = 1, + + /// + /// Two dimensions. + /// + Two = 2, + + /// + /// Three dimensions. + /// + Three = 3 +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs new file mode 100644 index 00000000..68eea2a7 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs @@ -0,0 +1,53 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Module; + +/// +/// Creates a dropout layer. +/// +[Combinator] +[Description("Creates a dropout layer.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Dropout +{ + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.None; + + /// + /// The p parameter for the Dropout1d module. + /// + [Description("The p parameter for the Dropout1d module")] + public double P { get; set; } = 0.5D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a Dropout module. + /// + public IObservable> Process() + { + return Dimensions switch + { + Dimensions.None => Observable.Return(Dropout(P, Inplace)), + Dimensions.One => Observable.Return(Dropout1d(P, Inplace)), + Dimensions.Two => Observable.Return(Dropout2d(P, Inplace)), + Dimensions.Three => Observable.Return(Dropout3d(P, Inplace)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs similarity index 97% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs index e10972d5..f4e2202d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Embedding layer. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Embedding layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingModule +public class Embedding { /// /// The num_embeddings parameter for the Embedding module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs similarity index 97% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs index 2f429733..fc918b71 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/EmbeddingFromPretrainedModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a EmbeddingFromPretrained module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a EmbeddingFromPretrained module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingFromPretrainedModule +public class EmbeddingFromPretrained { /// /// The embeddings parameter for the Embedding_from_pretrained module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ExponentialLinearUnit.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/ExponentialLinearUnit.cs index 362ad7e0..085682ce 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ExponentialLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a ELU activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a ELU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ELUModule +public class ExponentialLinearUnit { /// /// The alpha parameter for the ELU module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/FeatureAlphaDropout.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/FeatureAlphaDropout.cs index 544e3108..a95fc8ea 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FeatureAlphaDropoutModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/FeatureAlphaDropout.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a FeatureAlphaDropout module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Flatten.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Flatten.cs index e51f8189..566a65ae 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FlattenModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Flatten.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Flatten module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Flatten module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class FlattenModule +public class Flatten { /// /// The startdim parameter for the Flatten module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Fold.cs similarity index 97% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Fold.cs index 10310bea..c5efc375 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FoldModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Fold.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Fold module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/FractionalMaxPooling.cs similarity index 57% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/FractionalMaxPooling.cs index f049dc07..f349a02c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/FractionalMaxPooling.cs @@ -8,16 +8,22 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// -/// Creates a FractionalMaxPool3d module. +/// Creates a dropout layer. /// [Combinator] -[Description("Creates a FractionalMaxPool3d module.")] +[Description("Creates a fractional max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class FractionalMaxPool3dModule +public class FractionalMaxPooling { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.Two; + /// /// The kernel_size parameter for the FractionalMaxPool3d module. /// @@ -37,10 +43,15 @@ public class FractionalMaxPool3dModule public double? OutputRatio { get; set; } = null; /// - /// Generates an observable sequence that creates a FractionalMaxPool3dModule module. + /// Generates an observable sequence that creates a Dropout module. /// public IObservable> Process() { - return Observable.Return(FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)); + return Dimensions switch + { + Dimensions.Two => Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)), + Dimensions.Three => Observable.Return(FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/GatedLinearUnit.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/GatedLinearUnit.cs index 590be20b..1ba175e0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/GLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/GatedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a GLU module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a GLU module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class GLUModule +public class GatedLinearUnit { /// /// The dim parameter for the GLU module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/GaussianErrorLinearUnit.cs similarity index 56% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/GaussianErrorLinearUnit.cs index c8091be2..27880315 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/GELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/GaussianErrorLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a GELU activation function. @@ -16,13 +16,19 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a GELU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class GELUModule +public class GaussianErrorLinearUnit { /// - /// Generates an observable sequence that creates a GELUModule module. + /// The inPlace parameter for the GELU module. + /// + [Description("The inPlace parameter for the GELU module")] + public bool InPlace { get; set; } = false; + + /// + /// Generates an observable sequence that creates a GaussianErrorLinearUnit module. /// public IObservable> Process() { - return Observable.Return(GELU()); + return Observable.Return(GELU(InPlace)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs index db1efbed..9276627d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/GroupNormModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Group normalization. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Group normalization.")] [WorkflowElementCategory(ElementCategory.Source)] -public class GroupNormModule +public class GroupNormalization { /// /// The num_groups parameter for the GroupNorm module. @@ -46,7 +46,7 @@ public class GroupNormModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/HardShrinkage.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/HardShrinkage.cs index cd07c78b..42033009 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardshrinkModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/HardShrinkage.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Hardshrink module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Hardshrink module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HardshrinkModule +public class HardShrinkage { /// /// The lambda parameter for the Hardshrink module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/HardSigmoid.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/HardSigmoid.cs index f6d7191c..c1b90b97 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardsigmoidModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/HardSigmoid.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Hardsigmoid module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Hardsigmoid module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HardsigmoidModule +public class HardSigmoid { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/HardSwish.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/HardSwish.cs index 72c7579c..9140a9f4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardswishModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/HardSwish.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Hardswish module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Hardswish module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HardswishModule +public class HardSwish { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/HardTanh.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/HardTanh.cs index e7598136..5fb75ec4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/HardtanhModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/HardTanh.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Hardtanh module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Hardtanh module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class HardtanhModule +public class HardTanh { /// /// The min_val parameter for the Hardtanh module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Identity.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Identity.cs index 2cce74ef..d2bd4fdc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/IdentityModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Identity.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Identity module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Identity module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class IdentityModule +public class Identity { /// /// Generates an observable sequence that creates a IdentityModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs similarity index 67% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs index 8079b53f..4e417935 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs @@ -8,16 +8,22 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// -/// Creates a 1D instance normalization. +/// Creates a dropout layer. /// [Combinator] -[Description("Creates a 1D instance normalization.")] +[Description("Creates a fractional max pooling layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class InstanceNorm1dModule +public class InstanceNormalization { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.Two; + /// /// The features parameter for the InstanceNorm1d module. /// @@ -62,10 +68,16 @@ public class InstanceNorm1dModule public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a InstanceNorm1dModule module. + /// Generates an observable sequence that creates a Dropout module. /// public IObservable> Process() { - return Observable.Return(InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + return Dimensions switch + { + Dimensions.One => Observable.Return(InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), + Dimensions.Two => Observable.Return(InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), + Dimensions.Three => Observable.Return(InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs index 192345de..b77d6fba 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LayerNormModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Layer normalization. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Layer normalization.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LayerNormModule +public class LayerNormalization { /// /// The normalized_shape parameter for the LayerNorm module. @@ -47,7 +47,7 @@ public class LayerNormModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/LeakyRectifiedLinearUnit.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/LeakyRectifiedLinearUnit.cs index 2a9c5bb5..0d5a0536 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LeakyReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/LeakyRectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a LeakyReLU activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a LeakyReLU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LeakyReLUModule +public class LeakyRectifiedLinearUnit { /// /// The negative_slope parameter for the LeakyReLU module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs index 031b8634..49c6e9f5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LinearModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Linear transformation layer. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Linear transformation layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LinearModule +public class Linear { /// /// The inputsize parameter for the Linear module. @@ -40,7 +40,7 @@ public class LinearModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/LocalResponseNormalization.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/LocalResponseNormalization.cs index f2bbd077..9c7ca273 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LocalResponseNormModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/LocalResponseNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a LocalResponseNorm module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a LocalResponseNorm module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LocalResponseNormModule +public class LocalResponseNormalization { /// /// The size parameter for the LocalResponseNorm module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/LogSigmoid.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/LogSigmoid.cs index 53cdd8d2..51f79a31 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSigmoidModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/LogSigmoid.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a LogSigmoid module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a LogSigmoid module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LogSigmoidModule +public class LogSigmoid { /// /// Generates an observable sequence that creates a LogSigmoidModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/LogSoftmax.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/LogSoftmax.cs index 6b10d805..51e58c46 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LogSoftmaxModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/LogSoftmax.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a LogSoftmax activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a LogSoftmax activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LogSoftmaxModule +public class LogSoftmax { /// /// The dim parameter for the LogSoftmax module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/MaxPooling.cs similarity index 60% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/MaxPooling.cs index d6b4a2f5..12c2bf39 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool3dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/MaxPooling.cs @@ -7,17 +7,24 @@ using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; +using Bonsai.Reactive; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// -/// Creates a 3D max pooling layer. +/// Creates a LPPool1d module. /// [Combinator] -[Description("Creates a 3D max pooling layer.")] +[Description("Creates a LPPool1d module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MaxPool3dModule +public class MaxPooling { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + /// /// The kernelsize parameter for the MaxPool3d module. /// @@ -49,10 +56,16 @@ public class MaxPool3dModule public bool CeilMode { get; set; } = false; /// - /// Generates an observable sequence that creates a MaxPool3dModule module. + /// Generates an observable sequence that creates a LPPool1dModule module. /// public IObservable> Process() { - return Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); + return Dimensions switch + { + Dimensions.One => Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)), + Dimensions.Two => Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)), + Dimensions.Three => Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Mish.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Mish.cs index b7d3a8b5..1132590a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MishModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Mish.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Mish module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Mish module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MishModule +public class Mish { /// /// Generates an observable sequence that creates a MishModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs new file mode 100644 index 00000000..af2efe24 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs @@ -0,0 +1,82 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Module; + +/// +/// Creates a 1D adaptive average pooling layer. +/// +[Combinator] +[Description("Creates a 1D adaptive average pooling layer.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class Padding +{ + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + + /// + /// The padding mode for the ConstantPad module. + /// + [Description("The padding mode for the ConstantPad module")] + public PaddingMode Mode { get; set; } = PaddingMode.Constant; + + /// + /// The padding parameter for the ConstantPad1d module. + /// + [Description("The padding parameter for the ConstantPad1d module")] + public long PaddingSize { get; set; } + + /// + /// The value parameter for the ConstantPad1d module. + /// + [Description("The value parameter for the ConstantPad1d module")] + public double Value { get; set; } + + /// + /// If true, uses reflection padding instead of constant padding. + /// + + public bool Reflection { get; set; } = false; + + /// + /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. + /// + public IObservable> Process() + { + return Dimensions switch + { + Dimensions.One => Mode switch + { + PaddingMode.Constant => Observable.Return(ConstantPad1d(PaddingSize, Value)), + PaddingMode.Reflection => Observable.Return(ReflectionPad1d(PaddingSize)), + PaddingMode.Replication => Observable.Return(ReplicationPad1d(PaddingSize)), + _ => throw new InvalidOperationException("The specified padding mode is not supported."), + }, + Dimensions.Two => Mode switch + { + PaddingMode.Constant => Observable.Return(ConstantPad2d(PaddingSize, Value)), + PaddingMode.Reflection => Observable.Return(ReflectionPad2d(PaddingSize)), + PaddingMode.Replication => Observable.Return(ReplicationPad2d(PaddingSize)), + _ => throw new InvalidOperationException("The specified padding mode is not supported."), + }, + Dimensions.Three => Mode switch + { + PaddingMode.Constant => Observable.Return(ConstantPad3d(PaddingSize, Value)), + PaddingMode.Reflection => Observable.Return(ReflectionPad3d(PaddingSize)), + PaddingMode.Replication => Observable.Return(ReplicationPad3d(PaddingSize)), + _ => throw new InvalidOperationException("The specified padding mode is not supported."), + }, + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs new file mode 100644 index 00000000..d6f600a7 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs @@ -0,0 +1,22 @@ +namespace Bonsai.ML.Torch.NeuralNets.Module; + +/// +/// Specifies the padding mode for a neural network module. +/// +public enum PaddingMode +{ + /// + /// Constant padding. + /// + Constant, + + /// + /// Reflection padding. + /// + Reflection, + + /// + /// Replication padding. + /// + Replication +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs index 6f486350..bae385a3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/PReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a PReLU module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a PReLU module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PReLUModule +public class ParametricRectifiedLinearUnit { /// /// The num_parameters parameter for the PReLU module. @@ -34,7 +34,7 @@ public class PReLUModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/PixelShuffle.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/PixelShuffle.cs index b023d96c..453830b9 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelShuffleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/PixelShuffle.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a PixelShuffle module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a PixelShuffle module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PixelShuffleModule +public class PixelShuffle { /// /// The upscalefactor parameter for the PixelShuffle module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/PixelUnshuffle.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/PixelUnshuffle.cs index 151030fb..9a31cdbd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/PixelUnshuffleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/PixelUnshuffle.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a PixelUnshuffle module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a PixelUnshuffle module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class PixelUnshuffleModule +public class PixelUnshuffle { /// /// The downscalefactor parameter for the PixelUnshuffle module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/PowerAveragePooling.cs similarity index 67% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/PowerAveragePooling.cs index f19a93a8..39e05b2f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/PowerAveragePooling.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a LPPool1d module. @@ -16,8 +16,14 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a LPPool1d module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LPPool1dModule +public class PowerAveragePooling { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + /// /// The norm_type parameter for the LPPool1d module. /// @@ -47,6 +53,11 @@ public class LPPool1dModule /// public IObservable> Process() { - return Observable.Return(LPPool1d(NormType, KernelSize, Stride, CeilMode)); + return Dimensions switch + { + Dimensions.One => Observable.Return(LPPool1d(NormType, KernelSize, Stride, CeilMode)), + Dimensions.Two => Observable.Return(LPPool2d(NormType, KernelSize, Stride, CeilMode)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/RandomizedLeakyRectifiedLinearUnit.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/RandomizedLeakyRectifiedLinearUnit.cs index 85dec289..80e484ff 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/RReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/RandomizedLeakyRectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a RReLU module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a RReLU module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class RReLUModule +public class RandomizedLeakyRectifiedLinearUnit { /// /// The lower parameter for the RReLU module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/RectifiedLinearUnit.cs similarity index 56% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/RectifiedLinearUnit.cs index 1c69453c..c6b808b1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/RectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a ReLU activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a ReLU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ReLUModule +public class RectifiedLinearUnit { /// /// If set to true, will do this operation in-place. @@ -25,10 +25,19 @@ public class ReLUModule public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a ReLUModule module. + /// If set to true, will use the bounded version of ReLU with a maximum value of 6. + /// + [Description("If set to true, will use the bounded version of ReLU with a maximum value of 6")] + public bool Bounded { get; set; } = false; + + /// + /// Generates an observable sequence that creates a RectifiedLinearUnit module. /// public IObservable> Process() { - return Observable.Return(ReLU(Inplace)); + if (Bounded) + return Observable.Return(ReLU6(Inplace)); + else + return Observable.Return(ReLU(Inplace)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ScaledExponentialLinearUnit.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/ScaledExponentialLinearUnit.cs index 8446030b..e1d1d10c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SELUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ScaledExponentialLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a SELU activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a SELU activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SELUModule +public class ScaledExponentialLinearUnit { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Sigmoid.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Sigmoid.cs index 3dab5921..c6d6895a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SigmoidModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Sigmoid.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Sigmoid activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SigmoidWeightedLinearUnit.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SigmoidWeightedLinearUnit.cs index 0f816495..e14a1b09 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SiLUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SigmoidWeightedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a SiLU module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a SiLU module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SiLUModule +public class SigmoidWeightedLinearUnit { /// /// Generates an observable sequence that creates a SiLUModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax.cs index 9c577a60..23a75f04 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftmaxModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Softmax activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Softmax activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftmaxModule +public class SoftMax { /// /// The dim parameter for the Softmax module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax2D.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax2D.cs index aa9029a7..3c0cb859 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Softmax2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax2D.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Softmax2d module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Softmax2d module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class Softmax2dModule +public class SoftMax2D { /// /// Generates an observable sequence that creates a Softmax2dModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMin.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SoftMin.cs index 4d2bb4cd..658e7489 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftminModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMin.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Softmin module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Softmin module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftminModule +public class SoftMin { /// /// The dim parameter for the Softmin module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftPlus.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SoftPlus.cs index 9d59cbfc..e1cf1446 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftplusModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftPlus.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Softplus module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Softplus module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftplusModule +public class SoftPlus { /// /// The beta parameter for the Softplus module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftShrinkage.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SoftShrinkage.cs index 295649ba..fa9aaa9e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftshrinkModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftShrinkage.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Softshrink module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Softshrink module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftshrinkModule +public class SoftShrinkage { /// /// The lambda parameter for the Softshrink module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftSign.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/SoftSign.cs index fba24ebb..6502a7c1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SoftsignModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/SoftSign.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Softsign module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Softsign module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SoftsignModule +public class SoftSign { /// /// Generates an observable sequence that creates a SoftsignModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Tanh.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Tanh.cs index 97647a43..a7ed425f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Tanh.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Tanh activation function. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Tanh activation function.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TanhModule +public class Tanh { /// /// Generates an observable sequence that creates a TanhModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/TanhShrinkage.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/TanhShrinkage.cs index bea3e2dc..cbee7316 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TanhshrinkModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/TanhShrinkage.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Tanhshrink module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Tanhshrink module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TanhshrinkModule +public class TanhShrinkage { /// /// Generates an observable sequence that creates a TanhshrinkModule module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Threshold.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Threshold.cs index b63c9f64..f7079138 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ThresholdModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Threshold.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Threshold module. @@ -16,19 +16,19 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Threshold module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ThresholdModule +public class Threshold { /// /// The threshold parameter for the Threshold module. /// [Description("The threshold parameter for the Threshold module")] - public double Threshold { get; set; } + public double ThresholdValue { get; set; } /// /// The value parameter for the Threshold module. /// [Description("The value parameter for the Threshold module")] - public double Value { get; set; } + public double FillValue { get; set; } /// /// If set to true, will do this operation in-place. @@ -41,6 +41,6 @@ public class ThresholdModule /// public IObservable> Process() { - return Observable.Return(Threshold(Threshold, Value, Inplace)); + return Observable.Return(Threshold(ThresholdValue, FillValue, Inplace)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoder.cs similarity index 86% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoder.cs index 4a543f31..883c9744 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoder.cs @@ -4,11 +4,10 @@ using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a TransformerEncoder module. @@ -16,13 +15,14 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a TransformerEncoder module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TransformerEncoderModule +public class TransformerEncoder { /// /// The encoder_layer parameter for the TransformerEncoder module. /// [Description("The encoder_layer parameter for the TransformerEncoder module")] - public TransformerEncoderLayer EncoderLayer { get; set; } + [XmlIgnore] + public TorchSharp.Modules.TransformerEncoderLayer EncoderLayer { get; set; } /// /// The num_layers parameter for the TransformerEncoder module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoderLayer.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoderLayer.cs index 06f6fac2..cd578548 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/TransformerEncoderLayerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoderLayer.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a TransformerEncoderLayer module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a TransformerEncoderLayer module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TransformerEncoderLayerModule +public class TransformerEncoderLayer { /// /// The d_model parameter for the TransformerEncoderLayer module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Unflatten.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Unflatten.cs index ed618115..2451689b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnflattenModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Unflatten.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Unflatten module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Unflatten module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class UnflattenModule +public class Unflatten { /// /// The dim parameter for the Unflatten module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Unfold.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Unfold.cs index 0b2c2774..09aeac09 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/UnfoldModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Unfold.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Unfold module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Modules; [Combinator] [Description("Creates a Unfold module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class UnfoldModule +public class Unfold { /// /// The kernel_size parameter for the Unfold module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Upsample.cs similarity index 97% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/Upsample.cs index 1c5230ac..4a4c048a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/UpsampleModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Upsample.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a Upsample module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs index acb90c8d..b009fc03 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ZeroPad2dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Modules; +namespace Bonsai.ML.Torch.NeuralNets.Module; /// /// Creates a ZeroPad2d module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs deleted file mode 100644 index e1b2ad60..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool1dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 1D adaptive average pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveAvgPool1dModule -{ - /// - /// The outputsize parameter for the AdaptiveAvgPool1d module. - /// - [Description("The outputsize parameter for the AdaptiveAvgPool1d module")] - public long OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AdaptiveAvgPool1d(OutputSize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs deleted file mode 100644 index b9b4c665..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveAvgPool3dModule.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D adaptive average pooling layer. -/// -[Combinator] -[Description("Creates a 3D adaptive average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveAvgPool3dModule -{ - /// - /// The outputsize parameter for the AdaptiveAvgPool3d module. - /// - [Description("The outputsize parameter for the AdaptiveAvgPool3d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveAvgPool3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AdaptiveAvgPool3d(OutputSize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs deleted file mode 100644 index 2cdd9d09..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool1dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 1D adaptive max pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveMaxPool1dModule -{ - /// - /// The outputsize parameter for the AdaptiveMaxPool1d module. - /// - [Description("The outputsize parameter for the AdaptiveMaxPool1d module")] - public long OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveMaxPool1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AdaptiveMaxPool1d(OutputSize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs deleted file mode 100644 index 7a6d75f2..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool2dModule.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D adaptive max pooling layer. -/// -[Combinator] -[Description("Creates a 2D adaptive max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveMaxPool2dModule -{ - /// - /// The outputsize parameter for the AdaptiveMaxPool2d module. - /// - [Description("The outputsize parameter for the AdaptiveMaxPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveMaxPool2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AdaptiveMaxPool2d(OutputSize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs deleted file mode 100644 index 0d9a5dcf..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AdaptiveMaxPool3dModule.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D adaptive max pooling layer. -/// -[Combinator] -[Description("Creates a 3D adaptive max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveMaxPool3dModule -{ - /// - /// The outputsize parameter for the AdaptiveMaxPool3d module. - /// - [Description("The outputsize parameter for the AdaptiveMaxPool3d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveMaxPool3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AdaptiveMaxPool3d(OutputSize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs deleted file mode 100644 index 38f1b5fc..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool1dModule.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 1D average pooling layer. -/// -[Combinator] -[Description("Creates a 1D average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AvgPool1dModule -{ - /// - /// The kernel_size parameter for the AvgPool1d module. - /// - [Description("The kernel_size parameter for the AvgPool1d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the AvgPool1d module. - /// - [Description("The stride parameter for the AvgPool1d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the AvgPool1d module. - /// - [Description("The padding parameter for the AvgPool1d module")] - public long Padding { get; set; } = 0; - - /// - /// The ceil_mode parameter for the AvgPool1d module. - /// - [Description("The ceil_mode parameter for the AvgPool1d module")] - public bool CeilMode { get; set; } = false; - - /// - /// The count_include_pad parameter for the AvgPool1d module. - /// - [Description("The count_include_pad parameter for the AvgPool1d module")] - public bool CountIncludePad { get; set; } = true; - - /// - /// Generates an observable sequence that creates a AvgPool1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs deleted file mode 100644 index 7c37acf8..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool2dModule.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D average pooling layer. -/// -[Combinator] -[Description("Creates a 2D average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AvgPool2dModule -{ - /// - /// The kernel_size parameter for the AvgPool2d module. - /// - [Description("The kernel_size parameter for the AvgPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] KernelSize { get; set; } - - /// - /// The strides parameter for the AvgPool2d module. - /// - [Description("The strides parameter for the AvgPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] Strides { get; set; } = null; - - /// - /// The padding parameter for the AvgPool2d module. - /// - [Description("The padding parameter for the AvgPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] Padding { get; set; } = null; - - /// - /// The ceil_mode parameter for the AvgPool2d module. - /// - [Description("The ceil_mode parameter for the AvgPool2d module")] - public bool CeilMode { get; set; } = false; - - /// - /// The count_include_pad parameter for the AvgPool2d module. - /// - [Description("The count_include_pad parameter for the AvgPool2d module")] - public bool CountIncludePad { get; set; } = true; - - /// - /// The divisor_override parameter for the AvgPool2d module. - /// - [Description("The divisor_override parameter for the AvgPool2d module")] - public long? DivisorOverride { get; set; } = null; - - /// - /// Generates an observable sequence that creates a AvgPool2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AvgPool2d(KernelSize, Strides, Padding, CeilMode, CountIncludePad, DivisorOverride)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs deleted file mode 100644 index dab8d078..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/AvgPool3dModule.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D average pooling layer. -/// -[Combinator] -[Description("Creates a 3D average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AvgPool3dModule -{ - /// - /// The kernel_size parameter for the AvgPool3d module. - /// - [Description("The kernel_size parameter for the AvgPool3d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] KernelSize { get; set; } - - /// - /// The strides parameter for the AvgPool3d module. - /// - [Description("The strides parameter for the AvgPool3d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] Strides { get; set; } = null; - - /// - /// The padding parameter for the AvgPool3d module. - /// - [Description("The padding parameter for the AvgPool3d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] Padding { get; set; } = null; - - /// - /// The ceil_mode parameter for the AvgPool3d module. - /// - [Description("The ceil_mode parameter for the AvgPool3d module")] - public bool CeilMode { get; set; } = false; - - /// - /// The count_include_pad parameter for the AvgPool3d module. - /// - [Description("The count_include_pad parameter for the AvgPool3d module")] - public bool CountIncludePad { get; set; } = true; - - /// - /// The divisor_override parameter for the AvgPool3d module. - /// - [Description("The divisor_override parameter for the AvgPool3d module")] - public long? DivisorOverride { get; set; } = null; - - /// - /// Generates an observable sequence that creates a AvgPool3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(AvgPool3d(KernelSize, Strides, Padding, CeilMode, CountIncludePad, DivisorOverride)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs deleted file mode 100644 index 076df14d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm2dModule.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D batch normalization layer. -/// -[Combinator] -[Description("Creates a 2D batch normalization layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class BatchNorm2dModule -{ - /// - /// The features parameter for the BatchNorm2d module. - /// - [Description("The features parameter for the BatchNorm2d module")] - public long Features { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = true; - - /// - /// The track_running_stats parameter for the BatchNorm2d module. - /// - [Description("The track_running_stats parameter for the BatchNorm2d module")] - public bool TrackRunningStats { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a BatchNorm2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs deleted file mode 100644 index 21ff9a0d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/BatchNorm3dModule.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D batch normalization layer. -/// -[Combinator] -[Description("Creates a 3D batch normalization layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class BatchNorm3dModule -{ - /// - /// The features parameter for the BatchNorm3d module. - /// - [Description("The features parameter for the BatchNorm3d module")] - public long Features { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = true; - - /// - /// The track_running_stats parameter for the BatchNorm3d module. - /// - [Description("The track_running_stats parameter for the BatchNorm3d module")] - public bool TrackRunningStats { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a BatchNorm3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs deleted file mode 100644 index 8aa934c9..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad1dModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ConstantPad1d module. -/// -[Combinator] -[Description("Creates a ConstantPad1d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ConstantPad1dModule -{ - /// - /// The padding parameter for the ConstantPad1d module. - /// - [Description("The padding parameter for the ConstantPad1d module")] - public long Padding { get; set; } - - /// - /// The value parameter for the ConstantPad1d module. - /// - [Description("The value parameter for the ConstantPad1d module")] - public double Value { get; set; } - - /// - /// Generates an observable sequence that creates a ConstantPad1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ConstantPad1d(Padding, Value)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs deleted file mode 100644 index 47ff62c0..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad2dModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ConstantPad2d module. -/// -[Combinator] -[Description("Creates a ConstantPad2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ConstantPad2dModule -{ - /// - /// The padding parameter for the ConstantPad2d module. - /// - [Description("The padding parameter for the ConstantPad2d module")] - public long Padding { get; set; } - - /// - /// The value parameter for the ConstantPad2d module. - /// - [Description("The value parameter for the ConstantPad2d module")] - public double Value { get; set; } - - /// - /// Generates an observable sequence that creates a ConstantPad2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ConstantPad2d(Padding, Value)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs deleted file mode 100644 index 37b9b69f..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConstantPad3dModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ConstantPad3d module. -/// -[Combinator] -[Description("Creates a ConstantPad3d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ConstantPad3dModule -{ - /// - /// The padding parameter for the ConstantPad3d module. - /// - [Description("The padding parameter for the ConstantPad3d module")] - public long Padding { get; set; } - - /// - /// The value parameter for the ConstantPad3d module. - /// - [Description("The value parameter for the ConstantPad3d module")] - public double Value { get; set; } - - /// - /// Generates an observable sequence that creates a ConstantPad3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ConstantPad3d(Padding, Value)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs deleted file mode 100644 index a3c1025d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv2dModule.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D convolution layer. -/// -[Combinator] -[Description("Creates a 2D convolution layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Conv2dModule -{ - /// - /// The in_channels parameter for the Conv2d module. - /// - [Description("The in_channels parameter for the Conv2d module")] - public long InChannels { get; set; } - - /// - /// The out_channels parameter for the Conv2d module. - /// - [Description("The out_channels parameter for the Conv2d module")] - public long OutChannels { get; set; } - - /// - /// The kernelsize parameter for the Conv2d module. - /// - [Description("The kernelsize parameter for the Conv2d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the Conv2d module. - /// - [Description("The stride parameter for the Conv2d module")] - public long Stride { get; set; } = 1; - - /// - /// The padding parameter for the Conv2d module. - /// - [Description("The padding parameter for the Conv2d module")] - public long Padding { get; set; } = 0; - - /// - /// The dilation parameter for the Conv2d module. - /// - [Description("The dilation parameter for the Conv2d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the Conv2d module. - /// - [Description("The padding_mode parameter for the Conv2d module")] - public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; - - /// - /// The groups parameter for the Conv2d module. - /// - [Description("The groups parameter for the Conv2d module")] - public long Groups { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a Conv2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs deleted file mode 100644 index 68f6dd6d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Conv3dModule.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D convolution layer. -/// -[Combinator] -[Description("Creates a 3D convolution layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Conv3dModule -{ - /// - /// The in_channels parameter for the Conv3d module. - /// - [Description("The in_channels parameter for the Conv3d module")] - public long InChannels { get; set; } - - /// - /// The out_channels parameter for the Conv3d module. - /// - [Description("The out_channels parameter for the Conv3d module")] - public long OutChannels { get; set; } - - /// - /// The kernelsize parameter for the Conv3d module. - /// - [Description("The kernelsize parameter for the Conv3d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the Conv3d module. - /// - [Description("The stride parameter for the Conv3d module")] - public long Stride { get; set; } = 1; - - /// - /// The padding parameter for the Conv3d module. - /// - [Description("The padding parameter for the Conv3d module")] - public long Padding { get; set; } = 0; - - /// - /// The dilation parameter for the Conv3d module. - /// - [Description("The dilation parameter for the Conv3d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the Conv3d module. - /// - [Description("The padding_mode parameter for the Conv3d module")] - public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; - - /// - /// The groups parameter for the Conv3d module. - /// - [Description("The groups parameter for the Conv3d module")] - public long Groups { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a Conv3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs deleted file mode 100644 index 88037d99..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose1dModule.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 1D transposed convolution layer. -/// -[Combinator] -[Description("Creates a 1D transposed convolution layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ConvTranspose1dModule -{ - /// - /// The in_channels parameter for the ConvTranspose1d module. - /// - [Description("The in_channels parameter for the ConvTranspose1d module")] - public long InChannels { get; set; } - - /// - /// The out_channels parameter for the ConvTranspose1d module. - /// - [Description("The out_channels parameter for the ConvTranspose1d module")] - public long OutChannels { get; set; } - - /// - /// The kernelsize parameter for the ConvTranspose1d module. - /// - [Description("The kernelsize parameter for the ConvTranspose1d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the ConvTranspose1d module. - /// - [Description("The stride parameter for the ConvTranspose1d module")] - public long Stride { get; set; } = 1; - - /// - /// The padding parameter for the ConvTranspose1d module. - /// - [Description("The padding parameter for the ConvTranspose1d module")] - public long Padding { get; set; } = 0; - - /// - /// The output_padding parameter for the ConvTranspose1d module. - /// - [Description("The output_padding parameter for the ConvTranspose1d module")] - public long OutputPadding { get; set; } = 0; - - /// - /// The dilation parameter for the ConvTranspose1d module. - /// - [Description("The dilation parameter for the ConvTranspose1d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the ConvTranspose1d module. - /// - [Description("The padding_mode parameter for the ConvTranspose1d module")] - public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; - - /// - /// The groups parameter for the ConvTranspose1d module. - /// - [Description("The groups parameter for the ConvTranspose1d module")] - public long Groups { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a ConvTranspose1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs deleted file mode 100644 index b49ec7d8..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose2dModule.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D transposed convolution layer. -/// -[Combinator] -[Description("Creates a 2D transposed convolution layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ConvTranspose2dModule -{ - /// - /// The in_channels parameter for the ConvTranspose2d module. - /// - [Description("The in_channels parameter for the ConvTranspose2d module")] - public long InChannels { get; set; } - - /// - /// The out_channels parameter for the ConvTranspose2d module. - /// - [Description("The out_channels parameter for the ConvTranspose2d module")] - public long OutChannels { get; set; } - - /// - /// The kernelsize parameter for the ConvTranspose2d module. - /// - [Description("The kernelsize parameter for the ConvTranspose2d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the ConvTranspose2d module. - /// - [Description("The stride parameter for the ConvTranspose2d module")] - public long Stride { get; set; } = 1; - - /// - /// The padding parameter for the ConvTranspose2d module. - /// - [Description("The padding parameter for the ConvTranspose2d module")] - public long Padding { get; set; } = 0; - - /// - /// The output_padding parameter for the ConvTranspose2d module. - /// - [Description("The output_padding parameter for the ConvTranspose2d module")] - public long OutputPadding { get; set; } = 0; - - /// - /// The dilation parameter for the ConvTranspose2d module. - /// - [Description("The dilation parameter for the ConvTranspose2d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the ConvTranspose2d module. - /// - [Description("The padding_mode parameter for the ConvTranspose2d module")] - public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; - - /// - /// The groups parameter for the ConvTranspose2d module. - /// - [Description("The groups parameter for the ConvTranspose2d module")] - public long Groups { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a ConvTranspose2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs deleted file mode 100644 index d8785475..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ConvTranspose3dModule.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D transposed convolution layer. -/// -[Combinator] -[Description("Creates a 3D transposed convolution layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ConvTranspose3dModule -{ - /// - /// The in_channels parameter for the ConvTranspose3d module. - /// - [Description("The in_channels parameter for the ConvTranspose3d module")] - public long InChannels { get; set; } - - /// - /// The out_channels parameter for the ConvTranspose3d module. - /// - [Description("The out_channels parameter for the ConvTranspose3d module")] - public long OutChannels { get; set; } - - /// - /// The kernelsize parameter for the ConvTranspose3d module. - /// - [Description("The kernelsize parameter for the ConvTranspose3d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the ConvTranspose3d module. - /// - [Description("The stride parameter for the ConvTranspose3d module")] - public long Stride { get; set; } = 1; - - /// - /// The padding parameter for the ConvTranspose3d module. - /// - [Description("The padding parameter for the ConvTranspose3d module")] - public long Padding { get; set; } = 0; - - /// - /// The output_padding parameter for the ConvTranspose3d module. - /// - [Description("The output_padding parameter for the ConvTranspose3d module")] - public long OutputPadding { get; set; } = 0; - - /// - /// The dilation parameter for the ConvTranspose3d module. - /// - [Description("The dilation parameter for the ConvTranspose3d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the ConvTranspose3d module. - /// - [Description("The padding_mode parameter for the ConvTranspose3d module")] - public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; - - /// - /// The groups parameter for the ConvTranspose3d module. - /// - [Description("The groups parameter for the ConvTranspose3d module")] - public long Groups { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a ConvTranspose3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs deleted file mode 100644 index 207d9bd7..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout1dModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a Dropout1d module. -/// -[Combinator] -[Description("Creates a Dropout1d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Dropout1dModule -{ - /// - /// The p parameter for the Dropout1d module. - /// - [Description("The p parameter for the Dropout1d module")] - public double P { get; set; } = 0.5D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a Dropout1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(Dropout1d(P, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs deleted file mode 100644 index e7b7a90a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout2dModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D dropout regularization layer. -/// -[Combinator] -[Description("Creates a 2D dropout regularization layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Dropout2dModule -{ - /// - /// The p parameter for the Dropout2d module. - /// - [Description("The p parameter for the Dropout2d module")] - public double P { get; set; } = 0.5D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a Dropout2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(Dropout2d(P, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs deleted file mode 100644 index 45188054..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/Dropout3dModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D dropout regularization layer. -/// -[Combinator] -[Description("Creates a 3D dropout regularization layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Dropout3dModule -{ - /// - /// The p parameter for the Dropout3d module. - /// - [Description("The p parameter for the Dropout3d module")] - public double P { get; set; } = 0.5D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a Dropout3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(Dropout3d(P, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs deleted file mode 100644 index b25a87d3..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/DropoutModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a Dropout regularization layer. -/// -[Combinator] -[Description("Creates a Dropout regularization layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class DropoutModule -{ - /// - /// The p parameter for the Dropout module. - /// - [Description("The p parameter for the Dropout module")] - public double P { get; set; } = 0.5D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a DropoutModule module. - /// - public IObservable> Process() - { - return Observable.Return(Dropout(P, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs deleted file mode 100644 index d075aa8c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/FractionalMaxPool2dModule.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a FractionalMaxPool2d module. -/// -[Combinator] -[Description("Creates a FractionalMaxPool2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class FractionalMaxPool2dModule -{ - /// - /// The kernel_size parameter for the FractionalMaxPool2d module. - /// - [Description("The kernel_size parameter for the FractionalMaxPool2d module")] - public long KernelSize { get; set; } - - /// - /// The output_size parameter for the FractionalMaxPool2d module. - /// - [Description("The output_size parameter for the FractionalMaxPool2d module")] - public long? OutputSize { get; set; } = null; - - /// - /// The output_ratio parameter for the FractionalMaxPool2d module. - /// - [Description("The output_ratio parameter for the FractionalMaxPool2d module")] - public double? OutputRatio { get; set; } = null; - - /// - /// Generates an observable sequence that creates a FractionalMaxPool2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs deleted file mode 100644 index 4c0a4079..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm2dModule.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D instance normalization. -/// -[Combinator] -[Description("Creates a 2D instance normalization.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class InstanceNorm2dModule -{ - /// - /// The features parameter for the InstanceNorm2d module. - /// - [Description("The features parameter for the InstanceNorm2d module")] - public long Features { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = false; - - /// - /// The track_running_stats parameter for the InstanceNorm2d module. - /// - [Description("The track_running_stats parameter for the InstanceNorm2d module")] - public bool TrackRunningStats { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a InstanceNorm2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs deleted file mode 100644 index f0a9b572..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/InstanceNorm3dModule.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 3D instance normalization. -/// -[Combinator] -[Description("Creates a 3D instance normalization.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class InstanceNorm3dModule -{ - /// - /// The features parameter for the InstanceNorm3d module. - /// - [Description("The features parameter for the InstanceNorm3d module")] - public long Features { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = false; - - /// - /// The track_running_stats parameter for the InstanceNorm3d module. - /// - [Description("The track_running_stats parameter for the InstanceNorm3d module")] - public bool TrackRunningStats { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a InstanceNorm3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs deleted file mode 100644 index dc9a5a95..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/LPPool2dModule.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a LPPool2d module. -/// -[Combinator] -[Description("Creates a LPPool2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class LPPool2dModule -{ - /// - /// The norm_type parameter for the LPPool2d module. - /// - [Description("The norm_type parameter for the LPPool2d module")] - public double NormType { get; set; } - - /// - /// The kernel_size parameter for the LPPool2d module. - /// - [Description("The kernel_size parameter for the LPPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] KernelSize { get; set; } - - /// - /// The strides parameter for the LPPool2d module. - /// - [Description("The strides parameter for the LPPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] Strides { get; set; } = null; - - /// - /// The ceil_mode parameter for the LPPool2d module. - /// - [Description("The ceil_mode parameter for the LPPool2d module")] - public bool CeilMode { get; set; } = false; - - /// - /// Generates an observable sequence that creates a LPPool2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(LPPool2d(NormType, KernelSize, Strides, CeilMode)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs deleted file mode 100644 index 9ac69236..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool1dModule.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 1D max pooling layer. -/// -[Combinator] -[Description("Creates a 1D max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MaxPool1dModule -{ - /// - /// The kernelsize parameter for the MaxPool1d module. - /// - [Description("The kernelsize parameter for the MaxPool1d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the MaxPool1d module. - /// - [Description("The stride parameter for the MaxPool1d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the MaxPool1d module. - /// - [Description("The padding parameter for the MaxPool1d module")] - public long? Padding { get; set; } = null; - - /// - /// The dilation parameter for the MaxPool1d module. - /// - [Description("The dilation parameter for the MaxPool1d module")] - public long? Dilation { get; set; } = null; - - /// - /// The ceilmode parameter for the MaxPool1d module. - /// - [Description("The ceilmode parameter for the MaxPool1d module")] - public bool CeilMode { get; set; } = false; - - /// - /// Generates an observable sequence that creates a MaxPool1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs deleted file mode 100644 index 3347eb4d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/MaxPool2dModule.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a 2D max pooling layer. -/// -[Combinator] -[Description("Creates a 2D max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MaxPool2dModule -{ - /// - /// The kernelsize parameter for the MaxPool2d module. - /// - [Description("The kernelsize parameter for the MaxPool2d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the MaxPool2d module. - /// - [Description("The stride parameter for the MaxPool2d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the MaxPool2d module. - /// - [Description("The padding parameter for the MaxPool2d module")] - public long? Padding { get; set; } = null; - - /// - /// The dilation parameter for the MaxPool2d module. - /// - [Description("The dilation parameter for the MaxPool2d module")] - public long? Dilation { get; set; } = null; - - /// - /// The ceilmode parameter for the MaxPool2d module. - /// - [Description("The ceilmode parameter for the MaxPool2d module")] - public bool CeilMode { get; set; } = false; - - /// - /// Generates an observable sequence that creates a MaxPool2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs deleted file mode 100644 index 9b019fc0..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReLU6Module.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReLU6 module. -/// -[Combinator] -[Description("Creates a ReLU6 module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReLU6Module -{ - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a ReLU6Module module. - /// - public IObservable> Process() - { - return Observable.Return(ReLU6(Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs deleted file mode 100644 index 2b4be88b..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad1dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReflectionPad1d module. -/// -[Combinator] -[Description("Creates a ReflectionPad1d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReflectionPad1dModule -{ - /// - /// The padding parameter for the ReflectionPad1d module. - /// - [Description("The padding parameter for the ReflectionPad1d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ReflectionPad1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ReflectionPad1d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs deleted file mode 100644 index 9851b5d8..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad2dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReflectionPad2d module. -/// -[Combinator] -[Description("Creates a ReflectionPad2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReflectionPad2dModule -{ - /// - /// The padding parameter for the ReflectionPad2d module. - /// - [Description("The padding parameter for the ReflectionPad2d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ReflectionPad2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ReflectionPad2d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs deleted file mode 100644 index 09f98c7c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReflectionPad3dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReflectionPad3d module. -/// -[Combinator] -[Description("Creates a ReflectionPad3d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReflectionPad3dModule -{ - /// - /// The padding parameter for the ReflectionPad3d module. - /// - [Description("The padding parameter for the ReflectionPad3d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ReflectionPad3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ReflectionPad3d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs deleted file mode 100644 index 74396b14..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad1dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReplicationPad1d module. -/// -[Combinator] -[Description("Creates a ReplicationPad1d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReplicationPad1dModule -{ - /// - /// The padding parameter for the ReplicationPad1d module. - /// - [Description("The padding parameter for the ReplicationPad1d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ReplicationPad1dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ReplicationPad1d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs deleted file mode 100644 index f6495564..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad2dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReplicationPad2d module. -/// -[Combinator] -[Description("Creates a ReplicationPad2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReplicationPad2dModule -{ - /// - /// The padding parameter for the ReplicationPad2d module. - /// - [Description("The padding parameter for the ReplicationPad2d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ReplicationPad2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ReplicationPad2d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs deleted file mode 100644 index 4b20fb36..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/ReplicationPad3dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a ReplicationPad3d module. -/// -[Combinator] -[Description("Creates a ReplicationPad3d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ReplicationPad3dModule -{ - /// - /// The padding parameter for the ReplicationPad3d module. - /// - [Description("The padding parameter for the ReplicationPad3d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ReplicationPad3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ReplicationPad3d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs deleted file mode 100644 index 36aca372..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Modules/SequentialModule.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Modules; - -/// -/// Creates a Sequential module. -/// -[Combinator] -[Description("Creates a Sequential module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SequentialModule -{ - /// - /// Generates an observable sequence that creates a SequentialModule module. - /// - public IObservable> Process() - { - return Observable.Return(Sequential()); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs index b5a49ad3..2aa7fc8c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdadeltaOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a Adadelta optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a Adadelta optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdadeltaOptimizer +public class AdaDelta { /// /// The parameters parameter for the Adadelta module. @@ -58,7 +58,7 @@ public class AdadeltaOptimizer /// /// Generates an observable sequence that creates a AdadeltaOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(Adadelta(Parameters, Lr, Rho, Eps, WeightDecay, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs index eaefab47..0e657f1c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdagradOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a Adagrad optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a Adagrad optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdagradOptimizer +public class AdaGrad { /// /// The parameters parameter for the Adagrad module. @@ -58,7 +58,7 @@ public class AdagradOptimizer /// /// Generates an observable sequence that creates a AdagradOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(Adagrad(Parameters, Lr, LrDecay, WeightDecay, InitialAccumulatorValue, Eps)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs index 25ed75ac..8acc808d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamaxOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a Adamax optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a Adamax optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdamaxOptimizer +public class AdaMax { /// /// The parameters parameter for the Adamax module. @@ -58,7 +58,7 @@ public class AdamaxOptimizer /// /// Generates an observable sequence that creates a AdamaxOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(Adamax(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs index 941fd4ea..ea72e2d7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a Adam optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a Adam optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdamOptimizer +public class Adam { /// /// The parameters parameter for the Adam module. @@ -70,7 +70,7 @@ public class AdamOptimizer /// /// Generates an observable sequence that creates a AdamOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(Adam(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay, Amsgrad, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs index df9eab19..fc89f918 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/AdamWOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a AdamW optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a AdamW optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class AdamWOptimizer +public class AdamW { /// /// The parameters parameter for the AdamW module. @@ -70,7 +70,7 @@ public class AdamWOptimizer /// /// Generates an observable sequence that creates a AdamWOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(AdamW(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay, Amsgrad, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs index 9c3a2aa8..5504bbc1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/ASGDOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a ASGD optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a ASGD optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class ASGDOptimizer +public class Asgd { /// /// The parameters parameter for the ASGD module. @@ -64,7 +64,7 @@ public class ASGDOptimizer /// /// Generates an observable sequence that creates a ASGDOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(ASGD(Parameters, Lr, Lambd, Alpha, T0, WeightDecay, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs index c5aa5fbd..cf9ed6da 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/LBFGSOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a LBFGS optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a LBFGS optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LBFGSOptimizer +public class Lbfgs { /// /// The parameters parameter for the LBFGS module. @@ -64,7 +64,7 @@ public class LBFGSOptimizer /// /// Generates an observable sequence that creates a LBFGSOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(LBFGS(Parameters, Lr, MaxIter, MaxEval, TolerangeGrad, ToleranceChange, HistorySize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs index 39fd0ef6..eba14f66 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RpropOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a Rprop optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a Rprop optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class RpropOptimizer +public class ResilientBackprop { /// /// The parameters parameter for the Rprop module. @@ -64,7 +64,7 @@ public class RpropOptimizer /// /// Generates an observable sequence that creates a RpropOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(Rprop(Parameters, Lr, Etaminus, Etaplus, MinStep, MaxStep, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs index 81283738..4c5d4949 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/RMSPropOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a RMSProp optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a RMSProp optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class RMSPropOptimizer +public class RmsPropogation { /// /// The parameters parameter for the RMSProp module. @@ -70,7 +70,7 @@ public class RMSPropOptimizer /// /// Generates an observable sequence that creates a RMSPropOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(RMSProp(Parameters, Lr, Alpha, Eps, WeightDecay, Momentum, Centered, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizer.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs index 910ffb03..4832b4f4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizers/SGDOptimizer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; -namespace Bonsai.ML.Torch.NeuralNets.Optimizers; +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// /// Creates a SGD optimizer. @@ -17,7 +17,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizers; [Combinator] [Description("Creates a SGD optimizer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SGDOptimizer +public class Sgd { /// /// The parameters parameter for the SGD module. @@ -64,7 +64,7 @@ public class SGDOptimizer /// /// Generates an observable sequence that creates a SGDOptimizer. /// - public IObservable Process() + public IObservable Process() { return Observable.Return(SGD(Parameters, LearningRate, Momentum, Dampening, WeightDecay, Nesterov, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs similarity index 53% rename from src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs rename to src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs index bcf6db88..9c360ecf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs @@ -15,34 +15,46 @@ namespace Bonsai.ML.Torch.NeuralNets [ResetCombinator] [Description("Saves the model to a file.")] [WorkflowElementCategory(ElementCategory.Sink)] - public class SaveModel + public class SaveModule { /// - /// The model to save. + /// The module to save. /// - [Description("The model to save.")] + [Description("The module to save.")] [XmlIgnore] - public IModule Model { get; set; } + public IModule Module { get; set; } /// - /// The path to save the model. + /// The path to save the module. /// - [Description("The path to save the model.")] + [Description("The path to save the module.")] [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModelPath { get; set; } + public string ModulePath { get; set; } /// - /// Saves the model to the specified file path. + /// Saves the input module to the specified file path. + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + var module = input as nn.Module; + module?.save(ModulePath); + }); + } + + /// + /// Saves the module to the specified file path. /// /// /// /// public IObservable Process(IObservable source) { - var model = Model as Module; return source.Do(input => { - model.save(ModelPath); + var module = Module as nn.Module; + module?.save(ModulePath); }); } } From 20d4cea6d49c373f23c5ae5d7c158d630b322638 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 15 Oct 2025 11:16:19 +0100 Subject: [PATCH 05/30] Added XmlIgnore attribute to device property on modules --- .../NeuralNets/Module/AdaptiveAveragePooling.cs | 1 - src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs | 1 + src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs | 1 + src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs | 3 ++- .../NeuralNets/Module/EmbeddingFromPretrained.cs | 3 ++- src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs | 1 + src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs | 3 ++- src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs | 1 + src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs | 1 + .../NeuralNets/Module/ParametricRectifiedLinearUnit.cs | 1 + 10 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs index 8f1595d3..407b956e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs @@ -21,7 +21,6 @@ public class AdaptiveAveragePooling /// /// The number of dimensions for the AdaptiveAvgPool module. /// - public Dimensions Dimensions { get; set; } = Dimensions.One; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs index b4a79a38..7dd66111 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs @@ -57,6 +57,7 @@ public class BatchNormalization /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs index e84ad751..6b51005c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs @@ -93,6 +93,7 @@ public class Convolution /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs index f4e2202d..91e1d84c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs @@ -63,8 +63,9 @@ public class Embedding /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs index fc918b71..7a9f0404 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs @@ -63,8 +63,9 @@ public class EmbeddingFromPretrained /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs index 9276627d..beb6efc3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs @@ -45,6 +45,7 @@ public class GroupNormalization /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs index 4e417935..d810a185 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs @@ -57,8 +57,9 @@ public class InstanceNormalization /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs index b77d6fba..98574278 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs @@ -46,6 +46,7 @@ public class LayerNormalization /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs index 49c6e9f5..5b978e72 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs @@ -39,6 +39,7 @@ public class Linear /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs index bae385a3..572a4eba 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs @@ -33,6 +33,7 @@ public class ParametricRectifiedLinearUnit /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; From 96e561141af3084f6d4755d060ee5a245fee92c3 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Sat, 1 Nov 2025 12:40:25 +0000 Subject: [PATCH 06/30] Added operator to explicitly allow setting the modules training mode --- .../NeuralNets/SetTrainingMode.cs | 60 +++++++++++++++++++ .../NeuralNets/TrainingMode.cs | 17 ++++++ 2 files changed, 77 insertions(+) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs new file mode 100644 index 00000000..5d1e8495 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Sets the training mode for the model. +/// +[Combinator] +[ResetCombinator] +[Description("Sets the training mode for the model.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class SetTrainingMode +{ + /// + /// The model for which to set the training mode. + /// + [Description("The model for which to set the training mode.")] + [XmlIgnore] + public IModule Model { get; set; } + + /// + /// The training mode to set for the model. + /// + [Description("The training mode to set for the model.")] + public TrainingMode Mode { get; set; } = TrainingMode.Train; + + /// + /// Saves the model to the specified file path. + /// + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => + { + + var training = Mode == TrainingMode.Train; + + switch (Model) + { + case Module module: + module.train(training); + break; + case ScriptModule scriptModule: + scriptModule.train(training); + break; + default: + throw new InvalidOperationException("Unsupported model type for setting training mode."); + } + }); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs new file mode 100644 index 00000000..f5dbba0c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs @@ -0,0 +1,17 @@ +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Specifies the training mode for a torch module. +/// +public enum TrainingMode +{ + /// + /// Sets the model to training mode. + /// + Train, + + /// + /// Sets the model to evaluation mode. + /// + Evaluation +} From bc1e4bb33f56405f1b0037581b478336f9240e36 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Mon, 10 Nov 2025 15:06:12 +0000 Subject: [PATCH 07/30] Updated forward method to handle both `Module` and `ScriptModule` types --- src/Bonsai.ML.Torch/NeuralNets/Forward.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs index 88d89872..37e90989 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs @@ -2,6 +2,8 @@ using System.ComponentModel; using System.Reactive.Linq; using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets @@ -28,8 +30,15 @@ public class Forward /// public IObservable Process(IObservable source) { - Model.Module.eval(); - return source.Select(Model.Forward); + return source.Select(input => + { + return Model switch + { + Module module => module.forward(input), + ScriptModule scriptModule => scriptModule.forward(input), + _ => throw new InvalidOperationException("Unsupported model type for forward inference.") + }; + }); } } } \ No newline at end of file From 7457b0b1ff7de9caf77333ee44f7121dafd884b7 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 20 Nov 2025 13:55:55 -0800 Subject: [PATCH 08/30] Testing inference expression builder --- .../NeuralNets/ForwardInferenceBuilder.cs | 177 ++++++++++++++++++ .../NeuralNets/LoadModuleArchitecture.cs | 3 +- .../NeuralNets/LoadScriptModule.cs | 102 +++++++--- .../NeuralNets/Module/Convolution.cs | 2 +- .../NeuralNets/OtherModules/BilinearModule.cs | 2 +- .../NeuralNets/OtherModules/LSTMCellModule.cs | 2 +- .../NeuralNets/OtherModules/RNNCellModule.cs | 3 +- .../NeuralNets/OtherModules/RNNModule.cs | 6 +- src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs | 46 +++++ src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs | 85 +++++---- 10 files changed, 347 insertions(+), 81 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs new file mode 100644 index 00000000..61ceb73a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs @@ -0,0 +1,177 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Reflection; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; +using System.Xml.Serialization; +using Bonsai.Expressions; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.Linq; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Runs forward inference on the input tensor using the specified model. +/// +[Combinator] +[ResetCombinator] +[Description("Runs forward inference on the input tensor using the specified model.")] +[WorkflowElementCategory(ElementCategory.Combinator)] +public class ForwardInferenceBuilder : SingleArgumentExpressionBuilder +{ + /// + /// + /// + /// + /// + /// + public override Expression Build(IEnumerable arguments) + { + Console.WriteLine($"Arguments: {string.Join(", ", arguments.Select(a => a.Type.Name))}"); + + var firstArg = arguments.First(); + Console.WriteLine($"First argument: {firstArg}"); + + var secondArg = arguments.Last(); + Console.WriteLine($"Second argument: {secondArg}"); + + var inputType = arguments.First().Type; + + Console.WriteLine($"Input type: {inputType}"); + + var inputTypeName = inputType.Name; + Console.WriteLine($"Input type name: {inputTypeName}"); + + var inputTypeType = inputType.GetType(); + Console.WriteLine($"Input type type: {inputTypeType}"); + + var genericInputTypes = inputType.IsGenericType ? inputType.GetGenericArguments() : []; + + Console.WriteLine($"Generic input: {genericInputTypes}. Types: {string.Join(", ", genericInputTypes.Select(t => t.Name))}. Length: {genericInputTypes.Length}"); + + if (genericInputTypes.Length != 1) + throw new InvalidOperationException("The input must be a single generic argument."); + + var genericInputType = genericInputTypes[0]; + Console.WriteLine($"Generic input type: {genericInputType}"); + + // Get the generic type arguments of the tuple + var genericArguments = genericInputType.GetGenericArguments(); + Console.WriteLine($"Generic arguments: {string.Join(", ", genericArguments.Select(t => t.Name))}"); + + var sourceArguments = genericArguments.First().GetGenericArguments(); + + // The last argument is the model + var model = genericArguments.Last(); + Console.WriteLine($"Model type: {model.FullName}"); + Console.WriteLine($"Model type: {model}"); + + var genericModelArguments = model.GetGenericArguments(); + Console.WriteLine($"Generic model arguments: {string.Join(", ", genericModelArguments.Select(t => t.Name))}"); + + var modelOutputType = genericModelArguments.Last(); + Console.WriteLine($"Model output type: {modelOutputType.Name}"); + + var modelInputTypes = genericModelArguments.Take(genericModelArguments.Length - 1).ToArray(); + Console.WriteLine($"Model input types: {string.Join(", ", modelInputTypes.Select(t => t.Name))}"); + + // Ensure that the input argument types up to the model type match the model input types + for (int i = 0; i < modelInputTypes.Length; i++) + { + if (sourceArguments[i] != modelInputTypes[i]) + throw new InvalidOperationException($"The input argument type '{sourceArguments[i].Name}' does not match the model input type '{modelInputTypes[i].Name}'."); + } + + // Create a parameter expression for the model + // var modelExpression = Expression.Call(Expression.Parameter(model, "model"), + // Console.WriteLine($"Model expression: {modelExpression}"); + + Expression sourceParameter; + if (modelInputTypes.Length == 1) + { + var observableSource = typeof(IObservable<>).MakeGenericType(modelInputTypes[0]); + sourceParameter = Expression.Parameter(observableSource, "source"); + } + else + { + // Create a tuple expression for the input types + var tupleType = System.Type.GetType($"System.Tuple`{modelInputTypes.Length}").MakeGenericType(modelInputTypes); + var observableSource = typeof(IObservable<>).MakeGenericType(tupleType); + sourceParameter = Expression.Parameter(observableSource, "source"); + } + + if (sourceParameter is null) + throw new InvalidOperationException($"The source expression could not be created: {sourceParameter}."); + + Console.WriteLine($"Source expression: {sourceParameter}"); + + // var sourceExpression = Expression.Constant(sourceParameter, firstArg); + + // Get the appropriate Process method + MethodInfo processMethod = null; + if (model.IsSubclassOf(typeof(ScriptModule))) + processMethod = typeof(ForwardInferenceBuilder).GetMethods(BindingFlags.NonPublic | BindingFlags.Static) + .First(m => m.Name == "Process" && m.GetGenericArguments().Length == genericModelArguments.Length && m.GetParameters().Length == 2 && m.GetParameters()[1].ParameterType.IsSubclassOf(typeof(ScriptModule))) + .MakeGenericMethod(modelInputTypes.Concat([modelOutputType]).ToArray()); + else + processMethod = typeof(ForwardInferenceBuilder).GetMethods(BindingFlags.NonPublic | BindingFlags.Static) + .First(m => m.Name == "Process" && m.GetGenericArguments().Length == genericModelArguments.Length && m.GetParameters().Length == 2 && m.GetParameters()[1].ParameterType.IsSubclassOf(typeof(nn.Module))) + .MakeGenericMethod(modelInputTypes.Concat([modelOutputType]).ToArray()); + + Console.WriteLine($"Process method: {processMethod}"); + if (processMethod is null) + throw new InvalidOperationException($"No method overload for Process could be found that matches the input and output types: {string.Join(", ", modelInputTypes.Concat([modelOutputType]).Select(t => t.Name))}."); + + // var callExpression = Expression.Call(processMethod, sourceExpression, modelExpression); + return Expression.Call(processMethod, firstArg, secondArg); + } + + /// + private static IObservable Process(IObservable source, Module model) + { + return source.Select(model.forward); + } + + private static IObservable Process(IObservable> source, Module model) + { + return source.Select(input => model.forward(input.Item1, input.Item2)); + } + + private static IObservable Process(IObservable> source, Module model) + { + return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3)); + } + + private static IObservable Process(IObservable> source, Module model) + { + return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3, input.Item4)); + } + + private static IObservable Process(IObservable> source, Module model) + { + return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3, input.Item4, input.Item5)); + } + + private static IObservable Process(IObservable> source, Module model) + { + return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3, input.Item4, input.Item5, input.Item6)); + } + + private static IObservable Process(IObservable source, ScriptModule model) + { + return source.Select(input => model.forward()); + } + + private static IObservable Process(IObservable source, ScriptModule model) + { + return source.Select(model.forward); + } + + private static IObservable Process(IObservable> source, ScriptModule model) + { + return source.Select(input => model.forward(input.Item1, input.Item2)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs index 70550353..0025d016 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs @@ -2,9 +2,8 @@ using System.ComponentModel; using System.Reactive.Linq; using static TorchSharp.torch; -using System.Xml.Serialization; -using TorchSharp; using static TorchSharp.torch.nn; +using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets { diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs index baded318..48062c57 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs @@ -1,44 +1,88 @@ using System; using System.ComponentModel; using System.Reactive.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; using TorchSharp; using static TorchSharp.torch; using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; using System.Xml.Serialization; +using Bonsai.Expressions; -namespace Bonsai.ML.Torch.NeuralNets +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Loads a TorchScript module from the specified file path. +/// +[XmlInclude(typeof(TypeMapping))] +[XmlInclude(typeof(TypeMapping>))] +[XmlInclude(typeof(TypeMapping>))] +[Combinator] +[ResetCombinator] +[Description("Loads a TorchScript module from the specified file path.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LoadScriptModule: ExpressionBuilder { + /// + public override Range ArgumentRange => new(0, 1); + + /// + /// The device on which to load the model. + /// + [Description("The device on which to load the model.")] + [XmlIgnore] + public Device Device { get; set; } + /// - /// Loads a TorchScript module from the specified file path. + /// The path to the TorchScript model file. /// - [Combinator] - [ResetCombinator] - [Description("Loads a TorchScript module from the specified file path.")] - [WorkflowElementCategory(ElementCategory.Source)] - public class LoadScriptModule + [Description("The path to the TorchScript model file.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModelPath { get; set; } + + /// + /// The type mapping for the loaded script module. + /// + public TypeMapping ScriptModuleType { get; set; } = new TypeMapping>(); + + /// + public override Expression Build(IEnumerable arguments) { - /// - /// The device on which to load the model. - /// - [Description("The device on which to load the model.")] - [XmlIgnore] - public Device Device { get; set; } - - /// - /// The path to the TorchScript model file. - /// - [Description("The path to the TorchScript model file.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModelPath { get; set; } - - /// - /// Loads the TorchScript module from the specified file path. - /// - /// - public IObservable> Process() + var scriptModuleType = ScriptModuleType.GetType().GetGenericArguments()[0]; + var outputType = typeof(IObservable<>).MakeGenericType(scriptModuleType); + var modelPathExpression = Expression.Constant(ModelPath, typeof(string)); + var deviceExpression = Expression.Constant(Device, typeof(Device)); + + var callExpression = scriptModuleType switch { - var scriptModule = Device is null ? jit.load(ModelPath) : jit.load(ModelPath, Device); - return Observable.Return(scriptModule); - } + System.Type t when t == typeof(ScriptModule) => + Expression.Call(typeof(LoadScriptModule).GetMethod(nameof(ProcessScriptModule), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static), modelPathExpression, deviceExpression), + System.Type t when t == typeof(ScriptModule) => + Expression.Call(typeof(LoadScriptModule).GetMethod(nameof(ProcessScriptModuleTensor), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static), modelPathExpression, deviceExpression), + System.Type t when t == typeof(ScriptModule) => + Expression.Call(typeof(LoadScriptModule).GetMethod(nameof(ProcessScriptModuleTensorTensor), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static), modelPathExpression, deviceExpression), + _ => throw new NotSupportedException($"The specified script module type '{scriptModuleType}' is not supported."), + }; + + return Expression.Call(callExpression.Method, callExpression.Arguments); + } + + private static IObservable ProcessScriptModule(string modelPath, Device device) + { + var scriptModule = device is null ? jit.load(modelPath) : load(modelPath, device); + return Observable.Return(scriptModule); + } + + private static IObservable> ProcessScriptModuleTensor(string modelPath, Device device) + { + var scriptModule = device is null ? load(modelPath) : load(modelPath, device); + return Observable.Return(scriptModule); + } + + private static IObservable> ProcessScriptModuleTensorTensor(string modelPath, Device device) + { + var scriptModule = device is null ? load(modelPath) : load(modelPath, device); + return Observable.Return(scriptModule); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs index 6b51005c..d4c89d46 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs @@ -107,7 +107,7 @@ public class Convolution /// /// Generates an observable sequence that creates a Conv1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs index 894a3432..a381a6b7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs @@ -58,7 +58,7 @@ public class BilinearModule /// /// Generates an observable sequence that creates a BilinearModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Bilinear(In1Features, In2Features, OutputSize, HasBias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs index 8cf160e8..41f14ed1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs @@ -52,7 +52,7 @@ public class LSTMCellModule /// /// Generates an observable sequence that creates a LSTMCellModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs index b635465e..0fa1c0eb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs @@ -45,6 +45,7 @@ public class RNNCellModule /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public torch.Device Device { get; set; } = null; @@ -58,7 +59,7 @@ public class RNNCellModule /// /// Generates an observable sequence that creates a RNNCellModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs index caff68ae..3f703357 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs @@ -69,20 +69,20 @@ public class RNNModule /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// /// Generates an observable sequence that creates a RNNModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs new file mode 100644 index 00000000..ae801d79 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Saves the model to a file. +/// +[Combinator] +[ResetCombinator] +[Description("Saves the model to a file.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class SaveModel +{ + /// + /// The model to save. + /// + [Description("The model to save.")] + [XmlIgnore] + public nn.Module Model { get; set; } + + /// + /// The path to save the model. + /// + [Description("The path to save the model.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModelPath { get; set; } + + /// + /// Saves the model to the specified file path. + /// + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => + { + Model.save(ModelPath); + }); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs index 9c360ecf..31a7f921 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs @@ -6,56 +6,55 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Saves the model to a file. +/// +[Combinator] +[ResetCombinator] +[Description("Saves the model to a file.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class SaveModule { /// - /// Saves the model to a file. + /// The module to save. /// - [Combinator] - [ResetCombinator] - [Description("Saves the model to a file.")] - [WorkflowElementCategory(ElementCategory.Sink)] - public class SaveModule - { - /// - /// The module to save. - /// - [Description("The module to save.")] - [XmlIgnore] - public IModule Module { get; set; } + [Description("The module to save.")] + [XmlIgnore] + public IModule Module { get; set; } - /// - /// The path to save the module. - /// - [Description("The path to save the module.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModulePath { get; set; } + /// + /// The path to save the module. + /// + [Description("The path to save the module.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModulePath { get; set; } - /// - /// Saves the input module to the specified file path. - /// - public IObservable> Process(IObservable> source) + /// + /// Saves the input module to the specified file path. + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => { - return source.Do(input => - { - var module = input as nn.Module; - module?.save(ModulePath); - }); - } + var module = input as nn.Module; + module?.save(ModulePath); + }); + } - /// - /// Saves the module to the specified file path. - /// - /// - /// - /// - public IObservable Process(IObservable source) + /// + /// Saves the module to the specified file path. + /// + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => { - return source.Do(input => - { - var module = Module as nn.Module; - module?.save(ModulePath); - }); - } + var module = Module as nn.Module; + module?.save(ModulePath); + }); } } \ No newline at end of file From a7d3a6265f2c8b829df5a6700d36e28019ff3df4 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 3 Dec 2025 12:48:19 +0000 Subject: [PATCH 09/30] Organized modules into categories that better describe the module's functionality --- .../NeuralNets/Architecture/AlexNet.cs | 12 +- .../NeuralNets/Architecture/Mnist.cs | 10 +- .../NeuralNets/Architecture/MobileNet.cs | 4 +- src/Bonsai.ML.Torch/NeuralNets/Backward.cs | 77 +------- .../Sequential.cs} | 18 +- .../{Module => Convolution}/Convolution.cs | 2 +- .../{Module => Convolution}/Fold.cs | 4 +- .../{Module => Convolution}/Unfold.cs | 4 +- .../NeuralNets/{Module => }/Dimensions.cs | 2 +- .../CosineSimilarityModule.cs | 4 +- .../PairwiseDistanceModule.cs | 4 +- .../{Module => Dropout}/AlphaDropout.cs | 4 +- .../NeuralNets/{Module => Dropout}/Dropout.cs | 6 +- .../FeatureAlphaDropout.cs | 4 +- .../NeuralNets/{Module => Flatten}/Flatten.cs | 6 +- .../{Module => Flatten}/Unflatten.cs | 4 +- src/Bonsai.ML.Torch/NeuralNets/Forward.cs | 165 +++++++++++++--- .../NeuralNets/ForwardInferenceBuilder.cs | 177 ------------------ .../BilinearModule.cs | 4 +- .../NeuralNets/{Module => Linear}/Identity.cs | 6 +- .../NeuralNets/{Module => Linear}/Linear.cs | 6 +- src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs | 63 +++---- .../NeuralNets/Loss/BinaryCrossEntropy.cs | 2 + .../Loss/BinaryCrossEntropyWithLogits.cs | 11 +- .../NeuralNets/Loss/MeanSquaredError.cs | 2 +- .../NeuralNets/Module/ZeroPad2dModule.cs | 34 ---- ...uslyDifferentiableExponentialLinearUnit.cs | 4 +- .../ExponentialLinearUnit.cs | 4 +- .../GatedLinearUnit.cs | 4 +- .../GaussianErrorLinearUnit.cs | 4 +- .../HardShrinkage.cs | 4 +- .../HardSigmoid.cs | 4 +- .../HardSwish.cs | 4 +- .../HardTanh.cs | 4 +- .../LeakyRectifiedLinearUnit.cs | 4 +- .../LogSigmoid.cs | 4 +- .../LogSoftmax.cs | 4 +- .../{Module => NonLinearActivations}/Mish.cs | 4 +- .../MultiheadAttentionModule.cs | 4 +- .../ParametricRectifiedLinearUnit.cs | 4 +- .../RandomizedLeakyRectifiedLinearUnit.cs | 4 +- .../RectifiedLinearUnit.cs | 4 +- .../ScaledExponentialLinearUnit.cs | 4 +- .../Sigmoid.cs | 4 +- .../SigmoidWeightedLinearUnit.cs | 4 +- .../SoftMax.cs | 4 +- .../SoftMax2D.cs | 4 +- .../SoftMin.cs | 4 +- .../SoftPlus.cs | 4 +- .../SoftShrinkage.cs | 4 +- .../SoftSign.cs | 4 +- .../{Module => NonLinearActivations}/Tanh.cs | 4 +- .../TanhShrinkage.cs | 4 +- .../Threshold.cs | 4 +- .../BatchNormalization.cs | 4 +- .../GroupNormalization.cs | 4 +- .../InstanceNormalization.cs | 4 +- .../LayerNormalization.cs | 4 +- .../LocalResponseNormalization.cs | 4 +- .../NeuralNets/OptimizationStep.cs | 34 ++++ .../NeuralNets/Optimizer/AdaDelta.cs | 1 + .../NeuralNets/Optimizer/AdaGrad.cs | 1 + .../NeuralNets/Optimizer/AdaMax.cs | 1 + .../NeuralNets/Optimizer/Adam.cs | 1 + .../NeuralNets/Optimizer/AdamW.cs | 1 + .../NeuralNets/Optimizer/Asgd.cs | 1 + .../NeuralNets/Optimizer/Lbfgs.cs | 3 +- .../NeuralNets/Optimizer/ResilientBackprop.cs | 1 + .../NeuralNets/Optimizer/RmsPropogation.cs | 1 + .../NeuralNets/Optimizer/Sgd.cs | 1 + .../OtherModules/MaxUnpool2dModule.cs | 46 ----- .../OtherModules/MaxUnpool3dModule.cs | 46 ----- .../NeuralNets/{Module => Padding}/Padding.cs | 5 +- .../{Module => Padding}/PaddingMode.cs | 9 +- .../AdaptiveAveragePooling.cs | 4 +- .../{Module => Pooling}/AdaptiveMaxPooling.cs | 4 +- .../{Module => Pooling}/AveragePooling.cs | 4 +- .../FractionalMaxPooling.cs | 4 +- .../{Module => Pooling}/MaxPooling.cs | 4 +- .../MaxUnpoolModule.cs} | 26 ++- .../PowerAveragePooling.cs | 4 +- .../GRUCellModule.cs | 6 +- .../{OtherModules => Recurrent}/GRUModule.cs | 6 +- .../LSTMCellModule.cs | 4 +- .../{OtherModules => Recurrent}/LSTMModule.cs | 6 +- .../RNNCellModule.cs | 4 +- .../{OtherModules => Recurrent}/RNNModule.cs | 2 +- src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs | 46 ----- src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs | 27 +-- .../NeuralNets/SetTrainingMode.cs | 36 +--- .../{Module => Shuffle}/ChannelShuffle.cs | 4 +- .../{Module => Sparse}/Embedding.cs | 4 +- .../EmbeddingBagModule.cs | 6 +- .../EmbeddingFromPretrained.cs | 12 +- .../EmbeddingbagFromPretrainedModule.cs | 14 +- .../NeuralNets/TrainingMode.cs | 8 +- .../TransformerDecoderLayerModule.cs | 4 +- .../TransformerDecoderModule.cs | 10 +- .../TransformerEncoderLayerModule.cs} | 6 +- .../TransformerEncoderModule.cs} | 9 +- .../TransformerModule.cs | 6 +- .../{Module => Vision}/PixelShuffle.cs | 4 +- .../{Module => Vision}/PixelUnshuffle.cs | 4 +- .../NeuralNets/{Module => Vision}/Upsample.cs | 4 +- .../NeuralNets/ZeroGradient.cs | 34 ++++ 105 files changed, 490 insertions(+), 745 deletions(-) rename src/Bonsai.ML.Torch/NeuralNets/{CreateSequential.cs => Container/Sequential.cs} (75%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Convolution}/Convolution.cs (98%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Convolution}/Fold.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Convolution}/Unfold.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => }/Dimensions.cs (90%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Distance}/CosineSimilarityModule.cs (90%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Distance}/PairwiseDistanceModule.cs (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Dropout}/AlphaDropout.cs (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Dropout}/Dropout.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Dropout}/FeatureAlphaDropout.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Flatten}/Flatten.cs (85%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Flatten}/Unflatten.cs (91%) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Linear}/BilinearModule.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Linear}/Identity.cs (79%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Linear}/Linear.cs (89%) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/ContinuouslyDifferentiableExponentialLinearUnit.cs (90%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/ExponentialLinearUnit.cs (90%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/GatedLinearUnit.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/GaussianErrorLinearUnit.cs (88%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/HardShrinkage.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/HardSigmoid.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/HardSwish.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/HardTanh.cs (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/LeakyRectifiedLinearUnit.cs (90%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/LogSigmoid.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/LogSoftmax.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/Mish.cs (83%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => NonLinearActivations}/MultiheadAttentionModule.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/ParametricRectifiedLinearUnit.cs (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/RandomizedLeakyRectifiedLinearUnit.cs (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/RectifiedLinearUnit.cs (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/ScaledExponentialLinearUnit.cs (88%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/Sigmoid.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SigmoidWeightedLinearUnit.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SoftMax.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SoftMax2D.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SoftMin.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SoftPlus.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SoftShrinkage.cs (87%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/SoftSign.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/Tanh.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/TanhShrinkage.cs (84%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => NonLinearActivations}/Threshold.cs (91%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Normalization}/BatchNormalization.cs (96%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Normalization}/GroupNormalization.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Normalization}/InstanceNormalization.cs (96%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Normalization}/LayerNormalization.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Normalization}/LocalResponseNormalization.cs (93%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs rename src/Bonsai.ML.Torch/NeuralNets/{Module => Padding}/Padding.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Padding}/PaddingMode.cs (72%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Pooling}/AdaptiveAveragePooling.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Pooling}/AdaptiveMaxPooling.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Pooling}/AveragePooling.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Pooling}/FractionalMaxPooling.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Pooling}/MaxPooling.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules/MaxUnpool1dModule.cs => Pooling/MaxUnpoolModule.cs} (50%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Pooling}/PowerAveragePooling.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Recurrent}/GRUCellModule.cs (90%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Recurrent}/GRUModule.cs (93%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Recurrent}/LSTMCellModule.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Recurrent}/LSTMModule.cs (92%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Recurrent}/RNNCellModule.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Recurrent}/RNNModule.cs (98%) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs rename src/Bonsai.ML.Torch/NeuralNets/{Module => Shuffle}/ChannelShuffle.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Sparse}/Embedding.cs (96%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Sparse}/EmbeddingBagModule.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Sparse}/EmbeddingFromPretrained.cs (88%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Sparse}/EmbeddingbagFromPretrainedModule.cs (88%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Transformer}/TransformerDecoderLayerModule.cs (92%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Transformer}/TransformerDecoderModule.cs (77%) rename src/Bonsai.ML.Torch/NeuralNets/{Module/TransformerEncoderLayer.cs => Transformer/TransformerEncoderLayerModule.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{Module/TransformerEncoder.cs => Transformer/TransformerEncoderModule.cs} (81%) rename src/Bonsai.ML.Torch/NeuralNets/{OtherModules => Transformer}/TransformerModule.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Vision}/PixelShuffle.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Vision}/PixelUnshuffle.cs (89%) rename src/Bonsai.ML.Torch/NeuralNets/{Module => Vision}/Upsample.cs (94%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs index 189c06d4..07047937 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/AlexNet.cs @@ -39,14 +39,14 @@ public AlexNet(string name, int numClasses, Device device = null) : base(name) avgPool = AdaptiveAvgPool2d([ 2, 2 ]); classifier = Sequential( - ("d1", Dropout()), - ("l1", Linear(256 * 2 * 2, 4096)), + ("d1", nn.Dropout()), + ("l1", nn.Linear(256 * 2 * 2, 4096)), ("r1", ReLU(inplace: true)), - ("d2", Dropout()), - ("l2", Linear(4096, 4096)), + ("d2", nn.Dropout()), + ("l2", nn.Linear(4096, 4096)), ("r3", ReLU(inplace: true)), - ("d3", Dropout()), - ("l3", Linear(4096, numClasses)) + ("d3", nn.Dropout()), + ("l3", nn.Linear(4096, numClasses)) ); RegisterComponents(); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs index 9bafaa2e..aca74443 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/Mnist.cs @@ -35,8 +35,8 @@ public Mnist(string name, int numClasses, Device device = null) : base(name) { conv1 = Conv2d(1, 32, 3); conv2 = Conv2d(32, 64, 3); - fc1 = Linear(9216, 128); - fc2 = Linear(128, numClasses); + fc1 = nn.Linear(9216, 128); + fc2 = nn.Linear(128, numClasses); pool1 = MaxPool2d(kernel_size: [2, 2]); @@ -44,10 +44,10 @@ public Mnist(string name, int numClasses, Device device = null) : base(name) relu2 = ReLU(); relu3 = ReLU(); - dropout1 = Dropout(0.25); - dropout2 = Dropout(0.5); + dropout1 = nn.Dropout(0.25); + dropout2 = nn.Dropout(0.5); - flatten = Flatten(); + flatten = nn.Flatten(); logsm = LogSoftmax(1); RegisterComponents(); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs b/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs index 16a77454..60a67d49 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Architecture/MobileNet.cs @@ -35,8 +35,8 @@ public MobileNet(string name, int numClasses, Device device = null) : base(name) }; MakeLayers(modules, 32); modules.Add(("avgpool", AvgPool2d([2, 2]))); - modules.Add(("flatten", Flatten())); - modules.Add(($"linear", Linear(planes[planes.Length-1], numClasses))); + modules.Add(("flatten", nn.Flatten())); + modules.Add(($"linear", nn.Linear(planes[planes.Length-1], numClasses))); layers = Sequential(modules); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs index d288d38c..342b6dc6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs @@ -14,86 +14,21 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Trains a model using backpropagation. +/// Represents an operator that computes backward on the input tensor. /// [Combinator] [ResetCombinator] -[Description("Trains a model using backpropagation.")] -[WorkflowElementCategory(ElementCategory.Transform)] +[Description("Computes backward on the input tensor.")] +[WorkflowElementCategory(ElementCategory.Sink)] public class Backward { /// - /// The optimizer to use for training. - /// - [XmlIgnore] - public optim.Optimizer Optimizer { get; set; } = null; - - /// - /// The module to train. - /// - [XmlIgnore] - public IModule Module { get; set; } - - /// - /// The criterion (loss function) to use for training. - /// - [XmlIgnore] - public IModule Criterion { get; set; } = null; - - /// - /// The learning rate scheduler to use for training. - /// - [XmlIgnore] - public LRScheduler LRScheduler { get; set; } = null; - - /// - /// The number of epochs to train the model. - /// - public int Epochs { get; set; } = 1; - - /// - /// Trains the model using backpropagation. + /// Computes backward on the input tensor. /// /// /// - public IObservable> Process(IObservable> source) + public IObservable Process(IObservable source) { - return source.Select(input => Observable.Create>((observer, cancellationToken) => - { - return Task.Run(() => - { - var module = Module as Module; - - Criterion ??= NLLLoss(); - var criterion = Criterion as Module; - - Optimizer ??= SGD(module.parameters(), 0.01); - if (!module.training) - module.train(); - - var (data, target) = input; - - for (int epoch = 0; epoch < Epochs; epoch++) - { - if (cancellationToken.IsCancellationRequested) break; - using (_ = NewDisposeScope()) - { - Optimizer.zero_grad(); - - var prediction = module.forward(data); - var loss = criterion.forward(prediction, target); - - loss.backward(); - - Optimizer.step(); - - observer.OnNext(Tuple.Create(prediction, loss)); - } - LRScheduler?.step(); - } - observer.OnCompleted(); - return Disposable.Empty; - }, cancellationToken); - })).Concat(); + return source.Do(input => input.backward()); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs similarity index 75% rename from src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs rename to src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs index 01d4bc6b..3b5f498e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/CreateSequential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs @@ -8,7 +8,7 @@ using System.Xml.Serialization; using System.Linq; -namespace Bonsai.ML.Torch.NeuralNets; +namespace Bonsai.ML.Torch.NeuralNets.Container; /// /// Creates a sequential model from the specified modules. @@ -17,13 +17,13 @@ namespace Bonsai.ML.Torch.NeuralNets; [ResetCombinator] [Description("Creates a sequential model from the specified modules.")] [WorkflowElementCategory(ElementCategory.Source)] -public class CreateSequential +public class Sequential { /// /// The modules to include in the sequential model. /// [XmlIgnore] - public IModule[] Modules { get; set; } + public Module[] Modules { get; set; } /// /// The device on which to create the sequential model. @@ -35,11 +35,11 @@ public class CreateSequential /// Generates an observable sequence that creates a sequential model from the specified modules. /// /// - public IObservable> Process() + public IObservable> Process() { return Observable.Defer(() => { - var sequential = Sequential([.. Modules.Select(m => (Module)m)]); + var sequential = Sequential(Modules); if (Device is not null && Device != CPU) { sequential.to(Device); @@ -52,11 +52,11 @@ public IObservable> Process() /// Generates an observable sequence that creates a sequential model from the input modules. /// /// - public IObservable> Process(IObservable[]> source) + public IObservable> Process(IObservable[]> source) { return source.SelectMany(modules => { - var sequential = Sequential([.. modules.Select(m => (Module)m)]); + var sequential = Sequential(modules); if (Device is not null && Device != CPU) { sequential.to(Device); @@ -69,11 +69,11 @@ public IObservable> Process(IObservable /// - public IObservable> Process(IObservable source) + public IObservable> Process(IObservable source) { return source.SelectMany(_ => { - var sequential = Sequential([..Modules.Select(m => (Module)m)]); + var sequential = Sequential(Modules); if (Device is not null && Device != CPU) { sequential.to(Device); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs similarity index 98% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs rename to src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs index d4c89d46..09147a35 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Convolution.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Creates a 1D convolution layer. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Fold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Fold.cs rename to src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs index c5efc375..cdb2a002 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Fold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Creates a Fold module. @@ -51,7 +51,7 @@ public class FoldModule /// /// Generates an observable sequence that creates a FoldModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Unfold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Unfold.cs rename to src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs index 09aeac09..bf85c945 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Unfold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Creates a Unfold module. @@ -45,7 +45,7 @@ public class Unfold /// /// Generates an observable sequence that creates a UnfoldModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Unfold(KernelSize, Dilation, Padding, Stride)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs b/src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs rename to src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs index fb1d9ab3..d767e690 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Dimensions.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs @@ -1,4 +1,4 @@ -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets; /// /// Specifies the number of dimensions for a neural network module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs index 7f545536..11db730b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/CosineSimilarityModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Distance; /// /// Creates a CosineSimilarity module. @@ -33,7 +33,7 @@ public class CosineSimilarityModule /// /// Generates an observable sequence that creates a CosineSimilarityModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(CosineSimilarity(Dim, Eps)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs index 51d2f94f..9ca3368c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/PairwiseDistanceModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Distance; /// /// Creates a PairwiseDistance module. @@ -39,7 +39,7 @@ public class PairwiseDistanceModule /// /// Generates an observable sequence that creates a PairwiseDistanceModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(PairwiseDistance(P, Eps, KeepDim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AlphaDropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Module/AlphaDropout.cs rename to src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs index 4819d72d..266b4185 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/AlphaDropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Dropout; /// /// Creates a AlphaDropout module. @@ -33,7 +33,7 @@ public class AlphaDropout /// /// Generates an observable sequence that creates a AlphaDropoutModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(AlphaDropout(P, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs rename to src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs index 68eea2a7..e2711e22 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Dropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Dropout; /// /// Creates a dropout layer. @@ -39,11 +39,11 @@ public class Dropout /// /// Generates an observable sequence that creates a Dropout module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { - Dimensions.None => Observable.Return(Dropout(P, Inplace)), + Dimensions.None => Observable.Return(nn.Dropout(P, Inplace)), Dimensions.One => Observable.Return(Dropout1d(P, Inplace)), Dimensions.Two => Observable.Return(Dropout2d(P, Inplace)), Dimensions.Three => Observable.Return(Dropout3d(P, Inplace)), diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/FeatureAlphaDropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/FeatureAlphaDropout.cs rename to src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs index a95fc8ea..7e9af75a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/FeatureAlphaDropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Dropout; /// /// Creates a FeatureAlphaDropout module. @@ -27,7 +27,7 @@ public class FeatureAlphaDropoutModule /// /// Generates an observable sequence that creates a FeatureAlphaDropoutModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(FeatureAlphaDropout(P)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Flatten.cs b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs similarity index 85% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Flatten.cs rename to src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs index 566a65ae..0617952c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Flatten.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Flatten; /// /// Creates a Flatten module. @@ -33,8 +33,8 @@ public class Flatten /// /// Generates an observable sequence that creates a FlattenModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(Flatten(StartDim, EndDim)); + return Observable.Return(nn.Flatten(StartDim, EndDim)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Unflatten.cs b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Unflatten.cs rename to src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs index 2451689b..bfe6c78a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Unflatten.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Flatten; /// /// Creates a Unflatten module. @@ -34,7 +34,7 @@ public class Unflatten /// /// Generates an observable sequence that creates a UnflattenModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Unflatten(Dim, UnflattenedSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs index 37e90989..9ac1df44 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs @@ -1,44 +1,149 @@ using System; using System.ComponentModel; using System.Reactive.Linq; +using System.Reflection; using static TorchSharp.torch; using static TorchSharp.torch.nn; using static TorchSharp.torch.jit; using System.Xml.Serialization; +using Bonsai.Expressions; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.Linq; +using TorchSharp; -namespace Bonsai.ML.Torch.NeuralNets +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that runs forward inference on the input tensor using the specified model. +/// +[Combinator] +[ResetCombinator] +[Description("Runs forward inference on the input tensor using the specified model.")] +[WorkflowElementCategory(ElementCategory.Transform)] +public class Forward { /// /// Runs forward inference on the input tensor using the specified model. /// - [Combinator] - [ResetCombinator] - [Description("Runs forward inference on the input tensor using the specified model.")] - [WorkflowElementCategory(ElementCategory.Transform)] - public class Forward - { - /// - /// The model to use for inference. - /// - [XmlIgnore] - public IModule Model { get; set; } - - /// - /// Runs forward inference on the input tensor using the specified model. - /// - /// - /// - public IObservable Process(IObservable source) - { - return source.Select(input => - { - return Model switch - { - Module module => module.forward(input), - ScriptModule scriptModule => scriptModule.forward(input), - _ => throw new InvalidOperationException("Unsupported model type for forward inference.") - }; - }); - } + /// + /// + /// + /// + public IObservable Process(IObservable>> source) + { + return source.Select(input => input.Item2.forward(input.Item1)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + /// + public IObservable Process(IObservable, Module>> source) + { + return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + /// + /// + public IObservable Process(IObservable, Module>> source) + { + return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable Process(IObservable, Module>> source) + { + return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3, input.Item1.Item4)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable Process(IObservable, Module>> source) + { + return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3, input.Item1.Item4, input.Item1.Item5)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable Process(IObservable, Module>> source) + { + return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3, input.Item1.Item4, input.Item1.Item5, input.Item1.Item6)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + public IObservable Process(IObservable>> source) + { + return source.Select(input => input.Item2.forward()); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + public IObservable Process(IObservable>> source) + { + return source.Select(input => input.Item2.forward(input.Item1)); + } + + /// + /// Runs forward inference on the input tensor using the specified model. + /// + /// + /// + /// + /// + /// + public IObservable Process(IObservable, ScriptModule>> source) + { + return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2)); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs deleted file mode 100644 index 61ceb73a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/ForwardInferenceBuilder.cs +++ /dev/null @@ -1,177 +0,0 @@ -using System; -using System.ComponentModel; -using System.Reactive.Linq; -using System.Reflection; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.jit; -using System.Xml.Serialization; -using Bonsai.Expressions; -using System.Linq.Expressions; -using System.Collections.Generic; -using System.Linq; - -namespace Bonsai.ML.Torch.NeuralNets; - -/// -/// Runs forward inference on the input tensor using the specified model. -/// -[Combinator] -[ResetCombinator] -[Description("Runs forward inference on the input tensor using the specified model.")] -[WorkflowElementCategory(ElementCategory.Combinator)] -public class ForwardInferenceBuilder : SingleArgumentExpressionBuilder -{ - /// - /// - /// - /// - /// - /// - public override Expression Build(IEnumerable arguments) - { - Console.WriteLine($"Arguments: {string.Join(", ", arguments.Select(a => a.Type.Name))}"); - - var firstArg = arguments.First(); - Console.WriteLine($"First argument: {firstArg}"); - - var secondArg = arguments.Last(); - Console.WriteLine($"Second argument: {secondArg}"); - - var inputType = arguments.First().Type; - - Console.WriteLine($"Input type: {inputType}"); - - var inputTypeName = inputType.Name; - Console.WriteLine($"Input type name: {inputTypeName}"); - - var inputTypeType = inputType.GetType(); - Console.WriteLine($"Input type type: {inputTypeType}"); - - var genericInputTypes = inputType.IsGenericType ? inputType.GetGenericArguments() : []; - - Console.WriteLine($"Generic input: {genericInputTypes}. Types: {string.Join(", ", genericInputTypes.Select(t => t.Name))}. Length: {genericInputTypes.Length}"); - - if (genericInputTypes.Length != 1) - throw new InvalidOperationException("The input must be a single generic argument."); - - var genericInputType = genericInputTypes[0]; - Console.WriteLine($"Generic input type: {genericInputType}"); - - // Get the generic type arguments of the tuple - var genericArguments = genericInputType.GetGenericArguments(); - Console.WriteLine($"Generic arguments: {string.Join(", ", genericArguments.Select(t => t.Name))}"); - - var sourceArguments = genericArguments.First().GetGenericArguments(); - - // The last argument is the model - var model = genericArguments.Last(); - Console.WriteLine($"Model type: {model.FullName}"); - Console.WriteLine($"Model type: {model}"); - - var genericModelArguments = model.GetGenericArguments(); - Console.WriteLine($"Generic model arguments: {string.Join(", ", genericModelArguments.Select(t => t.Name))}"); - - var modelOutputType = genericModelArguments.Last(); - Console.WriteLine($"Model output type: {modelOutputType.Name}"); - - var modelInputTypes = genericModelArguments.Take(genericModelArguments.Length - 1).ToArray(); - Console.WriteLine($"Model input types: {string.Join(", ", modelInputTypes.Select(t => t.Name))}"); - - // Ensure that the input argument types up to the model type match the model input types - for (int i = 0; i < modelInputTypes.Length; i++) - { - if (sourceArguments[i] != modelInputTypes[i]) - throw new InvalidOperationException($"The input argument type '{sourceArguments[i].Name}' does not match the model input type '{modelInputTypes[i].Name}'."); - } - - // Create a parameter expression for the model - // var modelExpression = Expression.Call(Expression.Parameter(model, "model"), - // Console.WriteLine($"Model expression: {modelExpression}"); - - Expression sourceParameter; - if (modelInputTypes.Length == 1) - { - var observableSource = typeof(IObservable<>).MakeGenericType(modelInputTypes[0]); - sourceParameter = Expression.Parameter(observableSource, "source"); - } - else - { - // Create a tuple expression for the input types - var tupleType = System.Type.GetType($"System.Tuple`{modelInputTypes.Length}").MakeGenericType(modelInputTypes); - var observableSource = typeof(IObservable<>).MakeGenericType(tupleType); - sourceParameter = Expression.Parameter(observableSource, "source"); - } - - if (sourceParameter is null) - throw new InvalidOperationException($"The source expression could not be created: {sourceParameter}."); - - Console.WriteLine($"Source expression: {sourceParameter}"); - - // var sourceExpression = Expression.Constant(sourceParameter, firstArg); - - // Get the appropriate Process method - MethodInfo processMethod = null; - if (model.IsSubclassOf(typeof(ScriptModule))) - processMethod = typeof(ForwardInferenceBuilder).GetMethods(BindingFlags.NonPublic | BindingFlags.Static) - .First(m => m.Name == "Process" && m.GetGenericArguments().Length == genericModelArguments.Length && m.GetParameters().Length == 2 && m.GetParameters()[1].ParameterType.IsSubclassOf(typeof(ScriptModule))) - .MakeGenericMethod(modelInputTypes.Concat([modelOutputType]).ToArray()); - else - processMethod = typeof(ForwardInferenceBuilder).GetMethods(BindingFlags.NonPublic | BindingFlags.Static) - .First(m => m.Name == "Process" && m.GetGenericArguments().Length == genericModelArguments.Length && m.GetParameters().Length == 2 && m.GetParameters()[1].ParameterType.IsSubclassOf(typeof(nn.Module))) - .MakeGenericMethod(modelInputTypes.Concat([modelOutputType]).ToArray()); - - Console.WriteLine($"Process method: {processMethod}"); - if (processMethod is null) - throw new InvalidOperationException($"No method overload for Process could be found that matches the input and output types: {string.Join(", ", modelInputTypes.Concat([modelOutputType]).Select(t => t.Name))}."); - - // var callExpression = Expression.Call(processMethod, sourceExpression, modelExpression); - return Expression.Call(processMethod, firstArg, secondArg); - } - - /// - private static IObservable Process(IObservable source, Module model) - { - return source.Select(model.forward); - } - - private static IObservable Process(IObservable> source, Module model) - { - return source.Select(input => model.forward(input.Item1, input.Item2)); - } - - private static IObservable Process(IObservable> source, Module model) - { - return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3)); - } - - private static IObservable Process(IObservable> source, Module model) - { - return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3, input.Item4)); - } - - private static IObservable Process(IObservable> source, Module model) - { - return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3, input.Item4, input.Item5)); - } - - private static IObservable Process(IObservable> source, Module model) - { - return source.Select(input => model.forward(input.Item1, input.Item2, input.Item3, input.Item4, input.Item5, input.Item6)); - } - - private static IObservable Process(IObservable source, ScriptModule model) - { - return source.Select(input => model.forward()); - } - - private static IObservable Process(IObservable source, ScriptModule model) - { - return source.Select(model.forward); - } - - private static IObservable Process(IObservable> source, ScriptModule model) - { - return source.Select(input => model.forward(input.Item1, input.Item2)); - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/BilinearModule.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Linear/BilinearModule.cs index a381a6b7..f7a8b013 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/BilinearModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/BilinearModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Linear; /// /// Creates a Bilinear module. @@ -46,7 +46,7 @@ public class BilinearModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Identity.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs similarity index 79% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Identity.cs rename to src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs index d2bd4fdc..bfd257c8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Identity.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Linear; /// /// Creates a Identity module. @@ -21,8 +21,8 @@ public class Identity /// /// Generates an observable sequence that creates a IdentityModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(Identity()); + return Observable.Return(nn.Identity()); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs rename to src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs index 5b978e72..a2df6e5a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Linear.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Linear; /// /// Creates a Linear transformation layer. @@ -53,8 +53,8 @@ public class Linear /// /// Generates an observable sequence that creates a LinearModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(Linear(InputSize, OutputSize, HasBias, Device, Type)); + return Observable.Return(nn.Linear(InputSize, OutputSize, HasBias, Device, Type)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs index 0006a06d..3e57d1d0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs @@ -6,50 +6,33 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Saves the module to a file. +/// +[Combinator] +[ResetCombinator] +[Description("Saves the module to a file.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class LoadModule { /// - /// Saves the model to a file. + /// The path to the modules state. /// - [Combinator] - [ResetCombinator] - [Description("Saves the model to a file.")] - [WorkflowElementCategory(ElementCategory.Sink)] - public class LoadModule - { - /// - /// The model to save. - /// - [Description("The model to save.")] - [XmlIgnore] - public nn.Module Module { get; set; } - - /// - /// The path to the modules state. - /// - [Description("The path to the modules state.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModulePath { get; set; } - - /// - /// Loads the modules state from the specified file path. - /// - /// - public IObservable Process() - { - return Observable.Return(Module.load(ModulePath)); - } + [Description("The path to the modules state.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModulePath { get; set; } - /// - /// Loads the module's state from the specified file path. - /// - /// - public IObservable Process(IObservable source) + /// + /// Loads the module's state from the specified file path. + /// + /// + public IObservable Process(IObservable source) + { + return source.Select(module => { - return source.Select(module => - { - return module.load(ModulePath); - }); - } + return module.load(ModulePath); + }); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs index 9c26e358..18e80972 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs @@ -16,12 +16,14 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; [Combinator] [Description("Creates a binary cross entropy loss module.")] [WorkflowElementCategory(ElementCategory.Source)] +[TypeConverter(typeof(TensorOperatorConverter))] public class BinaryCrossEntropy : IScalarTypeProvider { /// /// The weight parameter for the BinaryCrossEntropy module. /// [Description("The weight parameter for the BinaryCrossEntropy module")] + [TypeConverter(typeof(TensorConverter))] public Tensor Weight { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs index d7b8b9a5..c171ce99 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs @@ -16,12 +16,14 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; [Combinator] [Description("Measures the Binary Cross Entropy between the target and the output logits.")] [WorkflowElementCategory(ElementCategory.Source)] -public class BinaryCrossEntropyWithLogits +[TypeConverter(typeof(TensorOperatorConverter))] +public class BinaryCrossEntropyWithLogits : IScalarTypeProvider { /// /// The weight parameter for the BinaryCrossEntropyWithLogitsLoss module. /// [Description("The weight parameter for the BinaryCrossEntropyWithLogitsLoss module")] + [TypeConverter(typeof(TensorConverter))] public Tensor Weight { get; set; } = null; /// @@ -34,8 +36,15 @@ public class BinaryCrossEntropyWithLogits /// The pos_weights parameter for the BinaryCrossEntropyWithLogitsLoss module. /// [Description("The pos_weights parameter for the BinaryCrossEntropyWithLogitsLoss module")] + [TypeConverter(typeof(TensorConverter))] public Tensor PosWeights { get; set; } = null; + /// + /// The data type of the tensor elements. + /// + [Description("The data type of the tensor elements.")] + public ScalarType Type { get; set; } = ScalarType.Float32; + /// /// Generates an observable sequence that creates a BinaryCrossEntropyWithLogitsLoss. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs index b1502da4..bbdaf42f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs @@ -27,7 +27,7 @@ public class MeanSquaredError /// /// Generates an observable sequence that creates a MSELoss. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(MSELoss(Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs deleted file mode 100644 index b009fc03..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ZeroPad2dModule.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Module; - -/// -/// Creates a ZeroPad2d module. -/// -[Combinator] -[Description("Creates a ZeroPad2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ZeroPad2dModule -{ - /// - /// The padding parameter for the ZeroPad2d module. - /// - [Description("The padding parameter for the ZeroPad2d module")] - public long Padding { get; set; } - - /// - /// Generates an observable sequence that creates a ZeroPad2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(ZeroPad2d(Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ContinuouslyDifferentiableExponentialLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Module/ContinuouslyDifferentiableExponentialLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs index 85bbd7f0..937c27dc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ContinuouslyDifferentiableExponentialLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a CELU module. @@ -33,7 +33,7 @@ public class ContinuouslyDifferentiableExponentialLinearUnit /// /// Generates an observable sequence that creates a CELUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(CELU(Alpha, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ExponentialLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Module/ExponentialLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs index 085682ce..a006035b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ExponentialLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a ELU activation function. @@ -33,7 +33,7 @@ public class ExponentialLinearUnit /// /// Generates an observable sequence that creates a ELUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(ELU(Alpha, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/GatedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/GatedLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs index 1ba175e0..2d0559e2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/GatedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a GLU module. @@ -27,7 +27,7 @@ public class GatedLinearUnit /// /// Generates an observable sequence that creates a GLUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(GLU(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/GaussianErrorLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Module/GaussianErrorLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs index 27880315..4a3c537d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/GaussianErrorLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a GELU activation function. @@ -27,7 +27,7 @@ public class GaussianErrorLinearUnit /// /// Generates an observable sequence that creates a GaussianErrorLinearUnit module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(GELU(InPlace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/HardShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/HardShrinkage.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs index 42033009..dbab3044 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/HardShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Hardshrink module. @@ -27,7 +27,7 @@ public class HardShrinkage /// /// Generates an observable sequence that creates a HardshrinkModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Hardshrink(Lambda)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/HardSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/HardSigmoid.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs index c1b90b97..5db6598a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/HardSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Hardsigmoid module. @@ -27,7 +27,7 @@ public class HardSigmoid /// /// Generates an observable sequence that creates a HardsigmoidModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Hardsigmoid(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/HardSwish.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSwish.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/HardSwish.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSwish.cs index 9140a9f4..7018727c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/HardSwish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSwish.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Hardswish module. @@ -27,7 +27,7 @@ public class HardSwish /// /// Generates an observable sequence that creates a HardswishModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Hardswish(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/HardTanh.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Module/HardTanh.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs index 5fb75ec4..90aa5a67 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/HardTanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Hardtanh module. @@ -39,7 +39,7 @@ public class HardTanh /// /// Generates an observable sequence that creates a HardtanhModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Hardtanh(MinVal, MaxVal, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/LeakyRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/Module/LeakyRectifiedLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs index 0d5a0536..bf63072c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/LeakyRectifiedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a LeakyReLU activation function. @@ -33,7 +33,7 @@ public class LeakyRectifiedLinearUnit /// /// Generates an observable sequence that creates a LeakyReLUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LeakyReLU(NegativeSlope, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/LogSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/LogSigmoid.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs index 51f79a31..8675d223 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/LogSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a LogSigmoid module. @@ -21,7 +21,7 @@ public class LogSigmoid /// /// Generates an observable sequence that creates a LogSigmoidModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LogSigmoid()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/LogSoftmax.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/LogSoftmax.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs index 51e58c46..ff723673 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/LogSoftmax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a LogSoftmax activation function. @@ -27,7 +27,7 @@ public class LogSoftmax /// /// Generates an observable sequence that creates a LogSoftmaxModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LogSoftmax(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Mish.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs similarity index 83% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Mish.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs index 1132590a..1a28a3d4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Mish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Mish module. @@ -21,7 +21,7 @@ public class Mish /// /// Generates an observable sequence that creates a MishModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Mish()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs index d3829b6c..12bb310f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MultiheadAttentionModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Multi-head attention mechanism. @@ -69,7 +69,7 @@ public class MultiheadAttentionModule /// /// Generates an observable sequence that creates a MultiheadAttentionModule module. /// - public IObservable>> Process() + public IObservable>> Process() { return Observable.Return(MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKv, AddZeroAttn, Kdim, Vdim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs index 572a4eba..ab31ff3b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ParametricRectifiedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a PReLU module. @@ -47,7 +47,7 @@ public class ParametricRectifiedLinearUnit /// /// Generates an observable sequence that creates a PReLUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(PReLU(NumParameters, Init, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/RandomizedLeakyRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Module/RandomizedLeakyRectifiedLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs index 80e484ff..4e1c42cd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/RandomizedLeakyRectifiedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a RReLU module. @@ -39,7 +39,7 @@ public class RandomizedLeakyRectifiedLinearUnit /// /// Generates an observable sequence that creates a RReLUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(RReLU(Lower, Upper, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/RectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Module/RectifiedLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs index c6b808b1..a550d54d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/RectifiedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a ReLU activation function. @@ -33,7 +33,7 @@ public class RectifiedLinearUnit /// /// Generates an observable sequence that creates a RectifiedLinearUnit module. /// - public IObservable> Process() + public IObservable> Process() { if (Bounded) return Observable.Return(ReLU6(Inplace)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ScaledExponentialLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Module/ScaledExponentialLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs index e1d1d10c..0d297f4d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ScaledExponentialLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a SELU activation function. @@ -27,7 +27,7 @@ public class ScaledExponentialLinearUnit /// /// Generates an observable sequence that creates a SELUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(SELU(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Sigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Sigmoid.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs index c6d6895a..dc6a1ee0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Sigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Sigmoid activation function. @@ -21,7 +21,7 @@ public class SigmoidModule /// /// Generates an observable sequence that creates a SigmoidModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Sigmoid()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SigmoidWeightedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SigmoidWeightedLinearUnit.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs index e14a1b09..c362ba2a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SigmoidWeightedLinearUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a SiLU module. @@ -21,7 +21,7 @@ public class SigmoidWeightedLinearUnit /// /// Generates an observable sequence that creates a SiLUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(SiLU()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs index 23a75f04..5710f34e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Softmax activation function. @@ -27,7 +27,7 @@ public class SoftMax /// /// Generates an observable sequence that creates a SoftmaxModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Softmax(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax2D.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax2D.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs index 3c0cb859..06a12652 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMax2D.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Softmax2d module. @@ -21,7 +21,7 @@ public class SoftMax2D /// /// Generates an observable sequence that creates a Softmax2dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Softmax2d()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMin.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SoftMin.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs index 658e7489..9b09a191 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftMin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Softmin module. @@ -27,7 +27,7 @@ public class SoftMin /// /// Generates an observable sequence that creates a SoftminModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Softmin(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftPlus.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SoftPlus.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs index e1cf1446..372d47d6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftPlus.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Softplus module. @@ -33,7 +33,7 @@ public class SoftPlus /// /// Generates an observable sequence that creates a SoftplusModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Softplus(Beta, Threshold)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs similarity index 87% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SoftShrinkage.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs index fa9aaa9e..e1cb1f5f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Softshrink module. @@ -27,7 +27,7 @@ public class SoftShrinkage /// /// Generates an observable sequence that creates a SoftshrinkModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Softshrink(Lambda)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftSign.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/SoftSign.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs index 6502a7c1..67cb333c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/SoftSign.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Softsign module. @@ -21,7 +21,7 @@ public class SoftSign /// /// Generates an observable sequence that creates a SoftsignModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Softsign()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Tanh.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Tanh.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs index a7ed425f..2d090a6b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Tanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Tanh activation function. @@ -21,7 +21,7 @@ public class Tanh /// /// Generates an observable sequence that creates a TanhModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Tanh()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/TanhShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs similarity index 84% rename from src/Bonsai.ML.Torch/NeuralNets/Module/TanhShrinkage.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs index cbee7316..b429afdf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/TanhShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Tanhshrink module. @@ -21,7 +21,7 @@ public class TanhShrinkage /// /// Generates an observable sequence that creates a TanhshrinkModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Tanhshrink()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Threshold.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs similarity index 91% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Threshold.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs index f7079138..ea695c12 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Threshold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// /// Creates a Threshold module. @@ -39,7 +39,7 @@ public class Threshold /// /// Generates an observable sequence that creates a ThresholdModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Threshold(ThresholdValue, FillValue, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs rename to src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs index 7dd66111..6d33685a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/BatchNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Normalization; /// /// Creates a 1D adaptive average pooling layer. @@ -71,7 +71,7 @@ public class BatchNormalization /// /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs rename to src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs index beb6efc3..a60bcaaa 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/GroupNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Normalization; /// /// Creates a Group normalization. @@ -59,7 +59,7 @@ public class GroupNormalization /// /// Generates an observable sequence that creates a GroupNormModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs rename to src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs index d810a185..8015ffcd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/InstanceNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Normalization; /// /// Creates a dropout layer. @@ -71,7 +71,7 @@ public class InstanceNormalization /// /// Generates an observable sequence that creates a Dropout module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs rename to src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs index 98574278..d6268ade 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/LayerNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Normalization; /// /// Creates a Layer normalization. @@ -60,7 +60,7 @@ public class LayerNormalization /// /// Generates an observable sequence that creates a LayerNormModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/LocalResponseNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Module/LocalResponseNormalization.cs rename to src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs index 9c7ca273..dc6ab30a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/LocalResponseNormalization.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Normalization; /// /// Creates a LocalResponseNorm module. @@ -45,7 +45,7 @@ public class LocalResponseNormalization /// /// Generates an observable sequence that creates a LocalResponseNormModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LocalResponseNorm(Size, Alpha, Beta, K)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs new file mode 100644 index 00000000..241d6999 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs @@ -0,0 +1,34 @@ +using System; +using System.Linq; +using System.ComponentModel; +using System.Reactive; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; +using System.Threading.Tasks; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that performs a single optimization step using the specified optimizer. +/// +[Combinator] +[ResetCombinator] +[Description("Performs a single optimization step.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class OptimizationStep +{ + /// + /// Performs a single optimization step using the specified optimizer. + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => input.step()); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs index 2aa7fc8c..eff80e97 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs @@ -23,6 +23,7 @@ public class AdaDelta /// The parameters parameter for the Adadelta module. /// [Description("The parameters parameter for the Adadelta module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs index 0e657f1c..466fa3ae 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs @@ -23,6 +23,7 @@ public class AdaGrad /// The parameters parameter for the Adagrad module. /// [Description("The parameters parameter for the Adagrad module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs index 8acc808d..3e19ddca 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs @@ -23,6 +23,7 @@ public class AdaMax /// The parameters parameter for the Adamax module. /// [Description("The parameters parameter for the Adamax module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs index ea72e2d7..1ccb72b1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs @@ -23,6 +23,7 @@ public class Adam /// The parameters parameter for the Adam module. /// [Description("The parameters parameter for the Adam module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs index fc89f918..3c31c419 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs @@ -23,6 +23,7 @@ public class AdamW /// The parameters parameter for the AdamW module. /// [Description("The parameters parameter for the AdamW module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs index 5504bbc1..ff9bba2c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs @@ -23,6 +23,7 @@ public class Asgd /// The parameters parameter for the ASGD module. /// [Description("The parameters parameter for the ASGD module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs index cf9ed6da..7a4dbfe1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs @@ -23,7 +23,8 @@ public class Lbfgs /// The parameters parameter for the LBFGS module. /// [Description("The parameters parameter for the LBFGS module")] - public IEnumerable<(string, Parameter)> Parameters { get; set; } + [XmlIgnore] + public IEnumerable Parameters { get; set; } /// /// The lr parameter for the LBFGS module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs index eba14f66..9d538c30 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs @@ -23,6 +23,7 @@ public class ResilientBackprop /// The parameters parameter for the Rprop module. /// [Description("The parameters parameter for the Rprop module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs index 4c5d4949..4dc2ab80 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs @@ -23,6 +23,7 @@ public class RmsPropogation /// The parameters parameter for the RMSProp module. /// [Description("The parameters parameter for the RMSProp module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs index 4832b4f4..d08afe72 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs @@ -23,6 +23,7 @@ public class Sgd /// The parameters parameter for the SGD module. /// [Description("The parameters parameter for the SGD module")] + [XmlIgnore] public IEnumerable Parameters { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs deleted file mode 100644 index 80499cfb..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool2dModule.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; - -/// -/// Creates a MaxUnpool2d module. -/// -[Combinator] -[Description("Creates a MaxUnpool2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MaxUnpool2dModule -{ - /// - /// The kernelsize parameter for the MaxUnpool2d module. - /// - [Description("The kernelsize parameter for the MaxUnpool2d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the MaxUnpool2d module. - /// - [Description("The stride parameter for the MaxUnpool2d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the MaxUnpool2d module. - /// - [Description("The padding parameter for the MaxUnpool2d module")] - public long? Padding { get; set; } = null; - - /// - /// Generates an observable sequence that creates a MaxUnpool2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs deleted file mode 100644 index 6536073b..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool3dModule.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; - -/// -/// Creates a MaxUnpool3d module. -/// -[Combinator] -[Description("Creates a MaxUnpool3d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MaxUnpool3dModule -{ - /// - /// The kernelsize parameter for the MaxUnpool3d module. - /// - [Description("The kernelsize parameter for the MaxUnpool3d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the MaxUnpool3d module. - /// - [Description("The stride parameter for the MaxUnpool3d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the MaxUnpool3d module. - /// - [Description("The padding parameter for the MaxUnpool3d module")] - public long? Padding { get; set; } = null; - - /// - /// Generates an observable sequence that creates a MaxUnpool3dModule module. - /// - public IObservable> Process() - { - return Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs rename to src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs index af2efe24..6853d45a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Padding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Padding; /// /// Creates a 1D adaptive average pooling layer. @@ -51,7 +51,7 @@ public class Padding /// /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { @@ -64,6 +64,7 @@ public IObservable> Process() }, Dimensions.Two => Mode switch { + PaddingMode.Zero => Observable.Return(ZeroPad2d(PaddingSize)), PaddingMode.Constant => Observable.Return(ConstantPad2d(PaddingSize, Value)), PaddingMode.Reflection => Observable.Return(ReflectionPad2d(PaddingSize)), PaddingMode.Replication => Observable.Return(ReplicationPad2d(PaddingSize)), diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs similarity index 72% rename from src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs rename to src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs index d6f600a7..ed4b329a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/PaddingMode.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs @@ -1,10 +1,15 @@ -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Padding; /// /// Specifies the padding mode for a neural network module. /// public enum PaddingMode { + /// + /// Zero padding. + /// + Zero, + /// /// Constant padding. /// @@ -18,5 +23,5 @@ public enum PaddingMode /// /// Replication padding. /// - Replication + Replication, } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs index 407b956e..3039a38e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveAveragePooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Creates a 1D adaptive average pooling layer. @@ -33,7 +33,7 @@ public class AdaptiveAveragePooling /// /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs index 7883762a..c967e6e6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/AdaptiveMaxPooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Creates a 1D adaptive max pooling layer. @@ -34,7 +34,7 @@ public class AdaptiveMaxPooling /// /// Generates an observable sequence that creates a AdaptiveMaxPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs index c452d0b6..ed1ae6bb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/AveragePooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Creates a 1D adaptive average pooling layer. @@ -34,7 +34,7 @@ public class AveragePooling /// /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/FractionalMaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Module/FractionalMaxPooling.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs index f349a02c..7f582f66 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/FractionalMaxPooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Creates a dropout layer. @@ -45,7 +45,7 @@ public class FractionalMaxPooling /// /// Generates an observable sequence that creates a Dropout module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/MaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Module/MaxPooling.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs index 12c2bf39..4231d84a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/MaxPooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs @@ -9,7 +9,7 @@ using static TorchSharp.torch.nn; using Bonsai.Reactive; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Creates a LPPool1d module. @@ -58,7 +58,7 @@ public class MaxPooling /// /// Generates an observable sequence that creates a LPPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs similarity index 50% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs index 3fd0f4fc..b6bac9cf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/MaxUnpool1dModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs @@ -8,16 +8,22 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Creates a MaxUnpool1d module. +/// Creates a 1D convolution layer. /// [Combinator] -[Description("Creates a MaxUnpool1d module.")] +[Description("Creates a 1D convolution layer.")] [WorkflowElementCategory(ElementCategory.Source)] -public class MaxUnpool1dModule +public class MaxUnpoolModule { + /// + /// The number of dimensions for the AdaptiveAvgPool module. + /// + [Description("The number of dimensions for the AdaptiveAvgPool module")] + public Dimensions Dimensions { get; set; } = Dimensions.One; + /// /// The kernelsize parameter for the MaxUnpool1d module. /// @@ -37,10 +43,16 @@ public class MaxUnpool1dModule public long? Padding { get; set; } = null; /// - /// Generates an observable sequence that creates a MaxUnpool1dModule module. + /// Generates an observable sequence that creates a Conv1dModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)); + return Dimensions switch + { + Dimensions.One => Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)), + Dimensions.Two => Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)), + Dimensions.Three => Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/PowerAveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/Module/PowerAveragePooling.cs rename to src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs index 39e05b2f..1d48b386 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/PowerAveragePooling.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Creates a LPPool1d module. @@ -51,7 +51,7 @@ public class PowerAveragePooling /// /// Generates an observable sequence that creates a LPPool1dModule module. /// - public IObservable> Process() + public IObservable> Process() { return Dimensions switch { diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs similarity index 90% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs index 955ab994..5334ce87 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; /// /// Creates a GRUCell module. @@ -40,7 +40,7 @@ public class GRUCellModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -52,7 +52,7 @@ public class GRUCellModule /// /// Generates an observable sequence that creates a GRUCellModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(GRUCell(InputSize, HiddenSize, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs index 4cb86741..aeb52792 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/GRUModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; /// /// Creates a Gated Recurrent Unit layer. @@ -64,7 +64,7 @@ public class GRUModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -76,7 +76,7 @@ public class GRUModule /// /// Generates an observable sequence that creates a GRUModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs index 41f14ed1..f1743219 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; /// /// Creates a LSTMCell module. @@ -40,7 +40,7 @@ public class LSTMCellModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs index 903db710..5872daf7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/LSTMModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; /// /// Creates a Long Short-Term Memory layer. @@ -64,7 +64,7 @@ public class LSTMModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -76,7 +76,7 @@ public class LSTMModule /// /// Generates an observable sequence that creates a LSTMModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs index 0fa1c0eb..ff36a18e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNCellModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; /// /// Creates a RNNCell module. @@ -47,7 +47,7 @@ public class RNNCellModule /// [XmlIgnore] [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs similarity index 98% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs index 3f703357..98ad5047 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/RNNModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; /// /// Creates a Basic RNN layer. diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs deleted file mode 100644 index ae801d79..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModel.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Reactive.Linq; -using System.Xml.Serialization; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets; - -/// -/// Saves the model to a file. -/// -[Combinator] -[ResetCombinator] -[Description("Saves the model to a file.")] -[WorkflowElementCategory(ElementCategory.Sink)] -public class SaveModel -{ - /// - /// The model to save. - /// - [Description("The model to save.")] - [XmlIgnore] - public nn.Module Model { get; set; } - - /// - /// The path to save the model. - /// - [Description("The path to save the model.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModelPath { get; set; } - - /// - /// Saves the model to the specified file path. - /// - /// - /// - /// - public IObservable Process(IObservable source) - { - return source.Do(input => - { - Model.save(ModelPath); - }); - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs index 31a7f921..bf5bbd33 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs @@ -17,13 +17,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Sink)] public class SaveModule { - /// - /// The module to save. - /// - [Description("The module to save.")] - [XmlIgnore] - public IModule Module { get; set; } - /// /// The path to save the module. /// @@ -34,27 +27,11 @@ public class SaveModule /// /// Saves the input module to the specified file path. /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - var module = input as nn.Module; - module?.save(ModulePath); - }); - } - - /// - /// Saves the module to the specified file path. - /// - /// - /// - /// - public IObservable Process(IObservable source) + public IObservable> Process(IObservable> source) { return source.Do(input => { - var module = Module as nn.Module; - module?.save(ModulePath); + input.save(ModulePath); }); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs index 5d1e8495..d245e07d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs @@ -10,51 +10,31 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Sets the training mode for the model. +/// Represents an operator that sets the training mode for the module. /// [Combinator] [ResetCombinator] -[Description("Sets the training mode for the model.")] +[Description("Sets the training mode for the module.")] [WorkflowElementCategory(ElementCategory.Sink)] public class SetTrainingMode { /// - /// The model for which to set the training mode. + /// The training mode to set for the module. /// - [Description("The model for which to set the training mode.")] - [XmlIgnore] - public IModule Model { get; set; } - - /// - /// The training mode to set for the model. - /// - [Description("The training mode to set for the model.")] + [Description("The training mode to set for the module.")] public TrainingMode Mode { get; set; } = TrainingMode.Train; /// - /// Saves the model to the specified file path. + /// Sets the training mode for the module. /// - /// /// /// - public IObservable Process(IObservable source) + public IObservable Process(IObservable source) { return source.Do(input => { - - var training = Mode == TrainingMode.Train; - - switch (Model) - { - case Module module: - module.train(training); - break; - case ScriptModule scriptModule: - scriptModule.train(training); - break; - default: - throw new InvalidOperationException("Unsupported model type for setting training mode."); - } + // var training = Mode == TrainingMode.Train; + input.train(Mode == TrainingMode.Train); }); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/ChannelShuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/ChannelShuffle.cs rename to src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs index f22ae74a..c64c44f5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/ChannelShuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Shuffle; /// /// Creates a ChannelShuffle module. @@ -27,7 +27,7 @@ public class ChannelShuffle /// /// Generates an observable sequence that creates a ChannelShuffleModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(ChannelShuffle(Groups)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs rename to src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs index 91e1d84c..e5c3a980 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Embedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Sparse; /// /// Creates a Embedding layer. @@ -77,7 +77,7 @@ public class Embedding /// /// Generates an observable sequence that creates a EmbeddingModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Embedding(NumEmbeddings, EmbeddingDims, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs index 8100caa1..1a5848a0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingBagModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Sparse; /// /// Creates a EmbeddingBag module. @@ -76,7 +76,7 @@ public class EmbeddingBagModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. @@ -88,7 +88,7 @@ public class EmbeddingBagModule /// /// Generates an observable sequence that creates a EmbeddingBagModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs rename to src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs index 7a9f0404..42c2fb6f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/EmbeddingFromPretrained.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Sparse; /// /// Creates a EmbeddingFromPretrained module. @@ -16,13 +16,15 @@ namespace Bonsai.ML.Torch.NeuralNets.Module; [Combinator] [Description("Creates a EmbeddingFromPretrained module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingFromPretrained +[TypeConverter(typeof(TensorOperatorConverter))] +public class EmbeddingFromPretrained : IScalarTypeProvider { /// /// The embeddings parameter for the Embedding_from_pretrained module. /// [Description("The embeddings parameter for the Embedding_from_pretrained module")] - public torch.Tensor Embeddings { get; set; } + [TypeConverter(typeof(TensorConverter))] + public Tensor Embeddings { get; set; } /// /// The freeze parameter for the Embedding_from_pretrained module. @@ -72,12 +74,12 @@ public class EmbeddingFromPretrained /// [Description("The desired data type of returned tensor")] [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; + public ScalarType Type { get; set; } = ScalarType.Float32; /// /// Generates an observable sequence that creates a EmbeddingFromPretrainedModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Embedding_from_pretrained(Embeddings, Freeze, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs similarity index 88% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs index c9d731fe..68a9f02c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/EmbeddingbagFromPretrainedModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Sparse; /// /// Creates a EmbeddingbagFromPretrained module. @@ -16,13 +16,15 @@ namespace Bonsai.ML.Torch.NeuralNets.OtherModules; [Combinator] [Description("Creates a EmbeddingbagFromPretrained module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingbagFromPretrainedModule +[TypeConverter(typeof(TensorOperatorConverter))] +public class EmbeddingbagFromPretrainedModule : IScalarTypeProvider { /// /// The embeddings parameter for the EmbeddingBag_from_pretrained module. /// [Description("The embeddings parameter for the EmbeddingBag_from_pretrained module")] - public torch.Tensor Embeddings { get; set; } + [TypeConverter(typeof(TensorConverter))] + public Tensor Embeddings { get; set; } /// /// The freeze parameter for the EmbeddingBag_from_pretrained module. @@ -76,19 +78,19 @@ public class EmbeddingbagFromPretrainedModule /// The desired device of returned tensor. /// [Description("The desired device of returned tensor")] - public torch.Device Device { get; set; } = null; + public Device Device { get; set; } = null; /// /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; + public ScalarType Type { get; set; } = ScalarType.Float32; /// /// Generates an observable sequence that creates a EmbeddingbagFromPretrainedModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs index f5dbba0c..9f9ebc9c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/TrainingMode.cs @@ -6,12 +6,12 @@ namespace Bonsai.ML.Torch.NeuralNets; public enum TrainingMode { /// - /// Sets the model to training mode. + /// Sets the module to evaluation mode. /// - Train, + Evaluation = 0, /// - /// Sets the model to evaluation mode. + /// Sets the module to training mode. /// - Evaluation + Train = 1, } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs index 0808af53..02a4fe99 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderLayerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Transformer; /// /// Creates a TransformerDecoderLayer module. @@ -51,7 +51,7 @@ public class TransformerDecoderLayerModule /// /// Generates an observable sequence that creates a TransformerDecoderLayerModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(TransformerDecoderLayer(DModel, Nhead, DimFeedforward, Dropout, Activation)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs similarity index 77% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs index 46571f01..5d2d2399 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerDecoderModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Transformer; /// /// Creates a TransformerDecoder module. @@ -22,7 +22,8 @@ public class TransformerDecoderModule /// The decoder_layer parameter for the TransformerDecoder module. /// [Description("The decoder_layer parameter for the TransformerDecoder module")] - public TransformerDecoderLayer DecoderLayer { get; set; } + [XmlIgnore] + public Module DecoderLayer { get; set; } /// /// The num_layers parameter for the TransformerDecoder module. @@ -33,8 +34,9 @@ public class TransformerDecoderModule /// /// Generates an observable sequence that creates a TransformerDecoderModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(TransformerDecoder(DecoderLayer, NumLayers)); + + return Observable.Return(TransformerDecoder(DecoderLayer as TorchSharp.Modules.TransformerDecoderLayer, NumLayers)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoderLayer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoderLayer.cs rename to src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs index cd578548..303630b0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoderLayer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Transformer; /// /// Creates a TransformerEncoderLayer module. @@ -16,7 +16,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Module; [Combinator] [Description("Creates a TransformerEncoderLayer module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class TransformerEncoderLayer +public class TransformerEncoderLayerModule { /// /// The d_model parameter for the TransformerEncoderLayer module. @@ -51,7 +51,7 @@ public class TransformerEncoderLayer /// /// Generates an observable sequence that creates a TransformerEncoderLayerModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(TransformerEncoderLayer(DModel, Nhead, DimFeedforward, Dropout, Activation)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs similarity index 81% rename from src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoder.cs rename to src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs index 883c9744..4b17af06 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/TransformerEncoder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs @@ -4,10 +4,11 @@ using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; +using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Transformer; /// /// Creates a TransformerEncoder module. @@ -22,7 +23,7 @@ public class TransformerEncoder /// [Description("The encoder_layer parameter for the TransformerEncoder module")] [XmlIgnore] - public TorchSharp.Modules.TransformerEncoderLayer EncoderLayer { get; set; } + public Module EncoderLayer { get; set; } /// /// The num_layers parameter for the TransformerEncoder module. @@ -33,8 +34,8 @@ public class TransformerEncoder /// /// Generates an observable sequence that creates a TransformerEncoderModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(TransformerEncoder(EncoderLayer, NumLayers)); + return Observable.Return(TransformerEncoder(EncoderLayer as TransformerEncoderLayer, NumLayers)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs index 80c159c6..838d85fa 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OtherModules/TransformerModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.OtherModules; +namespace Bonsai.ML.Torch.NeuralNets.Transformer; /// /// Creates a Transformer module. @@ -63,8 +63,8 @@ public class TransformerModule /// /// Generates an observable sequence that creates a TransformerModule module. /// - public IObservable> Process() + public IObservable> Process() { - return Observable.Return(Transformer(DModel, Nhead, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); + return Observable.Return(nn.Transformer(DModel, Nhead, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/PixelShuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/PixelShuffle.cs rename to src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs index 453830b9..0ac16542 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/PixelShuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Vision; /// /// Creates a PixelShuffle module. @@ -27,7 +27,7 @@ public class PixelShuffle /// /// Generates an observable sequence that creates a PixelShuffleModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(PixelShuffle(UpscaleFactor)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/PixelUnshuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs similarity index 89% rename from src/Bonsai.ML.Torch/NeuralNets/Module/PixelUnshuffle.cs rename to src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs index 9a31cdbd..bdc529e3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/PixelUnshuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Vision; /// /// Creates a PixelUnshuffle module. @@ -27,7 +27,7 @@ public class PixelUnshuffle /// /// Generates an observable sequence that creates a PixelUnshuffleModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(PixelUnshuffle(DownscaleFactor)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Module/Upsample.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/Module/Upsample.cs rename to src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs index 4a4c048a..186c96f3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Module/Upsample.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs @@ -8,7 +8,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.Module; +namespace Bonsai.ML.Torch.NeuralNets.Vision; /// /// Creates a Upsample module. @@ -47,7 +47,7 @@ public class UpsampleModule /// /// Generates an observable sequence that creates a UpsampleModule module. /// - public IObservable> Process() + public IObservable> Process() { return Observable.Return(Upsample(Size, ScaleFactor, Mode, AlignCorners)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs b/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs new file mode 100644 index 00000000..6fc38f87 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs @@ -0,0 +1,34 @@ +using System; +using System.Linq; +using System.ComponentModel; +using System.Reactive; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.optim; +using static TorchSharp.torch.optim.lr_scheduler; +using System.Threading.Tasks; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that sets the gradient of the specified optimizer to zero. +/// +[Combinator] +[ResetCombinator] +[Description("Sets the gradient to zero.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class ZeroGradient +{ + /// + /// Sets the gradient of the specified optimizer to zero. + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => input.zero_grad()); + } +} \ No newline at end of file From 8c0a1b240767457d7cb0306f33881a7612eb74bb Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 3 Dec 2025 18:14:46 +0000 Subject: [PATCH 10/30] Added correctly typed overloads to loading/saving modules and to setting the training mode --- src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs | 108 +++++++++++- .../NeuralNets/LoadModuleArchitecture.cs | 2 +- .../NeuralNets/LoadScriptModule.cs | 114 ++++++++---- src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs | 166 +++++++++++++++++- .../NeuralNets/SetTrainingMode.cs | 166 +++++++++++++++++- 5 files changed, 520 insertions(+), 36 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs index 3e57d1d0..16d1bde6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs @@ -5,6 +5,7 @@ using TorchSharp; using static TorchSharp.torch; using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; namespace Bonsai.ML.Torch.NeuralNets; @@ -28,11 +29,116 @@ public class LoadModule /// Loads the module's state from the specified file path. /// /// - public IObservable Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(module => { return module.load(ModulePath); }); } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (Module)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (Module)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (Module)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (Module)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (Module)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (Module)module.load(ModulePath); + }); + } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs index 0025d016..54544510 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs @@ -62,7 +62,7 @@ public int NumClasses /// /// /// - public IObservable> Process() + public IObservable> Process() { var device = Device; Module module = ModelArchitecture switch diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs index 48062c57..21cc2c8c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.ComponentModel; using System.Reactive.Linq; using System.Linq.Expressions; @@ -15,17 +16,14 @@ namespace Bonsai.ML.Torch.NeuralNets; /// /// Loads a TorchScript module from the specified file path. /// -[XmlInclude(typeof(TypeMapping))] -[XmlInclude(typeof(TypeMapping>))] -[XmlInclude(typeof(TypeMapping>))] [Combinator] [ResetCombinator] -[Description("Loads a TorchScript module from the specified file path.")] +[Description("Loads a TorchScript module from the specified file path. In order to correctly infer the module type, pass into the operator objects representing the desired ScriptModule generic argument types.")] [WorkflowElementCategory(ElementCategory.Source)] public class LoadScriptModule: ExpressionBuilder { /// - public override Range ArgumentRange => new(0, 1); + public override Range ArgumentRange => new(0, 3); /// /// The device on which to load the model. @@ -35,54 +33,108 @@ public class LoadScriptModule: ExpressionBuilder public Device Device { get; set; } /// - /// The path to the TorchScript model file. + /// The path to the TorchScript module file. /// - [Description("The path to the TorchScript model file.")] + [Description("The path to the TorchScript module file.")] [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModelPath { get; set; } - - /// - /// The type mapping for the loaded script module. - /// - public TypeMapping ScriptModuleType { get; set; } = new TypeMapping>(); + public string ScriptModulePath { get; set; } /// public override Expression Build(IEnumerable arguments) { - var scriptModuleType = ScriptModuleType.GetType().GetGenericArguments()[0]; + System.Type scriptModuleType; + if (!arguments.Any()) + // if no arguments are provided, the script module type is assumed to be non-generic + scriptModuleType = typeof(ScriptModule); + else + { + // otherwise, the script module type is inferred from the collection of input argument types + // arguments are always IObservable, so we need to extract the T type from each argument + var argumentTypes = arguments.Select(arg => arg.Type.GetGenericArguments()[0]).ToArray(); + var genericScriptModuleType = argumentTypes.Length switch + { + 1 => typeof(ScriptModule<>), + 2 => typeof(ScriptModule<,>), + 3 => typeof(ScriptModule<,,>), + _ => throw new NotSupportedException("Only ScriptModule types with up to three generic type arguments are supported."), + }; + scriptModuleType = genericScriptModuleType.MakeGenericType(argumentTypes); + } + var outputType = typeof(IObservable<>).MakeGenericType(scriptModuleType); - var modelPathExpression = Expression.Constant(ModelPath, typeof(string)); + var modulePathExpression = Expression.Constant(ScriptModulePath, typeof(string)); var deviceExpression = Expression.Constant(Device, typeof(Device)); - var callExpression = scriptModuleType switch + Expression callExpression; + if (scriptModuleType.IsGenericType) + { + var genericArguments = scriptModuleType.GetGenericArguments(); + var processMethod = typeof(LoadScriptModule).GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static) + .First(m => m.Name == nameof(ProcessScriptModule) && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == genericArguments.Length) + .MakeGenericMethod(genericArguments); + callExpression = Expression.Call(processMethod, modulePathExpression, deviceExpression); + } + else { - System.Type t when t == typeof(ScriptModule) => - Expression.Call(typeof(LoadScriptModule).GetMethod(nameof(ProcessScriptModule), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static), modelPathExpression, deviceExpression), - System.Type t when t == typeof(ScriptModule) => - Expression.Call(typeof(LoadScriptModule).GetMethod(nameof(ProcessScriptModuleTensor), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static), modelPathExpression, deviceExpression), - System.Type t when t == typeof(ScriptModule) => - Expression.Call(typeof(LoadScriptModule).GetMethod(nameof(ProcessScriptModuleTensorTensor), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static), modelPathExpression, deviceExpression), - _ => throw new NotSupportedException($"The specified script module type '{scriptModuleType}' is not supported."), - }; + var processMethod = typeof(LoadScriptModule).GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static) + .First(m => m.Name == nameof(ProcessScriptModule) && !m.IsGenericMethod); + callExpression = Expression.Call(processMethod, modulePathExpression, deviceExpression); + } - return Expression.Call(callExpression.Method, callExpression.Arguments); + return callExpression; } - private static IObservable ProcessScriptModule(string modelPath, Device device) + /// + /// Loads the scripted module from the specified file path. + /// + /// + /// + /// + private static IObservable ProcessScriptModule(string modulePath, Device device) { - var scriptModule = device is null ? jit.load(modelPath) : load(modelPath, device); + var scriptModule = device is null ? jit.load(modulePath) : load(modulePath, device); return Observable.Return(scriptModule); } - private static IObservable> ProcessScriptModuleTensor(string modelPath, Device device) + /// + /// Loads the scripted module from the specified file path. + /// + /// + /// + /// + /// + private static IObservable> ProcessScriptModule(string modulePath, Device device) { - var scriptModule = device is null ? load(modelPath) : load(modelPath, device); + var scriptModule = device is null ? load(modulePath) : load(modulePath, device); return Observable.Return(scriptModule); } - private static IObservable> ProcessScriptModuleTensorTensor(string modelPath, Device device) + /// + /// Loads the scripted module from the specified file path. + /// + /// + /// + /// + /// + /// + private static IObservable> ProcessScriptModule(string modulePath, Device device) + { + var scriptModule = device is null ? load(modulePath) : load(modulePath, device); + return Observable.Return(scriptModule); + } + + /// + /// Loads the scripted module from the specified file path. + /// + /// + /// + /// + /// + /// + /// + private static IObservable> ProcessScriptModule(string modulePath, Device device) { - var scriptModule = device is null ? load(modelPath) : load(modelPath, device); + var scriptModule = device is null ? load(modulePath) : load(modulePath, device); return Observable.Return(scriptModule); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs index bf5bbd33..dd11dcd2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs @@ -5,6 +5,7 @@ using TorchSharp; using static TorchSharp.torch; using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; namespace Bonsai.ML.Torch.NeuralNets; @@ -27,11 +28,174 @@ public class SaveModule /// /// Saves the input module to the specified file path. /// - public IObservable> Process(IObservable> source) + public IObservable Process(IObservable source) { return source.Do(input => { input.save(ModulePath); }); } + + /// + /// Saves the input module to the specified file path. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.save(ModulePath); + }); + } + + /// + /// Saves the input module to the specified file path. + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.save(ModulePath); + }); + } + + /// + /// Saves the input module to the specified file path. + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.save(ModulePath); + }); + } + + /// + /// Saves the input module to the specified file path. + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.save(ModulePath); + }); + } + + /// + /// Saves the input module to the specified file path. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.save(ModulePath); + }); + } + + /// + /// Saves the input module to the specified file path. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.save(ModulePath); + }); + } + + /// + /// Saves the input scripted module to the specified file path. + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => + { + save(input, ModulePath); + }); + } + + /// + /// Saves the input scripted module to the specified file path. + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + save(input, ModulePath); + }); + } + + /// + /// Saves the input scripted module to the specified file path. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + save(input, ModulePath); + }); + } + + /// + /// Saves the input scripted module to the specified file path. + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + save(input, ModulePath); + }); + } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs index d245e07d..6588fcb1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs @@ -29,11 +29,173 @@ public class SetTrainingMode /// /// /// - public IObservable Process(IObservable source) + public IObservable Process(IObservable source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Do(input => + { + input.train(Mode == TrainingMode.Train); + }); + } + + /// + /// Sets the training mode for the module. + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) { return source.Do(input => { - // var training = Mode == TrainingMode.Train; input.train(Mode == TrainingMode.Train); }); } From f11a427ab991b9dca6e7d2b6e357f002f0f6e3d6 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 3 Dec 2025 18:15:52 +0000 Subject: [PATCH 11/30] Added a type converter for converting module operator classes --- .../NeuralNets/ModuleTypeConverter.cs | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ModuleTypeConverter.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/ModuleTypeConverter.cs b/src/Bonsai.ML.Torch/NeuralNets/ModuleTypeConverter.cs new file mode 100644 index 00000000..a71ad672 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ModuleTypeConverter.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Provides a type converter to convert modules to and from other representations. +/// +public class ModuleTypeConverter : TypeConverter +{ + internal static IEnumerable GetInstanceTypes(ITypeDescriptorContext context) + { + var builderType = context.Instance?.GetType() ?? context.PropertyDescriptor.ComponentType; + var includeAttributes = (XmlIncludeAttribute[])builderType.GetCustomAttributes(typeof(XmlIncludeAttribute), inherit: true); + if (includeAttributes.Length > 0) + { + return includeAttributes.Select(attribute => attribute.Type); + } + + var propertyInfo = builderType.GetProperty(context.PropertyDescriptor.Name); + if (propertyInfo == null) return Enumerable.Empty(); + + var elementAttributes = (XmlElementAttribute[])propertyInfo.GetCustomAttributes(typeof(XmlElementAttribute), inherit: true); + if (elementAttributes.Length > 0) + { + return elementAttributes.Select(attribute => attribute.Type); + } + + return propertyInfo.PropertyType.GetCustomAttributes().Select(attribute => attribute.Type); + } + + static string GetDisplayName(Type type) + { + var displayNameAttribute = (DisplayNameAttribute)type.GetCustomAttribute(typeof(DisplayNameAttribute)); + if (!string.IsNullOrEmpty(displayNameAttribute?.DisplayName)) + { + return displayNameAttribute.DisplayName; + } + + return type.Name; + } + + /// + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof(string); + } + + /// + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + if (value is string typeName) + { + return GetInstanceTypes(context).FirstOrDefault( + type => string.Equals(GetDisplayName(type), typeName, StringComparison.OrdinalIgnoreCase)); + } + + return null; + } + + /// + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string) && value is Type valueType) + { + return GetDisplayName(valueType); + } + + return null; + } + + /// + public override bool GetStandardValuesSupported(ITypeDescriptorContext context) + { + return true; + } + + /// + public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) + { + return true; + } + + /// + public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) + { + var includeTypes = GetInstanceTypes(context).ToArray(); + return new StandardValuesCollection(includeTypes); + } +} \ No newline at end of file From 6c4fe9668832b5335020828abc2b823288888938 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 3 Dec 2025 18:17:13 +0000 Subject: [PATCH 12/30] Added an abstract class for module combinator builders --- .../NeuralNets/ModuleCombinatorBuilder.cs | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs new file mode 100644 index 00000000..c2319e66 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs @@ -0,0 +1,170 @@ +using Bonsai.Expressions; +using System; +using System.ComponentModel; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents a base class for module combinator builders. +/// +public abstract class ModuleCombinatorBuilder : ExpressionBuilder, ICustomTypeDescriptor +{ + internal ModuleCombinatorBuilder() + { + } + + internal object Module { get; set; } + + static readonly Attribute[] EmptyAttributes = new Attribute[0]; + + AttributeCollection ICustomTypeDescriptor.GetAttributes() + { + var attributes = TypeDescriptor.GetAttributes(GetType()); + var defaultProperty = TypeDescriptor.GetDefaultProperty(GetType()); + if (defaultProperty != null) + { + var instance = defaultProperty.GetValue(this); + var instanceAttributes = TypeDescriptor.GetAttributes(instance); + if (instanceAttributes[typeof(DescriptionAttribute)] is DescriptionAttribute description) + { + return AttributeCollection.FromExisting(attributes, description); + } + } + + return attributes; + } + + string ICustomTypeDescriptor.GetClassName() + { + return TypeDescriptor.GetClassName(GetType()); + } + + string ICustomTypeDescriptor.GetComponentName() + { + return null; + } + + TypeConverter ICustomTypeDescriptor.GetConverter() + { + return TypeDescriptor.GetConverter(GetType()); + } + + EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() + { + return null; + } + + PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() + { + var defaultProperty = TypeDescriptor.GetDefaultProperty(GetType()); + return defaultProperty != null ? new FactoryTypePropertyDescriptor(defaultProperty) : null; + } + + object ICustomTypeDescriptor.GetEditor(Type editorBaseType) + { + return TypeDescriptor.GetEditor(GetType(), editorBaseType); + } + + EventDescriptorCollection ICustomTypeDescriptor.GetEvents() + { + return EventDescriptorCollection.Empty; + } + + EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) + { + return EventDescriptorCollection.Empty; + } + + PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() + { + return ((ICustomTypeDescriptor)this).GetProperties(EmptyAttributes); + } + + PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) + { + var baseProperties = TypeDescriptor.GetProperties(GetType(), attributes); + var defaultProperty = TypeDescriptor.GetDefaultProperty(GetType()); + if (defaultProperty != null) + { + var instance = defaultProperty.GetValue(this); + var instanceProperties = TypeDescriptor.GetProperties(instance, attributes); + var properties = new PropertyDescriptor[baseProperties.Count + instanceProperties.Count]; + for (int i = 0; i < baseProperties.Count; i++) + { + var baseProperty = baseProperties[i]; + if (baseProperty == defaultProperty) + { + baseProperty = new FactoryTypePropertyDescriptor(defaultProperty); + } + + properties[i] = baseProperty; + } + + for (int i = 0; i < instanceProperties.Count; i++) + { + var expandedProperty = instanceProperties[i]; + properties[i + baseProperties.Count] = expandedProperty; + } + return new PropertyDescriptorCollection(properties); + } + + return baseProperties; + } + + object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) + { + return pd?.ComponentType.IsAssignableFrom(GetType()) == true ? this : Module; + } + + class FactoryTypePropertyDescriptor(PropertyDescriptor descr) : PropertyDescriptor(descr) + { + readonly PropertyDescriptor descriptor = descr; + + public override Type ComponentType => descriptor.ComponentType; + + public override bool IsReadOnly => false; + + public override Type PropertyType => typeof(Type); + + public override bool CanResetValue(object component) + { + return true; + } + + public override object GetValue(object component) + { + component = descriptor.GetValue(component); + return component?.GetType(); + } + + public override void ResetValue(object component) + { + descriptor.SetValue(component, null); + } + + public override void SetValue(object component, object value) + { + var currentValue = descriptor.GetValue(component); + var newValue = Activator.CreateInstance((Type)value); + + var newProperties = TypeDescriptor.GetProperties(newValue); + var currentProperties = TypeDescriptor.GetProperties(currentValue); + foreach (PropertyDescriptor property in newProperties) + { + var mergeProperty = currentProperties[property.Name]; + if (mergeProperty?.PropertyType == property.PropertyType) + { + var propertyValue = mergeProperty.GetValue(currentValue); + property.SetValue(newValue, propertyValue); + } + } + + descriptor.SetValue(component, newValue); + } + + public override bool ShouldSerializeValue(object component) + { + return true; + } + } +} \ No newline at end of file From 349b4f5e81f0987b107318449a319613abd33a47 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Wed, 3 Dec 2025 19:17:53 +0000 Subject: [PATCH 13/30] Added a convolution module builder class and modified convolution module operators --- .../NeuralNets/Convolution/Conv1d.cs | 112 ++++++++++++++++++ .../NeuralNets/Convolution/Conv2d.cs | 112 ++++++++++++++++++ .../NeuralNets/Convolution/Conv3d.cs | 112 ++++++++++++++++++ .../NeuralNets/Convolution/ConvTranspose1d.cs | 112 ++++++++++++++++++ .../NeuralNets/Convolution/ConvTranspose2d.cs | 112 ++++++++++++++++++ .../NeuralNets/Convolution/ConvTranspose3d.cs | 112 ++++++++++++++++++ .../NeuralNets/Convolution/Convolution.cs | 35 +++++- .../NeuralNets/Convolution/Fold.cs | 18 ++- .../NeuralNets/Convolution/Unfold.cs | 17 ++- .../NeuralNets/ConvolutionModuleBuilder.cs | 91 ++++++++++++++ 10 files changed, 822 insertions(+), 11 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs new file mode 100644 index 00000000..ffce0334 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Convolution; + +/// +/// Represents an operator that creates a 1D convolution module. +/// +[Description("Creates a 1D convolution module.")] +public class Conv1d +{ + /// + /// The in_channels parameter for the Conv1d module. + /// + [Description("The in_channels parameter for the Conv1d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a Conv1d module. + /// + public IObservable> Process() + { + return Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } + + /// + /// Creates a Conv1d module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs new file mode 100644 index 00000000..659c54e2 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Convolution; + +/// +/// Represents an operator that creates a 2D convolution module. +/// +[Description("Creates a 2D convolution module.")] +public class Conv2d +{ + /// + /// The in_channels parameter for the Conv2d module. + /// + [Description("The in_channels parameter for the Conv2d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a Conv2d module. + /// + public IObservable> Process() + { + return Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } + + /// + /// Creates a Conv2d module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs new file mode 100644 index 00000000..86695d06 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Convolution; + +/// +/// Represents an operator that creates a 3D convolution module. +/// +[Description("Creates a 3D convolution module.")] +public class Conv3d +{ + /// + /// The in_channels parameter for the Conv3d module. + /// + [Description("The in_channels parameter for the Conv3d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a Conv3d module. + /// + public IObservable> Process() + { + return Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } + + /// + /// Creates a Conv3d module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs new file mode 100644 index 00000000..b9b83f2f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Convolution; + +/// +/// Represents an operator that creates a 1D transposed convolution module. +/// +[Description("Creates a 1D transposed convolution module.")] +public class ConvTranspose1d +{ + /// + /// The in_channels parameter for the Conv1d module. + /// + [Description("The in_channels parameter for the Conv1d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a ConvTranspose1d module. + /// + public IObservable> Process() + { + return Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } + + /// + /// Creates a ConvTranspose1d module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs new file mode 100644 index 00000000..9818b267 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Convolution; + +/// +/// Represents an operator that creates a 2D transposed convolution module. +/// +[Description("Creates a 2D transposed convolution module.")] +public class ConvTranspose2d +{ + /// + /// The in_channels parameter for the Conv1d module. + /// + [Description("The in_channels parameter for the Conv1d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a ConvTranspose2d module. + /// + public IObservable> Process() + { + return Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } + + /// + /// Creates a ConvTranspose2d module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs new file mode 100644 index 00000000..81f72e12 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Convolution; + +/// +/// Represents an operator that creates a 3D transposed convolution module. +/// +[Description("Creates a 3D transposed convolution module.")] +public class ConvTranspose3d +{ + /// + /// The in_channels parameter for the Conv1d module. + /// + [Description("The in_channels parameter for the Conv1d module")] + public long InChannels { get; set; } + + /// + /// The out_channels parameter for the Conv1d module. + /// + [Description("The out_channels parameter for the Conv1d module")] + public long OutChannels { get; set; } + + /// + /// The kernelsize parameter for the Conv1d module. + /// + [Description("The kernelsize parameter for the Conv1d module")] + public long KernelSize { get; set; } + + /// + /// The stride parameter for the Conv1d module. + /// + [Description("The stride parameter for the Conv1d module")] + public long Stride { get; set; } = 1; + + /// + /// The padding parameter for the Conv1d module. + /// + [Description("The padding parameter for the Conv1d module")] + public long Padding { get; set; } = 0; + + /// + /// The output_padding parameter for the Conv1d module. + /// + [Description("The output_padding parameter for the ConvTransposed1d module")] + public long OutputPadding { get; set; } = 0; + + /// + /// The dilation parameter for the Conv1d module. + /// + [Description("The dilation parameter for the Conv1d module")] + public long Dilation { get; set; } = 1; + + /// + /// The padding_mode parameter for the Conv1d module. + /// + [Description("The padding_mode parameter for the Conv1d module")] + public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; + + /// + /// The groups parameter for the Conv1d module. + /// + [Description("The groups parameter for the Conv1d module")] + public long Groups { get; set; } = 1; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a ConvTranspose3d module. + /// + public IObservable> Process() + { + return Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } + + /// + /// Creates a ConvTranspose3d module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs index 09147a35..b17a4693 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs @@ -11,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// -/// Creates a 1D convolution layer. +/// Represents an operator that creates a specific convolution module. /// [Combinator] -[Description("Creates a 1D convolution layer.")] +[Description("Creates a specific convolution module.")] [WorkflowElementCategory(ElementCategory.Source)] public class Convolution { @@ -101,12 +101,13 @@ public class Convolution /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a Conv1dModule module. + /// Creates a specific convolution module. /// + /// + /// public IObservable> Process() { return Dimensions switch @@ -123,4 +124,30 @@ public IObservable> Process() _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), }; } + + /// + /// Creates a specific convolution module. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select>(_ => { + return Dimensions switch + { + Dimensions.One => Transposed + ? ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, 0, Dilation, PaddingMode, Groups, Bias, Device, Type) + : Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type), + Dimensions.Two => Transposed + ? ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type) + : Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type), + Dimensions.Three => Transposed + ? ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type) + : Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type), + _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), + }; + }); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs index cdb2a002..4dfde353 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs @@ -11,12 +11,11 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// -/// Creates a Fold module. +/// Represents an operator that creates a Fold module. /// -[Combinator] [Description("Creates a Fold module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class FoldModule +public class Fold { /// /// The output_size parameter for the Fold module. @@ -49,10 +48,21 @@ public class FoldModule public long Stride { get; set; } = 1; /// - /// Generates an observable sequence that creates a FoldModule module. + /// Creates a Fold module. /// public IObservable> Process() { return Observable.Return(Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); } + + /// + /// Creates a Fold module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs index bf85c945..e7a29121 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs @@ -11,10 +11,10 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// -/// Creates a Unfold module. +/// Represents an operator that creates an Unfold module. /// [Combinator] -[Description("Creates a Unfold module.")] +[Description("Creates an Unfold module.")] [WorkflowElementCategory(ElementCategory.Source)] public class Unfold { @@ -43,10 +43,21 @@ public class Unfold public long Stride { get; set; } = 1; /// - /// Generates an observable sequence that creates a UnfoldModule module. + /// Creates an Unfold module. /// public IObservable> Process() { return Observable.Return(Unfold(KernelSize, Dilation, Padding, Stride)); } + + /// + /// Creates an Unfold module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Unfold(KernelSize, Dilation, Padding, Stride)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs new file mode 100644 index 00000000..906c4b6c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs @@ -0,0 +1,91 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using Bonsai.Expressions; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using System.Reflection; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Creates a convolution module. +/// +[XmlInclude(typeof(Convolution.Convolution))] +[XmlInclude(typeof(Convolution.Fold))] +[XmlInclude(typeof(Convolution.Unfold))] +[DefaultProperty(nameof(ConvolutionModule))] +[Combinator] +[Description("Creates a convolution module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ConvolutionModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public ConvolutionModuleBuilder() + { + Module = new Convolution.Fold(); + } + + /// + /// Gets or sets the event parser used to filter and select event messages + /// reported by the device. + /// + [DesignOnly(true)] + [DisplayName("Type")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The type of the device event message to select.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object ConvolutionModule + { + get => Module; + set => Module = value; + } + + string INamedElement.Name => $"Convolution.{GetElementDisplayName(ConvolutionModule)}"; + + /// + public override Expression Build(IEnumerable arguments) + { + // We want to return an expression that constructs the module + // For now, the only module supported is the Fold module + // We want to return the Process method of the Fold class + var convolutionModule = ConvolutionModule.GetType(); + + // arguments can either be empty or contain a single argument + if (!arguments.Any()) + { + // if empty, we call the non genericProcess method + var methodInfo = convolutionModule.GetMethods(BindingFlags.Public | BindingFlags.Instance).First(m => m.Name == "Process" && !m.IsGenericMethod); + return Expression.Call( + Expression.Constant(ConvolutionModule, convolutionModule), + methodInfo + ); + } + else + { + // if there is an argument, we call the generic Process method + var argument = arguments.First(); + var argumentType = argument.Type.GetGenericArguments()[0]; + var methodInfo = convolutionModule.GetMethods(BindingFlags.Public | BindingFlags.Instance).First(m => m.Name == "Process" && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == 1); + var genericMethodInfo = methodInfo.MakeGenericMethod(argumentType); + return Expression.Call( + Expression.Constant(ConvolutionModule, convolutionModule), + genericMethodInfo, + argument + ); + } + } +} From d84c27795d5e886fa00b5a2cd46eebf0e797e4df Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 4 Dec 2025 14:34:04 +0000 Subject: [PATCH 14/30] Added module combinator property descriptor and updated builder to correctly display element name and properties of module --- .../NeuralNets/ModuleCombinatorBuilder.cs | 100 +++++++----------- .../ModuleCombinatorPropertyDescriptor.cs | 56 ++++++++++ 2 files changed, 96 insertions(+), 60 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorPropertyDescriptor.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs index c2319e66..1db7a3b7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs @@ -1,21 +1,53 @@ using Bonsai.Expressions; using System; using System.ComponentModel; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.Reflection; namespace Bonsai.ML.Torch.NeuralNets; /// /// Represents a base class for module combinator builders. /// -public abstract class ModuleCombinatorBuilder : ExpressionBuilder, ICustomTypeDescriptor +public abstract class ModuleCombinatorBuilder : ExpressionBuilder, ICustomTypeDescriptor, INamedElement { - internal ModuleCombinatorBuilder() - { - } + /// + string INamedElement.Name => $"Module.{GetElementDisplayName(Module)}"; internal object Module { get; set; } - static readonly Attribute[] EmptyAttributes = new Attribute[0]; + /// + public override Expression Build(IEnumerable arguments) + { + // We want to return an expression that constructs the module + var module = Module.GetType(); + + // arguments can either be empty or contain a single argument + if (!arguments.Any()) + { + // if empty, we call the non generic Process method + var methodInfo = module.GetMethods(BindingFlags.Public | BindingFlags.Instance).First(m => m.Name == "Process" && !m.IsGenericMethod); + return Expression.Call( + Expression.Constant(Module, module), + methodInfo + ); + } + else + { + // if there is an argument, we call the generic Process method + var argument = arguments.First(); + var argumentType = argument.Type.GetGenericArguments()[0]; + var methodInfo = module.GetMethods(BindingFlags.Public | BindingFlags.Instance).First(m => m.Name == "Process" && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == 1); + var genericMethodInfo = methodInfo.MakeGenericMethod(argumentType); + return Expression.Call( + Expression.Constant(Module, module), + genericMethodInfo, + argument + ); + } + } AttributeCollection ICustomTypeDescriptor.GetAttributes() { @@ -57,7 +89,7 @@ EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { var defaultProperty = TypeDescriptor.GetDefaultProperty(GetType()); - return defaultProperty != null ? new FactoryTypePropertyDescriptor(defaultProperty) : null; + return defaultProperty != null ? new ModuleCombinatorPropertyDescriptor(defaultProperty) : null; } object ICustomTypeDescriptor.GetEditor(Type editorBaseType) @@ -77,7 +109,7 @@ EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { - return ((ICustomTypeDescriptor)this).GetProperties(EmptyAttributes); + return ((ICustomTypeDescriptor)this).GetProperties([]); } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) @@ -94,7 +126,7 @@ PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] att var baseProperty = baseProperties[i]; if (baseProperty == defaultProperty) { - baseProperty = new FactoryTypePropertyDescriptor(defaultProperty); + baseProperty = new ModuleCombinatorPropertyDescriptor(defaultProperty); } properties[i] = baseProperty; @@ -115,56 +147,4 @@ object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return pd?.ComponentType.IsAssignableFrom(GetType()) == true ? this : Module; } - - class FactoryTypePropertyDescriptor(PropertyDescriptor descr) : PropertyDescriptor(descr) - { - readonly PropertyDescriptor descriptor = descr; - - public override Type ComponentType => descriptor.ComponentType; - - public override bool IsReadOnly => false; - - public override Type PropertyType => typeof(Type); - - public override bool CanResetValue(object component) - { - return true; - } - - public override object GetValue(object component) - { - component = descriptor.GetValue(component); - return component?.GetType(); - } - - public override void ResetValue(object component) - { - descriptor.SetValue(component, null); - } - - public override void SetValue(object component, object value) - { - var currentValue = descriptor.GetValue(component); - var newValue = Activator.CreateInstance((Type)value); - - var newProperties = TypeDescriptor.GetProperties(newValue); - var currentProperties = TypeDescriptor.GetProperties(currentValue); - foreach (PropertyDescriptor property in newProperties) - { - var mergeProperty = currentProperties[property.Name]; - if (mergeProperty?.PropertyType == property.PropertyType) - { - var propertyValue = mergeProperty.GetValue(currentValue); - property.SetValue(newValue, propertyValue); - } - } - - descriptor.SetValue(component, newValue); - } - - public override bool ShouldSerializeValue(object component) - { - return true; - } - } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorPropertyDescriptor.cs b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorPropertyDescriptor.cs new file mode 100644 index 00000000..fdf418a7 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorPropertyDescriptor.cs @@ -0,0 +1,56 @@ +using System; +using System.ComponentModel; + +namespace Bonsai.ML.Torch.NeuralNets; + +internal class ModuleCombinatorPropertyDescriptor(PropertyDescriptor descriptor) : PropertyDescriptor(descriptor) +{ + private readonly PropertyDescriptor descriptor = descriptor; + + public override Type ComponentType => descriptor.ComponentType; + + public override bool IsReadOnly => false; + + public override Type PropertyType => typeof(Type); + + public override bool CanResetValue(object component) + { + return true; + } + + public override object GetValue(object component) + { + component = descriptor.GetValue(component); + return component?.GetType(); + } + + public override void ResetValue(object component) + { + descriptor.SetValue(component, null); + } + + public override void SetValue(object component, object value) + { + var currentValue = descriptor.GetValue(component); + var newValue = Activator.CreateInstance((Type)value); + + var newProperties = TypeDescriptor.GetProperties(newValue); + var currentProperties = TypeDescriptor.GetProperties(currentValue); + foreach (PropertyDescriptor property in newProperties) + { + var mergeProperty = currentProperties[property.Name]; + if (mergeProperty?.PropertyType == property.PropertyType) + { + var propertyValue = mergeProperty.GetValue(currentValue); + property.SetValue(newValue, propertyValue); + } + } + + descriptor.SetValue(component, newValue); + } + + public override bool ShouldSerializeValue(object component) + { + return true; + } +} \ No newline at end of file From de16d5d0dec3aced8a2174f344ce18e30096a4e2 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 4 Dec 2025 14:36:17 +0000 Subject: [PATCH 15/30] Added operator to collect module parameters --- .../NeuralNets/CollectParameters.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs b/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs new file mode 100644 index 00000000..884d3627 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs @@ -0,0 +1,34 @@ +using Bonsai.Expressions; +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.Reflection; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that collects the parameters of torch modules into a collection. +/// +[Combinator] +[Description("Collects the parameters from torch modules into a collection.")] +[WorkflowElementCategory(ElementCategory.Combinator)] +public class CollectParameters +{ + /// + /// Collects the parameters from torch modules into a collection. + /// + public IObservable> Process(params IObservable[] sources) + { + return Observable.Concat(sources) + .SelectMany(module => + { + return module.parameters(recurse: true); + }).ToList(); + } +} \ No newline at end of file From 1b3fa2aacb956ea13253ff91bc7655185471c3b8 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 4 Dec 2025 14:36:55 +0000 Subject: [PATCH 16/30] Updated convolution module builder --- .../NeuralNets/Convolution/Conv1d.cs | 1 - .../NeuralNets/Convolution/Conv2d.cs | 1 - .../NeuralNets/Convolution/Conv3d.cs | 1 - .../NeuralNets/Convolution/ConvTranspose1d.cs | 1 - .../NeuralNets/Convolution/ConvTranspose2d.cs | 1 - .../NeuralNets/Convolution/ConvTranspose3d.cs | 1 - .../NeuralNets/Convolution/Fold.cs | 1 - .../NeuralNets/Convolution/Unfold.cs | 2 - .../NeuralNets/ConvolutionModuleBuilder.cs | 55 ++++--------------- 9 files changed, 12 insertions(+), 52 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs index ffce0334..aa658c40 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs @@ -87,7 +87,6 @@ public class Conv1d /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs index 659c54e2..2266ef5f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs @@ -87,7 +87,6 @@ public class Conv2d /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs index 86695d06..698e86ff 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs @@ -87,7 +87,6 @@ public class Conv3d /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs index b9b83f2f..b09bf7ce 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs @@ -87,7 +87,6 @@ public class ConvTranspose1d /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs index 9818b267..7ed36a6f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs @@ -87,7 +87,6 @@ public class ConvTranspose2d /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs index 81f72e12..0fa168bf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs @@ -87,7 +87,6 @@ public class ConvTranspose3d /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs index 4dfde353..53fd1fc4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs @@ -14,7 +14,6 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// Represents an operator that creates a Fold module. /// [Description("Creates a Fold module.")] -[WorkflowElementCategory(ElementCategory.Source)] public class Fold { /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs index e7a29121..9513b3e1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs @@ -13,9 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates an Unfold module. /// -[Combinator] [Description("Creates an Unfold module.")] -[WorkflowElementCategory(ElementCategory.Source)] public class Unfold { /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs index 906c4b6c..aa5744a1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs @@ -15,14 +15,19 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Creates a convolution module. +/// Represents an operator that creates a torch module for convolution operations. /// -[XmlInclude(typeof(Convolution.Convolution))] +[XmlInclude(typeof(Convolution.Conv1d))] +[XmlInclude(typeof(Convolution.Conv2d))] +[XmlInclude(typeof(Convolution.Conv3d))] +[XmlInclude(typeof(Convolution.ConvTranspose1d))] +[XmlInclude(typeof(Convolution.ConvTranspose2d))] +[XmlInclude(typeof(Convolution.ConvTranspose3d))] [XmlInclude(typeof(Convolution.Fold))] [XmlInclude(typeof(Convolution.Unfold))] [DefaultProperty(nameof(ConvolutionModule))] [Combinator] -[Description("Creates a convolution module.")] +[Description("Creates a torch module for convolution operations.")] [WorkflowElementCategory(ElementCategory.Source)] public class ConvolutionModuleBuilder : ModuleCombinatorBuilder, INamedElement { @@ -34,58 +39,22 @@ public class ConvolutionModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public ConvolutionModuleBuilder() { - Module = new Convolution.Fold(); + Module = new Convolution.Conv1d(); } /// - /// Gets or sets the event parser used to filter and select event messages - /// reported by the device. + /// Gets or sets the specific convolution module to create. /// [DesignOnly(true)] - [DisplayName("Type")] + [DisplayName("Module")] [Externalizable(false)] [RefreshProperties(RefreshProperties.All)] [Category(nameof(CategoryAttribute.Design))] - [Description("The type of the device event message to select.")] + [Description("The specific convolution module to create.")] [TypeConverter(typeof(ModuleTypeConverter))] public object ConvolutionModule { get => Module; set => Module = value; } - - string INamedElement.Name => $"Convolution.{GetElementDisplayName(ConvolutionModule)}"; - - /// - public override Expression Build(IEnumerable arguments) - { - // We want to return an expression that constructs the module - // For now, the only module supported is the Fold module - // We want to return the Process method of the Fold class - var convolutionModule = ConvolutionModule.GetType(); - - // arguments can either be empty or contain a single argument - if (!arguments.Any()) - { - // if empty, we call the non genericProcess method - var methodInfo = convolutionModule.GetMethods(BindingFlags.Public | BindingFlags.Instance).First(m => m.Name == "Process" && !m.IsGenericMethod); - return Expression.Call( - Expression.Constant(ConvolutionModule, convolutionModule), - methodInfo - ); - } - else - { - // if there is an argument, we call the generic Process method - var argument = arguments.First(); - var argumentType = argument.Type.GetGenericArguments()[0]; - var methodInfo = convolutionModule.GetMethods(BindingFlags.Public | BindingFlags.Instance).First(m => m.Name == "Process" && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == 1); - var genericMethodInfo = methodInfo.MakeGenericMethod(argumentType); - return Expression.Call( - Expression.Constant(ConvolutionModule, convolutionModule), - genericMethodInfo, - argument - ); - } - } } From a5f5d801d89cbbaf69e6a8d2d2df2e0028b13d0e Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 4 Dec 2025 15:35:47 +0000 Subject: [PATCH 17/30] Refactored sequential container to only support inputs instead of using a property --- .../NeuralNets/Container/Sequential.cs | 40 ------------------- 1 file changed, 40 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs index 3b5f498e..503189d3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs @@ -19,34 +19,11 @@ namespace Bonsai.ML.Torch.NeuralNets.Container; [WorkflowElementCategory(ElementCategory.Source)] public class Sequential { - /// - /// The modules to include in the sequential model. - /// - [XmlIgnore] - public Module[] Modules { get; set; } - /// /// The device on which to create the sequential model. /// [XmlIgnore] public Device? Device { get; set; } = null; - - /// - /// Generates an observable sequence that creates a sequential model from the specified modules. - /// - /// - public IObservable> Process() - { - return Observable.Defer(() => - { - var sequential = Sequential(Modules); - if (Device is not null && Device != CPU) - { - sequential.to(Device); - } - return Observable.Return(sequential); - }); - } /// /// Generates an observable sequence that creates a sequential model from the input modules. @@ -64,21 +41,4 @@ public IObservable> Process(IObservable - /// Generates an observable sequence of sequential models for each element in the input sequence. - /// - /// - public IObservable> Process(IObservable source) - { - return source.SelectMany(_ => - { - var sequential = Sequential(Modules); - if (Device is not null && Device != CPU) - { - sequential.to(Device); - } - return Observable.Return(sequential); - }); - } } \ No newline at end of file From 32ad8376dac01690594a83469ea0089bdab74a6a Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 4 Dec 2025 15:36:37 +0000 Subject: [PATCH 18/30] Added pooling module builder --- .../NeuralNets/Pooling/AdaptiveAvgPool1d.cs | 43 +++++++++++ .../NeuralNets/Pooling/AdaptiveAvgPool2d.cs | 44 +++++++++++ .../NeuralNets/Pooling/AdaptiveAvgPool3d.cs | 44 +++++++++++ .../NeuralNets/Pooling/AdaptiveMaxPool1d.cs | 43 +++++++++++ .../NeuralNets/Pooling/AdaptiveMaxPool2d.cs | 44 +++++++++++ .../NeuralNets/Pooling/AdaptiveMaxPool3d.cs | 44 +++++++++++ .../NeuralNets/Pooling/AvgPool1d.cs | 67 +++++++++++++++++ .../NeuralNets/Pooling/AvgPool2d.cs | 73 +++++++++++++++++++ .../NeuralNets/Pooling/AvgPool3d.cs | 73 +++++++++++++++++++ .../NeuralNets/Pooling/FractionalMaxPool2d.cs | 55 ++++++++++++++ .../NeuralNets/Pooling/MaxPool1d.cs | 67 +++++++++++++++++ .../NeuralNets/Pooling/MaxPool2d.cs | 67 +++++++++++++++++ .../NeuralNets/Pooling/MaxPool3d.cs | 67 +++++++++++++++++ .../NeuralNets/PoolingModuleBuilder.cs | 58 +++++++++++++++ 14 files changed, 789 insertions(+) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs new file mode 100644 index 00000000..a6822348 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 1D adaptive average pooling layer. +/// +[Description("Creates a 1D adaptive average pooling layer.")] +public class AdaptiveAvgPool1d +{ + /// + /// The output size. + /// + [Description("The output size")] + public long OutputSize { get; set; } + + /// + /// Creates an AdaptiveAvgPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveAvgPool1d(OutputSize)); + } + + /// + /// Creates an AdaptiveAvgPool1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AdaptiveAvgPool1d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs new file mode 100644 index 00000000..b21d0506 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D adaptive average pooling layer. +/// +[Description("Creates a 2D adaptive average pooling layer.")] +public class AdaptiveAvgPool2d +{ + /// + /// The output size. + /// + [Description("The output size")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Creates an AdaptiveAvgPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveAvgPool2d(OutputSize)); + } + + /// + /// Creates an AdaptiveAvgPool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AdaptiveAvgPool2d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs new file mode 100644 index 00000000..45d2478e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 3D adaptive average pooling layer. +/// +[Description("Creates a 3D adaptive average pooling layer.")] +public class AdaptiveAvgPool3d +{ + /// + /// The output size. + /// + [Description("The output size")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Creates an AdaptiveAvgPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveAvgPool3d(OutputSize)); + } + + /// + /// Creates an AdaptiveAvgPool3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AdaptiveAvgPool3d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs new file mode 100644 index 00000000..71628b4d --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 1D adaptive max pooling layer. +/// +[Description("Creates a 1D adaptive max pooling layer.")] +public class AdaptiveMaxPool1d +{ + /// + /// The output size. + /// + [Description("The output size")] + public long OutputSize { get; set; } + + /// + /// Creates an AdaptiveMaxPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveMaxPool1d(OutputSize)); + } + + /// + /// Creates an AdaptiveMaxPool1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AdaptiveMaxPool1d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs new file mode 100644 index 00000000..8d2b49fe --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D adaptive max pooling layer. +/// +[Description("Creates a 2D adaptive max pooling layer.")] +public class AdaptiveMaxPool2d +{ + /// + /// The output size. + /// + [Description("The output size")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Creates an AdaptiveMaxPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveMaxPool2d(OutputSize)); + } + + /// + /// Creates an AdaptiveMaxPool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AdaptiveMaxPool2d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs new file mode 100644 index 00000000..9e96f55a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 3D adaptive max pooling layer. +/// +[Description("Creates a 3D adaptive max pooling layer.")] +public class AdaptiveMaxPool3d +{ + /// + /// The output size. + /// + [Description("The output size")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] OutputSize { get; set; } + + /// + /// Creates an AdaptiveMaxPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(AdaptiveMaxPool3d(OutputSize)); + } + + /// + /// Creates an AdaptiveMaxPool3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AdaptiveMaxPool3d(OutputSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs new file mode 100644 index 00000000..391ca416 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 1D average pooling layer. +/// +[Description("Creates a 1D average pooling layer.")] +public class AvgPool1d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long KernelSize { get; set; } + + /// + /// The stride. + /// + [Description("The stride.")] + public long? Stride { get; set; } = null; + + /// + /// The padding. + /// + [Description("The padding.")] + public long Padding { get; set; } = 0; + + /// + /// The ceiling mode. + /// + [Description("The ceiling mode.")] + public bool CeilMode { get; set; } = false; + + /// + /// The count include pad parameter. + /// + [Description("The count include pad parameter.")] + public bool CountIncludePad { get; set; } = true; + + /// + /// Creates an AvgPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); + } + + /// + /// Creates an AvgPool1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs new file mode 100644 index 00000000..c5ea0556 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs @@ -0,0 +1,73 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D average pooling layer. +/// +[Description("Creates a 2D average pooling layer.")] +public class AvgPool2d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long[] KernelSize { get; set; } + + /// + /// The stride. + /// + [Description("The stride.")] + public long[] Stride { get; set; } = null; + + /// + /// The padding. + /// + [Description("The padding.")] + public long[] Padding { get; set; } = null; + + /// + /// The ceiling mode. + /// + [Description("The ceiling mode.")] + public bool CeilMode { get; set; } = false; + + /// + /// The count include pad parameter. + /// + [Description("The count include pad parameter.")] + public bool CountIncludePad { get; set; } = true; + + /// + /// The divisor override. + /// + [Description("The divisor override.")] + public long? DivisorOverride { get; set; } = null; + + /// + /// Creates an AvgPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(AvgPool2d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); + } + + /// + /// Creates an AvgPool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AvgPool2d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs new file mode 100644 index 00000000..25f56c25 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs @@ -0,0 +1,73 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 3D average pooling layer. +/// +[Description("Creates a 3D average pooling layer.")] +public class AvgPool3d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long[] KernelSize { get; set; } + + /// + /// The stride. + /// + [Description("The stride.")] + public long[] Stride { get; set; } = null; + + /// + /// The padding. + /// + [Description("The padding.")] + public long[] Padding { get; set; } = null; + + /// + /// The ceiling mode. + /// + [Description("The ceiling mode.")] + public bool CeilMode { get; set; } = false; + + /// + /// The count include pad parameter. + /// + [Description("The count include pad parameter.")] + public bool CountIncludePad { get; set; } = true; + + /// + /// The divisor override. + /// + [Description("The divisor override.")] + public long? DivisorOverride { get; set; } = null; + + /// + /// Creates an AvgPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(AvgPool3d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); + } + + /// + /// Creates an AvgPool3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AvgPool3d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs new file mode 100644 index 00000000..edbcb261 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D fractional max pooling layer. +/// +[Description("Creates a 2D fractional max pooling layer.")] +public class FractionalMaxPool2d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long KernelSize { get; set; } + + /// + /// The output size. + /// + [Description("The output size.")] + public long? OutputSize { get; set; } = null; + + /// + /// The output ratio. + /// + [Description("The output ratio.")] + public long? OutputRatio { get; set; } = null; + + /// + /// Creates a FractionalMaxPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); + } + + /// + /// Creates a FractionalMaxPool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs new file mode 100644 index 00000000..3888e489 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 1D max pooling layer. +/// +[Description("Creates a 1D max pooling layer.")] +public class MaxPool1d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long KernelSize { get; set; } + + /// + /// The stride. + /// + [Description("The stride.")] + public long? Stride { get; set; } = null; + + /// + /// The padding. + /// + [Description("The padding.")] + public long? Padding { get; set; } = null; + + /// + /// The dilation. + /// + [Description("The dilation.")] + public long? Dilation { get; set; } = null; + + /// + /// The ceiling mode. + /// + [Description("The ceiling mode.")] + public bool CeilMode { get; set; } = false; + + /// + /// Creates an MaxPool1d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } + + /// + /// Creates an MaxPool1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs new file mode 100644 index 00000000..518701ea --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D max pooling layer. +/// +[Description("Creates a 2D max pooling layer.")] +public class MaxPool2d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long[] KernelSize { get; set; } + + /// + /// The stride. + /// + [Description("The stride.")] + public long[] Stride { get; set; } = null; + + /// + /// The padding. + /// + [Description("The padding.")] + public long[] Padding { get; set; } = null; + + /// + /// The dilation. + /// + [Description("The dilation.")] + public long[] Dilation { get; set; } = null; + + /// + /// The ceiling mode. + /// + [Description("The ceiling mode.")] + public bool CeilMode { get; set; } = false; + + /// + /// Creates an MaxPool2d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } + + /// + /// Creates an MaxPool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs new file mode 100644 index 00000000..408f9222 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 3D max pooling layer. +/// +[Description("Creates a 3D max pooling layer.")] +public class MaxPool3d +{ + /// + /// The kernel size. + /// + [Description("The kernel size.")] + public long[] KernelSize { get; set; } + + /// + /// The stride. + /// + [Description("The stride.")] + public long[] Stride { get; set; } = null; + + /// + /// The padding. + /// + [Description("The padding.")] + public long[] Padding { get; set; } = null; + + /// + /// The dilation. + /// + [Description("The dilation.")] + public long[] Dilation { get; set; } = null; + + /// + /// The ceiling mode. + /// + [Description("The ceiling mode.")] + public bool CeilMode { get; set; } = false; + + /// + /// Creates an MaxPool3d module. + /// + public IObservable> Process() + { + return Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } + + /// + /// Creates an MaxPool3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs new file mode 100644 index 00000000..4fa81ac2 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using Bonsai.Expressions; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using System.Reflection; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a torch module for pooling operations. +/// +[XmlInclude(typeof(Pooling.AdaptiveAvgPool1d))] +[XmlInclude(typeof(Pooling.AdaptiveAvgPool2d))] +[XmlInclude(typeof(Pooling.AdaptiveAvgPool3d))] +[XmlInclude(typeof(Pooling.AdaptiveMaxPool1d))] +[XmlInclude(typeof(Pooling.AdaptiveMaxPool2d))] +[XmlInclude(typeof(Pooling.AdaptiveMaxPool3d))] +[DefaultProperty(nameof(PoolingModule))] +[Combinator] +[Description("Creates a torch module for pooling operations.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PoolingModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public PoolingModuleBuilder() + { + Module = new Pooling.AdaptiveAvgPool1d(); + } + + /// + /// Gets or sets the specific pooling module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific pooling module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object PoolingModule + { + get => Module; + set => Module = value; + } +} From 2d2a1a50c4c16198480538baaed65e28bf79da84 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Mon, 8 Dec 2025 17:01:06 +0000 Subject: [PATCH 19/30] Updated modules to support module combinator builder pattern --- .../NeuralNets/ActivationFunctionBuilder.cs | 72 +++++++++ src/Bonsai.ML.Torch/NeuralNets/Backward.cs | 9 -- .../NeuralNets/CollectParameters.cs | 14 +- .../NeuralNets/Container/Sequential.cs | 31 +--- .../NeuralNets/Convolution/Conv1d.cs | 53 +++--- .../NeuralNets/Convolution/Conv2d.cs | 65 ++++---- .../NeuralNets/Convolution/Conv3d.cs | 67 ++++---- .../NeuralNets/Convolution/ConvTranspose1d.cs | 51 +++--- .../NeuralNets/Convolution/ConvTranspose2d.cs | 64 ++++---- .../NeuralNets/Convolution/ConvTranspose3d.cs | 66 ++++---- .../NeuralNets/Convolution/Convolution.cs | 153 ------------------ .../NeuralNets/Convolution/Fold.cs | 47 +++--- .../NeuralNets/Convolution/Unfold.cs | 40 ++--- .../NeuralNets/ConvolutionModuleBuilder.cs | 11 -- src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs | 27 ---- .../NeuralNets/Distance/CosineSimilarity.cs | 49 ++++++ .../Distance/CosineSimilarityModule.cs | 40 ----- .../NeuralNets/Distance/PairwiseDistance.cs | 55 +++++++ .../Distance/PairwiseDistanceModule.cs | 46 ------ .../NeuralNets/DistanceModuleBuilder.cs | 43 +++++ .../NeuralNets/Dropout/AlphaDropout.cs | 37 +++-- .../NeuralNets/Dropout/Dropout.cs | 50 +++--- .../NeuralNets/Dropout/Dropout1d.cs | 49 ++++++ .../NeuralNets/Dropout/Dropout2d.cs | 49 ++++++ .../NeuralNets/Dropout/Dropout3d.cs | 49 ++++++ .../NeuralNets/Dropout/FeatureAlphaDropout.cs | 37 +++-- .../NeuralNets/DropoutModuleBuilder.cs | 47 ++++++ .../NeuralNets/Flatten/Flatten.cs | 33 ++-- .../NeuralNets/Flatten/Unflatten.cs | 35 ++-- .../NeuralNets/FlattenModuleBuilder.cs | 43 +++++ src/Bonsai.ML.Torch/NeuralNets/Forward.cs | 37 ++--- .../LearningRateScheduler/Constant.cs | 46 +++--- .../LearningRateScheduler/CosineAnnealing.cs | 38 ++--- .../LearningRateScheduler/Exponential.cs | 40 ++--- .../LearningRateScheduler/Linear.cs | 48 +++--- .../LearningRateScheduler/MultiStep.cs | 48 +++--- .../LearningRateScheduler/OneCycle.cs | 97 ++++++----- .../LearningRateScheduler/Polynomial.cs | 44 +++-- .../LearningRateScheduler/ReduceOnPlateau.cs | 92 ++++++----- .../NeuralNets/LearningRateScheduler/Step.cs | 48 +++--- .../LearningRateSchedulerBuilder.cs | 52 ++++++ .../Linear/{BilinearModule.cs => Bilinear.cs} | 44 +++-- .../NeuralNets/Linear/Identity.cs | 27 ++-- .../NeuralNets/Linear/Linear.cs | 39 +++-- .../NeuralNets/LinearModuleBuilder.cs | 44 +++++ src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs | 68 +++++++- .../NeuralNets/LoadModuleArchitecture.cs | 115 +++++++------ .../NeuralNets/LoadScriptModule.cs | 19 ++- .../NeuralNets/Loss/BinaryCrossEntropy.cs | 51 ++++-- .../Loss/BinaryCrossEntropyWithLogits.cs | 66 ++++++-- .../ConnectionistTemporalClassification.cs | 41 +++-- .../NeuralNets/Loss/CosineEmbedding.cs | 37 +++-- .../NeuralNets/Loss/CrossEntropy.cs | 65 ++++++-- .../Loss/GaussianNegativeLogLikelihood.cs | 41 +++-- .../NeuralNets/Loss/HingeEmbedding.cs | 39 +++-- src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs | 37 +++-- .../Loss/KullbackLeiblerDivergence.cs | 37 +++-- .../NeuralNets/Loss/MarginRanking.cs | 37 +++-- .../NeuralNets/Loss/MeanAbsoluteError.cs | 35 ++-- .../NeuralNets/Loss/MeanSquaredError.cs | 31 ++-- .../NeuralNets/Loss/MultiClassMargin.cs | 83 ++++++++++ .../NeuralNets/Loss/MultiLabelMargin.cs | 33 ++-- .../NeuralNets/Loss/MultiLabelSoftMargin.cs | 63 ++++++-- .../NeuralNets/Loss/MultiMarginLoss.cs | 52 ------ .../NeuralNets/Loss/NegativeLogLikelihood.cs | 59 +++++-- .../Loss/PoissonNegativeLogLikelihood.cs | 45 +++--- .../NeuralNets/Loss/SmoothL1.cs | 37 +++-- .../NeuralNets/Loss/SoftMargin.cs | 33 ++-- .../NeuralNets/Loss/TripletMargin.cs | 49 +++--- src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs | 63 ++++++++ .../ContinuouslyDifferentiableExponential.cs | 49 ++++++ ...uslyDifferentiableExponentialLinearUnit.cs | 40 ----- .../NonLinearActivations/Exponential.cs | 49 ++++++ .../ExponentialLinearUnit.cs | 40 ----- .../NeuralNets/NonLinearActivations/Gated.cs | 43 +++++ .../NonLinearActivations/GatedLinearUnit.cs | 34 ---- .../NonLinearActivations/GaussianError.cs | 43 +++++ .../GaussianErrorLinearUnit.cs | 34 ---- .../NonLinearActivations/HardShrinkage.cs | 29 ++-- .../NonLinearActivations/HardSigmoid.cs | 25 ++- .../NonLinearActivations/HardTanh.cs | 35 ++-- .../{HardSwish.cs => Hardswish.cs} | 27 ++-- .../NonLinearActivations/LeakyRectified.cs | 49 ++++++ .../LeakyRectifiedLinearUnit.cs | 40 ----- .../NonLinearActivations/LogSigmoid.cs | 27 ++-- .../NonLinearActivations/LogSoftmax.cs | 33 ++-- .../NeuralNets/NonLinearActivations/Mish.cs | 27 ++-- .../MultiheadAttention.cs | 85 ++++++++++ .../MultiheadAttentionModule.cs | 76 --------- .../ParametricRectified.cs | 64 ++++++++ .../ParametricRectifiedLinearUnit.cs | 54 ------- .../RandomizedLeakyRectified.cs | 55 +++++++ .../RandomizedLeakyRectifiedLinearUnit.cs | 46 ------ .../NonLinearActivations/Rectified.cs | 43 +++++ .../NonLinearActivations/RectifiedBounded.cs | 43 +++++ .../RectifiedLinearUnit.cs | 43 ----- .../NonLinearActivations/ScaledExponential.cs | 43 +++++ .../ScaledExponentialLinearUnit.cs | 34 ---- .../NonLinearActivations/Sigmoid.cs | 29 ++-- .../NonLinearActivations/SigmoidWeighted.cs | 37 +++++ .../SigmoidWeightedLinearUnit.cs | 28 ---- .../NonLinearActivations/SoftMax.cs | 34 ---- .../NonLinearActivations/SoftMax2D.cs | 28 ---- .../NonLinearActivations/SoftMin.cs | 34 ---- .../NonLinearActivations/SoftPlus.cs | 40 ----- .../NonLinearActivations/SoftShrinkage.cs | 31 ++-- .../NonLinearActivations/SoftSign.cs | 28 ---- .../NonLinearActivations/Softmax.cs | 43 +++++ .../NonLinearActivations/Softmax2D.cs | 37 +++++ .../NonLinearActivations/Softmin.cs | 43 +++++ .../NonLinearActivations/Softplus.cs | 49 ++++++ .../NonLinearActivations/Softsign.cs | 37 +++++ .../NeuralNets/NonLinearActivations/Tanh.cs | 27 ++-- .../NonLinearActivations/TanhShrinkage.cs | 27 ++-- .../NonLinearActivations/Threshold.cs | 37 +++-- .../NeuralNets/Normalization/BatchNorm1d.cs | 81 ++++++++++ .../NeuralNets/Normalization/BatchNorm2d.cs | 81 ++++++++++ .../NeuralNets/Normalization/BatchNorm3d.cs | 81 ++++++++++ .../Normalization/BatchNormalization.cs | 84 ---------- .../NeuralNets/Normalization/GroupNorm.cs | 75 +++++++++ .../Normalization/GroupNormalization.cs | 66 -------- .../Normalization/InstanceNorm1d.cs | 81 ++++++++++ .../Normalization/InstanceNorm2d.cs | 81 ++++++++++ .../Normalization/InstanceNorm3d.cs | 81 ++++++++++ .../Normalization/InstanceNormalization.cs | 84 ---------- .../NeuralNets/Normalization/LayerNorm.cs | 76 +++++++++ .../Normalization/LayerNormalization.cs | 67 -------- .../Normalization/LocalResponseNorm.cs | 61 +++++++ .../LocalResponseNormalization.cs | 52 ------ .../NeuralNets/NormalizationModuleBuilder.cs | 50 ++++++ .../NeuralNets/OptimizationStep.cs | 20 +-- .../NeuralNets/Optimizer/AdaDelta.cs | 66 -------- .../NeuralNets/Optimizer/AdaGrad.cs | 66 -------- .../NeuralNets/Optimizer/AdaMax.cs | 66 -------- .../NeuralNets/Optimizer/Adadelta.cs | 60 +++++++ .../NeuralNets/Optimizer/Adagrad.cs | 60 +++++++ .../NeuralNets/Optimizer/Adam.cs | 62 ++++--- .../NeuralNets/Optimizer/AdamW.cs | 62 ++++--- .../NeuralNets/Optimizer/Adamax.cs | 60 +++++++ .../NeuralNets/Optimizer/Asgd.cs | 72 --------- .../AveragedStochasticGradientDescent.cs | 66 ++++++++ .../NeuralNets/Optimizer/Lbfgs.cs | 56 +++---- .../NeuralNets/Optimizer/ResilientBackprop.cs | 72 --------- .../Optimizer/ResilientBackpropagation.cs | 66 ++++++++ .../NeuralNets/Optimizer/RmsPropogation.cs | 78 --------- .../Optimizer/RootMeanSquarePropagation.cs | 72 +++++++++ .../NeuralNets/Optimizer/Sgd.cs | 72 --------- .../Optimizer/StochasticGradientDescent.cs | 66 ++++++++ .../NeuralNets/OptimizerBuilder.cs | 53 ++++++ .../NeuralNets/Padding/ConstantPad1d.cs | 50 ++++++ .../NeuralNets/Padding/ConstantPad2d.cs | 50 ++++++ .../NeuralNets/Padding/ConstantPad3d.cs | 50 ++++++ .../NeuralNets/Padding/Padding.cs | 83 ---------- .../NeuralNets/Padding/PaddingMode.cs | 27 ---- .../NeuralNets/Padding/ReflectionPad1d.cs | 44 +++++ .../NeuralNets/Padding/ReflectionPad2d.cs | 44 +++++ .../NeuralNets/Padding/ReflectionPad3d.cs | 44 +++++ .../NeuralNets/Padding/ReplicationPad1d.cs | 44 +++++ .../NeuralNets/Padding/ReplicationPad2d.cs | 44 +++++ .../NeuralNets/Padding/ReplicationPad3d.cs | 44 +++++ .../NeuralNets/Padding/ZeroPad2d.cs | 44 +++++ .../NeuralNets/PaddingModuleBuilder.cs | 51 ++++++ .../Pooling/AdaptiveAveragePooling.cs | 46 ------ .../NeuralNets/Pooling/AdaptiveAvgPool1d.cs | 9 +- .../NeuralNets/Pooling/AdaptiveAvgPool2d.cs | 13 +- .../NeuralNets/Pooling/AdaptiveAvgPool3d.cs | 17 +- .../NeuralNets/Pooling/AdaptiveMaxPool1d.cs | 13 +- .../NeuralNets/Pooling/AdaptiveMaxPool2d.cs | 9 +- .../NeuralNets/Pooling/AdaptiveMaxPool3d.cs | 9 +- .../NeuralNets/Pooling/AdaptiveMaxPooling.cs | 47 ------ .../NeuralNets/Pooling/AveragePooling.cs | 47 ------ .../NeuralNets/Pooling/AvgPool1d.cs | 28 ++-- .../NeuralNets/Pooling/AvgPool2d.cs | 41 +++-- .../NeuralNets/Pooling/AvgPool3d.cs | 41 +++-- .../NeuralNets/Pooling/FractionalMaxPool2d.cs | 29 ++-- .../NeuralNets/Pooling/FractionalMaxPool3d.cs | 58 +++++++ .../Pooling/FractionalMaxPooling.cs | 57 ------- .../NeuralNets/Pooling/LPPool1d.cs | 65 ++++++++ .../NeuralNets/Pooling/LPPool2d.cs | 67 ++++++++ .../NeuralNets/Pooling/MaxPool1d.cs | 32 ++-- .../NeuralNets/Pooling/MaxPool2d.cs | 44 ++--- .../NeuralNets/Pooling/MaxPool3d.cs | 44 ++--- .../NeuralNets/Pooling/MaxPooling.cs | 71 -------- .../NeuralNets/Pooling/MaxUnpool1d.cs | 59 +++++++ .../NeuralNets/Pooling/MaxUnpool2d.cs | 62 +++++++ .../NeuralNets/Pooling/MaxUnpool3d.cs | 62 +++++++ .../NeuralNets/Pooling/MaxUnpoolModule.cs | 58 ------- .../NeuralNets/Pooling/PowerAveragePooling.cs | 63 -------- .../NeuralNets/PoolingModuleBuilder.cs | 28 ++-- .../NeuralNets/Recurrent/GRUCellModule.cs | 59 ------- .../NeuralNets/Recurrent/GRUModule.cs | 83 ---------- .../Recurrent/GatedRecurrentUnit.cs | 93 +++++++++++ .../Recurrent/GatedRecurrentUnitCell.cs | 69 ++++++++ .../NeuralNets/Recurrent/LSTMCellModule.cs | 59 ------- .../NeuralNets/Recurrent/LSTMModule.cs | 83 ---------- .../Recurrent/LongShortTermMemory.cs | 93 +++++++++++ .../Recurrent/LongShortTermMemoryCell.cs | 69 ++++++++ .../NeuralNets/Recurrent/RNNCellModule.cs | 66 -------- .../NeuralNets/Recurrent/RNNModule.cs | 89 ---------- .../Recurrent/RecurrentNeuralNetwork.cs | 99 ++++++++++++ .../Recurrent/RecurrentNeuralNetworkCell.cs | 75 +++++++++ .../NeuralNets/RecurrentModuleBuilder.cs | 47 ++++++ src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs | 26 +-- .../NeuralNets/SequentialBuilder.cs | 45 ++++++ .../NeuralNets/SetTrainingMode.cs | 26 +-- .../NeuralNets/Shuffle/ChannelShuffle.cs | 31 ++-- .../NeuralNets/ShuffleModuleBuilder.cs | 42 +++++ .../NeuralNets/Sparse/Embedding.cs | 63 ++++---- .../NeuralNets/Sparse/EmbeddingBag.cs | 106 ++++++++++++ .../Sparse/EmbeddingBagFromPretrained.cs | 121 ++++++++++++++ .../NeuralNets/Sparse/EmbeddingBagModule.cs | 95 ----------- .../Sparse/EmbeddingFromPretrained.cs | 76 +++++---- .../EmbeddingbagFromPretrainedModule.cs | 97 ----------- .../NeuralNets/SparseModuleBuilder.cs | 45 ++++++ .../NeuralNets/Transformer/Transformer.cs | 79 +++++++++ .../Transformer/TransformerDecoder.cs | 35 ++++ .../Transformer/TransformerDecoderLayer.cs | 67 ++++++++ .../TransformerDecoderLayerModule.cs | 58 ------- .../Transformer/TransformerDecoderModule.cs | 42 ----- .../Transformer/TransformerEncoder.cs | 35 ++++ .../Transformer/TransformerEncoderLayer.cs | 67 ++++++++ .../TransformerEncoderLayerModule.cs | 58 ------- .../Transformer/TransformerEncoderModule.cs | 41 ----- .../Transformer/TransformerModule.cs | 70 -------- .../NeuralNets/TransformerModuleBuilder.cs | 46 ++++++ .../NeuralNets/Vision/PixelShuffle.cs | 30 ++-- .../NeuralNets/Vision/PixelUnshuffle.cs | 31 ++-- .../NeuralNets/Vision/Upsample.cs | 59 ++++--- .../NeuralNets/VisionModuleBuilder.cs | 44 +++++ .../NeuralNets/ZeroGradient.cs | 7 +- 230 files changed, 7015 insertions(+), 4756 deletions(-) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs rename src/Bonsai.ML.Torch/NeuralNets/Linear/{BilinearModule.cs => Bilinear.cs} (51%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs rename src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/{HardSwish.cs => Hardswish.cs} (51%) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs new file mode 100644 index 00000000..003a3c4a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs @@ -0,0 +1,72 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates an activation function. +/// +[XmlInclude(typeof(NonLinearActivations.ContinuouslyDifferentiableExponential))] +[XmlInclude(typeof(NonLinearActivations.Exponential))] +[XmlInclude(typeof(NonLinearActivations.Gated))] +[XmlInclude(typeof(NonLinearActivations.GaussianError))] +[XmlInclude(typeof(NonLinearActivations.HardShrinkage))] +[XmlInclude(typeof(NonLinearActivations.HardSigmoid))] +[XmlInclude(typeof(NonLinearActivations.Hardswish))] +[XmlInclude(typeof(NonLinearActivations.HardTanh))] +[XmlInclude(typeof(NonLinearActivations.LeakyRectified))] +[XmlInclude(typeof(NonLinearActivations.LogSigmoid))] +[XmlInclude(typeof(NonLinearActivations.LogSoftmax))] +[XmlInclude(typeof(NonLinearActivations.Mish))] +[XmlInclude(typeof(NonLinearActivations.MultiheadAttention))] +[XmlInclude(typeof(NonLinearActivations.ParametricRectified))] +[XmlInclude(typeof(NonLinearActivations.RandomizedLeakyRectified))] +[XmlInclude(typeof(NonLinearActivations.Rectified))] +[XmlInclude(typeof(NonLinearActivations.RectifiedBounded))] +[XmlInclude(typeof(NonLinearActivations.ScaledExponential))] +[XmlInclude(typeof(NonLinearActivations.Sigmoid))] +[XmlInclude(typeof(NonLinearActivations.SigmoidWeighted))] +[XmlInclude(typeof(NonLinearActivations.Softmax))] +[XmlInclude(typeof(NonLinearActivations.Softmax2d))] +[XmlInclude(typeof(NonLinearActivations.Softmin))] +[XmlInclude(typeof(NonLinearActivations.Softplus))] +[XmlInclude(typeof(NonLinearActivations.SoftShrinkage))] +[XmlInclude(typeof(NonLinearActivations.Softsign))] +[XmlInclude(typeof(NonLinearActivations.Tanh))] +[XmlInclude(typeof(NonLinearActivations.TanhShrinkage))] +[XmlInclude(typeof(NonLinearActivations.Threshold))] +[DefaultProperty(nameof(ActivationFunction))] +[Combinator] +[Description("Creates an activation function.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ActivationFunctionBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + string INamedElement.Name => $"ActivationFunction.{GetElementDisplayName(ActivationFunction)}"; + + /// + /// Initializes a new instance of the class. + /// + public ActivationFunctionBuilder() + { + Module = new NonLinearActivations.Rectified(); + } + + /// + /// Gets or sets the specific activation function to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific activation function to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object ActivationFunction + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs index 342b6dc6..7468a210 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Backward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Backward.cs @@ -1,15 +1,7 @@ using System; -using System.Linq; using System.ComponentModel; -using System.Reactive; -using System.Reactive.Disposables; using System.Reactive.Linq; -using System.Xml.Serialization; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; -using static TorchSharp.torch.optim.lr_scheduler; -using System.Threading.Tasks; namespace Bonsai.ML.Torch.NeuralNets; @@ -17,7 +9,6 @@ namespace Bonsai.ML.Torch.NeuralNets; /// Represents an operator that computes backward on the input tensor. /// [Combinator] -[ResetCombinator] [Description("Computes backward on the input tensor.")] [WorkflowElementCategory(ElementCategory.Sink)] public class Backward diff --git a/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs b/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs index 884d3627..3b33577f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/CollectParameters.cs @@ -1,14 +1,10 @@ -using Bonsai.Expressions; using System; using System.ComponentModel; using System.Reactive.Linq; using System.Linq; -using System.Linq.Expressions; using System.Collections.Generic; -using System.Reflection; -using TorchSharp; using TorchSharp.Modules; -using static TorchSharp.torch; +using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets; @@ -23,9 +19,13 @@ public class CollectParameters /// /// Collects the parameters from torch modules into a collection. /// - public IObservable> Process(params IObservable[] sources) + /// + /// + public IObservable> Process(params IObservable[] sources) { - return Observable.Concat(sources) + return Observable + .Concat(sources.Select(source => + source.Take(1))) .SelectMany(module => { return module.parameters(recurse: true); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs index 503189d3..b1d35592 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs @@ -1,44 +1,27 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using System.Reactive.Linq; -using TorchSharp; using static TorchSharp.torch; using static TorchSharp.torch.nn; -using System.Xml.Serialization; using System.Linq; namespace Bonsai.ML.Torch.NeuralNets.Container; /// -/// Creates a sequential model from the specified modules. +/// Represents an operator that creates a sequential container. /// -[Combinator] -[ResetCombinator] -[Description("Creates a sequential model from the specified modules.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a sequential container.")] public class Sequential { /// - /// The device on which to create the sequential model. - /// - [XmlIgnore] - public Device? Device { get; set; } = null; - - /// - /// Generates an observable sequence that creates a sequential model from the input modules. + /// Creates a sequential container from the input modules. /// /// public IObservable> Process(IObservable[]> source) { - return source.SelectMany(modules => - { - var sequential = Sequential(modules); - if (Device is not null && Device != CPU) - { - sequential.to(Device); - } - return Observable.Return(sequential); - }); + return source.Select(modules => Sequential(modules)); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs index aa658c40..2200d94a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs @@ -1,10 +1,8 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,61 +11,58 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates a 1D convolution module. /// +/// +/// See for more information. +/// [Description("Creates a 1D convolution module.")] public class Conv1d { /// - /// The in_channels parameter for the Conv1d module. + /// The number of input channels in the input tensor. /// - [Description("The in_channels parameter for the Conv1d module")] + [Description("The number of input channels in the input tensor.")] public long InChannels { get; set; } /// - /// The out_channels parameter for the Conv1d module. + /// The number of output channels produced by the convolution. /// - [Description("The out_channels parameter for the Conv1d module")] + [Description("The number of output channels produced by the convolution.")] public long OutChannels { get; set; } /// - /// The kernelsize parameter for the Conv1d module. + /// The size of the convolution kernel. /// - [Description("The kernelsize parameter for the Conv1d module")] + [Description("The size of the convolution kernel.")] public long KernelSize { get; set; } /// - /// The stride parameter for the Conv1d module. + /// The stride of the convolution. /// - [Description("The stride parameter for the Conv1d module")] + [Description("The stride of the convolution.")] public long Stride { get; set; } = 1; /// - /// The padding parameter for the Conv1d module. + /// The padding added to both sides of the input. /// - [Description("The padding parameter for the Conv1d module")] + [Description("The padding added to both sides of the input.")] public long Padding { get; set; } = 0; /// - /// The output_padding parameter for the Conv1d module. + /// The spacing between kernel elements. /// - [Description("The output_padding parameter for the ConvTransposed1d module")] - public long OutputPadding { get; set; } = 0; - - /// - /// The dilation parameter for the Conv1d module. - /// - [Description("The dilation parameter for the Conv1d module")] + [Description("The spacing between kernel elements.")] public long Dilation { get; set; } = 1; /// - /// The padding_mode parameter for the Conv1d module. + /// The mode of padding. /// - [Description("The padding_mode parameter for the Conv1d module")] + [Description("The mode of padding.")] public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; /// - /// The groups parameter for the Conv1d module. + /// The number of blocked connections from input channels to output channels. /// - [Description("The groups parameter for the Conv1d module")] + [Description("The number of blocked connections from input channels to output channels.")] public long Groups { get; set; } = 1; /// @@ -77,21 +72,22 @@ public class Conv1d public bool Bias { get; set; } = true; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] + [Description("The desired data type of the returned tensor")] public ScalarType? Type { get; set; } = null; /// /// Creates a Conv1d module. /// + /// public IObservable> Process() { return Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); @@ -103,7 +99,6 @@ public IObservable> Process() /// /// /// - /// public IObservable> Process(IObservable source) { return source.Select(_ => Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs index 2266ef5f..0bdcedd9 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs @@ -1,10 +1,8 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,61 +11,62 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates a 2D convolution module. /// +/// +/// See for more information. +/// [Description("Creates a 2D convolution module.")] public class Conv2d { /// - /// The in_channels parameter for the Conv2d module. + /// The number of input channels in the input tensor. /// - [Description("The in_channels parameter for the Conv2d module")] + [Description("The number of input channels in the input tensor.")] public long InChannels { get; set; } /// - /// The out_channels parameter for the Conv1d module. + /// The number of output channels produced by the convolution. /// - [Description("The out_channels parameter for the Conv1d module")] + [Description("The number of output channels produced by the convolution.")] public long OutChannels { get; set; } /// - /// The kernelsize parameter for the Conv1d module. + /// The size of the convolution kernel. /// - [Description("The kernelsize parameter for the Conv1d module")] - public long KernelSize { get; set; } + [Description("The size of the convolution kernel.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// - /// The stride parameter for the Conv1d module. + /// The stride of the convolution. /// - [Description("The stride parameter for the Conv1d module")] - public long Stride { get; set; } = 1; + [Description("The stride of the convolution.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; /// - /// The padding parameter for the Conv1d module. + /// The padding added to all four sides of the input. /// - [Description("The padding parameter for the Conv1d module")] - public long Padding { get; set; } = 0; + [Description("The padding added to all four sides of the input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; /// - /// The output_padding parameter for the Conv1d module. + /// The spacing between kernel elements. /// - [Description("The output_padding parameter for the ConvTransposed1d module")] - public long OutputPadding { get; set; } = 0; + [Description("The spacing between kernel elements.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Dilation { get; set; } = null; /// - /// The dilation parameter for the Conv1d module. + /// The mode of padding. /// - [Description("The dilation parameter for the Conv1d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the Conv1d module. - /// - [Description("The padding_mode parameter for the Conv1d module")] + [Description("The mode of padding.")] public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; /// - /// The groups parameter for the Conv1d module. + /// The number of blocked connections from input channels to output channels. /// - [Description("The groups parameter for the Conv1d module")] + [Description("The number of blocked connections from input channels to output channels.")] public long Groups { get; set; } = 1; /// @@ -77,21 +76,22 @@ public class Conv2d public bool Bias { get; set; } = true; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] + [Description("The desired data type of the returned tensor")] public ScalarType? Type { get; set; } = null; /// /// Creates a Conv2d module. /// + /// public IObservable> Process() { return Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); @@ -103,7 +103,6 @@ public IObservable> Process() /// /// /// - /// public IObservable> Process(IObservable source) { return source.Select(_ => Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs index 698e86ff..3c248171 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs @@ -1,10 +1,8 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,61 +11,62 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates a 3D convolution module. /// +/// +/// See for more information. +/// [Description("Creates a 3D convolution module.")] public class Conv3d { /// - /// The in_channels parameter for the Conv3d module. + /// The number of input channels in the input tensor. /// - [Description("The in_channels parameter for the Conv3d module")] + [Description("The number of input channels in the input tensor.")] public long InChannels { get; set; } /// - /// The out_channels parameter for the Conv1d module. + /// The number of output channels produced by the convolution. /// - [Description("The out_channels parameter for the Conv1d module")] + [Description("The number of output channels produced by the convolution.")] public long OutChannels { get; set; } /// - /// The kernelsize parameter for the Conv1d module. + /// The size of the convolution kernel. /// - [Description("The kernelsize parameter for the Conv1d module")] - public long KernelSize { get; set; } + [Description("The size of the convolution kernel.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) KernelSize { get; set; } /// - /// The stride parameter for the Conv1d module. + /// The stride of the convolution. /// - [Description("The stride parameter for the Conv1d module")] - public long Stride { get; set; } = 1; + [Description("The stride of the convolution.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Stride { get; set; } = null; /// - /// The padding parameter for the Conv1d module. + /// The padding to add to all six sides of the input. /// - [Description("The padding parameter for the Conv1d module")] - public long Padding { get; set; } = 0; + [Description("The padding to add to all six sides of the input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Padding { get; set; } = null; /// - /// The output_padding parameter for the Conv1d module. + /// The spacing between kernel elements. /// - [Description("The output_padding parameter for the ConvTransposed1d module")] - public long OutputPadding { get; set; } = 0; + [Description("The spacing between kernel elements.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Dilation { get; set; } = null; /// - /// The dilation parameter for the Conv1d module. + /// The mode of padding. /// - [Description("The dilation parameter for the Conv1d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the Conv1d module. - /// - [Description("The padding_mode parameter for the Conv1d module")] + [Description("The mode of padding.")] public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; /// - /// The groups parameter for the Conv1d module. + /// The number of blocked connections from input channels to output channels. /// - [Description("The groups parameter for the Conv1d module")] + [Description("The number of blocked connections from input channels to output channels.")] public long Groups { get; set; } = 1; /// @@ -77,21 +76,22 @@ public class Conv3d public bool Bias { get; set; } = true; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] + [Description("The desired data type of the returned tensor")] public ScalarType? Type { get; set; } = null; /// /// Creates a Conv3d module. /// + /// public IObservable> Process() { return Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); @@ -103,9 +103,8 @@ public IObservable> Process() /// /// /// - /// public IObservable> Process(IObservable source) { - return source.Select(_ => Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); + return source.Select(_ => Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs index b09bf7ce..f0e36a7d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs @@ -1,10 +1,8 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,61 +11,64 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates a 1D transposed convolution module. /// +/// +/// See for more information. +/// [Description("Creates a 1D transposed convolution module.")] public class ConvTranspose1d { /// - /// The in_channels parameter for the Conv1d module. + /// The number of input channels in the input tensor. /// - [Description("The in_channels parameter for the Conv1d module")] + [Description("The number of input channels in the input tensor.")] public long InChannels { get; set; } /// - /// The out_channels parameter for the Conv1d module. + /// The number of output channels produced by the convolution. /// - [Description("The out_channels parameter for the Conv1d module")] + [Description("The number of output channels produced by the convolution.")] public long OutChannels { get; set; } /// - /// The kernelsize parameter for the Conv1d module. + /// The size of the convolution kernel. /// - [Description("The kernelsize parameter for the Conv1d module")] + [Description("The size of the convolution kernel.")] public long KernelSize { get; set; } /// - /// The stride parameter for the Conv1d module. + /// The stride of the convolution. /// - [Description("The stride parameter for the Conv1d module")] + [Description("The stride of the convolution.")] public long Stride { get; set; } = 1; /// - /// The padding parameter for the Conv1d module. + /// The padding to add to both sides of the input. /// - [Description("The padding parameter for the Conv1d module")] + [Description("The padding to add to both sides of the input.")] public long Padding { get; set; } = 0; /// - /// The output_padding parameter for the Conv1d module. + /// The additional size added to one side of the output shape. /// - [Description("The output_padding parameter for the ConvTransposed1d module")] + [Description("The additional size added to one side of the output shape.")] public long OutputPadding { get; set; } = 0; /// - /// The dilation parameter for the Conv1d module. + /// The spacing between kernel elements. /// - [Description("The dilation parameter for the Conv1d module")] + [Description("The spacing between kernel elements.")] public long Dilation { get; set; } = 1; /// - /// The padding_mode parameter for the Conv1d module. + /// The mode of padding. /// - [Description("The padding_mode parameter for the Conv1d module")] + [Description("The mode of padding.")] public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; /// - /// The groups parameter for the Conv1d module. + /// The number of blocked connections from input channels to output channels. /// - [Description("The groups parameter for the Conv1d module")] + [Description("The number of blocked connections from input channels to output channels.")] public long Groups { get; set; } = 1; /// @@ -77,21 +78,22 @@ public class ConvTranspose1d public bool Bias { get; set; } = true; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] + [Description("The desired data type of the returned tensor")] public ScalarType? Type { get; set; } = null; /// /// Creates a ConvTranspose1d module. /// + /// public IObservable> Process() { return Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); @@ -103,7 +105,6 @@ public IObservable> Process() /// /// /// - /// public IObservable> Process(IObservable source) { return source.Select(_ => ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs index 7ed36a6f..40edae5c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs @@ -1,10 +1,8 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,61 +11,69 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates a 2D transposed convolution module. /// +/// +/// See for more information. +/// [Description("Creates a 2D transposed convolution module.")] public class ConvTranspose2d { /// - /// The in_channels parameter for the Conv1d module. + /// The number of input channels in the input tensor. /// - [Description("The in_channels parameter for the Conv1d module")] + [Description("The number of input channels in the input tensor.")] public long InChannels { get; set; } /// - /// The out_channels parameter for the Conv1d module. + /// The number of output channels produced by the convolution. /// - [Description("The out_channels parameter for the Conv1d module")] + [Description("The number of output channels produced by the convolution.")] public long OutChannels { get; set; } /// - /// The kernelsize parameter for the Conv1d module. + /// The size of the convolution kernel. /// - [Description("The kernelsize parameter for the Conv1d module")] - public long KernelSize { get; set; } + [Description("The size of the convolution kernel.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// - /// The stride parameter for the Conv1d module. + /// The stride of the convolution. /// - [Description("The stride parameter for the Conv1d module")] - public long Stride { get; set; } = 1; + [Description("The stride of the convolution.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; /// - /// The padding parameter for the Conv1d module. + /// The zero-padding added to both sides of each dimension in the input. /// - [Description("The padding parameter for the Conv1d module")] - public long Padding { get; set; } = 0; + [Description("Zero-padding added to both sides of each dimension in the input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; /// - /// The output_padding parameter for the Conv1d module. + /// The additional size added to one side of each dimension in the output shape. /// - [Description("The output_padding parameter for the ConvTransposed1d module")] - public long OutputPadding { get; set; } = 0; + [Description("The additional size added to one side of each dimension in the output shape.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? OutputPadding { get; set; } = null; /// - /// The dilation parameter for the Conv1d module. + /// The spacing between kernel elements. /// [Description("The dilation parameter for the Conv1d module")] - public long Dilation { get; set; } = 1; + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Dilation { get; set; } = null; /// - /// The padding_mode parameter for the Conv1d module. + /// The mode of padding. /// - [Description("The padding_mode parameter for the Conv1d module")] + [Description("The mode of padding.")] public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; /// - /// The groups parameter for the Conv1d module. + /// The number of blocked connections from input channels to output channels. /// - [Description("The groups parameter for the Conv1d module")] + [Description("The number of blocked connections from input channels to output channels.")] public long Groups { get; set; } = 1; /// @@ -77,21 +83,22 @@ public class ConvTranspose2d public bool Bias { get; set; } = true; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] + [Description("The desired data type of the returned tensor")] public ScalarType? Type { get; set; } = null; /// /// Creates a ConvTranspose2d module. /// + /// public IObservable> Process() { return Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); @@ -103,7 +110,6 @@ public IObservable> Process() /// /// /// - /// public IObservable> Process(IObservable source) { return source.Select(_ => ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs index 0fa168bf..5129b3f6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs @@ -1,10 +1,8 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,61 +11,69 @@ namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// /// Represents an operator that creates a 3D transposed convolution module. /// +/// +/// See for more information. +/// [Description("Creates a 3D transposed convolution module.")] public class ConvTranspose3d { /// - /// The in_channels parameter for the Conv1d module. + /// The number of input channels in the input tensor. /// - [Description("The in_channels parameter for the Conv1d module")] + [Description("The number of input channels in the input tensor.")] public long InChannels { get; set; } /// - /// The out_channels parameter for the Conv1d module. + /// The number of output channels produced by the convolution. /// - [Description("The out_channels parameter for the Conv1d module")] + [Description("The number of output channels produced by the convolution.")] public long OutChannels { get; set; } /// - /// The kernelsize parameter for the Conv1d module. + /// The size of the convolution kernel. /// - [Description("The kernelsize parameter for the Conv1d module")] - public long KernelSize { get; set; } + [Description("The size of the convolution kernel.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) KernelSize { get; set; } /// - /// The stride parameter for the Conv1d module. + /// The stride of the convolution. /// - [Description("The stride parameter for the Conv1d module")] - public long Stride { get; set; } = 1; + [Description("The stride of the convolution.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Stride { get; set; } = null; /// - /// The padding parameter for the Conv1d module. + /// The zero-padding added to both sides of each dimension in the input. /// - [Description("The padding parameter for the Conv1d module")] - public long Padding { get; set; } = 0; + [Description("The zero-padding added to both sides of each dimension in the input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Padding { get; set; } = null; /// - /// The output_padding parameter for the Conv1d module. + /// The additional size added to one side of each dimension in the output shape. /// - [Description("The output_padding parameter for the ConvTransposed1d module")] - public long OutputPadding { get; set; } = 0; + [Description("The additional size added to one side of each dimension in the output shape.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? OutputPadding { get; set; } = null; /// - /// The dilation parameter for the Conv1d module. + /// The spacing between kernel elements. /// - [Description("The dilation parameter for the Conv1d module")] - public long Dilation { get; set; } = 1; + [Description("The spacing between kernel elements.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Dilation { get; set; } = null; /// - /// The padding_mode parameter for the Conv1d module. + /// The mode of padding. /// - [Description("The padding_mode parameter for the Conv1d module")] + [Description("The mode of padding.")] public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; /// - /// The groups parameter for the Conv1d module. + /// The number of blocked connections from input channels to output channels. /// - [Description("The groups parameter for the Conv1d module")] + [Description("The number of blocked connections from input channels to output channels.")] public long Groups { get; set; } = 1; /// @@ -77,21 +83,22 @@ public class ConvTranspose3d public bool Bias { get; set; } = true; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] + [Description("The desired data type of the returned tensor")] public ScalarType? Type { get; set; } = null; /// /// Creates a ConvTranspose3d module. /// + /// public IObservable> Process() { return Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); @@ -103,7 +110,6 @@ public IObservable> Process() /// /// /// - /// public IObservable> Process(IObservable source) { return source.Select(_ => ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs deleted file mode 100644 index b17a4693..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Convolution.cs +++ /dev/null @@ -1,153 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Convolution; - -/// -/// Represents an operator that creates a specific convolution module. -/// -[Combinator] -[Description("Creates a specific convolution module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Convolution -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The in_channels parameter for the Conv1d module. - /// - [Description("The in_channels parameter for the Conv1d module")] - public long InChannels { get; set; } - - /// - /// The out_channels parameter for the Conv1d module. - /// - [Description("The out_channels parameter for the Conv1d module")] - public long OutChannels { get; set; } - - /// - /// The kernelsize parameter for the Conv1d module. - /// - [Description("The kernelsize parameter for the Conv1d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the Conv1d module. - /// - [Description("The stride parameter for the Conv1d module")] - public long Stride { get; set; } = 1; - - /// - /// The padding parameter for the Conv1d module. - /// - [Description("The padding parameter for the Conv1d module")] - public long Padding { get; set; } = 0; - - /// - /// The output_padding parameter for the Conv1d module. - /// - [Description("The output_padding parameter for the ConvTransposed1d module")] - public long OutputPadding { get; set; } = 0; - - /// - /// The dilation parameter for the Conv1d module. - /// - [Description("The dilation parameter for the Conv1d module")] - public long Dilation { get; set; } = 1; - - /// - /// The padding_mode parameter for the Conv1d module. - /// - [Description("The padding_mode parameter for the Conv1d module")] - public PaddingModes PaddingMode { get; set; } = PaddingModes.Zeros; - - /// - /// The groups parameter for the Conv1d module. - /// - [Description("The groups parameter for the Conv1d module")] - public long Groups { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// If true, creates a transposed convolution layer. - /// - [Description("If true, creates a transposed convolution layer")] - public bool Transposed { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - public ScalarType? Type { get; set; } = null; - - /// - /// Creates a specific convolution module. - /// - /// - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Transposed - ? Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, 0, Dilation, PaddingMode, Groups, Bias, Device, Type)) - : Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)), - Dimensions.Two => Transposed - ? Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)) - : Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)), - Dimensions.Three => Transposed - ? Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)) - : Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } - - /// - /// Creates a specific convolution module. - /// - /// - /// - /// - /// - public IObservable> Process(IObservable source) - { - return source.Select>(_ => { - return Dimensions switch - { - Dimensions.One => Transposed - ? ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, 0, Dilation, PaddingMode, Groups, Bias, Device, Type) - : Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type), - Dimensions.Two => Transposed - ? ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type) - : Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type), - Dimensions.Three => Transposed - ? ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type) - : Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - }); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs index 53fd1fc4..8ed0cb53 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs @@ -1,54 +1,59 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// -/// Represents an operator that creates a Fold module. +/// Represents an operator that creates a fold module. /// -[Description("Creates a Fold module.")] +/// +/// See for more information. +/// +[Description("Creates a fold module.")] public class Fold { /// - /// The output_size parameter for the Fold module. + /// The shape of the spatial dimensions of the output tensor. /// - [Description("The output_size parameter for the Fold module")] - public long OutputSize { get; set; } + [Description("The shape of the spatial dimensions of the output tensor.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) OutputSize { get; set; } /// - /// The kernel_size parameter for the Fold module. + /// The size of the sliding blocks. /// - [Description("The kernel_size parameter for the Fold module")] - public long KernelSize { get; set; } + [Description("The size of the sliding blocks.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// - /// The dilation parameter for the Fold module. + /// The stride of elements within the neighborhood. /// - [Description("The dilation parameter for the Fold module")] - public long Dilation { get; set; } = 1; + [Description("The stride of elements within the neighborhood.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Dilation { get; set; } = null; /// - /// The padding parameter for the Fold module. + /// The implicit zero-padding to be added on both sides of input. /// - [Description("The padding parameter for the Fold module")] - public long Padding { get; set; } = 0; + [Description("The implicit zero-padding to be added on both sides of input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; /// - /// The stride parameter for the Fold module. + /// The stride of the sliding blocks in the input tensor. /// - [Description("The stride parameter for the Fold module")] - public long Stride { get; set; } = 1; + [Description("The stride of the sliding blocks in the input tensor.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; /// /// Creates a Fold module. /// + /// public IObservable> Process() { return Observable.Return(Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs index 9513b3e1..a5389f2e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs @@ -1,48 +1,52 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Convolution; /// -/// Represents an operator that creates an Unfold module. +/// Represents an operator that creates an unfold module. /// -[Description("Creates an Unfold module.")] +/// +/// See for more information. +/// +[Description("Creates an unfold module.")] public class Unfold { /// - /// The kernel_size parameter for the Unfold module. + /// The size of the sliding blocks. /// - [Description("The kernel_size parameter for the Unfold module")] - public long KernelSize { get; set; } + [Description("The size of the sliding blocks.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// - /// The dilation parameter for the Unfold module. + /// The stride of elements within the neighborhood. /// - [Description("The dilation parameter for the Unfold module")] - public long Dilation { get; set; } = 1; + [Description("The stride of elements within the neighborhood.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Dilation { get; set; } = null; /// - /// The padding parameter for the Unfold module. + /// The implicit zero-padding to be added on both sides of input. /// - [Description("The padding parameter for the Unfold module")] - public long Padding { get; set; } = 0; + [Description("The implicit zero-padding to be added on both sides of input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; /// - /// The stride parameter for the Unfold module. + /// The stride of the sliding blocks in the input tensor. /// - [Description("The stride parameter for the Unfold module")] - public long Stride { get; set; } = 1; + [Description("The stride of the sliding blocks in the input tensor.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; /// /// Creates an Unfold module. /// + /// public IObservable> Process() { return Observable.Return(Unfold(KernelSize, Dilation, Padding, Stride)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs index aa5744a1..58824d80 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs @@ -1,16 +1,5 @@ -using System; -using System.Linq; -using System.Linq.Expressions; using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; using System.Xml.Serialization; -using Bonsai.Expressions; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using System.Reflection; namespace Bonsai.ML.Torch.NeuralNets; diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs b/src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs deleted file mode 100644 index d767e690..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Dimensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace Bonsai.ML.Torch.NeuralNets; - -/// -/// Specifies the number of dimensions for a neural network module. -/// -public enum Dimensions -{ - /// - /// No dimensions. - /// - None = 0, - - /// - /// One dimension. - /// - One = 1, - - /// - /// Two dimensions. - /// - Two = 2, - - /// - /// Three dimensions. - /// - Three = 3 -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs new file mode 100644 index 00000000..883b9628 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Distance; + +/// +/// Represents an operator that creates a cosine similarity module. +/// +/// +/// See for more information. +/// +[Description("Creates a cosine similarity module.")] +public class CosineSimilarity +{ + /// + /// The dimension where cosine similarity is computed. + /// + [Description("The dimension where cosine similarity is computed.")] + public long Dim { get; set; } = 1; + + /// + /// The value added to the denominator to avoid division by zero. + /// + [Description("The value added to the denominator to avoid division by zero.")] + public double Eps { get; set; } = 1E-08D; + + /// + /// Creates a CosineSimilarity module. + /// + /// + public IObservable> Process() + { + return Observable.Return(CosineSimilarity(Dim, Eps)); + } + + /// + /// Creates a CosineSimilarity module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => CosineSimilarity(Dim, Eps)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs deleted file mode 100644 index 11db730b..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarityModule.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Distance; - -/// -/// Creates a CosineSimilarity module. -/// -[Combinator] -[Description("Creates a CosineSimilarity module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class CosineSimilarityModule -{ - /// - /// The dim parameter for the CosineSimilarity module. - /// - [Description("The dim parameter for the CosineSimilarity module")] - public long Dim { get; set; } = 1; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08D; - - /// - /// Generates an observable sequence that creates a CosineSimilarityModule module. - /// - public IObservable> Process() - { - return Observable.Return(CosineSimilarity(Dim, Eps)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs new file mode 100644 index 00000000..e3afa0ab --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Distance; + +/// +/// Represents an operator that creates a pairwise distance module. +/// +/// +/// See for more information. +/// +[Description("Creates a pairwise distance module.")] +public class PairwiseDistance +{ + /// + /// The norm degree which can be positive or negative. + /// + [Description("The norm degree which can be positive or negative.")] + public double P { get; set; } = 2D; + + /// + /// The value added to the denominator to avoid division by zero. + /// + [Description("The value added to the denominator to avoid division by zero.")] + public double Eps { get; set; } = 1E-06D; + + /// + /// Determines whether or not to keep the vector dimension. + /// + [Description("Determines whether or not to keep the vector dimension.")] + public bool KeepDim { get; set; } = false; + + /// + /// Creates a PairwiseDistance module. + /// + /// + public IObservable> Process() + { + return Observable.Return(PairwiseDistance(P, Eps, KeepDim)); + } + + /// + /// Creates a PairwiseDistance module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => PairwiseDistance(P, Eps, KeepDim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs deleted file mode 100644 index 9ca3368c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistanceModule.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Distance; - -/// -/// Creates a PairwiseDistance module. -/// -[Combinator] -[Description("Creates a PairwiseDistance module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class PairwiseDistanceModule -{ - /// - /// The p parameter for the PairwiseDistance module. - /// - [Description("The p parameter for the PairwiseDistance module")] - public double P { get; set; } = 2D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-06D; - - /// - /// The keep_dim parameter for the PairwiseDistance module. - /// - [Description("The keep_dim parameter for the PairwiseDistance module")] - public bool KeepDim { get; set; } = false; - - /// - /// Generates an observable sequence that creates a PairwiseDistanceModule module. - /// - public IObservable> Process() - { - return Observable.Return(PairwiseDistance(P, Eps, KeepDim)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs new file mode 100644 index 00000000..22e8b1ac --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs @@ -0,0 +1,43 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a module for distance computations. +/// +[XmlInclude(typeof(Distance.CosineSimilarity))] +[XmlInclude(typeof(Distance.PairwiseDistance))] +[DefaultProperty(nameof(DistanceModule))] +[Combinator] +[Description("Creates a module for distance computations.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class DistanceModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public DistanceModuleBuilder() + { + Module = new Distance.CosineSimilarity(); + } + + /// + /// Gets or sets the specific distance module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific distance module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object DistanceModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs index 266b4185..61111989 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs @@ -1,28 +1,25 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Dropout; /// -/// Creates a AlphaDropout module. +/// Represents an operator that creates an alpha dropout module. /// -[Combinator] -[Description("Creates a AlphaDropout module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an alpha dropout module.")] public class AlphaDropout { /// - /// The p parameter for the AlphaDropout module. + /// The probability of an element to be dropped. /// - [Description("The p parameter for the AlphaDropout module")] - public double P { get; set; } = 0.5D; + [Description("The probability of an element to be dropped.")] + public double Probability { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. @@ -31,10 +28,22 @@ public class AlphaDropout public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a AlphaDropoutModule module. + /// Creates an AlphaDropout module. /// + /// public IObservable> Process() { - return Observable.Return(AlphaDropout(P, Inplace)); + return Observable.Return(AlphaDropout(Probability, Inplace)); } -} + + /// + /// Creates an AlphaDropout module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => AlphaDropout(Probability, Inplace)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs index e2711e22..92dd6674 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs @@ -1,53 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Dropout; /// -/// Creates a dropout layer. +/// Represents an operator that creates a dropout module. /// -[Combinator] -[Description("Creates a dropout layer.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a dropout module.")] public class Dropout { /// - /// The number of dimensions for the AdaptiveAvgPool module. + /// The probability of an element to be zeroed. /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.None; - - /// - /// The p parameter for the Dropout1d module. - /// - [Description("The p parameter for the Dropout1d module")] - public double P { get; set; } = 0.5D; + [Description("The probability of an element to be zeroed.")] + public double Probability { get; set; } = 0.5D; /// /// If set to true, will do this operation in-place. /// - [Description("If set to true, will do this operation in-place")] + [Description("If set to true, will do this operation in-place.")] public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a Dropout module. + /// Creates a Dropout module. /// + /// public IObservable> Process() { - return Dimensions switch - { - Dimensions.None => Observable.Return(nn.Dropout(P, Inplace)), - Dimensions.One => Observable.Return(Dropout1d(P, Inplace)), - Dimensions.Two => Observable.Return(Dropout2d(P, Inplace)), - Dimensions.Three => Observable.Return(Dropout3d(P, Inplace)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; + return Observable.Return(nn.Dropout(Probability, Inplace)); + } + + /// + /// Creates a Dropout module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Dropout(Probability, Inplace)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs new file mode 100644 index 00000000..7831ad9c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Dropout; + +/// +/// Represents an operator that creates a 1D dropout module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D dropout module.")] +public class Dropout1d +{ + /// + /// The probability of an element to be zeroed. + /// + [Description("The probability of an element to be zeroed.")] + public double Probability { get; set; } = 0.5D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a Dropout1d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(nn.Dropout1d(Probability, Inplace)); + } + + /// + /// Creates a Dropout1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Dropout1d(Probability, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs new file mode 100644 index 00000000..66283a6f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Dropout; + +/// +/// Represents an operator that creates a 2D dropout module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D dropout module.")] +public class Dropout2d +{ + /// + /// The probability of an element to be zeroed. + /// + [Description("The probability of an element to be zeroed.")] + public double Probability { get; set; } = 0.5D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a Dropout2d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(nn.Dropout2d(Probability, Inplace)); + } + + /// + /// Creates a Dropout2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Dropout2d(Probability, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs new file mode 100644 index 00000000..d902bd31 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Dropout; + +/// +/// Represents an operator that creates a 3D dropout module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D dropout module.")] +public class Dropout3d +{ + /// + /// The probability of an element to be zeroed. + /// + [Description("The probability of an element to be zeroed.")] + public double Probability { get; set; } = 0.5D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a Dropout3d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(nn.Dropout3d(Probability, Inplace)); + } + + /// + /// Creates a Dropout3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Dropout3d(Probability, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs index 7e9af75a..8a798d6e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Dropout; /// -/// Creates a FeatureAlphaDropout module. +/// Represents an operator that creates a feature alpha dropout module. /// -[Combinator] -[Description("Creates a FeatureAlphaDropout module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class FeatureAlphaDropoutModule +/// +/// See for more information. +/// +[Description("Creates a feature alpha dropout module.")] +public class FeatureAlphaDropout { /// - /// The p parameter for the FeatureAlphaDropout module. + /// The probability of an element to be zeroed. /// - [Description("The p parameter for the FeatureAlphaDropout module")] - public double P { get; set; } = 0.5D; + [Description("The probability of an element to be zeroed.")] + public double Probability { get; set; } = 0.5D; /// - /// Generates an observable sequence that creates a FeatureAlphaDropoutModule module. + /// Creates a FeatureAlphaDropout module. /// + /// public IObservable> Process() { - return Observable.Return(FeatureAlphaDropout(P)); + return Observable.Return(FeatureAlphaDropout(Probability)); + } + + /// + /// Creates a FeatureAlphaDropout module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => FeatureAlphaDropout(Probability)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs new file mode 100644 index 00000000..b06e6a2c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs @@ -0,0 +1,47 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a module for dropout computations. +/// +[XmlInclude(typeof(Dropout.AlphaDropout))] +[XmlInclude(typeof(Dropout.Dropout))] +[XmlInclude(typeof(Dropout.Dropout1d))] +[XmlInclude(typeof(Dropout.Dropout2d))] +[XmlInclude(typeof(Dropout.Dropout3d))] +[XmlInclude(typeof(Dropout.FeatureAlphaDropout))] +[DefaultProperty(nameof(DropoutModule))] +[Combinator] +[Description("Creates a module for dropout computations.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class DropoutModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public DropoutModuleBuilder() + { + Module = new Dropout.AlphaDropout(); + } + + /// + /// Gets or sets the specific dropout module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific dropout module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object DropoutModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs index 0617952c..f849c9ef 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Flatten; /// -/// Creates a Flatten module. +/// Represents an operator that creates a Flatten module. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a Flatten module.")] -[WorkflowElementCategory(ElementCategory.Source)] public class Flatten { /// - /// The startdim parameter for the Flatten module. + /// The first dimension to flatten. /// - [Description("The startdim parameter for the Flatten module")] + [Description("The first dimension to flatten.")] public long StartDim { get; set; } = 1; /// - /// The enddim parameter for the Flatten module. + /// The last dimension to flatten. /// - [Description("The enddim parameter for the Flatten module")] + [Description("The last dimension to flatten.")] public long EndDim { get; set; } = -1; /// - /// Generates an observable sequence that creates a FlattenModule module. + /// Creates a Flatten module. /// + /// public IObservable> Process() { return Observable.Return(nn.Flatten(StartDim, EndDim)); } + + /// + /// Creates a Flatten module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Flatten(StartDim, EndDim)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs index bfe6c78a..d09bca9b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs @@ -1,41 +1,50 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Flatten; /// -/// Creates a Unflatten module. +/// Creates an Unflatten module. /// -[Combinator] -[Description("Creates a Unflatten module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an Unflatten module.")] public class Unflatten { /// - /// The dim parameter for the Unflatten module. + /// The dimension to unflatten. /// - [Description("The dim parameter for the Unflatten module")] + [Description("The dimension to unflatten.")] public long Dim { get; set; } /// - /// The unflattenedsize parameter for the Unflatten module. + /// The new shape of the unflattened dimension. /// - [Description("The unflattenedsize parameter for the Unflatten module")] + [Description("The new shape of the unflattened dimension.")] [TypeConverter(typeof(UnidimensionalArrayConverter))] public long[] UnflattenedSize { get; set; } /// - /// Generates an observable sequence that creates a UnflattenModule module. + /// Creates an Unflatten module. /// + /// public IObservable> Process() { return Observable.Return(Unflatten(Dim, UnflattenedSize)); } + + /// + /// Creates an Unflatten module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Unflatten(Dim, UnflattenedSize)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs new file mode 100644 index 00000000..cd1ec66e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs @@ -0,0 +1,43 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a module for flattening tensors. +/// +[XmlInclude(typeof(Flatten.Flatten))] +[XmlInclude(typeof(Flatten.Unflatten))] +[DefaultProperty(nameof(FlattenModule))] +[Combinator] +[Description("Creates a module for flattening tensors.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class FlattenModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public FlattenModuleBuilder() + { + Module = new Flatten.Flatten(); + } + + /// + /// Gets or sets the specific flatten module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific flatten module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object FlattenModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs index 9ac1df44..65b54d7f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs @@ -1,30 +1,22 @@ using System; +using System.Linq; using System.ComponentModel; using System.Reactive.Linq; -using System.Reflection; -using static TorchSharp.torch; using static TorchSharp.torch.nn; using static TorchSharp.torch.jit; -using System.Xml.Serialization; -using Bonsai.Expressions; -using System.Linq.Expressions; -using System.Collections.Generic; -using System.Linq; -using TorchSharp; namespace Bonsai.ML.Torch.NeuralNets; /// -/// Represents an operator that runs forward inference on the input tensor using the specified model. +/// Represents an operator that runs forward inference on the input using the specified module. /// [Combinator] -[ResetCombinator] -[Description("Runs forward inference on the input tensor using the specified model.")] +[Description("Runs forward inference on the input using the specified module.")] [WorkflowElementCategory(ElementCategory.Transform)] public class Forward { /// - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -36,7 +28,7 @@ public IObservable Process(IObservable - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -49,7 +41,7 @@ public IObservable Process(IObservable - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -63,7 +55,7 @@ public IObservable Process(IObservable - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -78,7 +70,7 @@ public IObservable Process(IObservable - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -94,7 +86,7 @@ public IObservable Process(IObservable - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -111,19 +103,18 @@ public IObservable Process(IObservable } /// - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// - /// /// /// /// - public IObservable Process(IObservable>> source) + public IObservable Process(IObservable> source) { - return source.Select(input => input.Item2.forward()); + return source.Select(input => input.forward()); } /// - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// @@ -135,7 +126,7 @@ public IObservable Process(IObservable - /// Runs forward inference on the input tensor using the specified model. + /// Runs forward inference on the input using the specified module. /// /// /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs index ccf8f915..84fae13b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Constant.cs @@ -1,60 +1,52 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a constant learning rate scheduler. +/// Represents an operator that creates a constant learning rate scheduler. /// -[Combinator] -[Description("Creates a ConstantLearningRate scheduler.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a constant learning rate scheduler.")] public class Constant { /// - /// The optimizer parameter for the Constant module. + /// The number that the learning rate will be multiplied by until the milestone. /// - [Description("The optimizer parameter for the Constant module")] - public optim.Optimizer Optimizer { get; set; } - - /// - /// The factor parameter for the Constant module. - /// - [Description("The factor parameter for the Constant module")] + [Description("The number that the learning rate will be multiplied by until the milestone.")] public double Factor { get; set; } = 0.3333333333333333D; /// - /// The total_iters parameter for the Constant module. + /// The number of steps that the scheduler multiplies the learning rate by the factor. /// - [Description("The total_iters parameter for the Constant module")] + [Description("The number of steps that the scheduler multiplies the learning rate by the factor.")] public int TotalIters { get; set; } = 5; /// - /// The last_epoch parameter for the Constant module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the Constant module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the Constant module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the Constant module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a ConstantLearningRateScheduler. + /// Creates a ConstantLR scheduler for the input optimizer. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(ConstantLR(Optimizer, Factor, TotalIters, LastEpoch, Verbose)); + return source.Select(optimizer => ConstantLR(optimizer, Factor, TotalIters, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs index 76006e6e..37457bc5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/CosineAnnealing.cs @@ -1,31 +1,20 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Sets the learning rate using a cosine annealing schedule. +/// Represents an operator that creates a cosine annealing learning rate scheduler. /// -[Combinator] -[Description("Sets the learning rate using a cosine annealing schedule.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a cosine annealing learning rate scheduler.")] public class CosineAnnealing { - /// - /// The wrapped optimizer. - /// - [Description("The wrapped optimizer.")] - public optim.Optimizer Optimizer { get; set; } - /// /// The maximum number of iterations. /// @@ -39,22 +28,25 @@ public class CosineAnnealing public double EtaMin { get; set; } = 0D; /// - /// The index + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the CosineAnnealing module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the CosineAnnealing module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the CosineAnnealing module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a CosineAnnealingLearningRateScheduler. + /// Creates a CosineAnnealingLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(CosineAnnealingLR(Optimizer, TMax, EtaMin, LastEpoch, Verbose)); + return source.Select(optimizer => CosineAnnealingLR(optimizer, TMax, EtaMin, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs index ecdb9af9..817f8f93 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Exponential.cs @@ -1,54 +1,46 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates an exponential learning rate scheduler. +/// Represents an operator that creates an exponential learning rate scheduler. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates an exponential learning rate scheduler.")] -[WorkflowElementCategory(ElementCategory.Source)] public class Exponential { /// - /// The optimizer parameter for the ExponentialLR module. + /// The multiplicative factor of learning rate decay. /// - [Description("The optimizer parameter for the ExponentialLR module")] - public optim.Optimizer Optimizer { get; set; } - - /// - /// The gamma parameter for the ExponentialLR module. - /// - [Description("The gamma parameter for the ExponentialLR module")] + [Description("The multiplicative factor of learning rate decay.")] public double Gamma { get; set; } = 0.1D; /// - /// The last_epoch parameter for the ExponentialLR module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the ExponentialLR module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the ExponentialLR module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the ExponentialLR module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a ExponentialLearningRateScheduler. + /// Creates an ExponentialLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(ExponentialLR(Optimizer, Gamma, LastEpoch, Verbose)); + return source.Select(optimizer => ExponentialLR(optimizer, Gamma, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs index c8d512ff..14901212 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Linear.cs @@ -1,66 +1,58 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a linear learning rate scheduler. +/// Represents an operator that creates a linear learning rate scheduler. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a linear learning rate scheduler.")] -[WorkflowElementCategory(ElementCategory.Source)] public class Linear { /// - /// The optimizer parameter for the LinearLR module. + /// The number used to multiply the learning rate in the first epoch. /// - [Description("The optimizer parameter for the LinearLR module")] - public optim.Optimizer Optimizer { get; set; } - - /// - /// The start_factor parameter for the LinearLR module. - /// - [Description("The start_factor parameter for the LinearLR module")] + [Description("The number used to multiply the learning rate in the first epoch.")] public double StartFactor { get; set; } = 0.3333333333333333D; /// - /// The end_factor parameter for the LinearLR module. + /// The number used to multiply the learning rate in the last epoch. /// - [Description("The end_factor parameter for the LinearLR module")] + [Description("The number used to multiply the learning rate in the last epoch.")] public double EndFactor { get; set; } = 5D; /// - /// The total_iters parameter for the LinearLR module. + /// The number of iterations over which the learning rate is adjusted. /// - [Description("The total_iters parameter for the LinearLR module")] + [Description("The number of iterations over which the learning rate is adjusted.")] public int TotalIters { get; set; } = 5; /// - /// The last_epoch parameter for the LinearLR module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the LinearLR module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the LinearLR module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the LinearLR module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a LinearLearningRateScheduler. + /// Creates a LinearLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(LinearLR(Optimizer, StartFactor, EndFactor, TotalIters, LastEpoch, Verbose)); + return source.Select(optimizer => LinearLR(optimizer, StartFactor, EndFactor, TotalIters, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs index 2b3ba8a5..eb487bc9 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/MultiStep.cs @@ -2,59 +2,53 @@ using System.ComponentModel; using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a MultiStepLearningRate scheduler. +/// Represents an operator that creates a multi-step learning rate scheduler. /// -[Combinator] -[Description("Creates a MultiStepLearningRate scheduler.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a multi-step learning rate scheduler.")] public class MultiStep { /// - /// The optimizer parameter for the MultiStepLR module. + /// The list of epoch indices. /// - [Description("The optimizer parameter for the MultiStepLR module")] - public optim.Optimizer Optimizer { get; set; } + [Description("The list of epoch indices.")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public int[] Milestones { get; set; } /// - /// The milestones parameter for the MultiStepLR module. + /// The multiplicative factor of learning rate decay. /// - [Description("The milestones parameter for the MultiStepLR module")] - public IList Milestones { get; set; } - - /// - /// The gamma parameter for the MultiStepLR module. - /// - [Description("The gamma parameter for the MultiStepLR module")] + [Description("The multiplicative factor of learning rate decay.")] public double Gamma { get; set; } = 0.1D; /// - /// The last_epoch parameter for the MultiStepLR module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the MultiStepLR module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the MultiStepLR module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the MultiStepLR module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a MultiStep. + /// Creates a MultiStepLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(MultiStepLR(Optimizer, Milestones, Gamma, LastEpoch, Verbose)); + return source.Select(optimizer => MultiStepLR(optimizer, Milestones, Gamma, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs index 7fab62a4..4200d44d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/OneCycle.cs @@ -1,120 +1,115 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a one cycle learning rate policy scheduler. +/// Represents an operator that creates a one cycle learning rate scheduler. /// -[Combinator] -[Description("Creates a one cycle learning rate policy scheduler.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a one cycle learning rate scheduler.")] public class OneCycle { /// - /// The optimizer parameter for the OneCycleLR module. + /// The upper learning rate boundaries in the cycle for each parameter group. /// - [Description("The optimizer parameter for the OneCycleLR module")] - public optim.Optimizer Optimizer { get; set; } + [Description("The upper learning rate boundaries in the cycle for each parameter group.")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public double[] MaxLearningRate { get; set; } /// - /// The max_lr parameter for the OneCycleLR module. + /// The total number of steps in the cycle. /// - [Description("The max_lr parameter for the OneCycleLR module")] - public double MaxLr { get; set; } - - /// - /// The total_steps parameter for the OneCycleLR module. - /// - [Description("The total_steps parameter for the OneCycleLR module")] + [Description("The total number of steps in the cycle.")] public int TotalSteps { get; set; } = -1; /// - /// The epochs parameter for the OneCycleLR module. + /// The number of epochs to train for. /// - [Description("The epochs parameter for the OneCycleLR module")] + [Description("The number of epochs to train for.")] public int Epochs { get; set; } = -1; /// - /// The steps_per_epoch parameter for the OneCycleLR module. + /// The number of steps per epoch. /// - [Description("The steps_per_epoch parameter for the OneCycleLR module")] + [Description("The number of steps per epoch.")] public int StepsPerEpoch { get; set; } = -1; /// - /// The pct_start parameter for the OneCycleLR module. + /// The percentage of the cycle spent increasing the learning rate. /// - [Description("The pct_start parameter for the OneCycleLR module")] - public double PctStart { get; set; } = 0.3D; + [Description("The percentage of the cycle spent increasing the learning rate.")] + public double PercentageStart { get; set; } = 0.3D; /// - /// The anneal_strategy parameter for the OneCycleLR module. + /// The annealing strategy to use. /// - [Description("The anneal_strategy parameter for the OneCycleLR module")] + [Description("The annealing strategy to use.")] public impl.OneCycleLR.AnnealStrategy AnnealStrategy { get; set; } = impl.OneCycleLR.AnnealStrategy.Cos; /// - /// The cycle_momentum parameter for the OneCycleLR module. + /// If true, momentum is cycled inversely to learning rate. /// - [Description("The cycle_momentum parameter for the OneCycleLR module")] + [Description("If true, momentum is cycled inversely to learning rate.")] public bool CycleMomentum { get; set; } = true; /// - /// The base_momentum parameter for the OneCycleLR module. + /// The lower momentum boundaries in the cycle for each parameter group /// - [Description("The base_momentum parameter for the OneCycleLR module")] - public double BaseMomentum { get; set; } = 0.85D; + [Description("The lower momentum boundaries in the cycle for each parameter group")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public double[] BaseMomentum { get; set; } = null; /// - /// The max_momentum parameter for the OneCycleLR module. + /// The upper momentum boundaries in the cycle for each parameter group /// - [Description("The max_momentum parameter for the OneCycleLR module")] - public double MaxMomentum { get; set; } = 0.95D; + [Description("The upper momentum boundaries in the cycle for each parameter group")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public double[] MaxMomentum { get; set; } = null; /// - /// The div_factor parameter for the OneCycleLR module. + /// Determines the initial learning rate. /// - [Description("The div_factor parameter for the OneCycleLR module")] + [Description("Determines the initial learning rate.")] public double DivFactor { get; set; } = 25D; /// - /// The final_div_factor parameter for the OneCycleLR module. + /// Determines the final learning rate. /// - [Description("The final_div_factor parameter for the OneCycleLR module")] + [Description("Determines the final learning rate.")] public double FinalDivFactor { get; set; } = 10000D; /// - /// The three_phase parameter for the OneCycleLR module. + /// If true, uses a third phase of the schedule to annihilate the learning rate. /// - [Description("The three_phase parameter for the OneCycleLR module")] + [Description("If true, uses a third phase of the schedule to annihilate the learning rate.")] public bool ThreePhase { get; set; } = false; /// - /// The last_epoch parameter for the OneCycleLR module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the OneCycleLR module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the OneCycleLR module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the OneCycleLR module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a OneCycle. + /// Creates a OneCycleLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(OneCycleLR(Optimizer, MaxLr, TotalSteps, Epochs, StepsPerEpoch, PctStart, AnnealStrategy, CycleMomentum, BaseMomentum, MaxMomentum, DivFactor, FinalDivFactor, ThreePhase, LastEpoch, Verbose)); + return source.Select(optimizer => OneCycleLR(optimizer, MaxLearningRate, TotalSteps, Epochs, StepsPerEpoch, PercentageStart, AnnealStrategy, CycleMomentum, BaseMomentum, MaxMomentum, DivFactor, FinalDivFactor, ThreePhase, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs index 699ce0c6..854dc6fe 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Polynomial.cs @@ -1,60 +1,52 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a polynomial learning rate scheduler. +/// Represents an operator that creates a polynomial learning rate scheduler. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a polynomial learning rate scheduler.")] -[WorkflowElementCategory(ElementCategory.Source)] public class Polynomial { /// - /// The optimizer parameter for the PolynomialLR module. + /// The total number of steps that the learning rate will be adjusted over. /// - [Description("The optimizer parameter for the PolynomialLR module")] - public optim.Optimizer Optimizer { get; set; } - - /// - /// The total_iters parameter for the PolynomialLR module. - /// - [Description("The total_iters parameter for the PolynomialLR module")] + [Description("The total number of steps that the learning rate will be adjusted over.")] public int TotalIters { get; set; } = 5; /// - /// The power parameter for the PolynomialLR module. + /// The power of the polynomial. /// - [Description("The power parameter for the PolynomialLR module")] + [Description("The power of the polynomial.")] public int Power { get; set; } = 1; /// - /// The last_epoch parameter for the PolynomialLR module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the PolynomialLR module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the PolynomialLR module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the PolynomialLR module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a Polynomial. + /// Creates a PolynomialLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(PolynomialLR(Optimizer, TotalIters, Power, LastEpoch, Verbose)); + return source.Select(optimizer => PolynomialLR(optimizer, TotalIters, Power, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs index ea1dd030..dea93e26 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/ReduceOnPlateau.cs @@ -1,90 +1,102 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a scheduler that reduces the learning rate when a metric has stopped improving. +/// Represents an operator that creates a scheduler that reduces the learning rate on plateau. /// -[Combinator] -[Description("Creates a scheduler that reduces the learning rate when a metric has stopped improving.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a scheduler that reduces the learning rate on plateau.")] public class ReduceOnPlateau { /// - /// The optimizer parameter for the ReduceLROnPlateau module. + /// The mode in which to determine when to reduce the learning rate. /// - [Description("The optimizer parameter for the ReduceLROnPlateau module")] - public optim.Optimizer Optimizer { get; set; } + [Description("The mode in which to determine when to reduce the learning rate.")] + public PlateauMode Mode { get; set; } = PlateauMode.Min; /// - /// The mode parameter for the ReduceLROnPlateau module. + /// The factor by which the learning rate will be reduced. /// - [Description("The mode parameter for the ReduceLROnPlateau module")] - public string Mode { get; set; } = "min"; - - /// - /// The factor parameter for the ReduceLROnPlateau module. - /// - [Description("The factor parameter for the ReduceLROnPlateau module")] + [Description("The factor by which the learning rate will be reduced.")] public double Factor { get; set; } = 0.1D; /// - /// The patience parameter for the ReduceLROnPlateau module. + /// The number of allowed epochs with no improvement after which the learning rate will be reduced. /// - [Description("The patience parameter for the ReduceLROnPlateau module")] + [Description("The number of allowed epochs with no improvement after which the learning rate will be reduced.")] public int Patience { get; set; } = 10; /// - /// The threshold parameter for the ReduceLROnPlateau module. + /// The threshold for measuring the new optimum, to only focus on significant changes. /// - [Description("The threshold parameter for the ReduceLROnPlateau module")] + [Description("The threshold for measuring the new optimum, to only focus on significant changes.")] public double Threshold { get; set; } = 0.0001D; /// - /// The threshold_mode parameter for the ReduceLROnPlateau module. + /// The mode for the threshold parameter. /// - [Description("The threshold_mode parameter for the ReduceLROnPlateau module")] - public string ThresholdMode { get; set; } = "rel"; + [Description("The mode for the threshold parameter.")] + public PlateauThresholdMode ThresholdMode { get; set; } = PlateauThresholdMode.Relative; /// - /// The cooldown parameter for the ReduceLROnPlateau module. + /// The number of epochs to wait before resuming normal operation after the learning rate has been reduced. /// - [Description("The cooldown parameter for the ReduceLROnPlateau module")] + [Description("The number of epochs to wait before resuming normal operation after the learning rate has been reduced.")] public int Cooldown { get; set; } = 0; /// - /// The min_lr parameter for the ReduceLROnPlateau module. + /// The lower bound on the learning rate of all param groups or each group respectively. /// - [Description("The min_lr parameter for the ReduceLROnPlateau module")] - public IList MinLr { get; set; } = null; + [Description("The lower bound on the learning rate of all param groups or each group respectively.")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public double[] MinLearningRate { get; set; } = null; /// - /// A value added to the denominator for numerical stability. + /// The minimal decay applied to the learning rate. /// - [Description("A value added to the denominator for numerical stability")] + [Description("The minimal decay applied to the learning rate")] public double Eps { get; set; } = 1E-08D; /// - /// The verbose parameter for the ReduceLROnPlateau module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the ReduceLROnPlateau module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; +#pragma warning disable // Ignore missing xml doc strings + public enum PlateauMode + { + Min, + Max + } + + public enum PlateauThresholdMode + { + Relative, + Absolute + } + + private string ModeString => Mode == PlateauMode.Min ? "min" : "max"; + + private string ThresholdModeString => ThresholdMode == PlateauThresholdMode.Relative ? "rel" : "abs"; + +#pragma warning restore + /// - /// Generates an observable sequence that creates a ReduceOnPlateau. + /// Creates a ReduceLROnPlateau scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(ReduceLROnPlateau(Optimizer, Mode, Factor, Patience, Threshold, ThresholdMode, Cooldown, MinLr, Eps, Verbose)); + return source.Select(optimizer => ReduceLROnPlateau(optimizer, ModeString, Factor, Patience, Threshold, ThresholdModeString, Cooldown, MinLearningRate, Eps, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs index 0af7606a..f9e5cf56 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateScheduler/Step.cs @@ -1,60 +1,52 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -namespace Bonsai.ML.Torch.NeuralNets.LinearRateScheduler; +namespace Bonsai.ML.Torch.NeuralNets.LearningRateScheduler; /// -/// Creates a scheduler that decays the learning rate of each parameter group by gamma every step_size epochs. +/// Represents an operator that creates a step learning rate scheduler. /// -[Combinator] -[Description("Creates a scheduler that decays the learning rate of each parameter group by gamma every step_size epochs.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a step learning rate scheduler.")] public class Step { /// - /// The optimizer parameter for the StepLR module. + /// The period of learning rate decay. /// - [Description("The optimizer parameter for the StepLR module")] - public optim.Optimizer Optimizer { get; set; } - - /// - /// The step_size parameter for the StepLR module. - /// - [Description("The step_size parameter for the StepLR module")] + [Description("The period of learning rate decay.")] public int StepSize { get; set; } /// - /// The gamma parameter for the StepLR module. + /// The multiplicative factor of learning rate decay. /// - [Description("The gamma parameter for the StepLR module")] + [Description("The multiplicative factor of learning rate decay.")] public double Gamma { get; set; } = 0.1D; /// - /// The last_epoch parameter for the StepLR module. + /// The index of the last epoch. /// - [Description("The last_epoch parameter for the StepLR module")] + [Description("The index of the last epoch.")] public int LastEpoch { get; set; } = -1; /// - /// The verbose parameter for the StepLR module. + /// Determines whether to write a message to stdout for each update. /// - [Description("The verbose parameter for the StepLR module")] + [Description("Determines whether to write a message to stdout for each update.")] public bool Verbose { get; set; } = false; /// - /// Generates an observable sequence that creates a Step. + /// Creates a StepLR scheduler. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : optim.Optimizer { - return Observable.Return(StepLR(Optimizer, StepSize, Gamma, LastEpoch, Verbose)); + return source.Select(optimizer => StepLR(optimizer, StepSize, Gamma, LastEpoch, Verbose)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs new file mode 100644 index 00000000..b23582e8 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs @@ -0,0 +1,52 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a learning rate scheduler. +/// +[XmlInclude(typeof(LearningRateScheduler.Constant))] +[XmlInclude(typeof(LearningRateScheduler.CosineAnnealing))] +[XmlInclude(typeof(LearningRateScheduler.Exponential))] +[XmlInclude(typeof(LearningRateScheduler.Linear))] +[XmlInclude(typeof(LearningRateScheduler.MultiStep))] +[XmlInclude(typeof(LearningRateScheduler.OneCycle))] +[XmlInclude(typeof(LearningRateScheduler.Polynomial))] +[XmlInclude(typeof(LearningRateScheduler.ReduceOnPlateau))] +[XmlInclude(typeof(LearningRateScheduler.Step))] +[DefaultProperty(nameof(LearningRateScheduler))] +[Combinator] +[Description("Creates a learning rate scheduler.")] +[WorkflowElementCategory(ElementCategory.Transform)] +public class LearningRateSchedulerBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + string INamedElement.Name => $"LearningRateScheduler.{GetElementDisplayName(LearningRateScheduler)}"; + + /// + /// Initializes a new instance of the class. + /// + public LearningRateSchedulerBuilder() + { + Module = new LearningRateScheduler.Constant(); + } + + /// + /// Gets or sets the specific learning rate scheduler to create. + /// + [DesignOnly(true)] + [DisplayName("Scheduler")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific learning rate scheduler to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object LearningRateScheduler + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Linear/BilinearModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs similarity index 51% rename from src/Bonsai.ML.Torch/NeuralNets/Linear/BilinearModule.cs rename to src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs index f7a8b013..3b719ead 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Linear/BilinearModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs @@ -1,50 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Linear; /// -/// Creates a Bilinear module. +/// Represents an operator that creates a Bilinear module. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a Bilinear module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class BilinearModule +public class Bilinear { /// - /// The in1features parameter for the Bilinear module. + /// The size of each first input sample. /// - [Description("The in1features parameter for the Bilinear module")] + [Description("The size of each first input sample.")] public long In1Features { get; set; } /// - /// The in2features parameter for the Bilinear module. + /// The size of each second input sample. /// - [Description("The in2features parameter for the Bilinear module")] + [Description("The size of each second input sample.")] public long In2Features { get; set; } /// - /// The outputsize parameter for the Bilinear module. + /// The size of each output sample. /// - [Description("The outputsize parameter for the Bilinear module")] + [Description("The size of each output sample.")] public long OutputSize { get; set; } /// - /// The hasbias parameter for the Bilinear module. + /// Determines whether the layer will learn an additive bias. /// - [Description("The hasbias parameter for the Bilinear module")] + [Description("Determines whether the layer will learn an additive bias.")] public bool HasBias { get; set; } = true; /// /// The desired device of returned tensor. /// + [XmlIgnore] [Description("The desired device of returned tensor")] public Device Device { get; set; } = null; @@ -52,14 +51,25 @@ public class BilinearModule /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a BilinearModule module. + /// Creates a Bilinear module. /// + /// public IObservable> Process() { return Observable.Return(Bilinear(In1Features, In2Features, OutputSize, HasBias, Device, Type)); } + + /// + /// Creates a Bilinear module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Bilinear(In1Features, In2Features, OutputSize, HasBias, Device, Type)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs index bfd257c8..c7f782dc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs @@ -1,28 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Linear; /// -/// Creates a Identity module. +/// Represents an operator that creates an Identity module. /// -[Combinator] -[Description("Creates a Identity module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an Identity module.")] public class Identity { /// - /// Generates an observable sequence that creates a IdentityModule module. + /// Creates an Identity module. /// + /// public IObservable> Process() { return Observable.Return(nn.Identity()); } + + /// + /// Creates an Identity module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Identity()); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs index a2df6e5a..21c526f1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs @@ -1,39 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Linear; /// -/// Creates a Linear transformation layer. +/// Represents an operator that creates a Linear module. /// -[Combinator] -[Description("Creates a Linear transformation layer.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a Linear module.")] public class Linear { /// - /// The inputsize parameter for the Linear module. + /// The size of each input sample. /// - [Description("The inputsize parameter for the Linear module")] + [Description("The size of each input sample.")] public long InputSize { get; set; } /// - /// The outputsize parameter for the Linear module. + /// The size of each output sample. /// - [Description("The outputsize parameter for the Linear module")] + [Description("The size of each output sample.")] public long OutputSize { get; set; } /// - /// The hasbias parameter for the Linear module. + /// Determines whether the layer will learn an additive bias. /// - [Description("The hasbias parameter for the Linear module")] + [Description("Determines whether the layer will learn an additive bias.")] public bool HasBias { get; set; } = true; /// @@ -47,14 +45,25 @@ public class Linear /// The desired data type of returned tensor. /// [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a LinearModule module. + /// Creates a Linear module. /// + /// public IObservable> Process() { return Observable.Return(nn.Linear(InputSize, OutputSize, HasBias, Device, Type)); } + + /// + /// Creates a Linear module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Linear(InputSize, OutputSize, HasBias, Device, Type)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs new file mode 100644 index 00000000..7787f293 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs @@ -0,0 +1,44 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a linear module. +/// +[XmlInclude(typeof(Linear.Bilinear))] +[XmlInclude(typeof(Linear.Identity))] +[XmlInclude(typeof(Linear.Linear))] +[DefaultProperty(nameof(LinearModule))] +[Combinator] +[Description("Creates a linear module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LinearModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public LinearModuleBuilder() + { + Module = new Linear.Linear(); + } + + /// + /// Gets or sets the specific linear module. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific linear module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object LinearModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs index 16d1bde6..3610d789 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs @@ -10,24 +10,24 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Saves the module to a file. +/// Represents an operator that loads a module's state from a file. /// [Combinator] -[ResetCombinator] -[Description("Saves the module to a file.")] +[Description("Loads a module's state from a file.")] [WorkflowElementCategory(ElementCategory.Sink)] public class LoadModule { /// - /// The path to the modules state. + /// The path to the module's saved state. /// - [Description("The path to the modules state.")] + [Description("The path to the module's saved state.")] [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] public string ModulePath { get; set; } /// /// Loads the module's state from the specified file path. /// + /// /// public IObservable Process(IObservable source) { @@ -141,4 +141,62 @@ public IObservable> Process)module.load(ModulePath); }); } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Select(module => + { + return (ScriptModule)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (ScriptModule)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (ScriptModule)module.load(ModulePath); + }); + } + + /// + /// Loads the module's state from the specified file path. + /// + /// + /// + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(module => + { + return (ScriptModule)module.load(ModulePath); + }); + } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs index 54544510..810d2c2c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleArchitecture.cs @@ -5,77 +5,76 @@ using static TorchSharp.torch.nn; using System.Xml.Serialization; -namespace Bonsai.ML.Torch.NeuralNets +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Loads a neural network module from a specified architecture. +/// +[Combinator] +[ResetCombinator] +[Description("Loads a neural network module from a specified architecture.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LoadModuleArchitecture { /// - /// Loads a neural network module from a specified architecture. + /// The model architecture to load. /// - [Combinator] - [ResetCombinator] - [Description("Loads a neural network module from a specified architecture.")] - [WorkflowElementCategory(ElementCategory.Source)] - public class LoadModuleArchitecture - { - /// - /// The model architecture to load. - /// - [Description("The model architecture to load.")] - public Architecture.ModelArchitecture ModelArchitecture { get; set; } + [Description("The model architecture to load.")] + public Architecture.ModelArchitecture ModelArchitecture { get; set; } - /// - /// The device on which to load the model. - /// - [Description("The device on which to load the model.")] - [XmlIgnore] - public Device Device { get; set; } + /// + /// The device on which to load the model. + /// + [Description("The device on which to load the model.")] + [XmlIgnore] + public Device Device { get; set; } - /// - /// The optional path to the model weights file. - /// - [Description("The optional path to the model weights file.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModelWeightsPath { get; set; } + /// + /// The optional path to the model weights file. + /// + [Description("The optional path to the model weights file.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModelWeightsPath { get; set; } - private int numClasses = 10; - /// - /// The number of classes in the dataset. - /// - [Description("The number of classes in the dataset.")] - public int NumClasses + private int numClasses = 10; + /// + /// The number of classes in the dataset. + /// + [Description("The number of classes in the dataset.")] + public int NumClasses + { + get => numClasses; + set { - get => numClasses; - set + if (value <= 0) { - if (value <= 0) - { - numClasses = 10; - } - else - { - numClasses = value; - } + numClasses = 10; + } + else + { + numClasses = value; } } + } - /// - /// Loads the neural network module from the specified architecture. - /// - /// - /// - public IObservable> Process() + /// + /// Loads the neural network module from the specified architecture. + /// + /// + /// + public IObservable> Process() + { + var device = Device; + Module module = ModelArchitecture switch { - var device = Device; - Module module = ModelArchitecture switch - { - Architecture.ModelArchitecture.AlexNet => new Architecture.AlexNet("alexnet", numClasses, device), - Architecture.ModelArchitecture.MobileNet => new Architecture.MobileNet("mobilenet", numClasses, device), - Architecture.ModelArchitecture.Mnist => new Architecture.Mnist("mnist", numClasses, device), - _ => throw new ArgumentException($"Model {ModelArchitecture} not supported.") - }; + Architecture.ModelArchitecture.AlexNet => new Architecture.AlexNet("alexnet", numClasses, device), + Architecture.ModelArchitecture.MobileNet => new Architecture.MobileNet("mobilenet", numClasses, device), + Architecture.ModelArchitecture.Mnist => new Architecture.Mnist("mnist", numClasses, device), + _ => throw new ArgumentException($"Model {ModelArchitecture} not supported.") + }; - if (ModelWeightsPath is not null) module.load(ModelWeightsPath); + if (ModelWeightsPath is not null) module.load(ModelWeightsPath); - return Observable.Return(module); - } + return Observable.Return(module); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs index 21cc2c8c..d2ea7438 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs @@ -17,7 +17,6 @@ namespace Bonsai.ML.Torch.NeuralNets; /// Loads a TorchScript module from the specified file path. /// [Combinator] -[ResetCombinator] [Description("Loads a TorchScript module from the specified file path. In order to correctly infer the module type, pass into the operator objects representing the desired ScriptModule generic argument types.")] [WorkflowElementCategory(ElementCategory.Source)] public class LoadScriptModule: ExpressionBuilder @@ -99,13 +98,13 @@ private static IObservable ProcessScriptModule(string modulePath, /// /// Loads the scripted module from the specified file path. /// - /// + /// /// /// /// - private static IObservable> ProcessScriptModule(string modulePath, Device device) + private static IObservable> ProcessScriptModule(string modulePath, Device device) { - var scriptModule = device is null ? load(modulePath) : load(modulePath, device); + var scriptModule = device is null ? load(modulePath) : load(modulePath, device); return Observable.Return(scriptModule); } @@ -113,13 +112,13 @@ private static IObservable> ProcessScriptModule(string module /// Loads the scripted module from the specified file path. /// /// - /// + /// /// /// /// - private static IObservable> ProcessScriptModule(string modulePath, Device device) + private static IObservable> ProcessScriptModule(string modulePath, Device device) { - var scriptModule = device is null ? load(modulePath) : load(modulePath, device); + var scriptModule = device is null ? load(modulePath) : load(modulePath, device); return Observable.Return(scriptModule); } @@ -128,13 +127,13 @@ private static IObservable> ProcessScriptModule(str /// /// /// - /// + /// /// /// /// - private static IObservable> ProcessScriptModule(string modulePath, Device device) + private static IObservable> ProcessScriptModule(string modulePath, Device device) { - var scriptModule = device is null ? load(modulePath) : load(modulePath, device); + var scriptModule = device is null ? load(modulePath) : load(modulePath, device); return Observable.Return(scriptModule); } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs index 18e80972..aa20f5d0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs @@ -1,35 +1,46 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a binary cross entropy loss module. +/// Represents an operator that creates a binary cross entropy (BCE) loss module. /// -[Combinator] -[Description("Creates a binary cross entropy loss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a binary cross entropy (BCE) loss module.")] [TypeConverter(typeof(TensorOperatorConverter))] public class BinaryCrossEntropy : IScalarTypeProvider { /// - /// The weight parameter for the BinaryCrossEntropy module. + /// The manually specified rescaling weight given to the loss of each batch element. /// - [Description("The weight parameter for the BinaryCrossEntropy module")] + [XmlIgnore] + [Description("The manually specified rescaling weight given to the loss of each batch element.")] [TypeConverter(typeof(TensorConverter))] public Tensor Weight { get; set; } = null; /// - /// The reduction parameter for the BinaryCrossEntropy module. + /// The values of the weight tensor in XML string format. /// - [Description("The reduction parameter for the BinaryCrossEntropy module")] + [Browsable(false)] + [XmlElement(nameof(Weight))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string WeightXml + { + get => TensorConverter.ConvertToString(Weight, Type); + set => Weight = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// The reduction type to apply to the output. + /// + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// @@ -39,10 +50,22 @@ public class BinaryCrossEntropy : IScalarTypeProvider public ScalarType Type { get; set; } = ScalarType.Float32; /// - /// Generates an observable sequence that creates a BinaryCrossEntropy. + /// Creates a binary cross entropy (BCE) loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(BCELoss(Weight, Reduction)); } -} + + /// + /// Creates a binary cross entropy (BCE) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => BCELoss(Weight, Reduction)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs index c171ce99..cce87f3d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs @@ -1,44 +1,68 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Measures the Binary Cross Entropy between the target and the output logits. +/// Represents an operator that creates a binary cross entropy with logits (BCEWithLogits) loss module. /// -[Combinator] -[Description("Measures the Binary Cross Entropy between the target and the output logits.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a binary cross entropy with logits (BCEWithLogits) loss module.")] [TypeConverter(typeof(TensorOperatorConverter))] public class BinaryCrossEntropyWithLogits : IScalarTypeProvider { /// - /// The weight parameter for the BinaryCrossEntropyWithLogitsLoss module. + /// The manually specified rescaling weight given to the loss of each batch element. /// - [Description("The weight parameter for the BinaryCrossEntropyWithLogitsLoss module")] + [XmlIgnore] + [Description("The manually specified rescaling weight given to the loss of each batch element.")] [TypeConverter(typeof(TensorConverter))] public Tensor Weight { get; set; } = null; /// - /// The reduction parameter for the BinaryCrossEntropyWithLogitsLoss module. + /// The values of the weight tensor in XML string format. /// - [Description("The reduction parameter for the BinaryCrossEntropyWithLogitsLoss module")] + [Browsable(false)] + [XmlElement(nameof(Weight))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string WeightXml + { + get => TensorConverter.ConvertToString(Weight, Type); + set => Weight = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// The reduction type to apply to the output. + /// + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The pos_weights parameter for the BinaryCrossEntropyWithLogitsLoss module. + /// The weight of positive examples to be broadcasted with the target. /// - [Description("The pos_weights parameter for the BinaryCrossEntropyWithLogitsLoss module")] + [XmlIgnore] + [Description("The weight of positive examples to be broadcasted with the target.")] [TypeConverter(typeof(TensorConverter))] public Tensor PosWeights { get; set; } = null; + /// + /// The values of the pos weights tensor in XML string format. + /// + [Browsable(false)] + [XmlElement(nameof(PosWeights))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string PosWeightsXml + { + get => TensorConverter.ConvertToString(PosWeights, Type); + set => PosWeights = TensorConverter.ConvertFromString(value, Type); + } + /// /// The data type of the tensor elements. /// @@ -46,10 +70,22 @@ public class BinaryCrossEntropyWithLogits : IScalarTypeProvider public ScalarType Type { get; set; } = ScalarType.Float32; /// - /// Generates an observable sequence that creates a BinaryCrossEntropyWithLogitsLoss. + /// Creates a binary cross entropy with logits (BCEWithLogits) loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(BCEWithLogitsLoss(Weight, Reduction, PosWeights)); } + + /// + /// Creates a binary cross entropy with logits (BCEWithLogits) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => BCEWithLogitsLoss(Weight, Reduction, PosWeights)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs index b4958c14..1dd22661 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs @@ -1,46 +1,55 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a CTCLoss module. +/// Represents an operator that creates a connectionist temporal classification (CTC) loss module. /// -[Combinator] -[Description("Creates a CTCLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a connectionist temporal classification (CTC) loss module.")] public class ConnectionistTemporalClassification { /// - /// The blank parameter for the CTCLoss module. + /// The blank label. /// - [Description("The blank parameter for the CTCLoss module")] + [Description("The blank label.")] public long Blank { get; set; } = 0; /// - /// The zero_infinity parameter for the CTCLoss module. + /// Determines whether to zero infinite losses and the associated gradients. /// - [Description("The zero_infinity parameter for the CTCLoss module")] + [Description("Determines whether to zero infinite losses and the associated gradients.")] public bool ZeroInfinity { get; set; } = false; /// - /// The reduction parameter for the CTCLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the CTCLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a CTCLoss. + /// Creates a connectionist temporal classification (CTC) loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(CTCLoss(Blank, ZeroInfinity, Reduction)); } + + /// + /// Creates a connectionist temporal classification (CTC) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => CTCLoss(Blank, ZeroInfinity, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs index c363d71a..4514f4a8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Measures the loss given two input tensor and a label tensor with values 1 or -1 +/// Represents an operator that creates a cosine embedding loss module. /// -[Combinator] -[Description("Measures the loss given two input tensor and a label tensor with values 1 or -1")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a cosine embedding loss module.")] public class CosineEmbedding { /// - /// The margin parameter for the CosineEmbeddingLoss module. + /// The margin, which is a value in the range [-1, 1]. /// - [Description("The margin parameter for the CosineEmbeddingLoss module")] + [Description("The margin, which is a value in the range [-1, 1].")] public double Margin { get; set; } = 0D; /// - /// The reduction parameter for the CosineEmbeddingLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the CosineEmbeddingLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a CosineEmbeddingLoss. + /// Creates a cosine embedding loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(CosineEmbeddingLoss(Margin, Reduction)); } + + /// + /// Creates a cosine embedding loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => CosineEmbeddingLoss(Margin, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs index 12d3de41..955f74cd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs @@ -1,46 +1,77 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Computes the cross entropy loss between input logits and target. +/// Represents an operator that creates a cross entropy loss module. /// -[Combinator] -[Description("Computes the cross entropy loss between input logits and target.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class CrossEntropy +/// +/// See for more information. +/// +[Description("Creates a cross entropy loss module.")] +[TypeConverter(typeof(TensorOperatorConverter))] +public class CrossEntropy : IScalarTypeProvider { /// - /// The weight parameter for the CrossEntropyLoss module. + /// The manually specified rescaling weight given to each class. /// - [Description("The weight parameter for the CrossEntropyLoss module")] + [XmlIgnore] + [Description("The manually specified rescaling weight given to each class.")] + [TypeConverter(typeof(TensorConverter))] public Tensor Weight { get; set; } = null; /// - /// The ignore_index parameter for the CrossEntropyLoss module. + /// The values of the weight tensor in XML string format. /// - [Description("The ignore_index parameter for the CrossEntropyLoss module")] + [Browsable(false)] + [XmlElement(nameof(Weight))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string WeightXml + { + get => TensorConverter.ConvertToString(Weight, Type); + set => Weight = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// The index to ignore in the target which does not contribute to the input gradient. + /// + [Description("The index to ignore in the target which does not contribute to the input gradient.")] public long? IgnoreIndex { get; set; } = null; /// - /// The reduction parameter for the CrossEntropyLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the CrossEntropyLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a CrossEntropyLoss. + /// The data type of the tensor elements. /// - public IObservable> Process() + [Description("The data type of the tensor elements.")] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Creates a cross entropy loss module. + /// + /// + public IObservable> Process() { return Observable.Return(CrossEntropyLoss(Weight, IgnoreIndex, Reduction)); } -} + + /// + /// Creates a cross entropy loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => CrossEntropyLoss(Weight, IgnoreIndex, Reduction)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs index 2e07867e..a2cefb63 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs @@ -1,46 +1,55 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a GaussianNLLLoss module. +/// Represents an operator that creates a gaussian negative log likelihood (GaussianNLL) loss module. /// -[Combinator] -[Description("Creates a GaussianNLLLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a gaussian negative log likelihood (GaussianNLL) loss module.")] public class GaussianNegativeLogLikelihood { /// - /// The full parameter for the GaussianNLLLoss module. + /// Determines whether to include the constant term in the loss. /// - [Description("The full parameter for the GaussianNLLLoss module")] + [Description("Determines whether to include the constant term in the loss.")] public bool Full { get; set; } = false; /// - /// A value added to the denominator for numerical stability. + /// The value used to clamp the variance for numerical stability. /// - [Description("A value added to the denominator for numerical stability")] + [Description("The value used to clamp the variance for numerical stability")] public float Eps { get; set; } = 1E-08F; /// - /// The reduction parameter for the GaussianNLLLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the GaussianNLLLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a GaussianNLLLoss. + /// Creates a gaussian negative log likelihood (GaussianNLL) loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(GaussianNLLLoss(Full, Eps, Reduction)); } + + /// + /// Creates a gaussian negative log likelihood (GaussianNLL) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => GaussianNLLLoss(Full, Eps, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs index 570b3d93..36a86404 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a HingeEmbeddingLoss module. +/// Represents an operator that creates a hinge embedding loss module. /// -[Combinator] -[Description("Creates a HingeEmbeddingLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class HingeEmbeddingLoss +/// +/// See for more information. +/// +[Description("Creates a hinge embedding loss module.")] +public class HingeEmbedding { /// - /// The margin parameter for the HingeEmbeddingLoss module. + /// The margin, a non-negative float value. /// - [Description("The margin parameter for the HingeEmbeddingLoss module")] + [Description("The margin, a non-negative float value.")] public double Margin { get; set; } = 1D; /// - /// The reduction parameter for the HingeEmbeddingLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the HingeEmbeddingLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a HingeEmbeddingLoss. + /// Creates a hinge embedding loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(HingeEmbeddingLoss(Margin, Reduction)); } + + /// + /// Creates a hinge embedding loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => HingeEmbeddingLoss(Margin, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs index 16d81183..32e3667a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a HuberLoss module. +/// Represents an operator that creates a Huber loss module. /// -[Combinator] -[Description("Creates a HuberLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a Huber loss module.")] public class Huber { /// - /// The delta parameter for the HuberLoss module. + /// The threshold at which to change between delta-scaled L1 and L2 loss. /// - [Description("The delta parameter for the HuberLoss module")] + [Description("The threshold at which to change between delta-scaled L1 and L2 loss.")] public double Delta { get; set; } = 1D; /// - /// The reduction parameter for the HuberLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the HuberLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a HuberLoss. + /// Creates a Huber loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(HuberLoss(Delta, Reduction)); } + + /// + /// Creates a Huber loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => HuberLoss(Delta, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs index 21770e36..d7e09740 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a KLDivLoss module. +/// Represents an operator that creates a Kullback-Leibler divergence loss module. /// -[Combinator] -[Description("Creates a KLDivLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a Kullback-Leibler divergence loss module.")] public class KullbackLeiblerDivergence { /// - /// The log_target parameter for the KLDivLoss module. + /// Determines whether the target is given as log-probabilities. /// - [Description("The log_target parameter for the KLDivLoss module")] + [Description("Determines whether the target is given as log-probabilities.")] public bool LogTarget { get; set; } = true; /// - /// The reduction parameter for the KLDivLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the KLDivLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a KLDivLoss. + /// Creates a Kullback-Leibler divergence loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(KLDivLoss(LogTarget, Reduction)); } + + /// + /// Creates a Kullback-Leibler divergence loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => KLDivLoss(LogTarget, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs index 0925be56..0cdf6956 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a MarginRankingLoss module. +/// Represents an operator that creates a margin ranking loss module. /// -[Combinator] -[Description("Creates a MarginRankingLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a margin ranking loss module.")] public class MarginRanking { /// - /// The margin parameter for the MarginRankingLoss module. + /// The margin, a float value. /// - [Description("The margin parameter for the MarginRankingLoss module")] + [Description("The margin, a float value.")] public double Margin { get; set; } = 0D; /// - /// The reduction parameter for the MarginRankingLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the MarginRankingLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MarginRankingLoss. + /// Creates a margin ranking loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(MarginRankingLoss(Margin, Reduction)); } + + /// + /// Creates a margin ranking loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MarginRankingLoss(Margin, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs index 898c0e6b..02cbc543 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a L1Loss module. +/// Represents an operator that creates an L1 loss (L1Loss) module, known as mean absolute error (MAE). /// -[Combinator] -[Description("Creates a L1Loss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an L1 loss (L1Loss) module, known as mean absolute error (MAE).")] public class MeanAbsoluteError { /// - /// The reduction parameter for the L1Loss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the L1Loss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a L1Loss. + /// Creates an L1 loss (L1Loss) module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(L1Loss(Reduction)); } -} + + /// + /// Creates an L1 loss (L1Loss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => L1Loss(Reduction)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs index bbdaf42f..92d9c958 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a MSELoss module. +/// Represents an operator that creates a mean squared error (MSE) loss module. /// -[Combinator] -[Description("Creates a MSELoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a mean squared error (MSE) loss module.")] public class MeanSquaredError { /// - /// The reduction parameter for the MSELoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the MSELoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MSELoss. + /// Creates a mean squared error (MSE) loss module. /// + /// public IObservable> Process() { return Observable.Return(MSELoss(Reduction)); } + + /// + /// Creates a mean squared error (MSE) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MSELoss(Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs new file mode 100644 index 00000000..0c6e866c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs @@ -0,0 +1,83 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Loss; + +/// +/// Represents an operator that creates a multi-class margin loss (MultiMarginLoss) module. +/// +/// +/// See for more information. +/// +[Description("Creates a multi-class margin loss (MultiMarginLoss) module.")] +[TypeConverter(typeof(TensorOperatorConverter))] +public class MultiClassMargin : IScalarTypeProvider +{ + /// + /// The p parameter, can be either 1 or 2. + /// + [Description("The p parameter, can be either 1 or 2.")] + public int P { get; set; } = 1; + + /// + /// The margin parameter, a float value. + /// + [Description("The margin parameter, a float value.")] + public double Margin { get; set; } = 1D; + + /// + /// The manual rescaling weight given to each class. + /// + [XmlIgnore] + [Description("The manual rescaling weight given to each class.")] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; + + /// + /// The values of the weight tensor in XML string format. + /// + [Browsable(false)] + [XmlElement(nameof(Weight))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string WeightXml + { + get => TensorConverter.ConvertToString(Weight, Type); + set => Weight = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// The reduction type to apply to the output. + /// + [Description("The reduction type to apply to the output.")] + public Reduction Reduction { get; set; } = Reduction.Mean; + + /// + /// The data type of the tensor elements. + /// + [Description("The data type of the tensor elements.")] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Creates a multi-class margin loss (MultiMarginLoss) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(MultiMarginLoss(P, Margin, Weight, Reduction)); + } + + /// + /// Creates a multi-class margin loss (MultiMarginLoss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MultiMarginLoss(P, Margin, Weight, Reduction)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs index eb640076..bbdb9b02 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a MultiLabelMarginLoss module. +/// Represents an operator that creates a multi-class multi-classification margin loss (MultiLabelMarginLoss) module. /// -[Combinator] -[Description("Creates a MultiLabelMarginLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a multi-class multi-classification margin loss (MultiLabelMarginLoss) module.")] public class MultiLabelMargin { /// - /// The reduction parameter for the MultiLabelMarginLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the MultiLabelMarginLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MultiLabelMarginLoss. + /// Creates a multi-label margin loss (MultiLabelMarginLoss) module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(MultiLabelMarginLoss(Reduction)); } + + /// + /// Creates a multi-label margin loss (MultiLabelMarginLoss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MultiLabelMarginLoss(Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs index a44bca1c..428d0dc4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs @@ -1,40 +1,71 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a MultiLabelSoftMarginLoss module. +/// Represents an operator that creates a multi-label soft margin loss (MultiLabelSoftMarginLoss) module. /// -[Combinator] -[Description("Creates a MultiLabelSoftMarginLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MultiLabelSoftMargin +/// +/// See for more information. +/// +[Description("Creates a multi-label soft margin loss (MultiLabelSoftMarginLoss) module.")] +[TypeConverter(typeof(TensorOperatorConverter))] +public class MultiLabelSoftMargin : IScalarTypeProvider { /// - /// The weight parameter for the MultiLabelSoftMarginLoss module. + /// The manual rescaling weight given to each class. /// - [Description("The weight parameter for the MultiLabelSoftMarginLoss module")] - public torch.Tensor Weight { get; set; } = null; + [XmlIgnore] + [Description("The manual rescaling weight given to each class.")] + [TypeConverter(typeof(TensorConverter))] + public Tensor Weight { get; set; } = null; /// - /// The reduction parameter for the MultiLabelSoftMarginLoss module. + /// The values of the weight tensor in XML string format. /// - [Description("The reduction parameter for the MultiLabelSoftMarginLoss module")] + [Browsable(false)] + [XmlElement(nameof(Weight))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string WeightXml + { + get => TensorConverter.ConvertToString(Weight, Type); + set => Weight = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// The reduction type to apply to the output. + /// + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a MultiLabelSoftMarginLoss. + /// The data type of the tensor elements. /// - public IObservable> Process() + [Description("The data type of the tensor elements.")] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Creates a multi-label soft margin loss (MultiLabelSoftMarginLoss) module. + /// + /// + public IObservable> Process() { return Observable.Return(MultiLabelSoftMarginLoss(Weight, Reduction)); } -} + + /// + /// Creates a multi-label soft margin loss (MultiLabelSoftMarginLoss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MultiLabelSoftMarginLoss(Weight, Reduction)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs deleted file mode 100644 index ec85027e..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMarginLoss.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Loss; - -/// -/// Creates a MultiMarginLoss module. -/// -[Combinator] -[Description("Creates a MultiMarginLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MultiMarginLoss -{ - /// - /// The p parameter for the MultiMarginLoss module. - /// - [Description("The p parameter for the MultiMarginLoss module")] - public int P { get; set; } = 1; - - /// - /// The margin parameter for the MultiMarginLoss module. - /// - [Description("The margin parameter for the MultiMarginLoss module")] - public double Margin { get; set; } = 1D; - - /// - /// The weight parameter for the MultiMarginLoss module. - /// - [Description("The weight parameter for the MultiMarginLoss module")] - public torch.Tensor Weight { get; set; } = null; - - /// - /// The reduction parameter for the MultiMarginLoss module. - /// - [Description("The reduction parameter for the MultiMarginLoss module")] - public Reduction Reduction { get; set; } = Reduction.Mean; - - /// - /// Generates an observable sequence that creates a MultiMarginLoss. - /// - public IObservable> Process() - { - return Observable.Return(MultiMarginLoss(P, Margin, Weight, Reduction)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs index d566ec9c..742187bd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs @@ -1,40 +1,71 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a NLLLoss module. +/// Represents an operator that creates a negative log likelihood (NLL) loss module. /// -[Combinator] -[Description("Creates a NLLLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class NegativeLogLikelihood +/// +/// See for more information. +/// +[Description("Creates a negative log likelihood (NLL) loss module.")] +[TypeConverter(typeof(TensorOperatorConverter))] +public class NegativeLogLikelihood : IScalarTypeProvider { /// - /// The weight parameter for the NLLLoss module. + /// The manual rescaling weight given to each class. /// - [Description("The weight parameter for the NLLLoss module")] + [XmlIgnore] + [Description("The manual rescaling weight given to each class.")] + [TypeConverter(typeof(TensorConverter))] public Tensor Weight { get; set; } = null; /// - /// The reduction parameter for the NLLLoss module. + /// The values of the weight tensor in XML string format. /// - [Description("The reduction parameter for the NLLLoss module")] + [Browsable(false)] + [XmlElement(nameof(Weight))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string WeightXml + { + get => TensorConverter.ConvertToString(Weight, Type); + set => Weight = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// The reduction type to apply to the output. + /// + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a NLLLoss. + /// The data type of the tensor elements. /// - public IObservable> Process() + [Description("The data type of the tensor elements.")] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Creates a negative log likelihood (NLL) loss module. + /// + /// + public IObservable> Process() { return Observable.Return(NLLLoss(Weight, Reduction)); } + + /// + /// Creates a negative log likelihood (NLL) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => NLLLoss(Weight, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs index 4e966ba4..be310bdf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs @@ -1,52 +1,61 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a PoissonNLLLoss module. +/// Represents an operator that creates a poisson negative log likelihood (PoissonNLL) loss module. /// -[Combinator] -[Description("Creates a PoissonNLLLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a poisson negative log likelihood (PoissonNLL) loss module.")] public class PoissonNegativeLogLikelihood { /// - /// The log_input parameter for the PoissonNLLLoss module. + /// Determines whether to take the logarithm of the input. /// - [Description("The log_input parameter for the PoissonNLLLoss module")] + [Description("Determines whether to take the logarithm of the input.")] public bool LogInput { get; set; } = true; /// - /// The full parameter for the PoissonNLLLoss module. + /// Determines whether to compute the full loss. /// - [Description("The full parameter for the PoissonNLLLoss module")] + [Description("Determines whether to compute the full loss.")] public bool Full { get; set; } = false; /// - /// A value added to the denominator for numerical stability. + /// The small value added to avoid evaluating log(0) when LogInput is false. /// - [Description("A value added to the denominator for numerical stability")] + [Description("The small value added to avoid evaluating log(0) when LogInput is false.")] public float Eps { get; set; } = 1E-08F; /// - /// The reduction parameter for the PoissonNLLLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the PoissonNLLLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a PoissonNLLLoss. + /// Creates a poisson negative log likelihood (PoissonNLL) loss module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(PoissonNLLLoss(LogInput, Full, Eps, Reduction)); } + + /// + /// Creates a poisson negative log likelihood (PoissonNLL) loss module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => PoissonNLLLoss(LogInput, Full, Eps, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs index 67e15cec..e3cf02fe 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs @@ -1,40 +1,49 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a SmoothL1Loss module. +/// Represents an operator that creates a smooth L1 loss (SmoothL1Loss) module. /// -[Combinator] -[Description("Creates a SmoothL1Loss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a smooth L1 loss (SmoothL1Loss) module.")] public class SmoothL1 { /// - /// The reduction parameter for the SmoothL1Loss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the SmoothL1Loss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// The beta parameter for the SmoothL1Loss module. + /// The threshold at which to change between L1 and L2 loss. /// - [Description("The beta parameter for the SmoothL1Loss module")] + [Description("The threshold at which to change between L1 and L2 loss.")] public double Beta { get; set; } = 1D; /// - /// Generates an observable sequence that creates a SmoothL1Loss. + /// Creates a smooth L1 loss (SmoothL1Loss) module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(SmoothL1Loss(Reduction, Beta)); } + + /// + /// Creates a smooth L1 loss (SmoothL1Loss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => SmoothL1Loss(Reduction, Beta)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs index 1f961810..e0973470 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a SoftMarginLoss module. +/// Represents an operator that creates a soft margin loss (SoftMarginLoss) module. /// -[Combinator] -[Description("Creates a SoftMarginLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a soft margin loss (SoftMarginLoss) module.")] public class SoftMargin { /// - /// The reduction parameter for the SoftMarginLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the SoftMarginLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a SoftMarginLoss. + /// Creates a soft margin loss (SoftMarginLoss) module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(SoftMarginLoss(Reduction)); } + + /// + /// Creates a soft margin loss (SoftMarginLoss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => SoftMarginLoss(Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs index 128acb55..3b1e6e70 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs @@ -1,58 +1,67 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Loss; /// -/// Creates a TripletMarginLoss module. +/// Represents an operator that creates a triplet margin loss (TripletMarginLoss) module. /// -[Combinator] -[Description("Creates a TripletMarginLoss module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a triplet margin loss (TripletMarginLoss) module.")] public class TripletMargin { /// - /// The margin parameter for the TripletMarginLoss module. + /// The margin parameter, a float value. /// - [Description("The margin parameter for the TripletMarginLoss module")] + [Description("The margin parameter, a float value.")] public double Margin { get; set; } = 1D; /// - /// The p parameter for the TripletMarginLoss module. + /// The p parameter, the norm degree for pairwise distance. /// - [Description("The p parameter for the TripletMarginLoss module")] + [Description("The p parameter, the norm degree for pairwise distance.")] public long P { get; set; } = 2; /// - /// A value added to the denominator for numerical stability. + /// The value added to the denominator for numerical stability. /// - [Description("A value added to the denominator for numerical stability")] + [Description("The value added to the denominator for numerical stability")] public double Eps { get; set; } = 1E-06D; /// - /// The swap parameter for the TripletMarginLoss module. + /// Determines whether to use the swapped distance. /// - [Description("The swap parameter for the TripletMarginLoss module")] + [Description("Determines whether to use the swapped distance.")] public bool Swap { get; set; } = false; /// - /// The reduction parameter for the TripletMarginLoss module. + /// The reduction type to apply to the output. /// - [Description("The reduction parameter for the TripletMarginLoss module")] + [Description("The reduction type to apply to the output.")] public Reduction Reduction { get; set; } = Reduction.Mean; /// - /// Generates an observable sequence that creates a TripletMarginLoss. + /// Creates a triplet margin loss (TripletMarginLoss) module. /// - public IObservable> Process() + /// + public IObservable> Process() { return Observable.Return(TripletMarginLoss(Margin, P, Eps, Swap, Reduction)); } + + /// + /// Creates a triplet margin loss (TripletMarginLoss) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => TripletMarginLoss(Margin, P, Eps, Swap, Reduction)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs new file mode 100644 index 00000000..74996062 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs @@ -0,0 +1,63 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a loss module. +/// +[XmlInclude(typeof(Loss.BinaryCrossEntropy))] +[XmlInclude(typeof(Loss.BinaryCrossEntropyWithLogits))] +[XmlInclude(typeof(Loss.ConnectionistTemporalClassification))] +[XmlInclude(typeof(Loss.CosineEmbedding))] +[XmlInclude(typeof(Loss.CrossEntropy))] +[XmlInclude(typeof(Loss.GaussianNegativeLogLikelihood))] +[XmlInclude(typeof(Loss.HingeEmbedding))] +[XmlInclude(typeof(Loss.Huber))] +[XmlInclude(typeof(Loss.KullbackLeiblerDivergence))] +[XmlInclude(typeof(Loss.MarginRanking))] +[XmlInclude(typeof(Loss.MeanAbsoluteError))] +[XmlInclude(typeof(Loss.MeanSquaredError))] +[XmlInclude(typeof(Loss.MultiClassMargin))] +[XmlInclude(typeof(Loss.MultiLabelMargin))] +[XmlInclude(typeof(Loss.MultiLabelSoftMargin))] +[XmlInclude(typeof(Loss.NegativeLogLikelihood))] +[XmlInclude(typeof(Loss.PoissonNegativeLogLikelihood))] +[XmlInclude(typeof(Loss.SmoothL1))] +[XmlInclude(typeof(Loss.SoftMargin))] +[XmlInclude(typeof(Loss.TripletMargin))] +[DefaultProperty(nameof(LossModule))] +[Combinator] +[Description("Creates a loss module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class LossModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + string INamedElement.Name => $"Loss.{GetElementDisplayName(LossModule)}"; + + /// + /// Initializes a new instance of the class. + /// + public LossModuleBuilder() + { + Module = new Loss.GaussianNegativeLogLikelihood(); + } + + /// + /// Gets or sets the specific loss module. + /// + [DesignOnly(true)] + [DisplayName("Loss")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific loss module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object LossModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs new file mode 100644 index 00000000..74829146 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a continuously differentiable exponential linear unit (CELU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a continuously differentiable exponential linear unit (CELU) activation function.")] +public class ContinuouslyDifferentiableExponential +{ + /// + /// The alpha value for the CELU activation function. + /// + [Description("The alpha value for the CELU activation function.")] + public double Alpha { get; set; } = 1D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a continuously differentiable exponential linear unit (CELU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(CELU(Alpha, Inplace)); + } + + /// + /// Creates a continuously differentiable exponential linear unit (CELU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => CELU(Alpha, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs deleted file mode 100644 index 937c27dc..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponentialLinearUnit.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a CELU module. -/// -[Combinator] -[Description("Creates a CELU module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ContinuouslyDifferentiableExponentialLinearUnit -{ - /// - /// The alpha parameter for the CELU module. - /// - [Description("The alpha parameter for the CELU module")] - public double Alpha { get; set; } = 1D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a CELUModule module. - /// - public IObservable> Process() - { - return Observable.Return(CELU(Alpha, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs new file mode 100644 index 00000000..db323f09 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates an exponential linear unit (ELU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates an exponential linear unit (ELU) activation function.")] +public class Exponential +{ + /// + /// The alpha value for the ELU activation function. + /// + [Description("The alpha value for the ELU activation function")] + public double Alpha { get; set; } = 1D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place")] + public bool Inplace { get; set; } = false; + + /// + /// Creates an exponential linear unit (ELU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ELU(Alpha, Inplace)); + } + + /// + /// Creates an exponential linear unit (ELU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ELU(Alpha, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs deleted file mode 100644 index a006035b..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ExponentialLinearUnit.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a ELU activation function. -/// -[Combinator] -[Description("Creates a ELU activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ExponentialLinearUnit -{ - /// - /// The alpha parameter for the ELU module. - /// - [Description("The alpha parameter for the ELU module")] - public double Alpha { get; set; } = 1D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a ELUModule module. - /// - public IObservable> Process() - { - return Observable.Return(ELU(Alpha, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs new file mode 100644 index 00000000..d26fa8b9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a gated linear unit (GLU) module. +/// +/// +/// See for more information. +/// +[Description("Creates a gated linear unit (GLU) module.")] +public class Gated +{ + /// + /// The dimension on which to split the input tensor. + /// + [Description("The dimension on which to split the input tensor.")] + public long Dim { get; set; } = -1; + + /// + /// Creates a gated linear unit (GLU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(GLU(Dim)); + } + + /// + /// Creates a gated linear unit (GLU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => GLU(Dim)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs deleted file mode 100644 index 2d0559e2..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GatedLinearUnit.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a GLU module. -/// -[Combinator] -[Description("Creates a GLU module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class GatedLinearUnit -{ - /// - /// The dim parameter for the GLU module. - /// - [Description("The dim parameter for the GLU module")] - public long Dim { get; set; } = -1; - - /// - /// Generates an observable sequence that creates a GLUModule module. - /// - public IObservable> Process() - { - return Observable.Return(GLU(Dim)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs new file mode 100644 index 00000000..5ec93e28 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a gaussian error linear unit (GELU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a gaussian error linear unit (GELU) activation function.")] +public class GaussianError +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool InPlace { get; set; } = false; + + /// + /// Creates a gaussian error linear unit (GELU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(GELU(InPlace)); + } + + /// + /// Creates a gaussian error linear unit (GELU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => GELU(InPlace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs deleted file mode 100644 index 4a3c537d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianErrorLinearUnit.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a GELU activation function. -/// -[Combinator] -[Description("Creates a GELU activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class GaussianErrorLinearUnit -{ - /// - /// The inPlace parameter for the GELU module. - /// - [Description("The inPlace parameter for the GELU module")] - public bool InPlace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a GaussianErrorLinearUnit module. - /// - public IObservable> Process() - { - return Observable.Return(GELU(InPlace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs index dbab3044..6006e22e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Hardshrink module. +/// Represents an operator that creates a Hardshrink module. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a Hardshrink module.")] -[WorkflowElementCategory(ElementCategory.Source)] public class HardShrinkage { /// - /// The lambda parameter for the Hardshrink module. + /// The lambda parameter for the Hardshrink function. /// - [Description("The lambda parameter for the Hardshrink module")] + [Description("The lambda parameter for the Hardshrink function")] public double Lambda { get; set; } = 0.5D; /// - /// Generates an observable sequence that creates a HardshrinkModule module. + /// Creates a Hardshrink module. /// + /// public IObservable> Process() { return Observable.Return(Hardshrink(Lambda)); } + + /// + /// Creates a Hardshrink module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Hardshrink(Lambda)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs index 5db6598a..395a4709 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs @@ -1,21 +1,18 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Hardsigmoid module. +/// Represents an operator that creates a Hardsigmoid module. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a Hardsigmoid module.")] -[WorkflowElementCategory(ElementCategory.Source)] public class HardSigmoid { /// @@ -25,10 +22,22 @@ public class HardSigmoid public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a HardsigmoidModule module. + /// Creates a Hardsigmoid module. /// + /// public IObservable> Process() { return Observable.Return(Hardsigmoid(Inplace)); } + + /// + /// Creates a Hardsigmoid module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Hardsigmoid(Inplace)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs index 90aa5a67..3376470d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs @@ -1,33 +1,30 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Hardtanh module. +/// Represents an operator that creates a Hardtanh module. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a Hardtanh module.")] -[WorkflowElementCategory(ElementCategory.Source)] public class HardTanh { /// - /// The min_val parameter for the Hardtanh module. + /// The minimum value of the linear region range. /// - [Description("The min_val parameter for the Hardtanh module")] + [Description("The minimum value of the linear region range.")] public double MinVal { get; set; } = -1D; /// - /// The max_val parameter for the Hardtanh module. + /// The maximum value of the linear region range. /// - [Description("The max_val parameter for the Hardtanh module")] + [Description("The maximum value of the linear region range.")] public double MaxVal { get; set; } = 1D; /// @@ -37,10 +34,22 @@ public class HardTanh public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a HardtanhModule module. + /// Creates a Hardtanh module. /// + /// public IObservable> Process() { return Observable.Return(Hardtanh(MinVal, MaxVal, Inplace)); } -} + + /// + /// Creates a Hardtanh module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Hardtanh(MinVal, MaxVal, Inplace)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSwish.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Hardswish.cs similarity index 51% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSwish.cs rename to src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Hardswish.cs index 7018727c..e42df6be 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSwish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Hardswish.cs @@ -1,22 +1,19 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Hardswish module. +/// Represents an operator that creates a Hardswish module. /// -[Combinator] +/// +/// See for more information. +/// [Description("Creates a Hardswish module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class HardSwish +public class Hardswish { /// /// If set to true, will do this operation in-place. @@ -25,10 +22,22 @@ public class HardSwish public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a HardswishModule module. + /// Creates a Hardswish module. /// + /// public IObservable> Process() { return Observable.Return(Hardswish(Inplace)); } + + /// + /// Creates a Hardswish module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Hardswish(Inplace)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs new file mode 100644 index 00000000..3d35c5dd --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a leaky rectified linear unit (LeakyReLU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a leaky rectified linear unit (LeakyReLU) activation function.")] +public class LeakyRectified +{ + /// + /// The angle of the negative slope. + /// + [Description("The angle of the negative slope.")] + public double NegativeSlope { get; set; } = 0.01D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a leaky rectified linear unit (LeakyReLU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LeakyReLU(NegativeSlope, Inplace)); + } + + /// + /// Creates a leaky rectified linear unit (LeakyReLU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LeakyReLU(NegativeSlope, Inplace)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs deleted file mode 100644 index bf63072c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectifiedLinearUnit.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a LeakyReLU activation function. -/// -[Combinator] -[Description("Creates a LeakyReLU activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class LeakyRectifiedLinearUnit -{ - /// - /// The negative_slope parameter for the LeakyReLU module. - /// - [Description("The negative_slope parameter for the LeakyReLU module")] - public double NegativeSlope { get; set; } = 0.01D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a LeakyReLUModule module. - /// - public IObservable> Process() - { - return Observable.Return(LeakyReLU(NegativeSlope, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs index 8675d223..6bd4f15a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs @@ -1,28 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a LogSigmoid module. +/// Represents an operator that creates a log sigmoid module. /// -[Combinator] -[Description("Creates a LogSigmoid module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a log sigmoid module.")] public class LogSigmoid { /// - /// Generates an observable sequence that creates a LogSigmoidModule module. + /// Creates a LogSigmoid module. /// + /// public IObservable> Process() { return Observable.Return(LogSigmoid()); } + + /// + /// Creates a LogSigmoid module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LogSigmoid()); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs index ff723673..26529819 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a LogSoftmax activation function. +/// Represents an operator that creates a log softmax activation function. /// -[Combinator] -[Description("Creates a LogSoftmax activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a log softmax activation function.")] public class LogSoftmax { /// - /// The dim parameter for the LogSoftmax module. + /// The dimension along which LogSoftmax will be computed. /// - [Description("The dim parameter for the LogSoftmax module")] + [Description("The dimension along which LogSoftmax will be computed")] public long Dim { get; set; } /// - /// Generates an observable sequence that creates a LogSoftmaxModule module. + /// Creates a LogSoftmax module. /// + /// public IObservable> Process() { return Observable.Return(LogSoftmax(Dim)); } -} + + /// + /// Creates a LogSoftmax module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LogSoftmax(Dim)); + } +} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs index 1a28a3d4..d0e94789 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs @@ -1,28 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Mish module. +/// Represents an operator that creates a mish activation function. /// -[Combinator] -[Description("Creates a Mish module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a mish activation function.")] public class Mish { /// - /// Generates an observable sequence that creates a MishModule module. + /// Creates a Mish module. /// + /// public IObservable> Process() { return Observable.Return(Mish()); } + + /// + /// Creates a Mish module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Mish()); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs new file mode 100644 index 00000000..6e93b8db --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs @@ -0,0 +1,85 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a multi-head attention mechanism. +/// +/// +/// See for more information. +/// +[Description("Creates a multi-head attention mechanism.")] +public class MultiheadAttention +{ + /// + /// The dimension of the model. + /// + [Description("The dimension of the model.")] + public long EmbeddedDim { get; set; } + + /// + /// The number of parallel attention heads. + /// + [Description("The number of parallel attention heads.")] + public long NumHeads { get; set; } + + /// + /// The dropout probability on attended weights. + /// + [Description("The dropout probability on attended weights.")] + public double Dropout { get; set; } = 0D; + + /// + /// If true, adds a learnable bias to the input/output projection layers. + /// + [Description("If true, adds a learnable bias to the input/output projection layers.")] + public bool Bias { get; set; } = true; + + /// + /// If true, adds bias to the key and value sequences at dimension 0. + /// + [Description("If true, adds bias to the key and value sequences at dimension 0.")] + public bool AddBiasKeyValue { get; set; } = false; + + /// + /// If true, adds a new batch of zeros to the key and value sequences at dimension 1. + /// + [Description("If true, adds a new batch of zeros to the key and value sequences at dimension 1.")] + public bool AddZeroAttention { get; set; } = false; + + /// + /// The total number of features for keys. + /// + [Description("The total number of features for keys.")] + public long? KeyDim { get; set; } = null; + + /// + /// The total number of features for values. + /// + [Description("The total number of features for values.")] + public long? ValueDim { get; set; } = null; + + /// + /// Creates a Multi-head attention module. + /// + /// + public IObservable>> Process() + { + return Observable.Return(MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKeyValue, AddZeroAttention, KeyDim, ValueDim)); + } + + /// + /// Creates a Multi-head attention module. + /// + /// + /// + /// + public IObservable>> Process(IObservable source) + { + return source.Select(_ => MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKeyValue, AddZeroAttention, KeyDim, ValueDim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs deleted file mode 100644 index 12bb310f..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttentionModule.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a Multi-head attention mechanism. -/// -[Combinator] -[Description("Creates a Multi-head attention mechanism.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MultiheadAttentionModule -{ - /// - /// The embedded_dim parameter for the MultiheadAttention module. - /// - [Description("The embedded_dim parameter for the MultiheadAttention module")] - public long EmbeddedDim { get; set; } - - /// - /// The num_heads parameter for the MultiheadAttention module. - /// - [Description("The num_heads parameter for the MultiheadAttention module")] - public long NumHeads { get; set; } - - /// - /// The dropout parameter for the MultiheadAttention module. - /// - [Description("The dropout parameter for the MultiheadAttention module")] - public double Dropout { get; set; } = 0D; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The add_bias_kv parameter for the MultiheadAttention module. - /// - [Description("The add_bias_kv parameter for the MultiheadAttention module")] - public bool AddBiasKv { get; set; } = false; - - /// - /// The add_zero_attn parameter for the MultiheadAttention module. - /// - [Description("The add_zero_attn parameter for the MultiheadAttention module")] - public bool AddZeroAttn { get; set; } = false; - - /// - /// The kdim parameter for the MultiheadAttention module. - /// - [Description("The kdim parameter for the MultiheadAttention module")] - public long? Kdim { get; set; } = null; - - /// - /// The vdim parameter for the MultiheadAttention module. - /// - [Description("The vdim parameter for the MultiheadAttention module")] - public long? Vdim { get; set; } = null; - - /// - /// Generates an observable sequence that creates a MultiheadAttentionModule module. - /// - public IObservable>> Process() - { - return Observable.Return(MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKv, AddZeroAttn, Kdim, Vdim)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs new file mode 100644 index 00000000..5c4a33f7 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs @@ -0,0 +1,64 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a parametric rectified linear unit (PReLU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a parametric rectified linear unit (PReLU) activation function.")] +public class ParametricRectified +{ + /// + /// The number of parameters to learn. + /// + [Description("The number of parameters to learn.")] + public long NumParameters { get; set; } + + /// + /// The initial value for the learnable parameters. + /// + [Description("The initial value for the learnable parameters.")] + public double InitialValue { get; set; } = 0.25D; + + /// + /// The desired device of returned tensor. + /// + [XmlIgnore] + [Description("The desired device of returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of returned tensor. + /// + [Description("The desired data type of returned tensor.")] + [TypeConverter(typeof(ScalarTypeConverter))] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a parametric rectified linear unit (PReLU) activation function. + /// + /// + public IObservable> Process() + { + return Observable.Return(PReLU(NumParameters, InitialValue, Device, Type)); + } + + /// + /// Creates a parametric rectified linear unit (PReLU) activation function. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => PReLU(NumParameters, InitialValue, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs deleted file mode 100644 index ab31ff3b..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectifiedLinearUnit.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a PReLU module. -/// -[Combinator] -[Description("Creates a PReLU module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ParametricRectifiedLinearUnit -{ - /// - /// The num_parameters parameter for the PReLU module. - /// - [Description("The num_parameters parameter for the PReLU module")] - public long NumParameters { get; set; } - - /// - /// The init parameter for the PReLU module. - /// - [Description("The init parameter for the PReLU module")] - public double Init { get; set; } = 0.25D; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a PReLUModule module. - /// - public IObservable> Process() - { - return Observable.Return(PReLU(NumParameters, Init, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs new file mode 100644 index 00000000..1bc54c53 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a randomized leaky rectified linear unit (RReLU) module. +/// +/// +/// See for more information. +/// +[Description("Creates a randomized leaky rectified linear unit (RReLU) module.")] +public class RandomizedLeakyRectified +{ + /// + /// The lower bound of the uniform distribution. + /// + [Description("The lower bound of the uniform distribution.")] + public double Lower { get; set; } = 0.125D; + + /// + /// The upper bound of the uniform distribution. + /// + [Description("The upper bound of the uniform distribution.")] + public double Upper { get; set; } = 0.3333333333333333D; + + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a randomized leaky rectified linear unit (RReLU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(RReLU(Lower, Upper, Inplace)); + } + + /// + /// Creates a randomized leaky rectified linear unit (RReLU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => RReLU(Lower, Upper, Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs deleted file mode 100644 index 4e1c42cd..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectifiedLinearUnit.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a RReLU module. -/// -[Combinator] -[Description("Creates a RReLU module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class RandomizedLeakyRectifiedLinearUnit -{ - /// - /// The lower parameter for the RReLU module. - /// - [Description("The lower parameter for the RReLU module")] - public double Lower { get; set; } = 0.125D; - - /// - /// The upper parameter for the RReLU module. - /// - [Description("The upper parameter for the RReLU module")] - public double Upper { get; set; } = 0.3333333333333333D; - - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a RReLUModule module. - /// - public IObservable> Process() - { - return Observable.Return(RReLU(Lower, Upper, Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs new file mode 100644 index 00000000..1c540e02 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a rectified linear unit (ReLU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a rectified linear unit (ReLU) activation function.")] +public class Rectified +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a rectified linear unit (ReLU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReLU(Inplace)); + } + + /// + /// Creates a rectified linear unit (ReLU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReLU(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs new file mode 100644 index 00000000..2c6fe0c8 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a bounded rectified linear unit (ReLU6) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a bounded rectified linear unit (ReLU6) activation function.")] +public class RectifiedBounded +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a bounded rectified linear unit (ReLU6) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReLU6(Inplace)); + } + + /// + /// Creates a bounded rectified linear unit (ReLU6) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReLU6(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs deleted file mode 100644 index a550d54d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedLinearUnit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a ReLU activation function. -/// -[Combinator] -[Description("Creates a ReLU activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class RectifiedLinearUnit -{ - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// If set to true, will use the bounded version of ReLU with a maximum value of 6. - /// - [Description("If set to true, will use the bounded version of ReLU with a maximum value of 6")] - public bool Bounded { get; set; } = false; - - /// - /// Generates an observable sequence that creates a RectifiedLinearUnit module. - /// - public IObservable> Process() - { - if (Bounded) - return Observable.Return(ReLU6(Inplace)); - else - return Observable.Return(ReLU(Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs new file mode 100644 index 00000000..26d3e75c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a scaled exponential linear unit (SELU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a scaled exponential linear unit (SELU) activation function.")] +public class ScaledExponential +{ + /// + /// If set to true, will do this operation in-place. + /// + [Description("If set to true, will do this operation in-place.")] + public bool Inplace { get; set; } = false; + + /// + /// Creates a scaled exponential linear unit (SELU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(SELU(Inplace)); + } + + /// + /// Creates a scaled exponential linear unit (SELU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => SELU(Inplace)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs deleted file mode 100644 index 0d297f4d..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponentialLinearUnit.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a SELU activation function. -/// -[Combinator] -[Description("Creates a SELU activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ScaledExponentialLinearUnit -{ - /// - /// If set to true, will do this operation in-place. - /// - [Description("If set to true, will do this operation in-place")] - public bool Inplace { get; set; } = false; - - /// - /// Generates an observable sequence that creates a SELUModule module. - /// - public IObservable> Process() - { - return Observable.Return(SELU(Inplace)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs index dc6a1ee0..4c76e691 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs @@ -1,28 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Sigmoid activation function. +/// Represents an operator that creates a sigmoid activation function. /// -[Combinator] -[Description("Creates a Sigmoid activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SigmoidModule +/// +/// See for more information. +/// +[Description("Creates a sigmoid activation function.")] +public class Sigmoid { /// - /// Generates an observable sequence that creates a SigmoidModule module. + /// Creates a sigmoid module. /// + /// public IObservable> Process() { return Observable.Return(Sigmoid()); } + + /// + /// Creates a sigmoid module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Sigmoid()); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs new file mode 100644 index 00000000..f2575b71 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs @@ -0,0 +1,37 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a sigmoid weighted linear unit (SiLU) activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a sigmoid weighted linear unit (SiLU) activation function.")] +public class SigmoidWeighted +{ + /// + /// Creates a sigmoid weighted linear unit (SiLU) module. + /// + /// + public IObservable> Process() + { + return Observable.Return(SiLU()); + } + + /// + /// Creates a sigmoid weighted linear unit (SiLU) module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => SiLU()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs deleted file mode 100644 index c362ba2a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeightedLinearUnit.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a SiLU module. -/// -[Combinator] -[Description("Creates a SiLU module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SigmoidWeightedLinearUnit -{ - /// - /// Generates an observable sequence that creates a SiLUModule module. - /// - public IObservable> Process() - { - return Observable.Return(SiLU()); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs deleted file mode 100644 index 5710f34e..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a Softmax activation function. -/// -[Combinator] -[Description("Creates a Softmax activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SoftMax -{ - /// - /// The dim parameter for the Softmax module. - /// - [Description("The dim parameter for the Softmax module")] - public long Dim { get; set; } - - /// - /// Generates an observable sequence that creates a SoftmaxModule module. - /// - public IObservable> Process() - { - return Observable.Return(Softmax(Dim)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs deleted file mode 100644 index 06a12652..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMax2D.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a Softmax2d module. -/// -[Combinator] -[Description("Creates a Softmax2d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SoftMax2D -{ - /// - /// Generates an observable sequence that creates a Softmax2dModule module. - /// - public IObservable> Process() - { - return Observable.Return(Softmax2d()); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs deleted file mode 100644 index 9b09a191..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftMin.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a Softmin module. -/// -[Combinator] -[Description("Creates a Softmin module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SoftMin -{ - /// - /// The dim parameter for the Softmin module. - /// - [Description("The dim parameter for the Softmin module")] - public long Dim { get; set; } - - /// - /// Generates an observable sequence that creates a SoftminModule module. - /// - public IObservable> Process() - { - return Observable.Return(Softmin(Dim)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs deleted file mode 100644 index 372d47d6..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftPlus.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a Softplus module. -/// -[Combinator] -[Description("Creates a Softplus module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SoftPlus -{ - /// - /// The beta parameter for the Softplus module. - /// - [Description("The beta parameter for the Softplus module")] - public double Beta { get; set; } = 1D; - - /// - /// The threshold parameter for the Softplus module. - /// - [Description("The threshold parameter for the Softplus module")] - public double Threshold { get; set; } = 20D; - - /// - /// Generates an observable sequence that creates a SoftplusModule module. - /// - public IObservable> Process() - { - return Observable.Return(Softplus(Beta, Threshold)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs index e1cb1f5f..5ac57345 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Softshrink module. +/// Represents an operator that creates a soft shrinkage activation function. /// -[Combinator] -[Description("Creates a Softshrink module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a soft shrinkage activation function.")] public class SoftShrinkage { /// - /// The lambda parameter for the Softshrink module. + /// The lambda value for the softshrink formula. /// - [Description("The lambda parameter for the Softshrink module")] + [Description("The lambda value for the softshrink formula.")] public double Lambda { get; set; } = 0.5D; /// - /// Generates an observable sequence that creates a SoftshrinkModule module. + /// Creates a Softshrink module. /// + /// public IObservable> Process() { return Observable.Return(Softshrink(Lambda)); } + + /// + /// Creates a Softshrink module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Softshrink(Lambda)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs deleted file mode 100644 index 67cb333c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftSign.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; - -/// -/// Creates a Softsign module. -/// -[Combinator] -[Description("Creates a Softsign module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class SoftSign -{ - /// - /// Generates an observable sequence that creates a SoftsignModule module. - /// - public IObservable> Process() - { - return Observable.Return(Softsign()); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs new file mode 100644 index 00000000..84210259 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a softmax activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a softmax activation function.")] +public class Softmax +{ + /// + /// The dimension along which Softmax will be computed. + /// + [Description("The dimension along which Softmax will be computed.")] + public long Dim { get; set; } + + /// + /// Creates a Softmax module. + /// + /// + public IObservable> Process() + { + return Observable.Return(Softmax(Dim)); + } + + /// + /// Creates a Softmax module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Softmax(Dim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs new file mode 100644 index 00000000..54032a87 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs @@ -0,0 +1,37 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a 2d softmax activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a 2d softmax activation function.")] +public class Softmax2d +{ + /// + /// Creates a Softmax2d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(Softmax2d()); + } + + /// + /// Creates a Softmax2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Softmax2d()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs new file mode 100644 index 00000000..4408f853 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a softmin activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a softmin activation function.")] +public class Softmin +{ + /// + /// The dimension along which softmin will be computed. + /// + [Description("The dimension along which softmin will be computed.")] + public long Dim { get; set; } + + /// + /// Creates a Softmin module. + /// + /// + public IObservable> Process() + { + return Observable.Return(Softmin(Dim)); + } + + /// + /// Creates a Softmin module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Softmin(Dim)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs new file mode 100644 index 00000000..9e8a62eb --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a softplus activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a softplus module.")] +public class Softplus +{ + /// + /// The beta value for the softplus formula. + /// + [Description("The beta value for the softplus formula.")] + public double Beta { get; set; } = 1D; + + /// + /// The threshold value for which values above it use a linear function. + /// + [Description("The threshold value for which values above it use a linear function.")] + public double Threshold { get; set; } = 20D; + + /// + /// Creates a Softplus module. + /// + /// + public IObservable> Process() + { + return Observable.Return(Softplus(Beta, Threshold)); + } + + /// + /// Creates a Softplus module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Softplus(Beta, Threshold)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs new file mode 100644 index 00000000..4fac0516 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs @@ -0,0 +1,37 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; + +/// +/// Represents an operator that creates a softsign activation function. +/// +/// +/// See for more information. +/// +[Description("Creates a softsign activation function.")] +public class Softsign +{ + /// + /// Creates a Softsign module. + /// + /// + public IObservable> Process() + { + return Observable.Return(Softsign()); + } + + /// + /// Creates a Softsign module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Softsign()); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs index 2d090a6b..c1ba28aa 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs @@ -1,28 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Tanh activation function. +/// Represents an operator that creates a hyperbolic tangent (tanh) activation function. /// -[Combinator] -[Description("Creates a Tanh activation function.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a hyperbolic tangent (tanh) activation function.")] public class Tanh { /// - /// Generates an observable sequence that creates a TanhModule module. + /// Creates a Tanh module. /// + /// public IObservable> Process() { return Observable.Return(Tanh()); } + + /// + /// Creates a Tanh module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Tanh()); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs index b429afdf..64f8c6f1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs @@ -1,28 +1,37 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Tanhshrink module. +/// Represents an operator that creates a tanh shrinkage activation function. /// -[Combinator] -[Description("Creates a Tanhshrink module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a tanh shrinkage activation function.")] public class TanhShrinkage { /// - /// Generates an observable sequence that creates a TanhshrinkModule module. + /// Creates a Tanhshrink module. /// + /// public IObservable> Process() { return Observable.Return(Tanhshrink()); } + + /// + /// Creates a Tanhshrink module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Tanhshrink()); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs index ea695c12..607c3f04 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs @@ -1,46 +1,55 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// -/// Creates a Threshold module. +/// Represents an operator that creates a threshold activation function. /// -[Combinator] -[Description("Creates a Threshold module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Represents an operator that creates a threshold activation function.")] public class Threshold { /// - /// The threshold parameter for the Threshold module. + /// The threshold value. /// - [Description("The threshold parameter for the Threshold module")] + [Description("The threshold value.")] public double ThresholdValue { get; set; } /// - /// The value parameter for the Threshold module. + /// The value used to replace values below the threshold. /// - [Description("The value parameter for the Threshold module")] + [Description("The value used to replace values below the threshold.")] public double FillValue { get; set; } /// /// If set to true, will do this operation in-place. /// - [Description("If set to true, will do this operation in-place")] + [Description("If set to true, will do this operation in-place.")] public bool Inplace { get; set; } = false; /// - /// Generates an observable sequence that creates a ThresholdModule module. + /// Creates a Threshold module. /// + /// public IObservable> Process() { return Observable.Return(Threshold(ThresholdValue, FillValue, Inplace)); } + + /// + /// Creates a Threshold module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Threshold(ThresholdValue, FillValue, Inplace)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs new file mode 100644 index 00000000..1e6e301f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a 1D batch normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D batch normalization module.")] +public class BatchNorm1d +{ + /// + /// The number of features or channels of the input. + /// + [Description("The number of features or channels of the input.")] + public long Features { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// The value used for computing the running mean and running variance. + /// + [Description("The value used for computing the running mean and running variance.")] + public double Momentum { get; set; } = 0.1D; + + /// + /// If set to true, this module has learnable affine parameters. + /// + [Description("If set to true, this module has learnable affine parameters.")] + public bool Affine { get; set; } = true; + + /// + /// If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics. + /// + [Description("If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics.")] + public bool TrackRunningStats { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a BatchNorm1d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } + + /// + /// Creates a BatchNorm1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs new file mode 100644 index 00000000..ef7f0496 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a 2D batch normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D batch normalization module.")] +public class BatchNorm2d +{ + /// + /// The number of features of the input. + /// + [Description("The number of features of the input.")] + public long Features { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// The value used for computing the running mean and running variance. + /// + [Description("The value used for computing the running mean and running variance.")] + public double Momentum { get; set; } = 0.1D; + + /// + /// If set to true, this module has learnable affine parameters. + /// + [Description("If set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = true; + + /// + /// If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics. + /// + [Description("If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics.")] + public bool TrackRunningStats { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a BatchNorm2d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } + + /// + /// Creates a BatchNorm2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs new file mode 100644 index 00000000..88df1447 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a 3D batch normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D batch normalization module.")] +public class BatchNorm3d +{ + /// + /// The number of features of the input. + /// + [Description("The number of features of the input.")] + public long Features { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// The value used for computing the running mean and running variance. + /// + [Description("The value used for computing the running mean and running variance.")] + public double Momentum { get; set; } = 0.1D; + + /// + /// If set to true, this module has learnable affine parameters. + /// + [Description("If set to true, this module has learnable affine parameters")] + public bool Affine { get; set; } = true; + + /// + /// If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics. + /// + [Description("If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics.")] + public bool TrackRunningStats { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a BatchNorm3d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } + + /// + /// Creates a BatchNorm3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs deleted file mode 100644 index 6d33685a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNormalization.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Normalization; - -/// -/// Creates a 1D adaptive average pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class BatchNormalization -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The features parameter for the BatchNorm1d module. - /// - [Description("The features parameter for the BatchNorm1d module")] - public long Features { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = true; - - /// - /// The track_running_stats parameter for the BatchNorm1d module. - /// - [Description("The track_running_stats parameter for the BatchNorm1d module")] - public bool TrackRunningStats { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), - Dimensions.Two => Observable.Return(BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), - Dimensions.Three => Observable.Return(BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs new file mode 100644 index 00000000..52ae8d51 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs @@ -0,0 +1,75 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a group normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a group normalization module.")] +public class GroupNorm +{ + /// + /// The number of groups to separate the channels into. + /// + [Description("The number of groups to separate the channels into.")] + public long NumGroups { get; set; } + + /// + /// The number of channels in the input. + /// + [Description("The number of channels in the input.")] + public long NumChannels { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// A boolean value that when set to true, this module has learnable per-channel affine parameters initialized to ones (for weights) and zeros (for biases). + /// + [Description("A boolean value that when set to true, this module has learnable per-channel affine parameters initialized to ones (for weights) and zeros (for biases).")] + public bool Affine { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a GroupNorm module. + /// + /// + public IObservable> Process() + { + return Observable.Return(GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); + } + + /// + /// Creates a GroupNorm module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs deleted file mode 100644 index a60bcaaa..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNormalization.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Normalization; - -/// -/// Creates a Group normalization. -/// -[Combinator] -[Description("Creates a Group normalization.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class GroupNormalization -{ - /// - /// The num_groups parameter for the GroupNorm module. - /// - [Description("The num_groups parameter for the GroupNorm module")] - public long NumGroups { get; set; } - - /// - /// The num_channels parameter for the GroupNorm module. - /// - [Description("The num_channels parameter for the GroupNorm module")] - public long NumChannels { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a GroupNormModule module. - /// - public IObservable> Process() - { - return Observable.Return(GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs new file mode 100644 index 00000000..b5b44a4c --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a 1d instance normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1d instance normalization module.")] +public class InstanceNorm1d +{ + /// + /// The number of features or channels in the input tensor. + /// + [Description("The number of features or channels in the input tensor.")] + public long Features { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// The value used for computing the running mean and running variance. + /// + [Description("The value used for computing the running mean and running variance.")] + public double Momentum { get; set; } = 0.1D; + + /// + /// If set to true, this module has learnable affine parameters. + /// + [Description("If set to true, this module has learnable affine parameters.")] + public bool Affine { get; set; } = false; + + /// + /// If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics. + /// + [Description("If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics.")] + public bool TrackRunningStats { get; set; } = false; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an InstanceNorm1d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } + + /// + /// Creates an InstanceNorm1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs new file mode 100644 index 00000000..22cbb7da --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a 2d instance normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2d instance normalization module.")] +public class InstanceNorm2d +{ + /// + /// The number of features in the input tensor. + /// + [Description("The number of features in the input tensor.")] + public long Features { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// The value used for computing the running mean and running variance. + /// + [Description("The value used for computing the running mean and running variance.")] + public double Momentum { get; set; } = 0.1D; + + /// + /// If set to true, this module has learnable affine parameters. + /// + [Description("If set to true, this module has learnable affine parameters.")] + public bool Affine { get; set; } = false; + + /// + /// If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics. + /// + [Description("If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics.")] + public bool TrackRunningStats { get; set; } = false; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an InstanceNorm2d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } + + /// + /// Creates an InstanceNorm2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs new file mode 100644 index 00000000..afe9bbe1 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a 3d instance normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3d instance normalization module.")] +public class InstanceNorm3d +{ + /// + /// The number of features in the input tensor. + /// + [Description("The number of features in the input tensor.")] + public long Features { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// The value used for computing the running mean and running variance. + /// + [Description("The value used for computing the running mean and running variance.")] + public double Momentum { get; set; } = 0.1D; + + /// + /// If set to true, this module has learnable affine parameters. + /// + [Description("If set to true, this module has learnable affine parameters.")] + public bool Affine { get; set; } = false; + + /// + /// If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics. + /// + [Description("If set to true, this module tracks the running mean and variance, otherwise, the module will use batch statistics.")] + public bool TrackRunningStats { get; set; } = false; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an InstanceNorm3d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } + + /// + /// Creates an InstanceNorm3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs deleted file mode 100644 index 8015ffcd..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNormalization.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Normalization; - -/// -/// Creates a dropout layer. -/// -[Combinator] -[Description("Creates a fractional max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class InstanceNormalization -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.Two; - - /// - /// The features parameter for the InstanceNorm1d module. - /// - [Description("The features parameter for the InstanceNorm1d module")] - public long Features { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0.1D; - - /// - /// A boolean value that when set to true, this module has learnable affine parameters. - /// - [Description("A boolean value that when set to true, this module has learnable affine parameters")] - public bool Affine { get; set; } = false; - - /// - /// The track_running_stats parameter for the InstanceNorm1d module. - /// - [Description("The track_running_stats parameter for the InstanceNorm1d module")] - public bool TrackRunningStats { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a Dropout module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), - Dimensions.Two => Observable.Return(InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), - Dimensions.Three => Observable.Return(InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs new file mode 100644 index 00000000..c8c176f4 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs @@ -0,0 +1,76 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a layer normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a layer normalization module.")] +public class LayerNorm +{ + /// + /// The input shape from an expected input of size `[* x normalized_shape[0] x normalized_shape[1] x ... x normalized_shape[-1]]`. + /// + [Description("The input shape from an expected input of size `[* x normalized_shape[0] x normalized_shape[1] x ... x normalized_shape[-1]]`.")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] NormalizedShape { get; set; } + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-05D; + + /// + /// If true, this module has learnable per-element affine parameters initialized to ones (for weights) and zeros (for biases). + /// + [Description("If true, this module has learnable per-element affine parameters initialized to ones (for weights) and zeros (for biases).")] + public bool ElementwiseAffine { get; set; } = true; + + /// + /// If true, adds a learnable bias to the output. + /// + [Description("If true, adds a learnable bias to the output.")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a LayerNorm module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); + } + + /// + /// Creates a LayerNorm module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs deleted file mode 100644 index d6268ade..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNormalization.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Normalization; - -/// -/// Creates a Layer normalization. -/// -[Combinator] -[Description("Creates a Layer normalization.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class LayerNormalization -{ - /// - /// The normalized_shape parameter for the LayerNorm module. - /// - [Description("The normalized_shape parameter for the LayerNorm module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] NormalizedShape { get; set; } - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-05D; - - /// - /// The elementwise_affine parameter for the LayerNorm module. - /// - [Description("The elementwise_affine parameter for the LayerNorm module")] - public bool ElementwiseAffine { get; set; } = true; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a LayerNormModule module. - /// - public IObservable> Process() - { - return Observable.Return(LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs new file mode 100644 index 00000000..c1ab17c1 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs @@ -0,0 +1,61 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Normalization; + +/// +/// Represents an operator that creates a local response normalization module. +/// +/// +/// See for more information. +/// +[Description("Creates a local response normalization module.")] +public class LocalResponseNorm +{ + /// + /// The number of neighboring channels used for normalization. + /// + [Description("The number of neighboring channels used for normalization.")] + public long Size { get; set; } + + /// + /// The alpha parameter, which serves as a scaling factor. + /// + [Description("The alpha parameter, which serves as a scaling factor.")] + public double Alpha { get; set; } = 0.0001D; + + /// + /// The beta parameter, which serves as an exponent. + /// + [Description("The beta parameter, which serves as an exponent.")] + public double Beta { get; set; } = 0.75D; + + /// + /// The k parameter, which serves as an additive constant. + /// + [Description("The k parameter, which serves as an additive constant.")] + public double K { get; set; } = 1D; + + /// + /// Creates a LocalResponseNorm module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LocalResponseNorm(Size, Alpha, Beta, K)); + } + + /// + /// Creates a LocalResponseNorm module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LocalResponseNorm(Size, Alpha, Beta, K)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs deleted file mode 100644 index dc6ab30a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNormalization.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Normalization; - -/// -/// Creates a LocalResponseNorm module. -/// -[Combinator] -[Description("Creates a LocalResponseNorm module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class LocalResponseNormalization -{ - /// - /// The size parameter for the LocalResponseNorm module. - /// - [Description("The size parameter for the LocalResponseNorm module")] - public long Size { get; set; } - - /// - /// The alpha parameter for the LocalResponseNorm module. - /// - [Description("The alpha parameter for the LocalResponseNorm module")] - public double Alpha { get; set; } = 0.0001D; - - /// - /// The beta parameter for the LocalResponseNorm module. - /// - [Description("The beta parameter for the LocalResponseNorm module")] - public double Beta { get; set; } = 0.75D; - - /// - /// The k parameter for the LocalResponseNorm module. - /// - [Description("The k parameter for the LocalResponseNorm module")] - public double K { get; set; } = 1D; - - /// - /// Generates an observable sequence that creates a LocalResponseNormModule module. - /// - public IObservable> Process() - { - return Observable.Return(LocalResponseNorm(Size, Alpha, Beta, K)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs new file mode 100644 index 00000000..8126a798 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs @@ -0,0 +1,50 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a normalization module. +/// +[XmlInclude(typeof(Normalization.BatchNorm1d))] +[XmlInclude(typeof(Normalization.BatchNorm2d))] +[XmlInclude(typeof(Normalization.BatchNorm3d))] +[XmlInclude(typeof(Normalization.GroupNorm))] +[XmlInclude(typeof(Normalization.InstanceNorm1d))] +[XmlInclude(typeof(Normalization.InstanceNorm2d))] +[XmlInclude(typeof(Normalization.InstanceNorm3d))] +[XmlInclude(typeof(Normalization.LayerNorm))] +[XmlInclude(typeof(Normalization.LocalResponseNorm))] +[DefaultProperty(nameof(NormalizationModule))] +[Combinator] +[Description("Creates a normalization module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class NormalizationModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public NormalizationModuleBuilder() + { + Module = new Normalization.BatchNorm1d(); + } + + /// + /// Gets or sets the specific normalization module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific normalization module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object NormalizationModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs index 241d6999..a88fc783 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs @@ -1,15 +1,8 @@ using System; -using System.Linq; using System.ComponentModel; -using System.Reactive; -using System.Reactive.Disposables; using System.Reactive.Linq; -using System.Xml.Serialization; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; using static TorchSharp.torch.optim.lr_scheduler; -using System.Threading.Tasks; namespace Bonsai.ML.Torch.NeuralNets; @@ -17,13 +10,12 @@ namespace Bonsai.ML.Torch.NeuralNets; /// Represents an operator that performs a single optimization step using the specified optimizer. /// [Combinator] -[ResetCombinator] [Description("Performs a single optimization step.")] [WorkflowElementCategory(ElementCategory.Sink)] public class OptimizationStep { /// - /// Performs a single optimization step using the specified optimizer. + /// Performs a single step using the specified optimizer. /// /// /// @@ -31,4 +23,14 @@ public class OptimizationStep { return source.Do(input => input.step()); } + + /// + /// Performs a single step using the specified learning rate scheduler. + /// + /// + /// + public IObservable Process(IObservable source) + { + return source.Do(input => input.step()); + } } \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs deleted file mode 100644 index eff80e97..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaDelta.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a Adadelta optimizer. -/// -[Combinator] -[Description("Creates a Adadelta optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaDelta -{ - /// - /// The parameters parameter for the Adadelta module. - /// - [Description("The parameters parameter for the Adadelta module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The lr parameter for the Adadelta module. - /// - [Description("The lr parameter for the Adadelta module")] - public double Lr { get; set; } = 1D; - - /// - /// The rho parameter for the Adadelta module. - /// - [Description("The rho parameter for the Adadelta module")] - public double Rho { get; set; } = 0.9D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-06D; - - /// - /// The weight_decay parameter for the Adadelta module. - /// - [Description("The weight_decay parameter for the Adadelta module")] - public double WeightDecay { get; set; } = 0D; - - /// - /// The maximize parameter for the Adadelta module. - /// - [Description("The maximize parameter for the Adadelta module")] - public bool Maximize { get; set; } = false; - - /// - /// Generates an observable sequence that creates a AdadeltaOptimizer. - /// - public IObservable Process() - { - return Observable.Return(Adadelta(Parameters, Lr, Rho, Eps, WeightDecay, Maximize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs deleted file mode 100644 index 466fa3ae..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaGrad.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a Adagrad optimizer. -/// -[Combinator] -[Description("Creates a Adagrad optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaGrad -{ - /// - /// The parameters parameter for the Adagrad module. - /// - [Description("The parameters parameter for the Adagrad module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The lr parameter for the Adagrad module. - /// - [Description("The lr parameter for the Adagrad module")] - public double Lr { get; set; } = 0.01D; - - /// - /// The lr_decay parameter for the Adagrad module. - /// - [Description("The lr_decay parameter for the Adagrad module")] - public double LrDecay { get; set; } = 0D; - - /// - /// The weight_decay parameter for the Adagrad module. - /// - [Description("The weight_decay parameter for the Adagrad module")] - public double WeightDecay { get; set; } = 0D; - - /// - /// The initial_accumulator_value parameter for the Adagrad module. - /// - [Description("The initial_accumulator_value parameter for the Adagrad module")] - public double InitialAccumulatorValue { get; set; } = 0D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-10D; - - /// - /// Generates an observable sequence that creates a AdagradOptimizer. - /// - public IObservable Process() - { - return Observable.Return(Adagrad(Parameters, Lr, LrDecay, WeightDecay, InitialAccumulatorValue, Eps)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs deleted file mode 100644 index 3e19ddca..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdaMax.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a Adamax optimizer. -/// -[Combinator] -[Description("Creates a Adamax optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaMax -{ - /// - /// The parameters parameter for the Adamax module. - /// - [Description("The parameters parameter for the Adamax module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The lr parameter for the Adamax module. - /// - [Description("The lr parameter for the Adamax module")] - public double Lr { get; set; } = 0.002D; - - /// - /// The beta1 parameter for the Adamax module. - /// - [Description("The beta1 parameter for the Adamax module")] - public double Beta1 { get; set; } = 0.9D; - - /// - /// The beta2 parameter for the Adamax module. - /// - [Description("The beta2 parameter for the Adamax module")] - public double Beta2 { get; set; } = 0.999D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08D; - - /// - /// The weight_decay parameter for the Adamax module. - /// - [Description("The weight_decay parameter for the Adamax module")] - public double WeightDecay { get; set; } = 0D; - - /// - /// Generates an observable sequence that creates a AdamaxOptimizer. - /// - public IObservable Process() - { - return Observable.Return(Adamax(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs new file mode 100644 index 00000000..38dc3f50 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates an Adadelta optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates an Adadelta optimizer.")] +public class Adadelta +{ + /// + /// The learning rate coefficient that scales delta before it is applied to the parameters. + /// + [Description("The learning rate coefficient that scales delta before it is applied to the parameters.")] + public double LearningRate { get; set; } = 1D; + + /// + /// The coefficient used to calculate a running average of squared gradients. + /// + [Description("The coefficient used to calculate a running average of squared gradients.")] + public double Rho { get; set; } = 0.9D; + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-06D; + + /// + /// The weight decay coefficient, which adds L2 regularization to the loss. + /// + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] + public double WeightDecay { get; set; } = 0D; + + /// + /// If set to true, performs maximization instead of minimization of the params based on the objective. + /// + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] + public bool Maximize { get; set; } = false; + + /// + /// Creates an Adadelta optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => Adadelta(parameters, LearningRate, Rho, Eps, WeightDecay, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs new file mode 100644 index 00000000..39e265f8 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates an Adagrad optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates an Adagrad optimizer.")] +public class Adagrad +{ + /// + /// The learning rate coefficient. + /// + [Description("The learning rate coefficient.")] + public double LearningRate { get; set; } = 0.01D; + + /// + /// The learning rate decay. + /// + [Description("The learning rate decay.")] + public double LearningRateDecay { get; set; } = 0D; + + /// + /// The weight decay coefficient, which adds L2 regularization to the loss. + /// + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] + public double WeightDecay { get; set; } = 0D; + + /// + /// The initial value of the sum of squares of gradients. + /// + [Description("The initial value of the sum of squares of gradients.")] + public double InitialAccumulatorValue { get; set; } = 0D; + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-10D; + + /// + /// Creates an Adagrad optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => Adagrad(parameters, LearningRate, LearningRateDecay, WeightDecay, InitialAccumulatorValue, Eps)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs index 1ccb72b1..5086a58f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs @@ -2,77 +2,71 @@ using System.ComponentModel; using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// -/// Creates a Adam optimizer. +/// Represents an operator that creates an Adam optimizer. /// -[Combinator] -[Description("Creates a Adam optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an Adam optimizer.")] public class Adam { /// - /// The parameters parameter for the Adam module. + /// The learning rate coefficient. /// - [Description("The parameters parameter for the Adam module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } + [Description("The learning rate coefficient.")] + public double LearningRate { get; set; } = 0.001D; /// - /// The lr parameter for the Adam module. + /// The beta coefficient for computing the running average of the gradient. /// - [Description("The lr parameter for the Adam module")] - public double Lr { get; set; } = 0.001D; + [Description("The beta coefficient for computing the running average of the gradient.")] + public double BetaGradient { get; set; } = 0.9D; /// - /// The beta1 parameter for the Adam module. + /// The beta coefficient for computing the running average of the squared gradient. /// - [Description("The beta1 parameter for the Adam module")] - public double Beta1 { get; set; } = 0.9D; + [Description("The beta coefficient for computing the running average of the squared gradient.")] + public double BetaSquaredGradient { get; set; } = 0.999D; /// - /// The beta2 parameter for the Adam module. + /// The value added to the denominator for numerical stability. /// - [Description("The beta2 parameter for the Adam module")] - public double Beta2 { get; set; } = 0.999D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] + [Description("The value added to the denominator for numerical stability.")] public double Eps { get; set; } = 1E-08D; /// - /// The weight_decay parameter for the Adam module. + /// The weight decay coefficient, which adds L2 regularization to the loss. /// - [Description("The weight_decay parameter for the Adam module")] + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] public double WeightDecay { get; set; } = 0D; /// - /// The amsgrad parameter for the Adam module. + /// If set to true, uses the AMSGrad variant of this algorithm. /// - [Description("The amsgrad parameter for the Adam module")] + [Description("If set to true, uses the AMSGrad variant of this algorithm.")] public bool Amsgrad { get; set; } = false; /// - /// The maximize parameter for the Adam module. + /// If set to true, performs maximization instead of minimization of the params based on the objective. /// - [Description("The maximize parameter for the Adam module")] + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a AdamOptimizer. + /// Creates an Adam optimizer from the input parameter collection. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable { - return Observable.Return(Adam(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay, Amsgrad, Maximize)); + return source.Select(parameters => Adam(parameters, LearningRate, BetaGradient, BetaSquaredGradient, Eps, WeightDecay, Amsgrad, Maximize)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs index 3c31c419..d34e819d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs @@ -2,77 +2,71 @@ using System.ComponentModel; using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// -/// Creates a AdamW optimizer. +/// Represents an operator that creates an AdamW optimizer. /// -[Combinator] -[Description("Creates a AdamW optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an AdamW optimizer.")] public class AdamW { /// - /// The parameters parameter for the AdamW module. + /// The learning rate. /// - [Description("The parameters parameter for the AdamW module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } + [Description("The learning rate.")] + public double LearningRate { get; set; } = 0.001D; /// - /// The lr parameter for the AdamW module. + /// The beta coefficient for computing the running average of the gradient. /// - [Description("The lr parameter for the AdamW module")] - public double Lr { get; set; } = 0.001D; + [Description("The beta coefficient for computing the running average of the gradient.")] + public double BetaGradient { get; set; } = 0.9D; /// - /// The beta1 parameter for the AdamW module. + /// The beta coefficient for computing the running average of the squared gradient. /// - [Description("The beta1 parameter for the AdamW module")] - public double Beta1 { get; set; } = 0.9D; + [Description("The beta coefficient for computing the running average of the squared gradient.")] + public double BetaSquaredGradient { get; set; } = 0.999D; /// - /// The beta2 parameter for the AdamW module. + /// The value added to the denominator for numerical stability. /// - [Description("The beta2 parameter for the AdamW module")] - public double Beta2 { get; set; } = 0.999D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] + [Description("The value added to the denominator for numerical stability.")] public double Eps { get; set; } = 1E-08D; /// - /// The weight_decay parameter for the AdamW module. + /// The weight decay coefficient, which adds L2 regularization to the loss. /// - [Description("The weight_decay parameter for the AdamW module")] + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] public double WeightDecay { get; set; } = 0D; /// - /// The amsgrad parameter for the AdamW module. + /// If set to true, uses the AMSGrad variant of this algorithm. /// - [Description("The amsgrad parameter for the AdamW module")] + [Description("If set to true, uses the AMSGrad variant of this algorithm.")] public bool Amsgrad { get; set; } = false; /// - /// The maximize parameter for the AdamW module. + /// If set to true, performs maximization instead of minimization of the params based on the objective. /// - [Description("The maximize parameter for the AdamW module")] + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] public bool Maximize { get; set; } = false; /// - /// Generates an observable sequence that creates a AdamWOptimizer. + /// Creates an AdamW optimizer from the input parameter collection. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable { - return Observable.Return(AdamW(Parameters, Lr, Beta1, Beta2, Eps, WeightDecay, Amsgrad, Maximize)); + return source.Select(parameters => AdamW(parameters, LearningRate, BetaGradient, BetaSquaredGradient, Eps, WeightDecay, Amsgrad, Maximize)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs new file mode 100644 index 00000000..3870bdc6 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs @@ -0,0 +1,60 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates an Adamax optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates an Adamax optimizer.")] +public class Adamax +{ + /// + /// The learning rate coefficient. + /// + [Description("The learning rate coefficient.")] + public double LearningRate { get; set; } = 0.002D; + + /// + /// The beta coefficient for computing the running average of the gradient. + /// + [Description("The beta coefficient for computing the running average of the gradient.")] + public double BetaGradient { get; set; } = 0.9D; + + /// + /// The beta coefficient for computing the running average of the squared gradient. + /// + [Description("The beta coefficient for computing the running average of the squared gradient.")] + public double BetaSquaredGradient { get; set; } = 0.999D; + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-08D; + + /// + /// The weight decay coefficient, which adds L2 regularization to the loss. + /// + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] + public double WeightDecay { get; set; } = 0D; + + /// + /// Creates an Adamax optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => Adamax(parameters, LearningRate, BetaGradient, BetaSquaredGradient, Eps, WeightDecay)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs deleted file mode 100644 index ff9bba2c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Asgd.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a ASGD optimizer. -/// -[Combinator] -[Description("Creates a ASGD optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Asgd -{ - /// - /// The parameters parameter for the ASGD module. - /// - [Description("The parameters parameter for the ASGD module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The lr parameter for the ASGD module. - /// - [Description("The lr parameter for the ASGD module")] - public double Lr { get; set; } = 0.001D; - - /// - /// The lambd parameter for the ASGD module. - /// - [Description("The lambd parameter for the ASGD module")] - public double Lambd { get; set; } = 0.0001D; - - /// - /// The alpha parameter for the ASGD module. - /// - [Description("The alpha parameter for the ASGD module")] - public double Alpha { get; set; } = 0.75D; - - /// - /// The t0 parameter for the ASGD module. - /// - [Description("The t0 parameter for the ASGD module")] - public double T0 { get; set; } = 1000000D; - - /// - /// The weight_decay parameter for the ASGD module. - /// - [Description("The weight_decay parameter for the ASGD module")] - public double WeightDecay { get; set; } = 0D; - - /// - /// The maximize parameter for the ASGD module. - /// - [Description("The maximize parameter for the ASGD module")] - public bool Maximize { get; set; } = false; - - /// - /// Generates an observable sequence that creates a ASGDOptimizer. - /// - public IObservable Process() - { - return Observable.Return(ASGD(Parameters, Lr, Lambd, Alpha, T0, WeightDecay, Maximize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs new file mode 100644 index 00000000..6a69b73b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates an averaged stochastic gradient descent (ASGD) optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates an averaged stochastic gradient descent (ASGD) optimizer.")] +public class AveragedStochasticGradientDescent +{ + /// + /// The learning rate. + /// + [Description("The learning rate.")] + public double LearningRate { get; set; } = 0.001D; + + /// + /// The lambda parameter, which determines the decay term. + /// + [Description("The lambda parameter, which determines the decay term.")] + public double Lambda { get; set; } = 0.0001D; + + /// + /// The alpha parameter, which controls the power law decay. + /// + [Description("The alpha parameter, which controls the power law decay.")] + public double Alpha { get; set; } = 0.75D; + + /// + /// The point when averaging starts. + /// + [Description("The point when averaging starts.")] + public double T0 { get; set; } = 1000000D; + + /// + /// The weight decay coefficient, which adds L2 regularization to the loss. + /// + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] + public double WeightDecay { get; set; } = 0D; + + /// + /// If set to true, performs maximization instead of minimization of the params based on the objective. + /// + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] + public bool Maximize { get; set; } = false; + + /// + /// Creates an ASGD optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => ASGD(parameters, LearningRate, Lambda, Alpha, T0, WeightDecay, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs index 7a4dbfe1..0c1b152b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs @@ -2,71 +2,65 @@ using System.ComponentModel; using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; using TorchSharp.Modules; using static TorchSharp.torch; -using static TorchSharp.torch.nn; using static TorchSharp.torch.optim; namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// -/// Creates a LBFGS optimizer. +/// Represents an operator that creates a limited-memory Broyden-Fletcher-Goldfarb-Shanno (LBFGS) optimizer. /// -[Combinator] -[Description("Creates a LBFGS optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a limited-memory Broyden-Fletcher-Goldfarb-Shanno (LBFGS) optimizer.")] public class Lbfgs { /// - /// The parameters parameter for the LBFGS module. + /// The learning rate. /// - [Description("The parameters parameter for the LBFGS module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } + [Description("The learning rate.")] + public double LearningRate { get; set; } = 0.01D; /// - /// The lr parameter for the LBFGS module. + /// The maximum number of iterations per optimization step. /// - [Description("The lr parameter for the LBFGS module")] - public double Lr { get; set; } = 0.01D; - - /// - /// The max_iter parameter for the LBFGS module. - /// - [Description("The max_iter parameter for the LBFGS module")] + [Description("The maximum number of iterations per optimization step.")] public long MaxIter { get; set; } = 20; /// - /// The max_eval parameter for the LBFGS module. + /// The maximum number of function evaluations per optimization step. /// - [Description("The max_eval parameter for the LBFGS module")] + [Description("The maximum number of function evaluations per optimization step.")] public long? MaxEval { get; set; } = null; /// - /// The tolerange_grad parameter for the LBFGS module. + /// The termination criterion that determines when to stop optimizing based on first-order optimality. /// - [Description("The tolerange_grad parameter for the LBFGS module")] - public double TolerangeGrad { get; set; } = 1E-05D; + [Description("The termination criterion that determines when to stop optimizing based on first-order optimality.")] + public double ToleranceGrad { get; set; } = 1E-05D; /// - /// The tolerance_change parameter for the LBFGS module. + /// The termination criterion that determines when to stop optimizing based on function value or parameter changes. /// - [Description("The tolerance_change parameter for the LBFGS module")] + [Description("The termination criterion that determines when to stop optimizing based on function value or parameter changes.")] public double ToleranceChange { get; set; } = 1E-09D; /// - /// The history_size parameter for the LBFGS module. + /// The update history size. /// - [Description("The history_size parameter for the LBFGS module")] + [Description("The update history size.")] public long HistorySize { get; set; } = 100; /// - /// Generates an observable sequence that creates a LBFGSOptimizer. + /// Creates an LBFGS optimizer from the input parameter collection. /// - public IObservable Process() + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable { - return Observable.Return(LBFGS(Parameters, Lr, MaxIter, MaxEval, TolerangeGrad, ToleranceChange, HistorySize)); + return source.Select(parameters => LBFGS(parameters, LearningRate, MaxIter, MaxEval, ToleranceGrad, ToleranceChange, HistorySize)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs deleted file mode 100644 index 9d538c30..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackprop.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a Rprop optimizer. -/// -[Combinator] -[Description("Creates a Rprop optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class ResilientBackprop -{ - /// - /// The parameters parameter for the Rprop module. - /// - [Description("The parameters parameter for the Rprop module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The lr parameter for the Rprop module. - /// - [Description("The lr parameter for the Rprop module")] - public double Lr { get; set; } = 0.01D; - - /// - /// The etaminus parameter for the Rprop module. - /// - [Description("The etaminus parameter for the Rprop module")] - public double Etaminus { get; set; } = 0.5D; - - /// - /// The etaplus parameter for the Rprop module. - /// - [Description("The etaplus parameter for the Rprop module")] - public double Etaplus { get; set; } = 1.2D; - - /// - /// The min_step parameter for the Rprop module. - /// - [Description("The min_step parameter for the Rprop module")] - public double MinStep { get; set; } = 1E-06D; - - /// - /// The max_step parameter for the Rprop module. - /// - [Description("The max_step parameter for the Rprop module")] - public double MaxStep { get; set; } = 50D; - - /// - /// The maximize parameter for the Rprop module. - /// - [Description("The maximize parameter for the Rprop module")] - public bool Maximize { get; set; } = false; - - /// - /// Generates an observable sequence that creates a RpropOptimizer. - /// - public IObservable Process() - { - return Observable.Return(Rprop(Parameters, Lr, Etaminus, Etaplus, MinStep, MaxStep, Maximize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs new file mode 100644 index 00000000..aa484ce5 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates a resilient backpropagation (Rprop) optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates a resilient backpropagation (Rprop) optimizer.")] +public class ResilientBackpropagation +{ + /// + /// The learning rate. + /// + [Description("The learning rate.")] + public double LearningRate { get; set; } = 0.01D; + + /// + /// The eta (-) parameter, which serves as a multiplicative factor to decrease the step size. + /// + [Description("The eta (-) parameter, which serves as a multiplicative factor to decrease the step size.")] + public double EtaMinus { get; set; } = 0.5D; + + /// + /// The eta (+) parameter, which serves as a multiplicative factor to increase the step size. + /// + [Description("The eta (+) parameter, which serves as a multiplicative factor to increase the step size.")] + public double EtaPlus { get; set; } = 1.2D; + + /// + /// The minimum allowed step size. + /// + [Description("The minimum allowed step size.")] + public double MinStep { get; set; } = 1E-06D; + + /// + /// The maximum allowed step size. + /// + [Description("The maximum allowed step size.")] + public double MaxStep { get; set; } = 50D; + + /// + /// If set to true, performs maximization instead of minimization of the params based on the objective. + /// + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] + public bool Maximize { get; set; } = false; + + /// + /// Creates an Rprop optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => Rprop(parameters, LearningRate, EtaMinus, EtaPlus, MinStep, MaxStep, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs deleted file mode 100644 index 4dc2ab80..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RmsPropogation.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a RMSProp optimizer. -/// -[Combinator] -[Description("Creates a RMSProp optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class RmsPropogation -{ - /// - /// The parameters parameter for the RMSProp module. - /// - [Description("The parameters parameter for the RMSProp module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The lr parameter for the RMSProp module. - /// - [Description("The lr parameter for the RMSProp module")] - public double Lr { get; set; } = 0.01D; - - /// - /// The alpha parameter for the RMSProp module. - /// - [Description("The alpha parameter for the RMSProp module")] - public double Alpha { get; set; } = 0.99D; - - /// - /// A value added to the denominator for numerical stability. - /// - [Description("A value added to the denominator for numerical stability")] - public double Eps { get; set; } = 1E-08D; - - /// - /// The weight_decay parameter for the RMSProp module. - /// - [Description("The weight_decay parameter for the RMSProp module")] - public double WeightDecay { get; set; } = 0D; - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0D; - - /// - /// The centered parameter for the RMSProp module. - /// - [Description("The centered parameter for the RMSProp module")] - public bool Centered { get; set; } = false; - - /// - /// The maximize parameter for the RMSProp module. - /// - [Description("The maximize parameter for the RMSProp module")] - public bool Maximize { get; set; } = false; - - /// - /// Generates an observable sequence that creates a RMSPropOptimizer. - /// - public IObservable Process() - { - return Observable.Return(RMSProp(Parameters, Lr, Alpha, Eps, WeightDecay, Momentum, Centered, Maximize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs new file mode 100644 index 00000000..ed4c3710 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs @@ -0,0 +1,72 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates a root mean square propagation (RMSProp) optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates a root mean square propagation (RMSProp) optimizer.")] +public class RootMeanSquarePropagation +{ + /// + /// The learning rate. + /// + [Description("The learning rate.")] + public double LearningRate { get; set; } = 0.01D; + + /// + /// The alpha parameter, which acts as a smoothing constant. + /// + [Description("The alpha parameter, which acts as a smoothing constant.")] + public double Alpha { get; set; } = 0.99D; + + /// + /// The value added to the denominator for numerical stability. + /// + [Description("The value added to the denominator for numerical stability.")] + public double Eps { get; set; } = 1E-08D; + + /// + /// The weight decay coefficient, which adds L2 regularization to the loss. + /// + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] + public double WeightDecay { get; set; } = 0D; + + /// + /// The momentum factor, which accelerates learning in the relevant direction. + /// + [Description("The momentum factor, which accelerates learning in the relevant direction.")] + public double Momentum { get; set; } = 0D; + + /// + /// If set to true, the gradient is normalized by an estimation of its variance. + /// + [Description("If set to true, the gradient is normalized by an estimation of its variance.")] + public bool Centered { get; set; } = false; + + /// + /// If set to true, performs maximization instead of minimization of the params based on the objective. + /// + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] + public bool Maximize { get; set; } = false; + + /// + /// Creates an RMSProp optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => RMSProp(parameters, LearningRate, Alpha, Eps, WeightDecay, Momentum, Centered, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs deleted file mode 100644 index d08afe72..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Sgd.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; - -namespace Bonsai.ML.Torch.NeuralNets.Optimizer; - -/// -/// Creates a SGD optimizer. -/// -[Combinator] -[Description("Creates a SGD optimizer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Sgd -{ - /// - /// The parameters parameter for the SGD module. - /// - [Description("The parameters parameter for the SGD module")] - [XmlIgnore] - public IEnumerable Parameters { get; set; } - - /// - /// The learningrate parameter for the SGD module. - /// - [Description("The learningrate parameter for the SGD module")] - public double LearningRate { get; set; } - - /// - /// The value used for the running_mean and running_var computation. - /// - [Description("The value used for the running_mean and running_var computation")] - public double Momentum { get; set; } = 0D; - - /// - /// The dampening parameter for the SGD module. - /// - [Description("The dampening parameter for the SGD module")] - public double Dampening { get; set; } = 0D; - - /// - /// The weight_decay parameter for the SGD module. - /// - [Description("The weight_decay parameter for the SGD module")] - public double WeightDecay { get; set; } = 0D; - - /// - /// The nesterov parameter for the SGD module. - /// - [Description("The nesterov parameter for the SGD module")] - public bool Nesterov { get; set; } = false; - - /// - /// The maximize parameter for the SGD module. - /// - [Description("The maximize parameter for the SGD module")] - public bool Maximize { get; set; } = false; - - /// - /// Generates an observable sequence that creates a SGDOptimizer. - /// - public IObservable Process() - { - return Observable.Return(SGD(Parameters, LearningRate, Momentum, Dampening, WeightDecay, Nesterov, Maximize)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs new file mode 100644 index 00000000..39f98fbc --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs @@ -0,0 +1,66 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.optim; + +namespace Bonsai.ML.Torch.NeuralNets.Optimizer; + +/// +/// Represents an operator that creates a stochastic gradient descent (SGD) optimizer. +/// +/// +/// See for more information. +/// +[Description("Creates a stochastic gradient descent (SGD) optimizer.")] +public class StochasticGradientDescent +{ + /// + /// The learning rate. + /// + [Description("The learning rate.")] + public double LearningRate { get; set; } + + /// + /// The momentum factor. + /// + [Description("The momentum factor.")] + public double Momentum { get; set; } = 0D; + + /// + /// The dampening for momentum. + /// + [Description("The dampening for momentum.")] + public double Dampening { get; set; } = 0D; + + /// + /// The weight decay coefficient, which adds L2 regularization to the loss. + /// + [Description("The weight decay coefficient, which adds L2 regularization to the loss.")] + public double WeightDecay { get; set; } = 0D; + + /// + /// If set to true, enables Nesterov momentum when momentum is non-zero. + /// + [Description("If set to true, enables Nesterov momentum when momentum is non-zero.")] + public bool Nesterov { get; set; } = false; + + /// + /// If set to true, performs maximization instead of minimization of the params based on the objective. + /// + [Description("If set to true, performs maximization instead of minimization of the params based on the objective.")] + public bool Maximize { get; set; } = false; + + /// + /// Creates an SGD optimizer from the input parameter collection. + /// + /// + /// + /// + public IObservable Process(IObservable source) where T : IEnumerable + { + return source.Select(parameters => SGD(parameters, LearningRate, Momentum, Dampening, WeightDecay, Nesterov, Maximize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs new file mode 100644 index 00000000..a4475ba3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs @@ -0,0 +1,53 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates an optimizer. +/// +[XmlInclude(typeof(Optimizer.Adadelta))] +[XmlInclude(typeof(Optimizer.Adagrad))] +[XmlInclude(typeof(Optimizer.Adam))] +[XmlInclude(typeof(Optimizer.Adamax))] +[XmlInclude(typeof(Optimizer.AdamW))] +[XmlInclude(typeof(Optimizer.AveragedStochasticGradientDescent))] +[XmlInclude(typeof(Optimizer.Lbfgs))] +[XmlInclude(typeof(Optimizer.ResilientBackpropagation))] +[XmlInclude(typeof(Optimizer.RootMeanSquarePropagation))] +[XmlInclude(typeof(Optimizer.StochasticGradientDescent))] +[DefaultProperty(nameof(OptimizerModule))] +[Combinator] +[Description("Creates an optimizer.")] +[WorkflowElementCategory(ElementCategory.Transform)] +public class OptimizerBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + string INamedElement.Name => $"Optimizer.{GetElementDisplayName(OptimizerModule)}"; + + /// + /// Initializes a new instance of the class. + /// + public OptimizerBuilder() + { + Module = new Optimizer.Adam(); + } + + /// + /// Gets or sets the specific optimizer to create. + /// + [DesignOnly(true)] + [DisplayName("Optimizer")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific optimizer to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object OptimizerModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs new file mode 100644 index 00000000..64f1cd78 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs @@ -0,0 +1,50 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 1D constant padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D constant padding module.")] +public class ConstantPad1d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) PaddingSize { get; set; } + + /// + /// The value to pad with. + /// + [Description("The value to pad with.")] + public double Value { get; set; } + + /// + /// Creates a 1D constant padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ConstantPad1d(PaddingSize, Value)); + } + + /// + /// Creates a 1D constant padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ConstantPad1d(PaddingSize, Value)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs new file mode 100644 index 00000000..b3346845 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs @@ -0,0 +1,50 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 2D constant padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D constant padding module.")] +public class ConstantPad2d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long) PaddingSize { get; set; } + + /// + /// The value to pad with. + /// + [Description("The value to pad with.")] + public double Value { get; set; } + + /// + /// Creates a 2D constant padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ConstantPad2d(PaddingSize, Value)); + } + + /// + /// Creates a 2D constant padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ConstantPad2d(PaddingSize, Value)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs new file mode 100644 index 00000000..d576d4ca --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs @@ -0,0 +1,50 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 3D constant padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D constant padding module.")] +public class ConstantPad3d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long, long, long) PaddingSize { get; set; } + + /// + /// The value to pad with. + /// + [Description("The value to pad with.")] + public double Value { get; set; } + + /// + /// Creates a 3D constant padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ConstantPad3d(PaddingSize, Value)); + } + + /// + /// Creates a 3D constant padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ConstantPad3d(PaddingSize, Value)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs deleted file mode 100644 index 6853d45a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/Padding.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Padding; - -/// -/// Creates a 1D adaptive average pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class Padding -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The padding mode for the ConstantPad module. - /// - [Description("The padding mode for the ConstantPad module")] - public PaddingMode Mode { get; set; } = PaddingMode.Constant; - - /// - /// The padding parameter for the ConstantPad1d module. - /// - [Description("The padding parameter for the ConstantPad1d module")] - public long PaddingSize { get; set; } - - /// - /// The value parameter for the ConstantPad1d module. - /// - [Description("The value parameter for the ConstantPad1d module")] - public double Value { get; set; } - - /// - /// If true, uses reflection padding instead of constant padding. - /// - - public bool Reflection { get; set; } = false; - - /// - /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Mode switch - { - PaddingMode.Constant => Observable.Return(ConstantPad1d(PaddingSize, Value)), - PaddingMode.Reflection => Observable.Return(ReflectionPad1d(PaddingSize)), - PaddingMode.Replication => Observable.Return(ReplicationPad1d(PaddingSize)), - _ => throw new InvalidOperationException("The specified padding mode is not supported."), - }, - Dimensions.Two => Mode switch - { - PaddingMode.Zero => Observable.Return(ZeroPad2d(PaddingSize)), - PaddingMode.Constant => Observable.Return(ConstantPad2d(PaddingSize, Value)), - PaddingMode.Reflection => Observable.Return(ReflectionPad2d(PaddingSize)), - PaddingMode.Replication => Observable.Return(ReplicationPad2d(PaddingSize)), - _ => throw new InvalidOperationException("The specified padding mode is not supported."), - }, - Dimensions.Three => Mode switch - { - PaddingMode.Constant => Observable.Return(ConstantPad3d(PaddingSize, Value)), - PaddingMode.Reflection => Observable.Return(ReflectionPad3d(PaddingSize)), - PaddingMode.Replication => Observable.Return(ReplicationPad3d(PaddingSize)), - _ => throw new InvalidOperationException("The specified padding mode is not supported."), - }, - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs deleted file mode 100644 index ed4b329a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/PaddingMode.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace Bonsai.ML.Torch.NeuralNets.Padding; - -/// -/// Specifies the padding mode for a neural network module. -/// -public enum PaddingMode -{ - /// - /// Zero padding. - /// - Zero, - - /// - /// Constant padding. - /// - Constant, - - /// - /// Reflection padding. - /// - Reflection, - - /// - /// Replication padding. - /// - Replication, -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs new file mode 100644 index 00000000..c15400b3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 1D reflection padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D reflection padding module.")] +public class ReflectionPad1d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) PaddingSize { get; set; } + + /// + /// Creates a 1D reflection padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReflectionPad1d(PaddingSize)); + } + + /// + /// Creates a 1D reflection padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReflectionPad1d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs new file mode 100644 index 00000000..478bfa39 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 2D reflection padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D reflection padding module.")] +public class ReflectionPad2d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long) PaddingSize { get; set; } + + /// + /// Creates a 2D reflection padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReflectionPad2d(PaddingSize)); + } + + /// + /// Creates a 2D reflection padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReflectionPad2d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs new file mode 100644 index 00000000..8e5b38eb --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 3D reflection padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D reflection padding module.")] +public class ReflectionPad3d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long, long, long) PaddingSize { get; set; } + + /// + /// Creates a 3D reflection padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReflectionPad3d(PaddingSize)); + } + + /// + /// Creates a 3D reflection padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReflectionPad3d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs new file mode 100644 index 00000000..5d09c938 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 1D replication padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D replication padding module.")] +public class ReplicationPad1d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) PaddingSize { get; set; } + + /// + /// Creates a 1D replication padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReplicationPad1d(PaddingSize)); + } + + /// + /// Creates a 1D replication padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReplicationPad1d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs new file mode 100644 index 00000000..ae83b409 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 2D replication padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D replication padding module.")] +public class ReplicationPad2d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long) PaddingSize { get; set; } + + /// + /// Creates a 2D replication padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReplicationPad2d(PaddingSize)); + } + + /// + /// Creates a 2D replication padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReplicationPad2d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs new file mode 100644 index 00000000..151c3b9b --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 3D replication padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D replication padding module.")] +public class ReplicationPad3d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long, long, long) PaddingSize { get; set; } + + /// + /// Creates a 3D replication padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ReplicationPad3d(PaddingSize)); + } + + /// + /// Creates a 3D replication padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ReplicationPad3d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs new file mode 100644 index 00000000..00120e23 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Padding; + +/// +/// Represents an operator that creates a 2D zero padding module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D zero padding module.")] +public class ZeroPad2d +{ + /// + /// The size of the padding. + /// + [Description("The size of the padding.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long, long) PaddingSize { get; set; } + + /// + /// Creates a 2D zero padding module. + /// + /// + public IObservable> Process() + { + return Observable.Return(ZeroPad2d(PaddingSize)); + } + + /// + /// Creates a 2D zero padding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ZeroPad2d(PaddingSize)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs new file mode 100644 index 00000000..bf3b85fb --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs @@ -0,0 +1,51 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a padding module. +/// +[XmlInclude(typeof(Padding.ConstantPad1d))] +[XmlInclude(typeof(Padding.ConstantPad2d))] +[XmlInclude(typeof(Padding.ConstantPad3d))] +[XmlInclude(typeof(Padding.ReflectionPad1d))] +[XmlInclude(typeof(Padding.ReflectionPad2d))] +[XmlInclude(typeof(Padding.ReflectionPad3d))] +[XmlInclude(typeof(Padding.ReplicationPad1d))] +[XmlInclude(typeof(Padding.ReplicationPad2d))] +[XmlInclude(typeof(Padding.ReplicationPad3d))] +[XmlInclude(typeof(Padding.ZeroPad2d))] +[DefaultProperty(nameof(PaddingModule))] +[Combinator] +[Description("Creates a padding module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class PaddingModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public PaddingModuleBuilder() + { + Module = new Padding.ConstantPad1d(); + } + + /// + /// Gets or sets the specific padding module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific padding module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object PaddingModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs deleted file mode 100644 index 3039a38e..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAveragePooling.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a 1D adaptive average pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveAveragePooling -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The outputsize parameter for the AdaptiveAvgPool2d module. - /// - [Description("The outputsize parameter for the AdaptiveAvgPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(AdaptiveAvgPool1d(OutputSize[0])), - Dimensions.Two => Observable.Return(AdaptiveAvgPool2d(OutputSize)), - Dimensions.Three => Observable.Return(AdaptiveAvgPool3d(OutputSize)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs index a6822348..a91692e4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs @@ -11,15 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 1D adaptive average pooling layer. +/// Represents an operator that creates a 1D adaptive average pooling module. /// -[Description("Creates a 1D adaptive average pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 1D adaptive average pooling module.")] public class AdaptiveAvgPool1d { /// /// The output size. /// - [Description("The output size")] + [Description("The output size.")] public long OutputSize { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs index b21d0506..c21963d5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs @@ -11,17 +11,20 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 2D adaptive average pooling layer. +/// Represents an operator that creates a 2D adaptive average pooling module. /// -[Description("Creates a 2D adaptive average pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 2D adaptive average pooling module.")] public class AdaptiveAvgPool2d { /// /// The output size. /// - [Description("The output size")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } + [Description("The output size.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) OutputSize { get; set; } /// /// Creates an AdaptiveAvgPool2d module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs index 45d2478e..991068bc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs @@ -1,27 +1,26 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 3D adaptive average pooling layer. +/// Represents an operator that creates a 3D adaptive average pooling module. /// -[Description("Creates a 3D adaptive average pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 3D adaptive average pooling module.")] public class AdaptiveAvgPool3d { /// /// The output size. /// - [Description("The output size")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } + [Description("The output size.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) OutputSize { get; set; } /// /// Creates an AdaptiveAvgPool3d module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs index 71628b4d..ceea2f4d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs @@ -1,25 +1,24 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 1D adaptive max pooling layer. +/// Represents an operator that creates a 1D adaptive max pooling module. /// -[Description("Creates a 1D adaptive max pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 1D adaptive max pooling module.")] public class AdaptiveMaxPool1d { /// /// The output size. /// - [Description("The output size")] + [Description("The output size.")] public long OutputSize { get; set; } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs index 8d2b49fe..9d9a530f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs @@ -11,15 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 2D adaptive max pooling layer. +/// Represents an operator that creates a 2D adaptive max pooling module. /// -[Description("Creates a 2D adaptive max pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 2D adaptive max pooling module.")] public class AdaptiveMaxPool2d { /// /// The output size. /// - [Description("The output size")] + [Description("The output size.")] [TypeConverter(typeof(UnidimensionalArrayConverter))] public long[] OutputSize { get; set; } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs index 9e96f55a..ce8fa8e7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs @@ -11,15 +11,18 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 3D adaptive max pooling layer. +/// Represents an operator that creates a 3D adaptive max pooling module. /// -[Description("Creates a 3D adaptive max pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 3D adaptive max pooling module.")] public class AdaptiveMaxPool3d { /// /// The output size. /// - [Description("The output size")] + [Description("The output size.")] [TypeConverter(typeof(UnidimensionalArrayConverter))] public long[] OutputSize { get; set; } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs deleted file mode 100644 index c967e6e6..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPooling.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a 1D adaptive max pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AdaptiveMaxPooling -{ - /// - /// The number of dimensions for the AdaptiveMaxPool module. - /// - [Description("The number of dimensions for the AdaptiveMaxPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The outputsize parameter for the AdaptiveMaxPool2d module. - /// - [Description("The outputsize parameter for the AdaptiveMaxPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveMaxPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(AdaptiveMaxPool1d(OutputSize[0])), - Dimensions.Two => Observable.Return(AdaptiveMaxPool2d(OutputSize)), - Dimensions.Three => Observable.Return(AdaptiveMaxPool3d(OutputSize)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs deleted file mode 100644 index ed1ae6bb..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AveragePooling.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a 1D adaptive average pooling layer. -/// -[Combinator] -[Description("Creates a 1D adaptive average pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class AveragePooling -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The outputsize parameter for the AdaptiveAvgPool2d module. - /// - [Description("The outputsize parameter for the AdaptiveAvgPool2d module")] - [TypeConverter(typeof(UnidimensionalArrayConverter))] - public long[] OutputSize { get; set; } - - /// - /// Generates an observable sequence that creates a AdaptiveAvgPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(AvgPool1d(OutputSize[0])), - Dimensions.Two => Observable.Return(AvgPool2d(OutputSize)), - Dimensions.Three => Observable.Return(AvgPool3d(OutputSize)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs index 391ca416..68916f90 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs @@ -11,44 +11,48 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 1D average pooling layer. +/// Represents an operator that creates a 1D average pooling module. /// -[Description("Creates a 1D average pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 1D average pooling module.")] public class AvgPool1d { /// - /// The kernel size. + /// The size of the window. /// - [Description("The kernel size.")] + [Description("The size of the window.")] public long KernelSize { get; set; } /// - /// The stride. + /// The stride of the window. /// - [Description("The stride.")] + [Description("The stride of the window.")] public long? Stride { get; set; } = null; /// - /// The padding. + /// The implicit zero padding to be added on both sides. /// - [Description("The padding.")] + [Description("The implicit zero padding to be added on both sides.")] public long Padding { get; set; } = 0; /// - /// The ceiling mode. + /// If set to true, will use ceil instead of floor to compute the output shape. /// - [Description("The ceiling mode.")] + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] public bool CeilMode { get; set; } = false; /// - /// The count include pad parameter. + /// If set to true, will include the zero-padding in the averaging calculation. /// - [Description("The count include pad parameter.")] + [Description("If set to true, will include the zero-padding in the averaging calculation.")] public bool CountIncludePad { get; set; } = true; /// /// Creates an AvgPool1d module. /// + /// public IObservable> Process() { return Observable.Return(AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs index c5ea0556..30f9c2f2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs @@ -11,50 +11,57 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 2D average pooling layer. +/// Represents an operator that creates a 2D average pooling module. /// -[Description("Creates a 2D average pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 2D average pooling module.")] public class AvgPool2d { /// - /// The kernel size. + /// The size of the window. /// - [Description("The kernel size.")] - public long[] KernelSize { get; set; } + [Description("The size of the window.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// - /// The stride. + /// The stride of the window. /// - [Description("The stride.")] - public long[] Stride { get; set; } = null; + [Description("The stride of the window.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; /// - /// The padding. + /// The implicit zero padding to be added on both sides. /// - [Description("The padding.")] - public long[] Padding { get; set; } = null; + [Description("The implicit zero padding to be added on both sides.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; /// - /// The ceiling mode. + /// If set to true, will use ceil instead of floor to compute the output shape. /// - [Description("The ceiling mode.")] + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] public bool CeilMode { get; set; } = false; /// - /// The count include pad parameter. + /// If set to true, will include the zero-padding in the averaging calculation. /// - [Description("The count include pad parameter.")] + [Description("If set to true, will include the zero-padding in the averaging calculation.")] public bool CountIncludePad { get; set; } = true; /// - /// The divisor override. + /// If specified, it will be used as divisor, otherwise size of the pooling region will be used. /// - [Description("The divisor override.")] + [Description("If specified, it will be used as divisor, otherwise size of the pooling region will be used.")] public long? DivisorOverride { get; set; } = null; /// /// Creates an AvgPool2d module. /// + /// public IObservable> Process() { return Observable.Return(AvgPool2d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs index 25f56c25..069b098c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs @@ -11,50 +11,57 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 3D average pooling layer. +/// Represents an operator that creates a 3D average pooling module. /// -[Description("Creates a 3D average pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 3D average pooling module.")] public class AvgPool3d { /// - /// The kernel size. + /// The size of the window. /// - [Description("The kernel size.")] - public long[] KernelSize { get; set; } + [Description("The size of the window.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) KernelSize { get; set; } /// - /// The stride. + /// The stride of the window. /// - [Description("The stride.")] - public long[] Stride { get; set; } = null; + [Description("The stride of the window.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Stride { get; set; } = null; /// - /// The padding. + /// The implicit zero padding to be added on all three sides. /// - [Description("The padding.")] - public long[] Padding { get; set; } = null; + [Description("The implicit zero padding to be added on all three sides.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Padding { get; set; } = null; /// - /// The ceiling mode. + /// If set to true, will use ceil instead of floor to compute the output shape. /// - [Description("The ceiling mode.")] + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] public bool CeilMode { get; set; } = false; /// - /// The count include pad parameter. + /// If set to true, will include the zero-padding in the averaging calculation. /// - [Description("The count include pad parameter.")] + [Description("If set to true, will include the zero-padding in the averaging calculation.")] public bool CountIncludePad { get; set; } = true; /// - /// The divisor override. + /// If specified, it will be used as divisor, otherwise size of the pooling region will be used. /// - [Description("The divisor override.")] + [Description("If specified, it will be used as divisor, otherwise size of the pooling region will be used.")] public long? DivisorOverride { get; set; } = null; /// /// Creates an AvgPool3d module. /// + /// public IObservable> Process() { return Observable.Return(AvgPool3d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs index edbcb261..dca58095 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs @@ -1,42 +1,45 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 2D fractional max pooling layer. +/// Represents an operator that creates a 2D fractional max pooling module. /// -[Description("Creates a 2D fractional max pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 2D fractional max pooling module.")] public class FractionalMaxPool2d { /// - /// The kernel size. + /// The size of the window to take a max over. /// - [Description("The kernel size.")] - public long KernelSize { get; set; } + [Description("The size of the window to take a max over.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// /// The output size. /// [Description("The output size.")] - public long? OutputSize { get; set; } = null; + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? OutputSize { get; set; } = null; /// - /// The output ratio. + /// Can be used to specify the output size as a ratio of the input size. /// - [Description("The output ratio.")] - public long? OutputRatio { get; set; } = null; + [Description("Can be used to specify the output size as a ratio of the input size.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? OutputRatio { get; set; } = null; /// /// Creates a FractionalMaxPool2d module. /// + /// public IObservable> Process() { return Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs new file mode 100644 index 00000000..63ef2e86 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs @@ -0,0 +1,58 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 3D fractional max pooling module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D fractional max pooling module.")] +public class FractionalMaxPool3d +{ + /// + /// The size of the window to take a max over. + /// + [Description("The size of the window to take a max over.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) KernelSize { get; set; } + + /// + /// The output size. + /// + [Description("The output size.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? OutputSize { get; set; } = null; + + /// + /// Can be used to specify the output size as a ratio of the input size. + /// + [Description("Can be used to specify the output size as a ratio of the input size.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? OutputRatio { get; set; } = null; + + /// + /// Creates a FractionalMaxPool3d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)); + } + + /// + /// Creates a FractionalMaxPool3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs deleted file mode 100644 index 7f582f66..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPooling.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a dropout layer. -/// -[Combinator] -[Description("Creates a fractional max pooling layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class FractionalMaxPooling -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.Two; - - /// - /// The kernel_size parameter for the FractionalMaxPool3d module. - /// - [Description("The kernel_size parameter for the FractionalMaxPool3d module")] - public long KernelSize { get; set; } - - /// - /// The output_size parameter for the FractionalMaxPool3d module. - /// - [Description("The output_size parameter for the FractionalMaxPool3d module")] - public long? OutputSize { get; set; } = null; - - /// - /// The output_ratio parameter for the FractionalMaxPool3d module. - /// - [Description("The output_ratio parameter for the FractionalMaxPool3d module")] - public double? OutputRatio { get; set; } = null; - - /// - /// Generates an observable sequence that creates a Dropout module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.Two => Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)), - Dimensions.Three => Observable.Return(FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs new file mode 100644 index 00000000..bc9caec3 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs @@ -0,0 +1,65 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 1D power average pooling (LPPool1d) module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D power average pooling (LPPool1d) module.")] +public class LPPool1d +{ + /// + /// The degree of the norm. + /// + [Description("The degree of the norm.")] + public double Norm { get; set; } + + /// + /// The size of the window. + /// + [Description("The size of the window.")] + public long KernelSize { get; set; } + + /// + /// The stride of the window. + /// + [Description("The stride of the window.")] + public long? Stride { get; set; } = null; + + /// + /// If set to true, will use ceil instead of floor to compute the output shape. + /// + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] + public bool CeilMode { get; set; } = false; + + /// + /// Creates a LPPool1d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LPPool1d(Norm, KernelSize, Stride, CeilMode)); + } + + /// + /// Creates a LPPool1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LPPool1d(Norm, KernelSize, Stride, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs new file mode 100644 index 00000000..75d5a9cc --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D power average pooling (LPPool2d) module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D power average pooling (LPPool2d) module.")] +public class LPPool2d +{ + /// + /// The degree of the norm. + /// + [Description("The degree of the norm.")] + public double Norm { get; set; } + + /// + /// The size of the window. + /// + [Description("The size of the window.")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] KernelSize { get; set; } + + /// + /// The stride of the window. + /// + [Description("The stride of the window.")] + [TypeConverter(typeof(UnidimensionalArrayConverter))] + public long[] Stride { get; set; } = null; + + /// + /// If set to true, will use ceil instead of floor to compute the output shape. + /// + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] + public bool CeilMode { get; set; } = false; + + /// + /// Creates a LPPool2d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LPPool2d(Norm, KernelSize, Stride, CeilMode)); + } + + /// + /// Creates a LPPool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LPPool2d(Norm, KernelSize, Stride, CeilMode)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs index 3888e489..e810ce94 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs @@ -11,51 +11,55 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// -/// Represents an operator that creates a 1D max pooling layer. +/// Represents an operator that creates a 1D max pooling module. /// -[Description("Creates a 1D max pooling layer.")] +/// +/// See for more information. +/// +[Description("Creates a 1D max pooling module.")] public class MaxPool1d { /// - /// The kernel size. + /// The size of the sliding window. /// - [Description("The kernel size.")] + [Description("The size of the sliding window.")] public long KernelSize { get; set; } /// - /// The stride. + /// The stride of the sliding window. /// - [Description("The stride.")] + [Description("The stride of the sliding window.")] public long? Stride { get; set; } = null; /// - /// The padding. + /// The implicit negative infinity padding to be added on both sides. /// - [Description("The padding.")] + [Description("The implicit negative infinity padding to be added on both sides.")] public long? Padding { get; set; } = null; /// - /// The dilation. + /// The spacing between kernel elements. /// - [Description("The dilation.")] + [Description("The spacing between kernel elements.")] public long? Dilation { get; set; } = null; /// - /// The ceiling mode. + /// If set to true, will use ceil instead of floor to compute the output shape. /// - [Description("The ceiling mode.")] + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] public bool CeilMode { get; set; } = false; /// - /// Creates an MaxPool1d module. + /// Creates a MaxPool1d module. /// + /// public IObservable> Process() { return Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); } /// - /// Creates an MaxPool1d module. + /// Creates a MaxPool1d module. /// /// /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs index 518701ea..4d51d77a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs @@ -1,10 +1,6 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,49 +9,57 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Represents an operator that creates a 2D max pooling layer. /// +/// +/// See for more information. +/// [Description("Creates a 2D max pooling layer.")] public class MaxPool2d { /// - /// The kernel size. + /// The size of the window to take a max over. /// - [Description("The kernel size.")] - public long[] KernelSize { get; set; } + [Description("The size of the window to take a max over.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } /// - /// The stride. + /// The stride of the window. /// - [Description("The stride.")] - public long[] Stride { get; set; } = null; + [Description("The stride of the window.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; /// - /// The padding. + /// The implicit negative infinity padding to be added on both sides. /// - [Description("The padding.")] - public long[] Padding { get; set; } = null; + [Description("The implicit negative infinity padding to be added on both sides.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; /// - /// The dilation. + /// The spacing between kernel elements. /// - [Description("The dilation.")] - public long[] Dilation { get; set; } = null; + [Description("The spacing between kernel elements.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Dilation { get; set; } = null; /// - /// The ceiling mode. + /// If set to true, will use ceil instead of floor to compute the output shape. /// - [Description("The ceiling mode.")] + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] public bool CeilMode { get; set; } = false; /// - /// Creates an MaxPool2d module. + /// Creates a MaxPool2d module. /// + /// public IObservable> Process() { return Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); } /// - /// Creates an MaxPool2d module. + /// Creates a MaxPool2d module. /// /// /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs index 408f9222..641f902d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs @@ -1,10 +1,6 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; @@ -13,49 +9,57 @@ namespace Bonsai.ML.Torch.NeuralNets.Pooling; /// /// Represents an operator that creates a 3D max pooling layer. /// +/// +/// See for more information. +/// [Description("Creates a 3D max pooling layer.")] public class MaxPool3d { /// - /// The kernel size. + /// The size of the window to take a max over. /// - [Description("The kernel size.")] - public long[] KernelSize { get; set; } + [Description("The size of the window to take a max over.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) KernelSize { get; set; } /// - /// The stride. + /// The stride of the window. /// - [Description("The stride.")] - public long[] Stride { get; set; } = null; + [Description("The stride of the window.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Stride { get; set; } = null; /// - /// The padding. + /// The implicit negative infinity padding to be added on all three sides. /// - [Description("The padding.")] - public long[] Padding { get; set; } = null; + [Description("The implicit negative infinity padding to be added on all three sides.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Padding { get; set; } = null; /// - /// The dilation. + /// The spacing between kernel elements. /// - [Description("The dilation.")] - public long[] Dilation { get; set; } = null; + [Description("The spacing between kernel elements.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Dilation { get; set; } = null; /// - /// The ceiling mode. + /// If set to true, will use ceil instead of floor to compute the output shape. /// - [Description("The ceiling mode.")] + [Description("If set to true, will use ceil instead of floor to compute the output shape.")] public bool CeilMode { get; set; } = false; /// - /// Creates an MaxPool3d module. + /// Creates a MaxPool3d module. /// + /// public IObservable> Process() { return Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); } /// - /// Creates an MaxPool3d module. + /// Creates a MaxPool3d module. /// /// /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs deleted file mode 100644 index 4231d84a..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPooling.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using Bonsai.Reactive; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a LPPool1d module. -/// -[Combinator] -[Description("Creates a LPPool1d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MaxPooling -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The kernelsize parameter for the MaxPool3d module. - /// - [Description("The kernelsize parameter for the MaxPool3d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the MaxPool3d module. - /// - [Description("The stride parameter for the MaxPool3d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the MaxPool3d module. - /// - [Description("The padding parameter for the MaxPool3d module")] - public long? Padding { get; set; } = null; - - /// - /// The dilation parameter for the MaxPool3d module. - /// - [Description("The dilation parameter for the MaxPool3d module")] - public long? Dilation { get; set; } = null; - - /// - /// The ceilmode parameter for the MaxPool3d module. - /// - [Description("The ceilmode parameter for the MaxPool3d module")] - public bool CeilMode { get; set; } = false; - - /// - /// Generates an observable sequence that creates a LPPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)), - Dimensions.Two => Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)), - Dimensions.Three => Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs new file mode 100644 index 00000000..25ffef76 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs @@ -0,0 +1,59 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 1D max unpooling module. +/// +/// +/// See for more information. +/// +[Description("Creates a 1D max unpooling module.")] +public class MaxUnpool1d +{ + /// + /// The size of the max pooling window. + /// + [Description("The size of the max pooling window.")] + public long KernelSize { get; set; } + + /// + /// The stride of the max pooling window. + /// + [Description("The stride of the max pooling window.")] + public long? Stride { get; set; } = null; + + /// + /// The padding that was added to the input. + /// + [Description("The padding that was added to the input.")] + public long? Padding { get; set; } = null; + + /// + /// Creates a MaxUnpool1d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)); + } + + /// + /// Creates a MaxUnpool1d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MaxUnpool1d(KernelSize, Stride, Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs new file mode 100644 index 00000000..d02cd3ce --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs @@ -0,0 +1,62 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 2D max unpooling module. +/// +/// +/// See for more information. +/// +[Description("Creates a 2D max unpooling module.")] +public class MaxUnpool2d +{ + /// + /// The size of the max pooling window. + /// + [Description("The size of the max pooling window.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long) KernelSize { get; set; } + + /// + /// The stride of the max pooling window. + /// + [Description("The stride of the max pooling window.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Stride { get; set; } = null; + + /// + /// The padding that was added to the input. + /// + [Description("The padding that was added to the input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long)? Padding { get; set; } = null; + + /// + /// Creates a MaxUnpool2d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)); + } + + /// + /// Creates a MaxUnpool2d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MaxUnpool2d(KernelSize, Stride, Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs new file mode 100644 index 00000000..83adc4fb --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs @@ -0,0 +1,62 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using TorchSharp.Modules; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Pooling; + +/// +/// Represents an operator that creates a 3D max unpooling module. +/// +/// +/// See for more information. +/// +[Description("Creates a 3D max unpooling module.")] +public class MaxUnpool3d +{ + /// + /// The size of the max pooling window. + /// + [Description("The size of the max pooling window.")] + [TypeConverter(typeof(ValueTupleConverter))] + public (long, long, long) KernelSize { get; set; } + + /// + /// The stride of the max pooling window. + /// + [Description("The stride of the max pooling window.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Stride { get; set; } = null; + + /// + /// The padding that was added to the input. + /// + [Description("The padding that was added to the input.")] + [TypeConverter(typeof(NullableValueTupleConverter))] + public (long, long, long)? Padding { get; set; } = null; + + /// + /// Creates a MaxUnpool3d module. + /// + /// + public IObservable> Process() + { + return Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)); + } + + /// + /// Creates a MaxUnpool3d module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => MaxUnpool3d(KernelSize, Stride, Padding)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs deleted file mode 100644 index b6bac9cf..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpoolModule.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a 1D convolution layer. -/// -[Combinator] -[Description("Creates a 1D convolution layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class MaxUnpoolModule -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The kernelsize parameter for the MaxUnpool1d module. - /// - [Description("The kernelsize parameter for the MaxUnpool1d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the MaxUnpool1d module. - /// - [Description("The stride parameter for the MaxUnpool1d module")] - public long? Stride { get; set; } = null; - - /// - /// The padding parameter for the MaxUnpool1d module. - /// - [Description("The padding parameter for the MaxUnpool1d module")] - public long? Padding { get; set; } = null; - - /// - /// Generates an observable sequence that creates a Conv1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)), - Dimensions.Two => Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)), - Dimensions.Three => Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs deleted file mode 100644 index 1d48b386..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/PowerAveragePooling.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Pooling; - -/// -/// Creates a LPPool1d module. -/// -[Combinator] -[Description("Creates a LPPool1d module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class PowerAveragePooling -{ - /// - /// The number of dimensions for the AdaptiveAvgPool module. - /// - [Description("The number of dimensions for the AdaptiveAvgPool module")] - public Dimensions Dimensions { get; set; } = Dimensions.One; - - /// - /// The norm_type parameter for the LPPool1d module. - /// - [Description("The norm_type parameter for the LPPool1d module")] - public double NormType { get; set; } - - /// - /// The kernelsize parameter for the LPPool1d module. - /// - [Description("The kernelsize parameter for the LPPool1d module")] - public long KernelSize { get; set; } - - /// - /// The stride parameter for the LPPool1d module. - /// - [Description("The stride parameter for the LPPool1d module")] - public long? Stride { get; set; } = null; - - /// - /// The ceil_mode parameter for the LPPool1d module. - /// - [Description("The ceil_mode parameter for the LPPool1d module")] - public bool CeilMode { get; set; } = false; - - /// - /// Generates an observable sequence that creates a LPPool1dModule module. - /// - public IObservable> Process() - { - return Dimensions switch - { - Dimensions.One => Observable.Return(LPPool1d(NormType, KernelSize, Stride, CeilMode)), - Dimensions.Two => Observable.Return(LPPool2d(NormType, KernelSize, Stride, CeilMode)), - _ => throw new InvalidOperationException("The specified number of dimensions is not supported."), - }; - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs index 4fa81ac2..3c800fd7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs @@ -1,21 +1,10 @@ -using System; -using System.Linq; -using System.Linq.Expressions; using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; using System.Xml.Serialization; -using Bonsai.Expressions; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using System.Reflection; namespace Bonsai.ML.Torch.NeuralNets; /// -/// Represents an operator that creates a torch module for pooling operations. +/// Represents an operator that creates a module for pooling operations. /// [XmlInclude(typeof(Pooling.AdaptiveAvgPool1d))] [XmlInclude(typeof(Pooling.AdaptiveAvgPool2d))] @@ -23,9 +12,22 @@ namespace Bonsai.ML.Torch.NeuralNets; [XmlInclude(typeof(Pooling.AdaptiveMaxPool1d))] [XmlInclude(typeof(Pooling.AdaptiveMaxPool2d))] [XmlInclude(typeof(Pooling.AdaptiveMaxPool3d))] +[XmlInclude(typeof(Pooling.AvgPool1d))] +[XmlInclude(typeof(Pooling.AvgPool2d))] +[XmlInclude(typeof(Pooling.AvgPool3d))] +[XmlInclude(typeof(Pooling.FractionalMaxPool2d))] +[XmlInclude(typeof(Pooling.FractionalMaxPool3d))] +[XmlInclude(typeof(Pooling.LPPool1d))] +[XmlInclude(typeof(Pooling.LPPool2d))] +[XmlInclude(typeof(Pooling.MaxPool1d))] +[XmlInclude(typeof(Pooling.MaxPool2d))] +[XmlInclude(typeof(Pooling.MaxPool3d))] +[XmlInclude(typeof(Pooling.MaxUnpool1d))] +[XmlInclude(typeof(Pooling.MaxUnpool2d))] +[XmlInclude(typeof(Pooling.MaxUnpool3d))] [DefaultProperty(nameof(PoolingModule))] [Combinator] -[Description("Creates a torch module for pooling operations.")] +[Description("Creates a module for pooling operations.")] [WorkflowElementCategory(ElementCategory.Source)] public class PoolingModuleBuilder : ModuleCombinatorBuilder, INamedElement { diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs deleted file mode 100644 index 5334ce87..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUCellModule.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Recurrent; - -/// -/// Creates a GRUCell module. -/// -[Combinator] -[Description("Creates a GRUCell module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class GRUCellModule -{ - /// - /// The inputsize parameter for the GRUCell module. - /// - [Description("The inputsize parameter for the GRUCell module")] - public long InputSize { get; set; } - - /// - /// The hiddensize parameter for the GRUCell module. - /// - [Description("The hiddensize parameter for the GRUCell module")] - public long HiddenSize { get; set; } - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a GRUCellModule module. - /// - public IObservable> Process() - { - return Observable.Return(GRUCell(InputSize, HiddenSize, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs deleted file mode 100644 index aeb52792..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GRUModule.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Recurrent; - -/// -/// Creates a Gated Recurrent Unit layer. -/// -[Combinator] -[Description("Creates a Gated Recurrent Unit layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class GRUModule -{ - /// - /// The inputsize parameter for the GRU module. - /// - [Description("The inputsize parameter for the GRU module")] - public long InputSize { get; set; } - - /// - /// The hiddensize parameter for the GRU module. - /// - [Description("The hiddensize parameter for the GRU module")] - public long HiddenSize { get; set; } - - /// - /// The numlayers parameter for the GRU module. - /// - [Description("The numlayers parameter for the GRU module")] - public long NumLayers { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The batchfirst parameter for the GRU module. - /// - [Description("The batchfirst parameter for the GRU module")] - public bool BatchFirst { get; set; } = false; - - /// - /// The dropout parameter for the GRU module. - /// - [Description("The dropout parameter for the GRU module")] - public double Dropout { get; set; } = 0D; - - /// - /// The bidirectional parameter for the GRU module. - /// - [Description("The bidirectional parameter for the GRU module")] - public bool Bidirectional { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a GRUModule module. - /// - public IObservable> Process() - { - return Observable.Return(GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs new file mode 100644 index 00000000..210e1e2e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs @@ -0,0 +1,93 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; + +/// +/// Represents an operator that creates a multi-layer gated recurrent unit (GRU) module. +/// +/// +/// See for more information. +/// +[Description("Creates a multi-layer gated recurrent unit (GRU) module.")] +public class GatedRecurrentUnit +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long InputSize { get; set; } + + /// + /// The number of features in the hidden state. + /// + [Description("The number of features in the hidden state.")] + public long HiddenSize { get; set; } + + /// + /// The number of recurrent layers. + /// + [Description("The number of recurrent layers.")] + public long NumLayers { get; set; } = 1; + + /// + /// If set to false, then the layer will not use bias weights. + /// + [Description("If set to false, then the layer will not use bias weights.")] + public bool Bias { get; set; } = true; + + /// + /// If set to true, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature). + /// + [Description("If set to true, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature).")] + public bool BatchFirst { get; set; } = false; + + /// + /// If non-zero, this introduces a Dropout layer with the provided dropout probability on the outputs of each GRU layer except the last layer. + /// + [Description("If non-zero, this introduces a Dropout layer with the provided dropout probability on the outputs of each GRU layer except the last layer.")] + public double Dropout { get; set; } = 0D; + + /// + /// The set to true, becomes a bidirectional GRU. + /// + [Description("If set to true, becomes a bidirectional GRU.")] + public bool Bidirectional { get; set; } = false; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a GRU module. + /// + /// + public IObservable> Process() + { + return Observable.Return(GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } + + /// + /// Creates a GRU module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs new file mode 100644 index 00000000..56677038 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs @@ -0,0 +1,69 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; + +/// +/// Represents an operator that creates a gated recurrent unit (GRU) cell. +/// +/// +/// See for more information. +/// +[Description("Creates a gated recurrent unit (GRU) cell module.")] +public class GatedRecurrentUnitCell +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long InputSize { get; set; } + + /// + /// The number of features in the hidden state. + /// + [Description("The number of features in the hidden state.")] + public long HiddenSize { get; set; } + + /// + /// If set to false, then the layer will not use bias weights. + /// + [Description("If set to false, then the layer will not use bias weights.")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates a GRUCell module. + /// + /// + public IObservable> Process() + { + return Observable.Return(GRUCell(InputSize, HiddenSize, Bias, Device, Type)); + } + + /// + /// Creates a GRUCell module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => GRUCell(InputSize, HiddenSize, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs deleted file mode 100644 index f1743219..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMCellModule.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Recurrent; - -/// -/// Creates a LSTMCell module. -/// -[Combinator] -[Description("Creates a LSTMCell module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class LSTMCellModule -{ - /// - /// The inputsize parameter for the LSTMCell module. - /// - [Description("The inputsize parameter for the LSTMCell module")] - public long InputSize { get; set; } - - /// - /// The hiddensize parameter for the LSTMCell module. - /// - [Description("The hiddensize parameter for the LSTMCell module")] - public long HiddenSize { get; set; } - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a LSTMCellModule module. - /// - public IObservable> Process() - { - return Observable.Return(LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs deleted file mode 100644 index 5872daf7..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LSTMModule.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Recurrent; - -/// -/// Creates a Long Short-Term Memory layer. -/// -[Combinator] -[Description("Creates a Long Short-Term Memory layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class LSTMModule -{ - /// - /// The inputsize parameter for the LSTM module. - /// - [Description("The inputsize parameter for the LSTM module")] - public long InputSize { get; set; } - - /// - /// The hiddensize parameter for the LSTM module. - /// - [Description("The hiddensize parameter for the LSTM module")] - public long HiddenSize { get; set; } - - /// - /// The numlayers parameter for the LSTM module. - /// - [Description("The numlayers parameter for the LSTM module")] - public long NumLayers { get; set; } = 1; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The batchfirst parameter for the LSTM module. - /// - [Description("The batchfirst parameter for the LSTM module")] - public bool BatchFirst { get; set; } = false; - - /// - /// The dropout parameter for the LSTM module. - /// - [Description("The dropout parameter for the LSTM module")] - public double Dropout { get; set; } = 0D; - - /// - /// The bidirectional parameter for the LSTM module. - /// - [Description("The bidirectional parameter for the LSTM module")] - public bool Bidirectional { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a LSTMModule module. - /// - public IObservable> Process() - { - return Observable.Return(LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs new file mode 100644 index 00000000..7386e8b6 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs @@ -0,0 +1,93 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; + +/// +/// Represents an operator that creates a multi-layer long short-term memory (LSTM) module. +/// +/// +/// See for more information. +/// +[Description("Creates a multi-layer long short-term memory (LSTM) module.")] +public class LongShortTermMemory +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long InputSize { get; set; } + + /// + /// The number of features in the hidden state. + /// + [Description("The number of features in the hidden state.")] + public long HiddenSize { get; set; } + + /// + /// The number of recurrent layers. + /// + [Description("The number of recurrent layers.")] + public long NumLayers { get; set; } = 1; + + /// + /// If set to false, then the layer will not use bias weights. + /// + [Description("If set to false, then the layer will not use bias weights.")] + public bool Bias { get; set; } = true; + + /// + /// If set to true, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature). + /// + [Description("If set to true, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature).")] + public bool BatchFirst { get; set; } = false; + + /// + /// If non-zero, this introduces a Dropout layer with the provided dropout probability on the outputs of each LSTM layer except the last layer. + /// + [Description("If non-zero, this introduces a Dropout layer with the provided dropout probability on the outputs of each LSTM layer except the last layer.")] + public double Dropout { get; set; } = 0D; + + /// + /// If set to true, becomes a bidirectional LSTM. + /// + [Description("If set to true, becomes a bidirectional LSTM.")] + public bool Bidirectional { get; set; } = false; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an LSTM module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } + + /// + /// Creates an LSTM module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs new file mode 100644 index 00000000..7611b7cd --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs @@ -0,0 +1,69 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; + +/// +/// Represents an operator that creates a long short-term memory (LSTM) cell. +/// +/// +/// See for more information. +/// +[Description("Creates a long short-term memory (LSTM) cell.")] +public class LongShortTermMemoryCell +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long InputSize { get; set; } + + /// + /// The number of features in the hidden state. + /// + [Description("The number of features in the hidden state.")] + public long HiddenSize { get; set; } + + /// + /// If set to false, then the layer will not use bias weights. + /// + [Description("If set to false, then the layer will not use bias weights.")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an LSTMCell module. + /// + /// + public IObservable> Process() + { + return Observable.Return(LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); + } + + /// + /// Creates an LSTMCell module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs deleted file mode 100644 index ff36a18e..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNCellModule.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Recurrent; - -/// -/// Creates a RNNCell module. -/// -[Combinator] -[Description("Creates a RNNCell module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class RNNCellModule -{ - /// - /// The inputsize parameter for the RNNCell module. - /// - [Description("The inputsize parameter for the RNNCell module")] - public long InputSize { get; set; } - - /// - /// The hiddensize parameter for the RNNCell module. - /// - [Description("The hiddensize parameter for the RNNCell module")] - public long HiddenSize { get; set; } - - /// - /// The nonlinearity parameter for the RNNCell module. - /// - [Description("The nonlinearity parameter for the RNNCell module")] - public NonLinearities NonLinearity { get; set; } = NonLinearities.Tanh; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a RNNCellModule module. - /// - public IObservable> Process() - { - return Observable.Return(RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs deleted file mode 100644 index 98ad5047..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RNNModule.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Recurrent; - -/// -/// Creates a Basic RNN layer. -/// -[Combinator] -[Description("Creates a Basic RNN layer.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class RNNModule -{ - /// - /// The inputsize parameter for the RNN module. - /// - [Description("The inputsize parameter for the RNN module")] - public long InputSize { get; set; } - - /// - /// The hiddensize parameter for the RNN module. - /// - [Description("The hiddensize parameter for the RNN module")] - public long HiddenSize { get; set; } - - /// - /// The numlayers parameter for the RNN module. - /// - [Description("The numlayers parameter for the RNN module")] - public long NumLayers { get; set; } = 1; - - /// - /// The nonlinearity parameter for the RNN module. - /// - [Description("The nonlinearity parameter for the RNN module")] - public NonLinearities NonLinearity { get; set; } = NonLinearities.Tanh; - - /// - /// If true, adds a learnable bias to the output. - /// - [Description("If true, adds a learnable bias to the output")] - public bool Bias { get; set; } = true; - - /// - /// The batchfirst parameter for the RNN module. - /// - [Description("The batchfirst parameter for the RNN module")] - public bool BatchFirst { get; set; } = false; - - /// - /// The dropout parameter for the RNN module. - /// - [Description("The dropout parameter for the RNN module")] - public double Dropout { get; set; } = 0D; - - /// - /// The bidirectional parameter for the RNN module. - /// - [Description("The bidirectional parameter for the RNN module")] - public bool Bidirectional { get; set; } = false; - - /// - /// The desired device of returned tensor. - /// - [XmlIgnore] - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a RNNModule module. - /// - public IObservable> Process() - { - return Observable.Return(RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs new file mode 100644 index 00000000..772ccd90 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs @@ -0,0 +1,99 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; + +/// +/// Represents an operator that creates a multi-layer recurrent neural network (RNN) module. +/// +/// +/// See for more information. +/// +[Description("Creates a multi-layer recurrent neural network (RNN) module.")] +public class RecurrentNeuralNetwork +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long InputSize { get; set; } + + /// + /// The number of features in the hidden state. + /// + [Description("The number of features in the hidden state.")] + public long HiddenSize { get; set; } + + /// + /// The number of recurrent layers. + /// + [Description("The number of recurrent layers.")] + public long NumLayers { get; set; } = 1; + + /// + /// The type of nonlinearity to use. + /// + [Description("The type of nonlinearity to use.")] + public NonLinearities NonLinearity { get; set; } = NonLinearities.Tanh; + + /// + /// If set to false, then the layer will not use bias weights. + /// + [Description("If set to false, then the layer will not use bias weights.")] + public bool Bias { get; set; } = true; + + /// + /// If set to true, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature). + /// + [Description("If set to true, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature).")] + public bool BatchFirst { get; set; } = false; + + /// + /// If non-zero, this introduces a Dropout layer with the provided dropout probability on the outputs of each RNN layer except the last layer. + /// + [Description("If non-zero, this introduces a Dropout layer with the provided dropout probability on the outputs of each RNN layer except the last layer.")] + public double Dropout { get; set; } = 0D; + + /// + /// If set to true, becomes a bidirectional RNN. + /// + [Description("If set to true, becomes a bidirectional RNN.")] + public bool Bidirectional { get; set; } = false; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an RNN module. + /// + /// + public IObservable> Process() + { + return Observable.Return(RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } + + /// + /// Creates an RNN module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs new file mode 100644 index 00000000..8ae3e317 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs @@ -0,0 +1,75 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Recurrent; + +/// +/// Represents an operator that creates a recurrent neural network (RNN) cell. +/// +/// +/// See for more information. +/// +[Description("Creates a recurrent neural network (RNN) cell.")] +public class RecurrentNeuralNetworkCell +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long InputSize { get; set; } + + /// + /// The number of features in the hidden state. + /// + [Description("The number of features in the hidden state.")] + public long HiddenSize { get; set; } + + /// + /// The type of non-linearity to use. + /// + [Description("The type of non-linearity to use.")] + public NonLinearities NonLinearity { get; set; } = NonLinearities.Tanh; + + /// + /// If set to false, then the layer will not use bias weights. + /// + [Description("If set to false, then the layer will not use bias weights.")] + public bool Bias { get; set; } = true; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor.")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor.")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an RNNCell module. + /// + /// + public IObservable> Process() + { + return Observable.Return(RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); + } + + /// + /// Creates an RNNCell module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs new file mode 100644 index 00000000..8b847bb0 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs @@ -0,0 +1,47 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a recurrent neural network module. +/// +[XmlInclude(typeof(Recurrent.GatedRecurrentUnit))] +[XmlInclude(typeof(Recurrent.GatedRecurrentUnitCell))] +[XmlInclude(typeof(Recurrent.LongShortTermMemory))] +[XmlInclude(typeof(Recurrent.LongShortTermMemoryCell))] +[XmlInclude(typeof(Recurrent.RecurrentNeuralNetwork))] +[XmlInclude(typeof(Recurrent.RecurrentNeuralNetworkCell))] +[DefaultProperty(nameof(RecurrentModule))] +[Combinator] +[Description("Creates a recurrent neural network module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class RecurrentModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public RecurrentModuleBuilder() + { + Module = new Recurrent.RecurrentNeuralNetwork(); + } + + /// + /// Gets or sets the specific recurrent neural network module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific recurrent neural network module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object RecurrentModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs index dd11dcd2..69435968 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs @@ -10,24 +10,26 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Saves the model to a file. +/// Represents an operator that saves a module's state to a file. /// [Combinator] [ResetCombinator] -[Description("Saves the model to a file.")] +[Description("Saves a module's state to a file.")] [WorkflowElementCategory(ElementCategory.Sink)] public class SaveModule { /// - /// The path to save the module. + /// The path to save the module's state. /// - [Description("The path to save the module.")] + [Description("The path to save the module's state.")] [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] public string ModulePath { get; set; } /// - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// + /// + /// public IObservable Process(IObservable source) { return source.Do(input => @@ -37,7 +39,7 @@ public IObservable Process(IObservable source) } /// - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// /// /// @@ -52,7 +54,7 @@ public IObservable> Process(IObservable - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// /// /// @@ -68,7 +70,7 @@ public IObservable> Process(IObservable } /// - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// /// /// @@ -85,7 +87,7 @@ public IObservable> Process(IOb } /// - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// /// /// @@ -103,7 +105,7 @@ public IObservable> Process - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// /// /// @@ -122,7 +124,7 @@ public IObservable> Process - /// Saves the input module to the specified file path. + /// Saves the input module's state to the specified file path. /// /// /// @@ -142,7 +144,7 @@ public IObservable> Process - /// Saves the input scripted module to the specified file path. + /// Saves the input scripted module's state to the specified file path. /// /// /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs new file mode 100644 index 00000000..63da38b0 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs @@ -0,0 +1,45 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a torch module for convolution operations. +/// +[XmlInclude(typeof(Container.Sequential))] +[DefaultProperty(nameof(ContainerModule))] +[Combinator] +[Description("Creates a sequential container for torch modules.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SequentialBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + string INamedElement.Name => "Sequential"; + + /// + /// Initializes a new instance of the class. + /// + public SequentialBuilder() + { + Module = new Container.Sequential(); + } + + /// + /// Gets or sets the specific container module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific container module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object ContainerModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs index 6588fcb1..0acce866 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SetTrainingMode.cs @@ -10,11 +10,11 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Represents an operator that sets the training mode for the module. +/// Represents an operator that sets the training mode of the input module. /// [Combinator] [ResetCombinator] -[Description("Sets the training mode for the module.")] +[Description("Sets the training mode of the input module.")] [WorkflowElementCategory(ElementCategory.Sink)] public class SetTrainingMode { @@ -25,7 +25,7 @@ public class SetTrainingMode public TrainingMode Mode { get; set; } = TrainingMode.Train; /// - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -38,7 +38,7 @@ public IObservable Process(IObservable source) } /// - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -53,7 +53,7 @@ public IObservable> Process(IObservable - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -69,7 +69,7 @@ public IObservable> Process(IObservable } /// - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -86,7 +86,7 @@ public IObservable> Process(IOb } /// - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -104,7 +104,7 @@ public IObservable> Process - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -123,7 +123,7 @@ public IObservable> Process - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -143,7 +143,7 @@ public IObservable> Process - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -156,7 +156,7 @@ public IObservable Process(IObservable source) } /// - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -170,7 +170,7 @@ public IObservable> Process(IObservable - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// @@ -185,7 +185,7 @@ public IObservable> Process(IObservable - /// Sets the training mode for the module. + /// Sets the training mode of the module. /// /// /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs index c64c44f5..ba822c5a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Shuffle; /// -/// Creates a ChannelShuffle module. +/// Represents an operator that creates a channel shuffle module. /// -[Combinator] -[Description("Creates a ChannelShuffle module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a channel shuffle module.")] public class ChannelShuffle { /// - /// The groups parameter for the ChannelShuffle module. + /// The number of groups to divide the channels into. /// - [Description("The groups parameter for the ChannelShuffle module")] + [Description("The number of groups to divide the channels into.")] public long Groups { get; set; } /// - /// Generates an observable sequence that creates a ChannelShuffleModule module. + /// Creates a channel shuffle module. /// + /// public IObservable> Process() { return Observable.Return(ChannelShuffle(Groups)); } + + /// + /// Creates a channel shuffle module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => ChannelShuffle(Groups)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs new file mode 100644 index 00000000..6c766821 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs @@ -0,0 +1,42 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a module for shuffling input. +/// +[XmlInclude(typeof(Shuffle.ChannelShuffle))] +[DefaultProperty(nameof(ShuffleModule))] +[Combinator] +[Description("Creates a module for shuffling input.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class ShuffleModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public ShuffleModuleBuilder() + { + Module = new Shuffle.ChannelShuffle(); + } + + /// + /// Gets or sets the specific shuffle module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific shuffle module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object ShuffleModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs index e5c3a980..f9ee39ce 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs @@ -1,84 +1,93 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Sparse; /// -/// Creates a Embedding layer. +/// Represents an operator that creates an embedding module. /// -[Combinator] -[Description("Creates a Embedding layer.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an embedding module.")] public class Embedding { /// - /// The num_embeddings parameter for the Embedding module. + /// The size of the dictionary of embeddings. /// - [Description("The num_embeddings parameter for the Embedding module")] + [Description("The size of the dictionary of embeddings.")] public long NumEmbeddings { get; set; } /// - /// The embedding_dims parameter for the Embedding module. + /// The size of each embedding vector. /// - [Description("The embedding_dims parameter for the Embedding module")] + [Description("The size of each embedding vector.")] public long EmbeddingDims { get; set; } /// - /// The padding_idx parameter for the Embedding module. + /// If specified, the padding indices do not contribute to the gradient and are not updated during training. /// - [Description("The padding_idx parameter for the Embedding module")] + [Description("If specified, the padding indices do not contribute to the gradient and are not updated during training.")] public long? PaddingIdx { get; set; } = null; /// - /// The max_norm parameter for the Embedding module. + /// If specified, each embedding vector is clipped to have this as its maximum norm. /// - [Description("The max_norm parameter for the Embedding module")] + [Description("If specified, each embedding vector is clipped to have this as its maximum norm.")] public double? MaxNorm { get; set; } = null; /// - /// The norm_type parameter for the Embedding module. + /// The degree of the norm to compute for the max norm. /// - [Description("The norm_type parameter for the Embedding module")] + [Description("The degree of the norm to compute for the max norm.")] public double NormType { get; set; } = 2D; /// - /// The scale_grad_by_freq parameter for the Embedding module. + /// If true, the embedding vectors are scaled by the inverse of their frequency in the input. /// - [Description("The scale_grad_by_freq parameter for the Embedding module")] + [Description("If true, the embedding vectors are scaled by the inverse of their frequency in the input.")] public bool ScaleGradByFreq { get; set; } = false; /// - /// The sparse parameter for the Embedding module. + /// If true, the gradient will be sparse. /// - [Description("The sparse parameter for the Embedding module")] + [Description("If true, the gradient will be sparse.")] public bool Sparse { get; set; } = false; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor.")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] + [Description("The desired data type of the returned tensor.")] public ScalarType? Type { get; set; } = null; /// - /// Generates an observable sequence that creates a EmbeddingModule module. + /// Creates an embedding module. /// + /// public IObservable> Process() { return Observable.Return(Embedding(NumEmbeddings, EmbeddingDims, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } + + /// + /// Creates an embedding module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Embedding(NumEmbeddings, EmbeddingDims, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs new file mode 100644 index 00000000..a8c6ea56 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs @@ -0,0 +1,106 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Sparse; + +/// +/// Represents an operator that creates an embedding bag module. +/// +/// +/// See for more information. +/// +[Description("Creates an embedding bag module.")] +public class EmbeddingBag +{ + /// + /// The size of the dictionary of embeddings. + /// + [Description("The size of the dictionary of embeddings.")] + public long NumEmbeddings { get; set; } + + /// + /// The size of each embedding vector. + /// + [Description("The size of each embedding vector.")] + public long EmbeddingDims { get; set; } + + /// + /// If specified, each embedding vector is clipped to have this as its maximum norm. + /// + [Description("If specified, each embedding vector is clipped to have this as its maximum norm.")] + public double? MaxNorm { get; set; } = null; + + /// + /// The degree of the norm to compute for the max norm. + /// + [Description("The degree of the norm to compute for the max norm.")] + public double NormType { get; set; } = 2D; + + /// + /// If set to true, the embeddings vectors are scaled by the inverse of their frequency in the input. + /// + [Description("If set to true, the embeddings vectors are scaled by the inverse of their frequency in the input.")] + public bool ScaleGradByFreq { get; set; } = false; + + /// + /// The type of reduction to apply to the embeddings. + /// + [Description("The type of reduction to apply to the embeddings.")] + public EmbeddingBagMode Mode { get; set; } = EmbeddingBagMode.Mean; + + /// + /// If set to true, the gradient will be sparse. + /// + [Description("If set to true, the gradient will be sparse.")] + public bool Sparse { get; set; } = false; + + /// + /// If set to true, the offsets tensor includes an additional element at the end equal to the size of indices. + /// + [Description("If set to true, the offsets tensor includes an additional element at the end equal to the size of indices.")] + public bool IncludeLastOffset { get; set; } = false; + + /// + /// If specified, the entries do not contribute to the gradient and are not updated during training. + /// + [Description("If specified, the entries do not contribute to the gradient and are not updated during training.")] + public long PaddingIndex { get; set; } = -1; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor")] + public ScalarType? Type { get; set; } = null; + + /// + /// Creates an embedding bag module. + /// + /// + public IObservable> Process() + { + return Observable.Return(EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); + } + + /// + /// Creates an embedding bag module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs new file mode 100644 index 00000000..3a7b0dd7 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs @@ -0,0 +1,121 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Sparse; + +/// +/// Represents an operator that creates an embedding module from pretrained weights. +/// +/// +/// See for more information. +/// +[Description("Creates an embedding module from pretrained weights.")] +[TypeConverter(typeof(TensorOperatorConverter))] +public class EmbeddingBagFromPretrained : IScalarTypeProvider +{ + /// + /// The pretrained weights for the embedding bag. + /// + [XmlIgnore] + [Description("The pretrained weights for the embedding bag.")] + [TypeConverter(typeof(TensorConverter))] + public Tensor Embeddings { get; set; } + + /// + /// The values of the embeddings tensor in XML string format. + /// + [Browsable(false)] + [XmlElement(nameof(Embeddings))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string EmbeddingsXml + { + get => TensorConverter.ConvertToString(Embeddings, Type); + set => Embeddings = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// Determines whether to freeze the embeddings weights. + /// + [Description("Determines whether to freeze the embeddings weights.")] + public bool Freeze { get; set; } = true; + + /// + /// If specified, each embedding vector is clipped to have this as its maximum norm. + /// + [Description("If specified, each embedding vector is clipped to have this as its maximum norm.")] + public double? MaxNorm { get; set; } = null; + + /// + /// The degree of the norm to compute for the max norm. + /// + [Description("The degree of the norm to compute for the max norm.")] + public double NormType { get; set; } = 2D; + + /// + /// If set to true, the embedding vectors are scaled by the inverse of their frequency in the input. + /// + [Description("If set to true, the embedding vectors are scaled by the inverse of their frequency in the input.")] + public bool ScaleGradByFreq { get; set; } = false; + + /// + /// The type of reduction to apply to the embeddings. + /// + [Description("The type of reduction to apply to the embeddings.")] + public EmbeddingBagMode Mode { get; set; } = EmbeddingBagMode.Mean; + + /// + /// If set to true, the gradient will be sparse. + /// + [Description("If set to true, the gradient will be sparse.")] + public bool Sparse { get; set; } = false; + + /// + /// If set to true, the offsets tensor includes an additional element at the end equal to the size of indices. + /// + [Description("If set to true, the offsets tensor includes an additional element at the end equal to the size of indices.")] + public bool IncludeLastOffset { get; set; } = false; + + /// + /// If specified, the entries do not contribute to the gradient and are not updated during training. + /// + [Description("If specified, the entries do not contribute to the gradient and are not updated during training.")] + public long PaddingIndex { get; set; } = -1; + + /// + /// The desired device of the returned tensor. + /// + [XmlIgnore] + [Description("The desired device of the returned tensor")] + public Device Device { get; set; } = null; + + /// + /// The desired data type of the returned tensor. + /// + [Description("The desired data type of the returned tensor")] + public ScalarType Type { get; set; } = ScalarType.Float32; + + /// + /// Creates an embedding bag module from pretrained weights. + /// + /// + public IObservable> Process() + { + return Observable.Return(EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); + } + + /// + /// Creates an embedding bag module from pretrained weights. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs deleted file mode 100644 index 1a5848a0..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagModule.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Sparse; - -/// -/// Creates a EmbeddingBag module. -/// -[Combinator] -[Description("Creates a EmbeddingBag module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class EmbeddingBagModule -{ - /// - /// The num_embeddings parameter for the EmbeddingBag module. - /// - [Description("The num_embeddings parameter for the EmbeddingBag module")] - public long NumEmbeddings { get; set; } - - /// - /// The embedding_dims parameter for the EmbeddingBag module. - /// - [Description("The embedding_dims parameter for the EmbeddingBag module")] - public long EmbeddingDims { get; set; } - - /// - /// The max_norm parameter for the EmbeddingBag module. - /// - [Description("The max_norm parameter for the EmbeddingBag module")] - public double? MaxNorm { get; set; } = null; - - /// - /// The norm_type parameter for the EmbeddingBag module. - /// - [Description("The norm_type parameter for the EmbeddingBag module")] - public double NormType { get; set; } = 2D; - - /// - /// The scale_grad_by_freq parameter for the EmbeddingBag module. - /// - [Description("The scale_grad_by_freq parameter for the EmbeddingBag module")] - public bool ScaleGradByFreq { get; set; } = false; - - /// - /// The mode parameter for the EmbeddingBag module. - /// - [Description("The mode parameter for the EmbeddingBag module")] - public EmbeddingBagMode Mode { get; set; } = EmbeddingBagMode.Mean; - - /// - /// The sparse parameter for the EmbeddingBag module. - /// - [Description("The sparse parameter for the EmbeddingBag module")] - public bool Sparse { get; set; } = false; - - /// - /// The include_last_offset parameter for the EmbeddingBag module. - /// - [Description("The include_last_offset parameter for the EmbeddingBag module")] - public bool IncludeLastOffset { get; set; } = false; - - /// - /// The padding_index parameter for the EmbeddingBag module. - /// - [Description("The padding_index parameter for the EmbeddingBag module")] - public long PaddingIndex { get; set; } = -1; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType? Type { get; set; } = null; - - /// - /// Generates an observable sequence that creates a EmbeddingBagModule module. - /// - public IObservable> Process() - { - return Observable.Return(EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs index 42c2fb6f..1af4eaee 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs @@ -1,86 +1,108 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Sparse; /// -/// Creates a EmbeddingFromPretrained module. +/// Represents an operator that creates an embedding module from pretrained weights. /// -[Combinator] -[Description("Creates a EmbeddingFromPretrained module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates an embedding module from pretrained weights.")] [TypeConverter(typeof(TensorOperatorConverter))] public class EmbeddingFromPretrained : IScalarTypeProvider { /// - /// The embeddings parameter for the Embedding_from_pretrained module. + /// The pretrained weights for the embedding module. /// - [Description("The embeddings parameter for the Embedding_from_pretrained module")] + [XmlIgnore] + [Description("The pretrained weights for the embedding module.")] [TypeConverter(typeof(TensorConverter))] public Tensor Embeddings { get; set; } /// - /// The freeze parameter for the Embedding_from_pretrained module. + /// The values of the embeddings tensor in XML string format. + /// + [Browsable(false)] + [XmlElement(nameof(Embeddings))] + [EditorBrowsable(EditorBrowsableState.Never)] + public string EmbeddingsXml + { + get => TensorConverter.ConvertToString(Embeddings, Type); + set => Embeddings = TensorConverter.ConvertFromString(value, Type); + } + + /// + /// Determines whether to freeze the embeddings weights. /// - [Description("The freeze parameter for the Embedding_from_pretrained module")] + [Description("Determines whether to freeze the embeddings weights.")] public bool Freeze { get; set; } = true; /// - /// The padding_idx parameter for the Embedding_from_pretrained module. + /// If specified, the entries do not contribute to the gradient and are not updated during training. /// - [Description("The padding_idx parameter for the Embedding_from_pretrained module")] + [Description("If specified, the entries do not contribute to the gradient and are not updated during training.")] public long? PaddingIdx { get; set; } = null; /// - /// The max_norm parameter for the Embedding_from_pretrained module. + /// If specified, each embedding vector is clipped to have this as its maximum norm. /// - [Description("The max_norm parameter for the Embedding_from_pretrained module")] + [Description("If specified, each embedding vector is clipped to have this as its maximum norm.")] public double? MaxNorm { get; set; } = null; /// - /// The norm_type parameter for the Embedding_from_pretrained module. + /// The degree of the norm to compute for the max norm. /// - [Description("The norm_type parameter for the Embedding_from_pretrained module")] + [Description("The degree of the norm to compute for the max norm.")] public double NormType { get; set; } = 2D; /// - /// The scale_grad_by_freq parameter for the Embedding_from_pretrained module. + /// If set to true, the embeddings vectors are scaled by the inverse of their frequency in the input. /// - [Description("The scale_grad_by_freq parameter for the Embedding_from_pretrained module")] + [Description("If set to true, the embeddings vectors are scaled by the inverse of their frequency in the input.")] public bool ScaleGradByFreq { get; set; } = false; /// - /// The sparse parameter for the Embedding_from_pretrained module. + /// If set to true, the gradient will be sparse. /// - [Description("The sparse parameter for the Embedding_from_pretrained module")] + [Description("If set to true, the gradient will be sparse.")] public bool Sparse { get; set; } = false; /// - /// The desired device of returned tensor. + /// The desired device of the returned tensor. /// [XmlIgnore] - [Description("The desired device of returned tensor")] + [Description("The desired device of the returned tensor.")] public Device Device { get; set; } = null; /// - /// The desired data type of returned tensor. + /// The desired data type of the returned tensor. /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] + [Description("The desired data type of the returned tensor.")] public ScalarType Type { get; set; } = ScalarType.Float32; /// - /// Generates an observable sequence that creates a EmbeddingFromPretrainedModule module. + /// Creates an embedding module from pretrained weights. /// + /// public IObservable> Process() { return Observable.Return(Embedding_from_pretrained(Embeddings, Freeze, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } + + /// + /// Creates an embedding module from pretrained weights. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Embedding_from_pretrained(Embeddings, Freeze, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs deleted file mode 100644 index 68a9f02c..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingbagFromPretrainedModule.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Sparse; - -/// -/// Creates a EmbeddingbagFromPretrained module. -/// -[Combinator] -[Description("Creates a EmbeddingbagFromPretrained module.")] -[WorkflowElementCategory(ElementCategory.Source)] -[TypeConverter(typeof(TensorOperatorConverter))] -public class EmbeddingbagFromPretrainedModule : IScalarTypeProvider -{ - /// - /// The embeddings parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The embeddings parameter for the EmbeddingBag_from_pretrained module")] - [TypeConverter(typeof(TensorConverter))] - public Tensor Embeddings { get; set; } - - /// - /// The freeze parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The freeze parameter for the EmbeddingBag_from_pretrained module")] - public bool Freeze { get; set; } = true; - - /// - /// The max_norm parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The max_norm parameter for the EmbeddingBag_from_pretrained module")] - public double? MaxNorm { get; set; } = null; - - /// - /// The norm_type parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The norm_type parameter for the EmbeddingBag_from_pretrained module")] - public double NormType { get; set; } = 2D; - - /// - /// The scale_grad_by_freq parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The scale_grad_by_freq parameter for the EmbeddingBag_from_pretrained module")] - public bool ScaleGradByFreq { get; set; } = false; - - /// - /// The mode parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The mode parameter for the EmbeddingBag_from_pretrained module")] - public EmbeddingBagMode Mode { get; set; } = EmbeddingBagMode.Mean; - - /// - /// The sparse parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The sparse parameter for the EmbeddingBag_from_pretrained module")] - public bool Sparse { get; set; } = false; - - /// - /// The include_last_offset parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The include_last_offset parameter for the EmbeddingBag_from_pretrained module")] - public bool IncludeLastOffset { get; set; } = false; - - /// - /// The padding_index parameter for the EmbeddingBag_from_pretrained module. - /// - [Description("The padding_index parameter for the EmbeddingBag_from_pretrained module")] - public long PaddingIndex { get; set; } = -1; - - /// - /// The desired device of returned tensor. - /// - [Description("The desired device of returned tensor")] - public Device Device { get; set; } = null; - - /// - /// The desired data type of returned tensor. - /// - [Description("The desired data type of returned tensor")] - [TypeConverter(typeof(ScalarTypeConverter))] - public ScalarType Type { get; set; } = ScalarType.Float32; - - /// - /// Generates an observable sequence that creates a EmbeddingbagFromPretrainedModule module. - /// - public IObservable> Process() - { - return Observable.Return(EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs new file mode 100644 index 00000000..c8aa829a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs @@ -0,0 +1,45 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a module for sparsification. +/// +[XmlInclude(typeof(Sparse.Embedding))] +[XmlInclude(typeof(Sparse.EmbeddingBag))] +[XmlInclude(typeof(Sparse.EmbeddingBagFromPretrained))] +[XmlInclude(typeof(Sparse.EmbeddingFromPretrained))] +[DefaultProperty(nameof(SparseModule))] +[Combinator] +[Description("Creates a module for sparsification.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class SparseModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public SparseModuleBuilder() + { + Module = new Sparse.Embedding(); + } + + /// + /// Gets or sets the specific sparse module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific sparse module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object SparseModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs new file mode 100644 index 00000000..489db872 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs @@ -0,0 +1,79 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Transformer; + +/// +/// Represents an operator that creates a Transformer module. +/// +/// +/// See for more information. +/// +[Description("Creates a Transformer module.")] +public class Transformer +{ + /// + /// The number of expected features in the encoder/decoder inputs. + /// + [Description("The number of expected features in the encoder/decoder inputs.")] + public long DimModel { get; set; } = 512; + + /// + /// The number of heads in the multi-head attention models. + /// + [Description("The number of heads in the multi-head attention models.")] + public long NumHeads { get; set; } = 8; + + /// + /// The number of sub-encoder layers in the encoder. + /// + [Description("The number of sub-encoder layers in the encoder.")] + public long NumEncoderLayers { get; set; } = 6; + + /// + /// The number of sub-decoder layers in the decoder. + /// + [Description("The number of sub-decoder layers in the decoder.")] + public long NumDecoderLayers { get; set; } = 6; + + /// + /// The dimension of the feedforward network model. + /// + [Description("The dimension of the feedforward network model.")] + public long DimFeedforward { get; set; } = 2048; + + /// + /// The probability of dropout. + /// + [Description("The probability of dropout.")] + public double Dropout { get; set; } = 0.1D; + + /// + /// The activation function of the intermediate layer. + /// + [Description("The activation function of the intermediate layer.")] + public Activations Activation { get; set; } = Activations.ReLU; + + /// + /// Creates a Transformer module. + /// + /// + public IObservable> Process() + { + return Observable.Return(nn.Transformer(DimModel, NumHeads, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); + } + + /// + /// Creates a Transformer module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => nn.Transformer(DimModel, NumHeads, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs new file mode 100644 index 00000000..01c237bd --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs @@ -0,0 +1,35 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Transformer; + +/// +/// Represents an operator that creates a transformer decoder module. +/// +/// +/// See for more information. +/// +[Description("Creates a transformer decoder module.")] +public class TransformerDecoder +{ + /// + /// The number of sub-decoder layers in the decoder. + /// + [Description("The number of sub-decoder layers in the decoder.")] + public long NumLayers { get; set; } + + /// + /// Creates a TransformerDecoder module from the input TransformerDecoderLayer. + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(transformerDecoderLayer => + TransformerDecoder(transformerDecoderLayer as TorchSharp.Modules.TransformerDecoderLayer, NumLayers)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs new file mode 100644 index 00000000..491f9454 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Transformer; + +/// +/// Represents an operator that creates a transformer decoder layer. +/// +/// +/// See for more information. +/// +[Description("Creates a transformer decoder layer.")] +public class TransformerDecoderLayer +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long DimModel { get; set; } = 512; + + /// + /// The number of heads in the multi-head attention models. + /// + [Description("The number of heads in the multi-head attention models.")] + public long NumHeads { get; set; } = 8; + + /// + /// The dimension of the feedforward network model. + /// + [Description("The dimension of the feedforward network model.")] + public long DimFeedforward { get; set; } = 2048; + + /// + /// The probability of dropout. + /// + [Description("The probability of dropout.")] + public double Dropout { get; set; } = 0.1D; + + /// + /// The activation function of the intermediate layer. + /// + [Description("The activation function of the intermediate layer.")] + public Activations Activation { get; set; } = Activations.ReLU; + + /// + /// Creates a TransformerDecoderLayer module. + /// + /// + public IObservable> Process() + { + return Observable.Return(TransformerDecoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); + } + + /// + /// Creates a TransformerDecoderLayer module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => TransformerDecoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs deleted file mode 100644 index 02a4fe99..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayerModule.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Transformer; - -/// -/// Creates a TransformerDecoderLayer module. -/// -[Combinator] -[Description("Creates a TransformerDecoderLayer module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class TransformerDecoderLayerModule -{ - /// - /// The d_model parameter for the TransformerDecoderLayer module. - /// - [Description("The d_model parameter for the TransformerDecoderLayer module")] - public long DModel { get; set; } = 512; - - /// - /// The nhead parameter for the TransformerDecoderLayer module. - /// - [Description("The nhead parameter for the TransformerDecoderLayer module")] - public long Nhead { get; set; } = 8; - - /// - /// The dim_feedforward parameter for the TransformerDecoderLayer module. - /// - [Description("The dim_feedforward parameter for the TransformerDecoderLayer module")] - public long DimFeedforward { get; set; } = 2048; - - /// - /// The dropout parameter for the TransformerDecoderLayer module. - /// - [Description("The dropout parameter for the TransformerDecoderLayer module")] - public double Dropout { get; set; } = 0.1D; - - /// - /// The activation parameter for the TransformerDecoderLayer module. - /// - [Description("The activation parameter for the TransformerDecoderLayer module")] - public Activations Activation { get; set; } = Activations.ReLU; - - /// - /// Generates an observable sequence that creates a TransformerDecoderLayerModule module. - /// - public IObservable> Process() - { - return Observable.Return(TransformerDecoderLayer(DModel, Nhead, DimFeedforward, Dropout, Activation)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs deleted file mode 100644 index 5d2d2399..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderModule.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Transformer; - -/// -/// Creates a TransformerDecoder module. -/// -[Combinator] -[Description("Creates a TransformerDecoder module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class TransformerDecoderModule -{ - /// - /// The decoder_layer parameter for the TransformerDecoder module. - /// - [Description("The decoder_layer parameter for the TransformerDecoder module")] - [XmlIgnore] - public Module DecoderLayer { get; set; } - - /// - /// The num_layers parameter for the TransformerDecoder module. - /// - [Description("The num_layers parameter for the TransformerDecoder module")] - public long NumLayers { get; set; } - - /// - /// Generates an observable sequence that creates a TransformerDecoderModule module. - /// - public IObservable> Process() - { - - return Observable.Return(TransformerDecoder(DecoderLayer as TorchSharp.Modules.TransformerDecoderLayer, NumLayers)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs new file mode 100644 index 00000000..d5cc1326 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs @@ -0,0 +1,35 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Xml.Serialization; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Transformer; + +/// +/// Represents an operator that creates a transformer encoder module. +/// +/// +/// See for more information. +/// +[Description("Creates a transformer encoder module.")] +public class TransformerEncoder +{ + /// + /// The number of sub-encoder layers in the encoder. + /// + [Description("The number of sub-encoder layers in the encoder.")] + public long NumLayers { get; set; } + + /// + /// Creates a TransformerEncoder module from the input TransformerEncoderLayer. + /// + /// + /// + public IObservable> Process(IObservable> source) + { + return source.Select(transformerEncoderLayer => + TransformerEncoder(transformerEncoderLayer as TorchSharp.Modules.TransformerEncoderLayer, NumLayers)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs new file mode 100644 index 00000000..26394e7e --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs @@ -0,0 +1,67 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets.Transformer; + +/// +/// Represents an operator that creates a transformer encoder layer. +/// +/// +/// See for more information. +/// +[Description("Creates a transformer encoder layer.")] +public class TransformerEncoderLayer +{ + /// + /// The number of expected features in the input. + /// + [Description("The number of expected features in the input.")] + public long DimModel { get; set; } = 512; + + /// + /// The number of heads in the multi-head attention models. + /// + [Description("The number of heads in the multi-head attention models.")] + public long NumHeads { get; set; } = 8; + + /// + /// The dimension of the feedforward network model. + /// + [Description("The dimension of the feedforward network model.")] + public long DimFeedforward { get; set; } = 2048; + + /// + /// The probability of dropout. + /// + [Description("The probability of dropout.")] + public double Dropout { get; set; } = 0.1D; + + /// + /// The activation function of the intermediate layer. + /// + [Description("The activation function of the intermediate layer.")] + public Activations Activation { get; set; } = Activations.ReLU; + + /// + /// Creates a TransformerEncoderLayer module. + /// + /// + public IObservable> Process() + { + return Observable.Return(TransformerEncoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); + } + + /// + /// Creates a TransformerEncoderLayer module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => TransformerEncoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs deleted file mode 100644 index 303630b0..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayerModule.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Transformer; - -/// -/// Creates a TransformerEncoderLayer module. -/// -[Combinator] -[Description("Creates a TransformerEncoderLayer module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class TransformerEncoderLayerModule -{ - /// - /// The d_model parameter for the TransformerEncoderLayer module. - /// - [Description("The d_model parameter for the TransformerEncoderLayer module")] - public long DModel { get; set; } = 512; - - /// - /// The nhead parameter for the TransformerEncoderLayer module. - /// - [Description("The nhead parameter for the TransformerEncoderLayer module")] - public long Nhead { get; set; } = 8; - - /// - /// The dim_feedforward parameter for the TransformerEncoderLayer module. - /// - [Description("The dim_feedforward parameter for the TransformerEncoderLayer module")] - public long DimFeedforward { get; set; } = 2048; - - /// - /// The dropout parameter for the TransformerEncoderLayer module. - /// - [Description("The dropout parameter for the TransformerEncoderLayer module")] - public double Dropout { get; set; } = 0.1D; - - /// - /// The activation parameter for the TransformerEncoderLayer module. - /// - [Description("The activation parameter for the TransformerEncoderLayer module")] - public Activations Activation { get; set; } = Activations.ReLU; - - /// - /// Generates an observable sequence that creates a TransformerEncoderLayerModule module. - /// - public IObservable> Process() - { - return Observable.Return(TransformerEncoderLayer(DModel, Nhead, DimFeedforward, Dropout, Activation)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs deleted file mode 100644 index 4b17af06..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderModule.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Transformer; - -/// -/// Creates a TransformerEncoder module. -/// -[Combinator] -[Description("Creates a TransformerEncoder module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class TransformerEncoder -{ - /// - /// The encoder_layer parameter for the TransformerEncoder module. - /// - [Description("The encoder_layer parameter for the TransformerEncoder module")] - [XmlIgnore] - public Module EncoderLayer { get; set; } - - /// - /// The num_layers parameter for the TransformerEncoder module. - /// - [Description("The num_layers parameter for the TransformerEncoder module")] - public long NumLayers { get; set; } - - /// - /// Generates an observable sequence that creates a TransformerEncoderModule module. - /// - public IObservable> Process() - { - return Observable.Return(TransformerEncoder(EncoderLayer as TransformerEncoderLayer, NumLayers)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs deleted file mode 100644 index 838d85fa..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerModule.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; - -namespace Bonsai.ML.Torch.NeuralNets.Transformer; - -/// -/// Creates a Transformer module. -/// -[Combinator] -[Description("Creates a Transformer module.")] -[WorkflowElementCategory(ElementCategory.Source)] -public class TransformerModule -{ - /// - /// The d_model parameter for the Transformer module. - /// - [Description("The d_model parameter for the Transformer module")] - public long DModel { get; set; } = 512; - - /// - /// The nhead parameter for the Transformer module. - /// - [Description("The nhead parameter for the Transformer module")] - public long Nhead { get; set; } = 8; - - /// - /// The num_encoder_layers parameter for the Transformer module. - /// - [Description("The num_encoder_layers parameter for the Transformer module")] - public long NumEncoderLayers { get; set; } = 6; - - /// - /// The num_decoder_layers parameter for the Transformer module. - /// - [Description("The num_decoder_layers parameter for the Transformer module")] - public long NumDecoderLayers { get; set; } = 6; - - /// - /// The dim_feedforward parameter for the Transformer module. - /// - [Description("The dim_feedforward parameter for the Transformer module")] - public long DimFeedforward { get; set; } = 2048; - - /// - /// The dropout parameter for the Transformer module. - /// - [Description("The dropout parameter for the Transformer module")] - public double Dropout { get; set; } = 0.1D; - - /// - /// The activation parameter for the Transformer module. - /// - [Description("The activation parameter for the Transformer module")] - public Activations Activation { get; set; } = Activations.ReLU; - - /// - /// Generates an observable sequence that creates a TransformerModule module. - /// - public IObservable> Process() - { - return Observable.Return(nn.Transformer(DModel, Nhead, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); - } -} diff --git a/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs new file mode 100644 index 00000000..66fb0904 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs @@ -0,0 +1,46 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a transformer module. +/// +[XmlInclude(typeof(Transformer.Transformer))] +[XmlInclude(typeof(Transformer.TransformerDecoder))] +[XmlInclude(typeof(Transformer.TransformerDecoderLayer))] +[XmlInclude(typeof(Transformer.TransformerEncoder))] +[XmlInclude(typeof(Transformer.TransformerEncoderLayer))] +[DefaultProperty(nameof(TransformerModule))] +[Combinator] +[Description("Creates a transformer module.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class TransformerModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public TransformerModuleBuilder() + { + Module = new Transformer.Transformer(); + } + + /// + /// Gets or sets the specific transformer module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific transformer module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object TransformerModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs index 0ac16542..07824ded 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs @@ -1,34 +1,42 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Vision; /// -/// Creates a PixelShuffle module. +/// Represents an operator that creates a pixel shuffle module. /// -[Combinator] -[Description("Creates a PixelShuffle module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a pixel shuffle module.")] public class PixelShuffle { /// - /// The upscalefactor parameter for the PixelShuffle module. + /// The factor to increase spatial resolution by. /// - [Description("The upscalefactor parameter for the PixelShuffle module")] + [Description("The factor to increase spatial resolution by.")] public long UpscaleFactor { get; set; } /// - /// Generates an observable sequence that creates a PixelShuffleModule module. + /// Creates a pixel shuffle module. /// public IObservable> Process() { return Observable.Return(PixelShuffle(UpscaleFactor)); } + + /// + /// Creates a pixel shuffle module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => PixelShuffle(UpscaleFactor)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs index bdc529e3..31e27cd8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs @@ -1,34 +1,43 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Vision; /// -/// Creates a PixelUnshuffle module. +/// Represents an operator that creates a pixel unshuffle module. /// -[Combinator] -[Description("Creates a PixelUnshuffle module.")] -[WorkflowElementCategory(ElementCategory.Source)] +/// +/// See for more information. +/// +[Description("Creates a pixel unshuffle module.")] public class PixelUnshuffle { /// - /// The downscalefactor parameter for the PixelUnshuffle module. + /// The factor to decrease spatial resolution by. /// - [Description("The downscalefactor parameter for the PixelUnshuffle module")] + [Description("The factor to decrease spatial resolution by.")] public long DownscaleFactor { get; set; } /// - /// Generates an observable sequence that creates a PixelUnshuffleModule module. + /// Creates a pixel unshuffle module. /// + /// public IObservable> Process() { return Observable.Return(PixelUnshuffle(DownscaleFactor)); } + + /// + /// Creates a pixel unshuffle module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => PixelUnshuffle(DownscaleFactor)); + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs index 186c96f3..b26d40fe 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs @@ -1,54 +1,77 @@ using System; using System.ComponentModel; -using System.Collections.Generic; using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using TorchSharp.Modules; using static TorchSharp.torch; using static TorchSharp.torch.nn; namespace Bonsai.ML.Torch.NeuralNets.Vision; /// -/// Creates a Upsample module. +/// Creates an Upsample module. /// -[Combinator] -[Description("Creates a Upsample module.")] +/// +/// See for more information. +/// +[Description("Creates an Upsample module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class UpsampleModule +public class Upsample { /// - /// The size parameter for the Upsample module. + /// The output spatial sizes. /// - [Description("The size parameter for the Upsample module")] + [Description("The output spatial sizes.")] [TypeConverter(typeof(UnidimensionalArrayConverter))] public long[] Size { get; set; } = null; /// - /// The scale_factor parameter for the Upsample module. + /// The multiplier for the spatial size. /// - [Description("The scale_factor parameter for the Upsample module")] + [Description("The multiplier for the spatial size.")] [TypeConverter(typeof(UnidimensionalArrayConverter))] public double[] ScaleFactor { get; set; } = null; /// - /// The mode parameter for the Upsample module. + /// The upsampling algorithm. /// - [Description("The mode parameter for the Upsample module")] + [Description("The upsampling algorithm.")] public UpsampleMode Mode { get; set; } = UpsampleMode.Nearest; /// - /// The align_corners parameter for the Upsample module. + /// If True, the corner pixels of the input and output tensors are aligned. /// - [Description("The align_corners parameter for the Upsample module")] + /// + /// This only has effect when mode is 'linear', 'bilinear', 'bicubic' or 'trilinear'. + /// + [Description("If True, the corner pixels of the input and output tensors are aligned.")] public bool? AlignCorners { get; set; } = null; /// - /// Generates an observable sequence that creates a UpsampleModule module. + /// Recomputes the scale factor for use in the interpolation calculation. /// + /// + /// If true, then the scale factor must be passed in and is used to compute the output size. + /// If false, then size or scale factor will be used for interpolation. + /// + [Description("Recomputes the scale factor for use in the interpolation calculation.")] + public bool? RecomputeScaleFactor { get; set; } = null; + + /// + /// Creates an Upsample module. + /// + /// public IObservable> Process() { - return Observable.Return(Upsample(Size, ScaleFactor, Mode, AlignCorners)); + return Observable.Return(Upsample(Size, ScaleFactor, Mode, AlignCorners, RecomputeScaleFactor)); + } + + /// + /// Creates an Upsample module. + /// + /// + /// + /// + public IObservable> Process(IObservable source) + { + return source.Select(_ => Upsample(Size, ScaleFactor, Mode, AlignCorners, RecomputeScaleFactor)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs new file mode 100644 index 00000000..66ca14f9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs @@ -0,0 +1,44 @@ +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that creates a module for image processing. +/// +[XmlInclude(typeof(Vision.PixelShuffle))] +[XmlInclude(typeof(Vision.PixelUnshuffle))] +[XmlInclude(typeof(Vision.Upsample))] +[DefaultProperty(nameof(VisionModule))] +[Combinator] +[Description("Creates a module for image processing.")] +[WorkflowElementCategory(ElementCategory.Source)] +public class VisionModuleBuilder : ModuleCombinatorBuilder, INamedElement +{ + /// + public override Range ArgumentRange => Range.Create(0, 1); + + /// + /// Initializes a new instance of the class. + /// + public VisionModuleBuilder() + { + Module = new Vision.PixelShuffle(); + } + + /// + /// Gets or sets the specific vision module to create. + /// + [DesignOnly(true)] + [DisplayName("Module")] + [Externalizable(false)] + [RefreshProperties(RefreshProperties.All)] + [Category(nameof(CategoryAttribute.Design))] + [Description("The specific vision module to create.")] + [TypeConverter(typeof(ModuleTypeConverter))] + public object VisionModule + { + get => Module; + set => Module = value; + } +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs b/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs index 6fc38f87..b0e872dd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs @@ -14,16 +14,15 @@ namespace Bonsai.ML.Torch.NeuralNets; /// -/// Represents an operator that sets the gradient of the specified optimizer to zero. +/// Represents an operator that sets the gradient of the input optimizer to zero. /// [Combinator] -[ResetCombinator] -[Description("Sets the gradient to zero.")] +[Description("Sets the gradient of the input optimizer to zero.")] [WorkflowElementCategory(ElementCategory.Sink)] public class ZeroGradient { /// - /// Sets the gradient of the specified optimizer to zero. + /// Sets the gradient of the input optimizer to zero. /// /// /// From 0a22aaae5728bf6bc57563c5d227d94c95570a28 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 15 Jan 2026 15:14:48 +0000 Subject: [PATCH 20/30] Fixed issue with `TransformerDecoder` and `TransformerEncoder` modules not accepting input --- .../NeuralNets/Transformer/TransformerDecoder.cs | 15 ++++++++++----- .../NeuralNets/Transformer/TransformerEncoder.cs | 13 +++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs index 01c237bd..c3adb41f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs @@ -23,13 +23,18 @@ public class TransformerDecoder public long NumLayers { get; set; } /// - /// Creates a TransformerDecoder module from the input TransformerDecoderLayer. + /// The transformer decoder layer to be stacked. + /// + [XmlIgnore] + [Description("The transformer decoder layer to be stacked.")] + public TorchSharp.Modules.TransformerDecoderLayer TransformerDecoderLayer { get; private set; } + + /// + /// Creates a TransformerDecoder module. /// - /// /// - public IObservable> Process(IObservable> source) + public IObservable> Process() { - return source.Select(transformerDecoderLayer => - TransformerDecoder(transformerDecoderLayer as TorchSharp.Modules.TransformerDecoderLayer, NumLayers)); + return Observable.Return(TransformerDecoder(TransformerDecoderLayer, NumLayers)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs index d5cc1326..384a5585 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs @@ -22,14 +22,19 @@ public class TransformerEncoder [Description("The number of sub-encoder layers in the encoder.")] public long NumLayers { get; set; } + /// + /// The transformer encoder layer to be stacked. + /// + [XmlIgnore] + [Description("The transformer encoder layer to be stacked.")] + public TorchSharp.Modules.TransformerEncoderLayer TransformerEncoderLayer { get; private set; } + /// /// Creates a TransformerEncoder module from the input TransformerEncoderLayer. /// - /// /// - public IObservable> Process(IObservable> source) + public IObservable> Process() { - return source.Select(transformerEncoderLayer => - TransformerEncoder(transformerEncoderLayer as TorchSharp.Modules.TransformerEncoderLayer, NumLayers)); + return Observable.Return(TransformerEncoder(TransformerEncoderLayer, NumLayers)); } } From b3da5e75aff63e1bcfd3b0857d1db369d1c4eb37 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 15 Jan 2026 16:11:22 +0000 Subject: [PATCH 21/30] Refactored modules into `ActivationFunction` namespace and changed to use pytorch naming conventions --- .../Celu.cs} | 4 +- .../Elu.cs} | 4 +- .../Gelu.cs} | 4 +- .../Gated.cs => ActivationFunction/Glu.cs} | 6 +- .../Hardshrink.cs} | 4 +- .../Hardsigmoid.cs} | 4 +- .../Hardswish.cs | 2 +- .../Hardtanh.cs} | 6 +- .../LeakyRelu.cs} | 6 +- .../LogSigmoid.cs | 2 +- .../LogSoftmax.cs | 4 +- .../Mish.cs | 2 +- .../MultiheadAttention.cs | 2 +- .../Prelu.cs} | 4 +- .../Relu.cs} | 4 +- .../ReluBounded.cs} | 4 +- .../Rrelu.cs} | 4 +- .../Selu.cs} | 4 +- .../Sigmoid.cs | 2 +- .../Silu.cs} | 4 +- .../Softmax.cs | 2 +- .../Softmax2d.cs} | 2 +- .../Softmin.cs | 2 +- .../Softplus.cs | 2 +- .../Softshrink.cs} | 8 +-- .../Softsign.cs | 2 +- .../Tanh.cs | 2 +- .../Tanhshrink.cs} | 8 +-- .../Threshold.cs | 2 +- .../NeuralNets/ActivationFunctionBuilder.cs | 64 +++++++++---------- 30 files changed, 85 insertions(+), 85 deletions(-) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/ContinuouslyDifferentiableExponential.cs => ActivationFunction/Celu.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/Exponential.cs => ActivationFunction/Elu.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/GaussianError.cs => ActivationFunction/Gelu.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/Gated.cs => ActivationFunction/Glu.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/HardShrinkage.cs => ActivationFunction/Hardshrink.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/HardSigmoid.cs => ActivationFunction/Hardsigmoid.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Hardswish.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/HardTanh.cs => ActivationFunction/Hardtanh.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/LeakyRectified.cs => ActivationFunction/LeakyRelu.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/LogSigmoid.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/LogSoftmax.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Mish.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/MultiheadAttention.cs (98%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/ParametricRectified.cs => ActivationFunction/Prelu.cs} (95%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/Rectified.cs => ActivationFunction/Relu.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/RectifiedBounded.cs => ActivationFunction/ReluBounded.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/RandomizedLeakyRectified.cs => ActivationFunction/Rrelu.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/ScaledExponential.cs => ActivationFunction/Selu.cs} (93%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Sigmoid.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/SigmoidWeighted.cs => ActivationFunction/Silu.cs} (92%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Softmax.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/Softmax2D.cs => ActivationFunction/Softmax2d.cs} (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Softmin.cs (95%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Softplus.cs (96%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/SoftShrinkage.cs => ActivationFunction/Softshrink.cs} (82%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Softsign.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Tanh.cs (94%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations/TanhShrinkage.cs => ActivationFunction/Tanhshrink.cs} (79%) rename src/Bonsai.ML.Torch/NeuralNets/{NonLinearActivations => ActivationFunction}/Threshold.cs (96%) diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs index 74829146..8e4555ba 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ContinuouslyDifferentiableExponential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a continuously differentiable exponential linear unit (CELU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a continuously differentiable exponential linear unit (CELU) activation function.")] -public class ContinuouslyDifferentiableExponential +public class Celu { /// /// The alpha value for the CELU activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs index db323f09..e1697c3c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Exponential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates an exponential linear unit (ELU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates an exponential linear unit (ELU) activation function.")] -public class Exponential +public class Elu { /// /// The alpha value for the ELU activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs index 5ec93e28..7749d402 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/GaussianError.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a gaussian error linear unit (GELU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a gaussian error linear unit (GELU) activation function.")] -public class GaussianError +public class Gelu { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs index d26fa8b9..8a578c3b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Gated.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a gated linear unit (GLU) module. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a gated linear unit (GLU) module.")] -public class Gated +public class Glu { /// /// The dimension on which to split the input tensor. @@ -40,4 +40,4 @@ public IObservable> Process(IObservable source) { return source.Select(_ => GLU(Dim)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs index 6006e22e..1764d88f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a Hardshrink module. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a Hardshrink module.")] -public class HardShrinkage +public class Hardshrink { /// /// The lambda parameter for the Hardshrink function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs index 395a4709..2529c819 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a Hardsigmoid module. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a Hardsigmoid module.")] -public class HardSigmoid +public class Hardsigmoid { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Hardswish.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Hardswish.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs index e42df6be..750708db 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Hardswish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a Hardswish module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs index 3376470d..1dedeed2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/HardTanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a Hardtanh module. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a Hardtanh module.")] -public class HardTanh +public class Hardtanh { /// /// The minimum value of the linear region range. @@ -52,4 +52,4 @@ public IObservable> Process(IObservable source) { return source.Select(_ => Hardtanh(MinVal, MaxVal, Inplace)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs index 3d35c5dd..83f55e16 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LeakyRectified.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a leaky rectified linear unit (LeakyReLU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a leaky rectified linear unit (LeakyReLU) activation function.")] -public class LeakyRectified +public class LeakyRelu { /// /// The angle of the negative slope. @@ -46,4 +46,4 @@ public IObservable> Process(IObservable source) { return source.Select(_ => LeakyReLU(NegativeSlope, Inplace)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs index 6bd4f15a..4a6007ba 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a log sigmoid module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs index 26529819..c31e5a8a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/LogSoftmax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a log softmax activation function. @@ -40,4 +40,4 @@ public IObservable> Process(IObservable source) { return source.Select(_ => LogSoftmax(Dim)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs index d0e94789..fd47c128 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Mish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a mish activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs similarity index 98% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs index 6e93b8db..784cee74 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/MultiheadAttention.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a multi-head attention mechanism. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs index 5c4a33f7..73e65e4a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ParametricRectified.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs @@ -5,7 +5,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a parametric rectified linear unit (PReLU) activation function. @@ -14,7 +14,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a parametric rectified linear unit (PReLU) activation function.")] -public class ParametricRectified +public class Prelu { /// /// The number of parameters to learn. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs index 1c540e02..73cf57b7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Rectified.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a rectified linear unit (ReLU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a rectified linear unit (ReLU) activation function.")] -public class Rectified +public class Relu { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs index 2c6fe0c8..a85f08e3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RectifiedBounded.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a bounded rectified linear unit (ReLU6) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a bounded rectified linear unit (ReLU6) activation function.")] -public class RectifiedBounded +public class ReluBounded { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs index 1bc54c53..60fa6010 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/RandomizedLeakyRectified.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a randomized leaky rectified linear unit (RReLU) module. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a randomized leaky rectified linear unit (RReLU) module.")] -public class RandomizedLeakyRectified +public class Rrelu { /// /// The lower bound of the uniform distribution. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs similarity index 93% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs index 26d3e75c..1de297b4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/ScaledExponential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a scaled exponential linear unit (SELU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a scaled exponential linear unit (SELU) activation function.")] -public class ScaledExponential +public class Selu { /// /// If set to true, will do this operation in-place. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs index 4c76e691..c7a7a2a6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Sigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a sigmoid activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs index f2575b71..d9270f3e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SigmoidWeighted.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a sigmoid weighted linear unit (SiLU) activation function. @@ -13,7 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; /// See for more information. /// [Description("Creates a sigmoid weighted linear unit (SiLU) activation function.")] -public class SigmoidWeighted +public class Silu { /// /// Creates a sigmoid weighted linear unit (SiLU) module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs index 84210259..c065cede 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a softmax activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs index 54032a87..c54027bc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmax2D.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a 2d softmax activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs similarity index 95% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs index 4408f853..de863e7c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softmin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a softmin activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs index 9e8a62eb..e726c8ac 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softplus.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a softplus activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs index 5ac57345..ba37b1ac 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/SoftShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs @@ -4,16 +4,16 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// -/// Represents an operator that creates a soft shrinkage activation function. +/// Represents an operator that creates a softshrink activation function. /// /// /// See for more information. /// -[Description("Creates a soft shrinkage activation function.")] -public class SoftShrinkage +[Description("Creates a softshrink activation function.")] +public class Softshrink { /// /// The lambda value for the softshrink formula. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs index 4fac0516..d6210607 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Softsign.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a softsign activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs similarity index 94% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs index c1ba28aa..0ec82c6d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Tanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a hyperbolic tangent (tanh) activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs similarity index 79% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs index 64f8c6f1..e42808c5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/TanhShrinkage.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs @@ -4,16 +4,16 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// -/// Represents an operator that creates a tanh shrinkage activation function. +/// Represents an operator that creates a tanhshrink activation function. /// /// /// See for more information. /// -[Description("Creates a tanh shrinkage activation function.")] -public class TanhShrinkage +[Description("Creates a tanhshrink activation function.")] +public class Tanhshrink { /// /// Creates a Tanhshrink module. diff --git a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs similarity index 96% rename from src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs rename to src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs index 607c3f04..a7e4bf6e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NonLinearActivations/Threshold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs @@ -4,7 +4,7 @@ using static TorchSharp.torch; using static TorchSharp.torch.nn; -namespace Bonsai.ML.Torch.NeuralNets.NonLinearActivations; +namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// /// Represents an operator that creates a threshold activation function. diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs index 003a3c4a..b60d318b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs @@ -6,35 +6,35 @@ namespace Bonsai.ML.Torch.NeuralNets; /// /// Represents an operator that creates an activation function. /// -[XmlInclude(typeof(NonLinearActivations.ContinuouslyDifferentiableExponential))] -[XmlInclude(typeof(NonLinearActivations.Exponential))] -[XmlInclude(typeof(NonLinearActivations.Gated))] -[XmlInclude(typeof(NonLinearActivations.GaussianError))] -[XmlInclude(typeof(NonLinearActivations.HardShrinkage))] -[XmlInclude(typeof(NonLinearActivations.HardSigmoid))] -[XmlInclude(typeof(NonLinearActivations.Hardswish))] -[XmlInclude(typeof(NonLinearActivations.HardTanh))] -[XmlInclude(typeof(NonLinearActivations.LeakyRectified))] -[XmlInclude(typeof(NonLinearActivations.LogSigmoid))] -[XmlInclude(typeof(NonLinearActivations.LogSoftmax))] -[XmlInclude(typeof(NonLinearActivations.Mish))] -[XmlInclude(typeof(NonLinearActivations.MultiheadAttention))] -[XmlInclude(typeof(NonLinearActivations.ParametricRectified))] -[XmlInclude(typeof(NonLinearActivations.RandomizedLeakyRectified))] -[XmlInclude(typeof(NonLinearActivations.Rectified))] -[XmlInclude(typeof(NonLinearActivations.RectifiedBounded))] -[XmlInclude(typeof(NonLinearActivations.ScaledExponential))] -[XmlInclude(typeof(NonLinearActivations.Sigmoid))] -[XmlInclude(typeof(NonLinearActivations.SigmoidWeighted))] -[XmlInclude(typeof(NonLinearActivations.Softmax))] -[XmlInclude(typeof(NonLinearActivations.Softmax2d))] -[XmlInclude(typeof(NonLinearActivations.Softmin))] -[XmlInclude(typeof(NonLinearActivations.Softplus))] -[XmlInclude(typeof(NonLinearActivations.SoftShrinkage))] -[XmlInclude(typeof(NonLinearActivations.Softsign))] -[XmlInclude(typeof(NonLinearActivations.Tanh))] -[XmlInclude(typeof(NonLinearActivations.TanhShrinkage))] -[XmlInclude(typeof(NonLinearActivations.Threshold))] +[XmlInclude(typeof(ActivationFunction.CELU))] +[XmlInclude(typeof(ActivationFunction.ELU))] +[XmlInclude(typeof(ActivationFunction.GLU))] +[XmlInclude(typeof(ActivationFunction.GELU))] +[XmlInclude(typeof(ActivationFunction.Hardshrink))] +[XmlInclude(typeof(ActivationFunction.Hardsigmoid))] +[XmlInclude(typeof(ActivationFunction.Hardswish))] +[XmlInclude(typeof(ActivationFunction.Hardtanh))] +[XmlInclude(typeof(ActivationFunction.LeakyReLU))] +[XmlInclude(typeof(ActivationFunction.LogSigmoid))] +[XmlInclude(typeof(ActivationFunction.LogSoftmax))] +[XmlInclude(typeof(ActivationFunction.Mish))] +[XmlInclude(typeof(ActivationFunction.MultiheadAttention))] +[XmlInclude(typeof(ActivationFunction.PReLU))] +[XmlInclude(typeof(ActivationFunction.RReLU))] +[XmlInclude(typeof(ActivationFunction.Rectified))] +[XmlInclude(typeof(ActivationFunction.RectifiedBounded))] +[XmlInclude(typeof(ActivationFunction.SELU))] +[XmlInclude(typeof(ActivationFunction.Sigmoid))] +[XmlInclude(typeof(ActivationFunction.SiLU))] +[XmlInclude(typeof(ActivationFunction.Softmax))] +[XmlInclude(typeof(ActivationFunction.Softmax2d))] +[XmlInclude(typeof(ActivationFunction.Softmin))] +[XmlInclude(typeof(ActivationFunction.Softplus))] +[XmlInclude(typeof(ActivationFunction.Softshrink))] +[XmlInclude(typeof(ActivationFunction.Softsign))] +[XmlInclude(typeof(ActivationFunction.Tanh))] +[XmlInclude(typeof(ActivationFunction.Tanhshrink))] +[XmlInclude(typeof(ActivationFunction.Threshold))] [DefaultProperty(nameof(ActivationFunction))] [Combinator] [Description("Creates an activation function.")] @@ -44,16 +44,16 @@ public class ActivationFunctionBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); - string INamedElement.Name => $"ActivationFunction.{GetElementDisplayName(ActivationFunction)}"; + internal override string BuilderName => "ActivationFunction"; /// /// Initializes a new instance of the class. /// public ActivationFunctionBuilder() { - Module = new NonLinearActivations.Rectified(); + Module = new ActivationFunction.Rectified(); } - + /// /// Gets or sets the specific activation function to create. /// From 8d3b49f256b61b684d17aa70092e9b6ad94490ca Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 15 Jan 2026 16:12:48 +0000 Subject: [PATCH 22/30] Refactored module builders to use name of builder --- .../{SequentialBuilder.cs => ContainerBuilder.cs} | 10 +++++----- .../NeuralNets/ConvolutionModuleBuilder.cs | 4 +++- .../NeuralNets/DistanceModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs | 4 +++- .../NeuralNets/LearningRateSchedulerBuilder.cs | 4 ++-- src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs | 10 +++++----- .../NeuralNets/ModuleCombinatorBuilder.cs | 6 ++++-- .../NeuralNets/NormalizationModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs | 4 ++-- src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs | 4 +++- .../NeuralNets/RecurrentModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs | 4 +++- .../NeuralNets/TransformerModuleBuilder.cs | 4 +++- src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs | 4 +++- 18 files changed, 57 insertions(+), 29 deletions(-) rename src/Bonsai.ML.Torch/NeuralNets/{SequentialBuilder.cs => ContainerBuilder.cs} (82%) diff --git a/src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs similarity index 82% rename from src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs rename to src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs index 63da38b0..65e69e6a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SequentialBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs @@ -11,22 +11,22 @@ namespace Bonsai.ML.Torch.NeuralNets; [Combinator] [Description("Creates a sequential container for torch modules.")] [WorkflowElementCategory(ElementCategory.Source)] -public class SequentialBuilder : ModuleCombinatorBuilder, INamedElement +public class ContainerBuilder : ModuleCombinatorBuilder, INamedElement { /// public override Range ArgumentRange => Range.Create(0, 1); /// - string INamedElement.Name => "Sequential"; + internal override string BuilderName => "Container"; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public SequentialBuilder() + public ContainerBuilder() { Module = new Container.Sequential(); } - + /// /// Gets or sets the specific container module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs index 58824d80..d570804b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs @@ -23,6 +23,8 @@ public class ConvolutionModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "ConvolutionModule"; + /// /// Initializes a new instance of the class. /// @@ -30,7 +32,7 @@ public ConvolutionModuleBuilder() { Module = new Convolution.Conv1d(); } - + /// /// Gets or sets the specific convolution module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs index 22e8b1ac..f0779b7a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs @@ -17,6 +17,8 @@ public class DistanceModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "DistanceModule"; + /// /// Initializes a new instance of the class. /// @@ -24,7 +26,7 @@ public DistanceModuleBuilder() { Module = new Distance.CosineSimilarity(); } - + /// /// Gets or sets the specific distance module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs index b06e6a2c..b448651c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs @@ -21,6 +21,8 @@ public class DropoutModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "DropoutModule"; + /// /// Initializes a new instance of the class. /// @@ -28,7 +30,7 @@ public DropoutModuleBuilder() { Module = new Dropout.AlphaDropout(); } - + /// /// Gets or sets the specific dropout module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs index cd1ec66e..64134556 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs @@ -17,6 +17,8 @@ public class FlattenModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "FlattenModule"; + /// /// Initializes a new instance of the class. /// @@ -24,7 +26,7 @@ public FlattenModuleBuilder() { Module = new Flatten.Flatten(); } - + /// /// Gets or sets the specific flatten module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs index b23582e8..83268749 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs @@ -24,7 +24,7 @@ public class LearningRateSchedulerBuilder : ModuleCombinatorBuilder, INamedEleme /// public override Range ArgumentRange => Range.Create(0, 1); - string INamedElement.Name => $"LearningRateScheduler.{GetElementDisplayName(LearningRateScheduler)}"; + internal override string BuilderName => "LearningRateScheduler"; /// /// Initializes a new instance of the class. @@ -33,7 +33,7 @@ public LearningRateSchedulerBuilder() { Module = new LearningRateScheduler.Constant(); } - + /// /// Gets or sets the specific learning rate scheduler to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs index 7787f293..b148ac40 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs @@ -18,6 +18,8 @@ public class LinearModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "LinearModule"; + /// /// Initializes a new instance of the class. /// @@ -25,7 +27,7 @@ public LinearModuleBuilder() { Module = new Linear.Linear(); } - + /// /// Gets or sets the specific linear module. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs index 74996062..8197c6ab 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs @@ -30,21 +30,21 @@ namespace Bonsai.ML.Torch.NeuralNets; [Combinator] [Description("Creates a loss module.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LossModuleBuilder : ModuleCombinatorBuilder, INamedElement +public class LossBuilder : ModuleCombinatorBuilder, INamedElement { /// public override Range ArgumentRange => Range.Create(0, 1); - string INamedElement.Name => $"Loss.{GetElementDisplayName(LossModule)}"; + internal override string BuilderName => "Loss"; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public LossModuleBuilder() + public LossBuilder() { Module = new Loss.GaussianNegativeLogLikelihood(); } - + /// /// Gets or sets the specific loss module. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs index 1db7a3b7..311be66c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs @@ -14,7 +14,9 @@ namespace Bonsai.ML.Torch.NeuralNets; public abstract class ModuleCombinatorBuilder : ExpressionBuilder, ICustomTypeDescriptor, INamedElement { /// - string INamedElement.Name => $"Module.{GetElementDisplayName(Module)}"; + string INamedElement.Name => $"{BuilderName}.{GetElementDisplayName(Module)}"; + + internal abstract string BuilderName { get; } internal object Module { get; set; } @@ -147,4 +149,4 @@ object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return pd?.ComponentType.IsAssignableFrom(GetType()) == true ? this : Module; } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs index 8126a798..8da81a75 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs @@ -24,6 +24,8 @@ public class NormalizationModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "NormalizationModule"; + /// /// Initializes a new instance of the class. /// @@ -31,7 +33,7 @@ public NormalizationModuleBuilder() { Module = new Normalization.BatchNorm1d(); } - + /// /// Gets or sets the specific normalization module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs index a4475ba3..716bd1de 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs @@ -25,7 +25,7 @@ public class OptimizerBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); - string INamedElement.Name => $"Optimizer.{GetElementDisplayName(OptimizerModule)}"; + internal override string BuilderName => "Optimizer"; /// /// Initializes a new instance of the class. @@ -34,7 +34,7 @@ public OptimizerBuilder() { Module = new Optimizer.Adam(); } - + /// /// Gets or sets the specific optimizer to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs index bf3b85fb..31cd6d18 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs @@ -25,6 +25,8 @@ public class PaddingModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "PaddingModule"; + /// /// Initializes a new instance of the class. /// @@ -32,7 +34,7 @@ public PaddingModuleBuilder() { Module = new Padding.ConstantPad1d(); } - + /// /// Gets or sets the specific padding module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs index 3c800fd7..89483de1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs @@ -34,6 +34,8 @@ public class PoolingModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "PoolingModule"; + /// /// Initializes a new instance of the class. /// @@ -41,7 +43,7 @@ public PoolingModuleBuilder() { Module = new Pooling.AdaptiveAvgPool1d(); } - + /// /// Gets or sets the specific pooling module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs index 8b847bb0..00dcdc96 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs @@ -21,6 +21,8 @@ public class RecurrentModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "RecurrentModule"; + /// /// Initializes a new instance of the class. /// @@ -28,7 +30,7 @@ public RecurrentModuleBuilder() { Module = new Recurrent.RecurrentNeuralNetwork(); } - + /// /// Gets or sets the specific recurrent neural network module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs index 6c766821..286d389d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs @@ -16,6 +16,8 @@ public class ShuffleModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "ShuffleModule"; + /// /// Initializes a new instance of the class. /// @@ -23,7 +25,7 @@ public ShuffleModuleBuilder() { Module = new Shuffle.ChannelShuffle(); } - + /// /// Gets or sets the specific shuffle module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs index c8aa829a..3cbb2e47 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs @@ -19,6 +19,8 @@ public class SparseModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "SparseModule"; + /// /// Initializes a new instance of the class. /// @@ -26,7 +28,7 @@ public SparseModuleBuilder() { Module = new Sparse.Embedding(); } - + /// /// Gets or sets the specific sparse module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs index 66fb0904..64504b34 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs @@ -20,6 +20,8 @@ public class TransformerModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "TransformerModule"; + /// /// Initializes a new instance of the class. /// @@ -27,7 +29,7 @@ public TransformerModuleBuilder() { Module = new Transformer.Transformer(); } - + /// /// Gets or sets the specific transformer module to create. /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs index 66ca14f9..56f0e725 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs @@ -18,6 +18,8 @@ public class VisionModuleBuilder : ModuleCombinatorBuilder, INamedElement /// public override Range ArgumentRange => Range.Create(0, 1); + internal override string BuilderName => "VisionModule"; + /// /// Initializes a new instance of the class. /// @@ -25,7 +27,7 @@ public VisionModuleBuilder() { Module = new Vision.PixelShuffle(); } - + /// /// Gets or sets the specific vision module to create. /// From cfd257e4177fee7d660738d29a4558b4054a593b Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 15 Jan 2026 19:09:20 +0000 Subject: [PATCH 23/30] Refactored forward method into a builder class to support derived module classes --- src/Bonsai.ML.Torch/NeuralNets/Forward.cs | 140 ------------------ .../NeuralNets/ForwardBuilder.cs | 122 +++++++++++++++ 2 files changed, 122 insertions(+), 140 deletions(-) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/Forward.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/ForwardBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs b/src/Bonsai.ML.Torch/NeuralNets/Forward.cs deleted file mode 100644 index 65b54d7f..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/Forward.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using System.Linq; -using System.ComponentModel; -using System.Reactive.Linq; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.jit; - -namespace Bonsai.ML.Torch.NeuralNets; - -/// -/// Represents an operator that runs forward inference on the input using the specified module. -/// -[Combinator] -[Description("Runs forward inference on the input using the specified module.")] -[WorkflowElementCategory(ElementCategory.Transform)] -public class Forward -{ - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - public IObservable Process(IObservable>> source) - { - return source.Select(input => input.Item2.forward(input.Item1)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - /// - public IObservable Process(IObservable, Module>> source) - { - return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - /// - /// - public IObservable Process(IObservable, Module>> source) - { - return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable Process(IObservable, Module>> source) - { - return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3, input.Item1.Item4)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable Process(IObservable, Module>> source) - { - return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3, input.Item1.Item4, input.Item1.Item5)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable Process(IObservable, Module>> source) - { - return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2, input.Item1.Item3, input.Item1.Item4, input.Item1.Item5, input.Item1.Item6)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - public IObservable Process(IObservable> source) - { - return source.Select(input => input.forward()); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - public IObservable Process(IObservable>> source) - { - return source.Select(input => input.Item2.forward(input.Item1)); - } - - /// - /// Runs forward inference on the input using the specified module. - /// - /// - /// - /// - /// - /// - public IObservable Process(IObservable, ScriptModule>> source) - { - return source.Select(input => input.Item2.forward(input.Item1.Item1, input.Item1.Item2)); - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/ForwardBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ForwardBuilder.cs new file mode 100644 index 00000000..79074611 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/ForwardBuilder.cs @@ -0,0 +1,122 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Reflection; +using static TorchSharp.torch; +using static TorchSharp.torch.jit; +using Bonsai.Expressions; +using TorchSharp; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that runs forward inference on the input using the specified module. +/// +[Combinator] +[Description("Runs forward inference on the input using the specified module.")] +[WorkflowElementCategory(ElementCategory.Transform)] +public class ForwardBuilder : SingleArgumentExpressionBuilder +{ + /// + public override Expression Build(IEnumerable arguments) + { + var argumentList = arguments.First().Type.GetGenericArguments()[0]; + var inputArgs = argumentList.GetGenericArguments()[0]; + var moduleArg = argumentList.GetGenericArguments()[1]; + + var sourceType = typeof(Tuple<,>).MakeGenericType(inputArgs, moduleArg); + + var selectMethod = typeof(Observable) + .GetMethods() + .First(m => + m.Name == nameof(Observable.Select) && + m.IsGenericMethodDefinition && + m.GetParameters().Length == 2); + + System.Type? moduleType = null; + Expression? selectExpression = null; + System.Type? resultType = null; + + for (var t = moduleArg; t != null && t != typeof(object); t = t.BaseType) + { + if (t.IsGenericType && t.GetGenericTypeDefinition().Name.StartsWith("Module`")) + { + moduleType = t; + + resultType = typeof(Tuple<,>).MakeGenericType(inputArgs, moduleType); + + var tuple1 = Expression.Parameter(sourceType, "t"); + var tuple1Item1 = Expression.Property(tuple1, "Item1"); + var tuple1Item2 = Expression.Property(tuple1, "Item2"); + + var moduleConversionExpression = Expression.Convert(tuple1Item2, moduleType); + var tupleCreateMethod = typeof(Tuple) + .GetMethods() + .Single(m => + m.Name == nameof(Tuple.Create) && + m.IsGenericMethodDefinition && + m.GetGenericArguments().Length == 2 && + m.GetParameters().Length == 2) + .MakeGenericMethod(inputArgs, moduleType); + + var newTuple = Expression.Call(tupleCreateMethod, tuple1Item1, moduleConversionExpression); + + var selector = Expression.Lambda(newTuple, tuple1); + + selectExpression = Expression.Call(selectMethod.MakeGenericMethod(sourceType, resultType), arguments.First(), selector); + break; + } + + else if (t.IsGenericType && t.GetGenericTypeDefinition().Name.StartsWith("ScriptModule`")) + { + moduleType = t; + resultType = sourceType; + selectExpression = arguments.First(); + break; + } + } + + if (moduleType is null) + throw new InvalidOperationException("The specified module type is not a valid TorchSharp module."); + + var tuple = Expression.Parameter(resultType, "t"); + var item1 = Expression.Property(tuple, "Item1"); + var item2 = Expression.Property(tuple, "Item2"); + + List forwardCallArgs = []; + + if (inputArgs.IsGenericType && inputArgs.GetGenericTypeDefinition().Name.StartsWith("Tuple`")) + { + var inputArgsTypes = inputArgs.GetGenericArguments(); + for (int i = 0; i < inputArgsTypes.Length; i++) + { + var itemN = Expression.Property(item1, $"Item{i + 1}"); + forwardCallArgs.Add(itemN); + } + } + else + { + forwardCallArgs.Add(item1); + } + + var moduleMethods = moduleType + .GetMethods(BindingFlags.Instance | BindingFlags.Public) + .Where(m => m.Name == "forward" && + m.IsPublic && + m.GetParameters().Length == forwardCallArgs.Count && + m.GetParameters().Select(p => p.ParameterType).SequenceEqual(forwardCallArgs.Select(a => a.Type))); + + if (!moduleMethods.Any()) + throw new InvalidOperationException("The module does not contain a matching forward method."); + + var forwardMethod = moduleMethods.First(); + + var forwardCall = Expression.Call(item2, forwardMethod, forwardCallArgs); + var forwardLambda = Expression.Lambda(forwardCall, tuple); + + return Expression.Call(selectMethod.MakeGenericMethod(resultType, forwardMethod.ReturnType), selectExpression, forwardLambda); + } +} From 5c8904f15037cf89875a9efd5d014f86fcfd84b5 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Fri, 16 Jan 2026 13:05:42 +0000 Subject: [PATCH 24/30] Refactored `LoadModule` into builder class to support derived modules --- src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs | 202 ------------------ .../NeuralNets/LoadModuleStateBuilder.cs | 86 ++++++++ 2 files changed, 86 insertions(+), 202 deletions(-) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs deleted file mode 100644 index 3610d789..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModule.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System; -using System.ComponentModel; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.jit; - -namespace Bonsai.ML.Torch.NeuralNets; - -/// -/// Represents an operator that loads a module's state from a file. -/// -[Combinator] -[Description("Loads a module's state from a file.")] -[WorkflowElementCategory(ElementCategory.Sink)] -public class LoadModule -{ - /// - /// The path to the module's saved state. - /// - [Description("The path to the module's saved state.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModulePath { get; set; } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - public IObservable Process(IObservable source) - { - return source.Select(module => - { - return module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (Module)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (Module)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (Module)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (Module)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (Module)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (Module)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - public IObservable Process(IObservable source) - { - return source.Select(module => - { - return (ScriptModule)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (ScriptModule)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (ScriptModule)module.load(ModulePath); - }); - } - - /// - /// Loads the module's state from the specified file path. - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Select(module => - { - return (ScriptModule)module.load(ModulePath); - }); - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs new file mode 100644 index 00000000..7351e49f --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs @@ -0,0 +1,86 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Reflection; +using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; +using Bonsai.Expressions; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that loads a module's state from a file. +/// +[Combinator] +[Description("Loads a module's state from a file.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class LoadModuleStateBuilder : SingleArgumentExpressionBuilder +{ + /// + /// The path to the module's saved state. + /// + [Description("The path to the module's saved state.")] + [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModulePath { get; set; } + + /// + public override Expression Build(IEnumerable args) + { + var inputArg = args.First().Type.GetGenericArguments()[0]; + bool isInputModule = false; + for (var t = inputArg; t != null && t != typeof(object); t = t.BaseType) + { + if (t.GetType() == typeof(nn.Module) || t.IsGenericType && t.GetGenericTypeDefinition().Name.StartsWith("Module`")) + { + isInputModule = true; + break; + } + } + + if (!isInputModule) + throw new InvalidOperationException("The LoadModuleState operator requires a Module type as input."); + + var selectMethod = typeof(Observable) + .GetMethods() + .First(m => + m.Name == nameof(Observable.Select) && + m.IsGenericMethodDefinition && + m.GetParameters().Length == 2); + + var moduleParameter = Expression.Parameter(inputArg, "m"); + + var moduleLoadMethods = typeof(nn.Module) + .GetMethods(BindingFlags.Instance | BindingFlags.Public) + .Where(m => m.Name == "load" && + m.GetParameters().Length == 4 && + m.GetParameters()[0].ParameterType == typeof(string) && + m.GetParameters()[1].ParameterType == typeof(bool) && + m.GetParameters()[2].ParameterType == typeof(IList) && + m.GetParameters()[3].ParameterType == typeof(Dictionary)); + + var moduleLoadMethod = moduleLoadMethods.First(); + + List moduleLoadArgs = + [ + Expression.Constant(ModulePath, typeof(string)), + Expression.Constant(true), // strict loading + Expression.Constant(null, typeof(IList)), // exclude list + Expression.Constant(null, typeof(Dictionary)) // exclude dictionary + ]; + + var loadCall = Expression.Call(moduleParameter, moduleLoadMethod, moduleLoadArgs); + + var convertLoadCall = Expression.Convert(loadCall, inputArg); + var lambda = Expression.Lambda(convertLoadCall, moduleParameter); + + var genericSelectMethod = selectMethod.MakeGenericMethod(inputArg, inputArg); + + return Expression.Call(genericSelectMethod, args.First(), lambda); + } +} From e9f4d091af42f8f399953b37e0e8f3ddd4dcf086 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Mon, 19 Jan 2026 14:28:50 +0000 Subject: [PATCH 25/30] Updated modules to return derived class which allows easy access to module-specific parameters --- .../NeuralNets/ActivationFunction/Celu.cs | 5 +- .../NeuralNets/ActivationFunction/Elu.cs | 5 +- .../NeuralNets/ActivationFunction/Gelu.cs | 5 +- .../NeuralNets/ActivationFunction/Glu.cs | 5 +- .../ActivationFunction/Hardshrink.cs | 4 +- .../ActivationFunction/Hardsigmoid.cs | 4 +- .../ActivationFunction/Hardswish.cs | 4 +- .../NeuralNets/ActivationFunction/Hardtanh.cs | 4 +- .../ActivationFunction/LeakyRelu.cs | 5 +- .../ActivationFunction/LogSigmoid.cs | 4 +- .../ActivationFunction/LogSoftmax.cs | 4 +- .../NeuralNets/ActivationFunction/Mish.cs | 4 +- .../ActivationFunction/MultiheadAttention.cs | 4 +- .../NeuralNets/ActivationFunction/Prelu.cs | 5 +- .../NeuralNets/ActivationFunction/Relu.cs | 5 +- .../ActivationFunction/ReluBounded.cs | 5 +- .../NeuralNets/ActivationFunction/Rrelu.cs | 5 +- .../NeuralNets/ActivationFunction/Selu.cs | 5 +- .../NeuralNets/ActivationFunction/Sigmoid.cs | 4 +- .../NeuralNets/ActivationFunction/Silu.cs | 5 +- .../NeuralNets/ActivationFunction/Softmax.cs | 4 +- .../ActivationFunction/Softmax2d.cs | 4 +- .../NeuralNets/ActivationFunction/Softmin.cs | 4 +- .../NeuralNets/ActivationFunction/Softplus.cs | 4 +- .../ActivationFunction/Softshrink.cs | 4 +- .../NeuralNets/ActivationFunction/Softsign.cs | 4 +- .../NeuralNets/ActivationFunction/Tanh.cs | 4 +- .../ActivationFunction/Tanhshrink.cs | 4 +- .../ActivationFunction/Threshold.cs | 4 +- .../NeuralNets/ActivationFunctionBuilder.cs | 24 ++-- .../NeuralNets/Container/Sequential.cs | 4 +- .../NeuralNets/Convolution/Conv1d.cs | 4 +- .../NeuralNets/Convolution/Conv2d.cs | 4 +- .../NeuralNets/Convolution/Conv3d.cs | 4 +- .../NeuralNets/Convolution/ConvTranspose1d.cs | 4 +- .../NeuralNets/Convolution/ConvTranspose2d.cs | 4 +- .../NeuralNets/Convolution/ConvTranspose3d.cs | 4 +- .../NeuralNets/Convolution/Fold.cs | 4 +- .../NeuralNets/Convolution/Unfold.cs | 4 +- .../NeuralNets/Distance/CosineSimilarity.cs | 4 +- .../NeuralNets/Distance/PairwiseDistance.cs | 4 +- .../NeuralNets/Dropout/AlphaDropout.cs | 6 +- .../NeuralNets/Dropout/Dropout.cs | 4 +- .../NeuralNets/Dropout/Dropout1d.cs | 4 +- .../NeuralNets/Dropout/Dropout2d.cs | 4 +- .../NeuralNets/Dropout/Dropout3d.cs | 4 +- .../NeuralNets/Dropout/FeatureAlphaDropout.cs | 4 +- .../NeuralNets/Flatten/Flatten.cs | 4 +- .../NeuralNets/Flatten/Unflatten.cs | 4 +- .../NeuralNets/Linear/Bilinear.cs | 4 +- .../NeuralNets/Linear/Identity.cs | 4 +- .../NeuralNets/Linear/Linear.cs | 4 +- .../NeuralNets/LoadScriptModule.cs | 105 ++---------------- .../NeuralNets/Loss/BinaryCrossEntropy.cs | 7 +- .../Loss/BinaryCrossEntropyWithLogits.cs | 7 +- .../ConnectionistTemporalClassification.cs | 5 +- .../NeuralNets/Loss/CosineEmbedding.cs | 4 +- .../NeuralNets/Loss/CrossEntropy.cs | 6 +- .../Loss/GaussianNegativeLogLikelihood.cs | 5 +- .../NeuralNets/Loss/HingeEmbedding.cs | 4 +- src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs | 4 +- .../Loss/KullbackLeiblerDivergence.cs | 5 +- .../NeuralNets/Loss/MarginRanking.cs | 4 +- .../NeuralNets/Loss/MeanAbsoluteError.cs | 9 +- .../NeuralNets/Loss/MeanSquaredError.cs | 5 +- .../NeuralNets/Loss/MultiLabelMargin.cs | 4 +- .../NeuralNets/Loss/MultiLabelSoftMargin.cs | 6 +- .../{MultiClassMargin.cs => MultiMargin.cs} | 6 +- .../NeuralNets/Loss/NegativeLogLikelihood.cs | 5 +- .../Loss/PoissonNegativeLogLikelihood.cs | 5 +- .../NeuralNets/Loss/SmoothL1.cs | 4 +- .../NeuralNets/Loss/SoftMargin.cs | 4 +- .../NeuralNets/Loss/TripletMargin.cs | 4 +- src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs | 2 +- .../NeuralNets/Normalization/BatchNorm1d.cs | 4 +- .../NeuralNets/Normalization/BatchNorm2d.cs | 4 +- .../NeuralNets/Normalization/BatchNorm3d.cs | 4 +- .../NeuralNets/Normalization/GroupNorm.cs | 4 +- .../Normalization/InstanceNorm1d.cs | 4 +- .../Normalization/InstanceNorm2d.cs | 4 +- .../Normalization/InstanceNorm3d.cs | 4 +- .../NeuralNets/Normalization/LayerNorm.cs | 4 +- .../Normalization/LocalResponseNorm.cs | 4 +- .../NeuralNets/OptimizationStep.cs | 8 +- .../NeuralNets/Optimizer/Adadelta.cs | 2 +- .../NeuralNets/Optimizer/Adagrad.cs | 2 +- .../NeuralNets/Optimizer/Adam.cs | 2 +- .../NeuralNets/Optimizer/AdamW.cs | 2 +- .../NeuralNets/Optimizer/Adamax.cs | 2 +- .../AveragedStochasticGradientDescent.cs | 3 +- .../NeuralNets/Optimizer/Lbfgs.cs | 3 +- .../Optimizer/ResilientBackpropagation.cs | 3 +- .../Optimizer/RootMeanSquarePropagation.cs | 3 +- .../Optimizer/StochasticGradientDescent.cs | 3 +- .../NeuralNets/Padding/ConstantPad1d.cs | 4 +- .../NeuralNets/Padding/ConstantPad2d.cs | 4 +- .../NeuralNets/Padding/ConstantPad3d.cs | 4 +- .../NeuralNets/Padding/ReflectionPad1d.cs | 4 +- .../NeuralNets/Padding/ReflectionPad2d.cs | 4 +- .../NeuralNets/Padding/ReflectionPad3d.cs | 4 +- .../NeuralNets/Padding/ReplicationPad1d.cs | 4 +- .../NeuralNets/Padding/ReplicationPad2d.cs | 4 +- .../NeuralNets/Padding/ReplicationPad3d.cs | 4 +- .../NeuralNets/Padding/ZeroPad2d.cs | 4 +- .../NeuralNets/Pooling/AdaptiveAvgPool1d.cs | 4 +- .../NeuralNets/Pooling/AdaptiveAvgPool2d.cs | 4 +- .../NeuralNets/Pooling/AdaptiveAvgPool3d.cs | 4 +- .../NeuralNets/Pooling/AdaptiveMaxPool1d.cs | 4 +- .../NeuralNets/Pooling/AdaptiveMaxPool2d.cs | 4 +- .../NeuralNets/Pooling/AdaptiveMaxPool3d.cs | 4 +- .../NeuralNets/Pooling/AvgPool1d.cs | 4 +- .../NeuralNets/Pooling/AvgPool2d.cs | 4 +- .../NeuralNets/Pooling/AvgPool3d.cs | 4 +- .../NeuralNets/Pooling/FractionalMaxPool2d.cs | 4 +- .../NeuralNets/Pooling/FractionalMaxPool3d.cs | 4 +- .../NeuralNets/Pooling/LPPool1d.cs | 4 +- .../NeuralNets/Pooling/LPPool2d.cs | 4 +- .../NeuralNets/Pooling/MaxPool1d.cs | 4 +- .../NeuralNets/Pooling/MaxPool2d.cs | 4 +- .../NeuralNets/Pooling/MaxPool3d.cs | 4 +- .../NeuralNets/Pooling/MaxUnpool1d.cs | 4 +- .../NeuralNets/Pooling/MaxUnpool2d.cs | 4 +- .../NeuralNets/Pooling/MaxUnpool3d.cs | 4 +- .../Recurrent/GatedRecurrentUnit.cs | 4 +- .../Recurrent/GatedRecurrentUnitCell.cs | 4 +- .../Recurrent/LongShortTermMemory.cs | 4 +- .../Recurrent/LongShortTermMemoryCell.cs | 4 +- .../Recurrent/RecurrentNeuralNetwork.cs | 4 +- .../Recurrent/RecurrentNeuralNetworkCell.cs | 4 +- .../NeuralNets/Shuffle/ChannelShuffle.cs | 4 +- .../NeuralNets/Sparse/Embedding.cs | 4 +- .../NeuralNets/Sparse/EmbeddingBag.cs | 4 +- .../Sparse/EmbeddingBagFromPretrained.cs | 4 +- .../Sparse/EmbeddingFromPretrained.cs | 4 +- .../NeuralNets/Transformer/Transformer.cs | 4 +- .../Transformer/TransformerDecoder.cs | 13 +-- .../Transformer/TransformerDecoderLayer.cs | 4 +- .../Transformer/TransformerEncoder.cs | 11 +- .../Transformer/TransformerEncoderLayer.cs | 4 +- .../NeuralNets/Vision/PixelShuffle.cs | 4 +- .../NeuralNets/Vision/PixelUnshuffle.cs | 4 +- .../NeuralNets/Vision/Upsample.cs | 4 +- 142 files changed, 327 insertions(+), 401 deletions(-) rename src/Bonsai.ML.Torch/NeuralNets/Loss/{MultiClassMargin.cs => MultiMargin.cs} (92%) diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs index 8e4555ba..fcbc08ce 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Celu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a continuously differentiable exponential linear unit (CELU) activation function.")] +[DisplayName("CELU")] public class Celu { /// @@ -31,7 +32,7 @@ public class Celu /// Creates a continuously differentiable exponential linear unit (CELU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(CELU(Alpha, Inplace)); } @@ -42,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => CELU(Alpha, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs index e1697c3c..039902cb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Elu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates an exponential linear unit (ELU) activation function.")] +[DisplayName("ELU")] public class Elu { /// @@ -31,7 +32,7 @@ public class Elu /// Creates an exponential linear unit (ELU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ELU(Alpha, Inplace)); } @@ -42,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ELU(Alpha, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs index 7749d402..978c9bf4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Gelu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a gaussian error linear unit (GELU) activation function.")] +[DisplayName("GELU")] public class Gelu { /// @@ -25,7 +26,7 @@ public class Gelu /// Creates a gaussian error linear unit (GELU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(GELU(InPlace)); } @@ -36,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => GELU(InPlace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs index 8a578c3b..a022ec4d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Glu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a gated linear unit (GLU) module.")] +[DisplayName("GLU")] public class Glu { /// @@ -25,7 +26,7 @@ public class Glu /// Creates a gated linear unit (GLU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(GLU(Dim)); } @@ -36,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => GLU(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs index 1764d88f..53cb6ff3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardshrink.cs @@ -25,7 +25,7 @@ public class Hardshrink /// Creates a Hardshrink module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Hardshrink(Lambda)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Hardshrink(Lambda)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs index 2529c819..1a5f4156 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardsigmoid.cs @@ -25,7 +25,7 @@ public class Hardsigmoid /// Creates a Hardsigmoid module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Hardsigmoid(Inplace)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Hardsigmoid(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs index 750708db..73d2a0c2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardswish.cs @@ -25,7 +25,7 @@ public class Hardswish /// Creates a Hardswish module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Hardswish(Inplace)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Hardswish(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs index 1dedeed2..3abbbc81 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Hardtanh.cs @@ -37,7 +37,7 @@ public class Hardtanh /// Creates a Hardtanh module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Hardtanh(MinVal, MaxVal, Inplace)); } @@ -48,7 +48,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Hardtanh(MinVal, MaxVal, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs index 83f55e16..e8fa6cee 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LeakyRelu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a leaky rectified linear unit (LeakyReLU) activation function.")] +[DisplayName("LeakyReLU")] public class LeakyRelu { /// @@ -31,7 +32,7 @@ public class LeakyRelu /// Creates a leaky rectified linear unit (LeakyReLU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LeakyReLU(NegativeSlope, Inplace)); } @@ -42,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LeakyReLU(NegativeSlope, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs index 4a6007ba..99f03b29 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSigmoid.cs @@ -19,7 +19,7 @@ public class LogSigmoid /// Creates a LogSigmoid module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LogSigmoid()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LogSigmoid()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs index c31e5a8a..b9d14bb7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/LogSoftmax.cs @@ -25,7 +25,7 @@ public class LogSoftmax /// Creates a LogSoftmax module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LogSoftmax(Dim)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LogSoftmax(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs index fd47c128..1877e79f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Mish.cs @@ -19,7 +19,7 @@ public class Mish /// Creates a Mish module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Mish()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Mish()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs index 784cee74..76370225 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/MultiheadAttention.cs @@ -67,7 +67,7 @@ public class MultiheadAttention /// Creates a Multi-head attention module. /// /// - public IObservable>> Process() + public IObservable Process() { return Observable.Return(MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKeyValue, AddZeroAttention, KeyDim, ValueDim)); } @@ -78,7 +78,7 @@ public IObservable /// /// - public IObservable>> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MultiheadAttention(EmbeddedDim, NumHeads, Dropout, Bias, AddBiasKeyValue, AddZeroAttention, KeyDim, ValueDim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs index 73e65e4a..ea8a0beb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Prelu.cs @@ -14,6 +14,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a parametric rectified linear unit (PReLU) activation function.")] +[DisplayName("PReLU")] public class Prelu { /// @@ -46,7 +47,7 @@ public class Prelu /// Creates a parametric rectified linear unit (PReLU) activation function. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(PReLU(NumParameters, InitialValue, Device, Type)); } @@ -57,7 +58,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => PReLU(NumParameters, InitialValue, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs index 73cf57b7..53107768 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Relu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a rectified linear unit (ReLU) activation function.")] +[DisplayName("ReLU")] public class Relu { /// @@ -25,7 +26,7 @@ public class Relu /// Creates a rectified linear unit (ReLU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReLU(Inplace)); } @@ -36,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReLU(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs index a85f08e3..80ca0788 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/ReluBounded.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a bounded rectified linear unit (ReLU6) activation function.")] +[DisplayName("ReLU6")] public class ReluBounded { /// @@ -25,7 +26,7 @@ public class ReluBounded /// Creates a bounded rectified linear unit (ReLU6) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReLU6(Inplace)); } @@ -36,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReLU6(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs index 60fa6010..632b8edc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Rrelu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a randomized leaky rectified linear unit (RReLU) module.")] +[DisplayName("RReLU")] public class Rrelu { /// @@ -37,7 +38,7 @@ public class Rrelu /// Creates a randomized leaky rectified linear unit (RReLU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(RReLU(Lower, Upper, Inplace)); } @@ -48,7 +49,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => RReLU(Lower, Upper, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs index 1de297b4..6d460b9e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Selu.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a scaled exponential linear unit (SELU) activation function.")] +[DisplayName("SeLU")] public class Selu { /// @@ -25,7 +26,7 @@ public class Selu /// Creates a scaled exponential linear unit (SELU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(SELU(Inplace)); } @@ -36,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => SELU(Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs index c7a7a2a6..17460059 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Sigmoid.cs @@ -19,7 +19,7 @@ public class Sigmoid /// Creates a sigmoid module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Sigmoid()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Sigmoid()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs index d9270f3e..a6f88818 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Silu.cs @@ -13,13 +13,14 @@ namespace Bonsai.ML.Torch.NeuralNets.ActivationFunction; /// See for more information. /// [Description("Creates a sigmoid weighted linear unit (SiLU) activation function.")] +[DisplayName("SiLU")] public class Silu { /// /// Creates a sigmoid weighted linear unit (SiLU) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(SiLU()); } @@ -30,7 +31,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => SiLU()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs index c065cede..a77ba716 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax.cs @@ -25,7 +25,7 @@ public class Softmax /// Creates a Softmax module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Softmax(Dim)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Softmax(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs index c54027bc..55ad3c75 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmax2d.cs @@ -19,7 +19,7 @@ public class Softmax2d /// Creates a Softmax2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Softmax2d()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Softmax2d()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs index de863e7c..2fe4119f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softmin.cs @@ -25,7 +25,7 @@ public class Softmin /// Creates a Softmin module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Softmin(Dim)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Softmin(Dim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs index e726c8ac..2f9a918b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softplus.cs @@ -31,7 +31,7 @@ public class Softplus /// Creates a Softplus module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Softplus(Beta, Threshold)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Softplus(Beta, Threshold)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs index ba37b1ac..2f00f8fa 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softshrink.cs @@ -25,7 +25,7 @@ public class Softshrink /// Creates a Softshrink module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Softshrink(Lambda)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Softshrink(Lambda)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs index d6210607..1c94bdb1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Softsign.cs @@ -19,7 +19,7 @@ public class Softsign /// Creates a Softsign module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Softsign()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Softsign()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs index 0ec82c6d..851ae14a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanh.cs @@ -19,7 +19,7 @@ public class Tanh /// Creates a Tanh module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Tanh()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Tanh()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs index e42808c5..904f3c41 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Tanhshrink.cs @@ -19,7 +19,7 @@ public class Tanhshrink /// Creates a Tanhshrink module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Tanhshrink()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Tanhshrink()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs index a7e4bf6e..1b31c2ca 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunction/Threshold.cs @@ -37,7 +37,7 @@ public class Threshold /// Creates a Threshold module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Threshold(ThresholdValue, FillValue, Inplace)); } @@ -48,7 +48,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Threshold(ThresholdValue, FillValue, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs index b60d318b..619e42fb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs @@ -6,26 +6,26 @@ namespace Bonsai.ML.Torch.NeuralNets; /// /// Represents an operator that creates an activation function. /// -[XmlInclude(typeof(ActivationFunction.CELU))] -[XmlInclude(typeof(ActivationFunction.ELU))] -[XmlInclude(typeof(ActivationFunction.GLU))] -[XmlInclude(typeof(ActivationFunction.GELU))] +[XmlInclude(typeof(ActivationFunction.Celu))] +[XmlInclude(typeof(ActivationFunction.Elu))] +[XmlInclude(typeof(ActivationFunction.Glu))] +[XmlInclude(typeof(ActivationFunction.Gelu))] [XmlInclude(typeof(ActivationFunction.Hardshrink))] [XmlInclude(typeof(ActivationFunction.Hardsigmoid))] [XmlInclude(typeof(ActivationFunction.Hardswish))] [XmlInclude(typeof(ActivationFunction.Hardtanh))] -[XmlInclude(typeof(ActivationFunction.LeakyReLU))] +[XmlInclude(typeof(ActivationFunction.LeakyRelu))] [XmlInclude(typeof(ActivationFunction.LogSigmoid))] [XmlInclude(typeof(ActivationFunction.LogSoftmax))] [XmlInclude(typeof(ActivationFunction.Mish))] [XmlInclude(typeof(ActivationFunction.MultiheadAttention))] -[XmlInclude(typeof(ActivationFunction.PReLU))] -[XmlInclude(typeof(ActivationFunction.RReLU))] -[XmlInclude(typeof(ActivationFunction.Rectified))] -[XmlInclude(typeof(ActivationFunction.RectifiedBounded))] -[XmlInclude(typeof(ActivationFunction.SELU))] +[XmlInclude(typeof(ActivationFunction.Prelu))] +[XmlInclude(typeof(ActivationFunction.Rrelu))] +[XmlInclude(typeof(ActivationFunction.Relu))] +[XmlInclude(typeof(ActivationFunction.ReluBounded))] +[XmlInclude(typeof(ActivationFunction.Selu))] [XmlInclude(typeof(ActivationFunction.Sigmoid))] -[XmlInclude(typeof(ActivationFunction.SiLU))] +[XmlInclude(typeof(ActivationFunction.Silu))] [XmlInclude(typeof(ActivationFunction.Softmax))] [XmlInclude(typeof(ActivationFunction.Softmax2d))] [XmlInclude(typeof(ActivationFunction.Softmin))] @@ -51,7 +51,7 @@ public class ActivationFunctionBuilder : ModuleCombinatorBuilder, INamedElement /// public ActivationFunctionBuilder() { - Module = new ActivationFunction.Rectified(); + Module = new ActivationFunction.Relu(); } /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs index b1d35592..2a44f23c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs @@ -20,8 +20,8 @@ public class Sequential /// Creates a sequential container from the input modules. /// /// - public IObservable> Process(IObservable[]> source) + public IObservable Process(IObservable source) where T : Module { return source.Select(modules => Sequential(modules)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs index 2200d94a..fdf76173 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv1d.cs @@ -88,7 +88,7 @@ public class Conv1d /// Creates a Conv1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } @@ -99,7 +99,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Conv1d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs index 0bdcedd9..c426b5e7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv2d.cs @@ -92,7 +92,7 @@ public class Conv2d /// Creates a Conv2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } @@ -103,7 +103,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Conv2d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs index 3c248171..c02e30d6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Conv3d.cs @@ -92,7 +92,7 @@ public class Conv3d /// Creates a Conv3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } @@ -103,7 +103,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Conv3d(InChannels, OutChannels, KernelSize, Stride, Padding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs index f0e36a7d..0aee1947 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose1d.cs @@ -94,7 +94,7 @@ public class ConvTranspose1d /// Creates a ConvTranspose1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } @@ -105,7 +105,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ConvTranspose1d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs index 40edae5c..6307420a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose2d.cs @@ -99,7 +99,7 @@ public class ConvTranspose2d /// Creates a ConvTranspose2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } @@ -110,7 +110,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ConvTranspose2d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs index 5129b3f6..c8388a76 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/ConvTranspose3d.cs @@ -99,7 +99,7 @@ public class ConvTranspose3d /// Creates a ConvTranspose3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } @@ -110,7 +110,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ConvTranspose3d(InChannels, OutChannels, KernelSize, Stride, Padding, OutputPadding, Dilation, PaddingMode, Groups, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs index 8ed0cb53..113c8ccd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Fold.cs @@ -54,7 +54,7 @@ public class Fold /// Creates a Fold module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); } @@ -65,7 +65,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Fold(OutputSize, KernelSize, Dilation, Padding, Stride)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs index a5389f2e..d32aa810 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Convolution/Unfold.cs @@ -47,7 +47,7 @@ public class Unfold /// Creates an Unfold module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Unfold(KernelSize, Dilation, Padding, Stride)); } @@ -58,7 +58,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Unfold(KernelSize, Dilation, Padding, Stride)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs index 883b9628..4e1de349 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Distance/CosineSimilarity.cs @@ -31,7 +31,7 @@ public class CosineSimilarity /// Creates a CosineSimilarity module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(CosineSimilarity(Dim, Eps)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => CosineSimilarity(Dim, Eps)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs index e3afa0ab..b269e2e2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Distance/PairwiseDistance.cs @@ -37,7 +37,7 @@ public class PairwiseDistance /// Creates a PairwiseDistance module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(PairwiseDistance(P, Eps, KeepDim)); } @@ -48,7 +48,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => PairwiseDistance(P, Eps, KeepDim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs index 61111989..85b52e95 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/AlphaDropout.cs @@ -31,7 +31,7 @@ public class AlphaDropout /// Creates an AlphaDropout module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AlphaDropout(Probability, Inplace)); } @@ -42,8 +42,8 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AlphaDropout(Probability, Inplace)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs index 92dd6674..cdda8cda 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout.cs @@ -31,7 +31,7 @@ public class Dropout /// Creates a Dropout module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Dropout(Probability, Inplace)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Dropout(Probability, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs index 7831ad9c..899affb6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout1d.cs @@ -31,7 +31,7 @@ public class Dropout1d /// Creates a Dropout1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Dropout1d(Probability, Inplace)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Dropout1d(Probability, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs index 66283a6f..4618e315 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout2d.cs @@ -31,7 +31,7 @@ public class Dropout2d /// Creates a Dropout2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Dropout2d(Probability, Inplace)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Dropout2d(Probability, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs index d902bd31..627a240c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/Dropout3d.cs @@ -31,7 +31,7 @@ public class Dropout3d /// Creates a Dropout3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Dropout3d(Probability, Inplace)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Dropout3d(Probability, Inplace)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs b/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs index 8a798d6e..dd827a83 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Dropout/FeatureAlphaDropout.cs @@ -25,7 +25,7 @@ public class FeatureAlphaDropout /// Creates a FeatureAlphaDropout module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(FeatureAlphaDropout(Probability)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => FeatureAlphaDropout(Probability)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs index f849c9ef..6f343414 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Flatten.cs @@ -31,7 +31,7 @@ public class Flatten /// Creates a Flatten module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Flatten(StartDim, EndDim)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Flatten(StartDim, EndDim)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs index d09bca9b..b00683de 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Flatten/Unflatten.cs @@ -32,7 +32,7 @@ public class Unflatten /// Creates an Unflatten module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Unflatten(Dim, UnflattenedSize)); } @@ -43,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Unflatten(Dim, UnflattenedSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs index 3b719ead..5c4665de 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Bilinear.cs @@ -57,7 +57,7 @@ public class Bilinear /// Creates a Bilinear module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Bilinear(In1Features, In2Features, OutputSize, HasBias, Device, Type)); } @@ -68,7 +68,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Bilinear(In1Features, In2Features, OutputSize, HasBias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs index c7f782dc..45028095 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Identity.cs @@ -19,7 +19,7 @@ public class Identity /// Creates an Identity module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Identity()); } @@ -30,7 +30,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Identity()); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs b/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs index 21c526f1..494c8c7f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Linear/Linear.cs @@ -51,7 +51,7 @@ public class Linear /// Creates a Linear module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Linear(InputSize, OutputSize, HasBias, Device, Type)); } @@ -62,7 +62,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Linear(InputSize, OutputSize, HasBias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs index d2ea7438..fe8d2614 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadScriptModule.cs @@ -19,11 +19,8 @@ namespace Bonsai.ML.Torch.NeuralNets; [Combinator] [Description("Loads a TorchScript module from the specified file path. In order to correctly infer the module type, pass into the operator objects representing the desired ScriptModule generic argument types.")] [WorkflowElementCategory(ElementCategory.Source)] -public class LoadScriptModule: ExpressionBuilder +public class LoadScriptModule { - /// - public override Range ArgumentRange => new(0, 3); - /// /// The device on which to load the model. /// @@ -38,102 +35,16 @@ public class LoadScriptModule: ExpressionBuilder [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] public string ScriptModulePath { get; set; } - /// - public override Expression Build(IEnumerable arguments) - { - System.Type scriptModuleType; - if (!arguments.Any()) - // if no arguments are provided, the script module type is assumed to be non-generic - scriptModuleType = typeof(ScriptModule); - else - { - // otherwise, the script module type is inferred from the collection of input argument types - // arguments are always IObservable, so we need to extract the T type from each argument - var argumentTypes = arguments.Select(arg => arg.Type.GetGenericArguments()[0]).ToArray(); - var genericScriptModuleType = argumentTypes.Length switch - { - 1 => typeof(ScriptModule<>), - 2 => typeof(ScriptModule<,>), - 3 => typeof(ScriptModule<,,>), - _ => throw new NotSupportedException("Only ScriptModule types with up to three generic type arguments are supported."), - }; - scriptModuleType = genericScriptModuleType.MakeGenericType(argumentTypes); - } - - var outputType = typeof(IObservable<>).MakeGenericType(scriptModuleType); - var modulePathExpression = Expression.Constant(ScriptModulePath, typeof(string)); - var deviceExpression = Expression.Constant(Device, typeof(Device)); - - Expression callExpression; - if (scriptModuleType.IsGenericType) - { - var genericArguments = scriptModuleType.GetGenericArguments(); - var processMethod = typeof(LoadScriptModule).GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static) - .First(m => m.Name == nameof(ProcessScriptModule) && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == genericArguments.Length) - .MakeGenericMethod(genericArguments); - callExpression = Expression.Call(processMethod, modulePathExpression, deviceExpression); - } - else - { - var processMethod = typeof(LoadScriptModule).GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static) - .First(m => m.Name == nameof(ProcessScriptModule) && !m.IsGenericMethod); - callExpression = Expression.Call(processMethod, modulePathExpression, deviceExpression); - } - - return callExpression; - } - - /// - /// Loads the scripted module from the specified file path. - /// - /// - /// - /// - private static IObservable ProcessScriptModule(string modulePath, Device device) - { - var scriptModule = device is null ? jit.load(modulePath) : load(modulePath, device); - return Observable.Return(scriptModule); - } - /// /// Loads the scripted module from the specified file path. /// - /// - /// - /// /// - private static IObservable> ProcessScriptModule(string modulePath, Device device) + public IObservable> Process() { - var scriptModule = device is null ? load(modulePath) : load(modulePath, device); - return Observable.Return(scriptModule); - } - - /// - /// Loads the scripted module from the specified file path. - /// - /// - /// - /// - /// - /// - private static IObservable> ProcessScriptModule(string modulePath, Device device) - { - var scriptModule = device is null ? load(modulePath) : load(modulePath, device); - return Observable.Return(scriptModule); - } - - /// - /// Loads the scripted module from the specified file path. - /// - /// - /// - /// - /// - /// - /// - private static IObservable> ProcessScriptModule(string modulePath, Device device) - { - var scriptModule = device is null ? load(modulePath) : load(modulePath, device); - return Observable.Return(scriptModule); + return Observable.Defer(() => + { + var scriptModule = Device is null ? load(ScriptModulePath) : load(ScriptModulePath, Device); + return Observable.Return(scriptModule); + }); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs index aa20f5d0..74fb378b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropy.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// [Description("Creates a binary cross entropy (BCE) loss module.")] [TypeConverter(typeof(TensorOperatorConverter))] +[DisplayName("BCE")] public class BinaryCrossEntropy : IScalarTypeProvider { /// @@ -53,7 +54,7 @@ public string WeightXml /// Creates a binary cross entropy (BCE) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(BCELoss(Weight, Reduction)); } @@ -64,8 +65,8 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => BCELoss(Weight, Reduction)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs index cce87f3d..42701344 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/BinaryCrossEntropyWithLogits.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// [Description("Creates a binary cross entropy with logits (BCEWithLogits) loss module.")] [TypeConverter(typeof(TensorOperatorConverter))] +[DisplayName("BCEWithLogits")] public class BinaryCrossEntropyWithLogits : IScalarTypeProvider { /// @@ -73,7 +74,7 @@ public string PosWeightsXml /// Creates a binary cross entropy with logits (BCEWithLogits) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(BCEWithLogitsLoss(Weight, Reduction, PosWeights)); } @@ -84,8 +85,8 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => BCEWithLogitsLoss(Weight, Reduction, PosWeights)); - } + } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs index 1dd22661..188aac53 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/ConnectionistTemporalClassification.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// See for more information. /// [Description("Creates a connectionist temporal classification (CTC) loss module.")] +[DisplayName("CTC")] public class ConnectionistTemporalClassification { /// @@ -37,7 +38,7 @@ public class ConnectionistTemporalClassification /// Creates a connectionist temporal classification (CTC) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(CTCLoss(Blank, ZeroInfinity, Reduction)); } @@ -48,7 +49,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => CTCLoss(Blank, ZeroInfinity, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs index 4514f4a8..5117b3dd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/CosineEmbedding.cs @@ -31,7 +31,7 @@ public class CosineEmbedding /// Creates a cosine embedding loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(CosineEmbeddingLoss(Margin, Reduction)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => CosineEmbeddingLoss(Margin, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs index 955f74cd..e17434e7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/CrossEntropy.cs @@ -59,7 +59,7 @@ public string WeightXml /// Creates a cross entropy loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(CrossEntropyLoss(Weight, IgnoreIndex, Reduction)); } @@ -70,8 +70,8 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => CrossEntropyLoss(Weight, IgnoreIndex, Reduction)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs index a2cefb63..3f8379c1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/GaussianNegativeLogLikelihood.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// See for more information. /// [Description("Creates a gaussian negative log likelihood (GaussianNLL) loss module.")] +[DisplayName("GaussianNLL")] public class GaussianNegativeLogLikelihood { /// @@ -37,7 +38,7 @@ public class GaussianNegativeLogLikelihood /// Creates a gaussian negative log likelihood (GaussianNLL) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(GaussianNLLLoss(Full, Eps, Reduction)); } @@ -48,7 +49,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => GaussianNLLLoss(Full, Eps, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs index 36a86404..7527a947 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/HingeEmbedding.cs @@ -31,7 +31,7 @@ public class HingeEmbedding /// Creates a hinge embedding loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(HingeEmbeddingLoss(Margin, Reduction)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => HingeEmbeddingLoss(Margin, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs index 32e3667a..2538c52f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/Huber.cs @@ -31,7 +31,7 @@ public class Huber /// Creates a Huber loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(HuberLoss(Delta, Reduction)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => HuberLoss(Delta, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs index d7e09740..f77a3b7e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/KullbackLeiblerDivergence.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// See for more information. /// [Description("Creates a Kullback-Leibler divergence loss module.")] +[DisplayName("KLDiv")] public class KullbackLeiblerDivergence { /// @@ -31,7 +32,7 @@ public class KullbackLeiblerDivergence /// Creates a Kullback-Leibler divergence loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(KLDivLoss(LogTarget, Reduction)); } @@ -42,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => KLDivLoss(LogTarget, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs index 0cdf6956..0ae39ebf 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MarginRanking.cs @@ -31,7 +31,7 @@ public class MarginRanking /// Creates a margin ranking loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MarginRankingLoss(Margin, Reduction)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MarginRankingLoss(Margin, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs index 02cbc543..ad5e0085 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanAbsoluteError.cs @@ -12,7 +12,8 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// /// See for more information. /// -[Description("Creates an L1 loss (L1Loss) module, known as mean absolute error (MAE).")] +[Description("Creates an L1 loss (L1Loss) module, also known as mean absolute error (MAE).")] +[DisplayName("L1")] public class MeanAbsoluteError { /// @@ -25,7 +26,7 @@ public class MeanAbsoluteError /// Creates an L1 loss (L1Loss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(L1Loss(Reduction)); } @@ -36,8 +37,8 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => L1Loss(Reduction)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs index 92d9c958..f262bc05 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MeanSquaredError.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// See for more information. /// [Description("Creates a mean squared error (MSE) loss module.")] +[DisplayName("MSE")] public class MeanSquaredError { /// @@ -25,7 +26,7 @@ public class MeanSquaredError /// Creates a mean squared error (MSE) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MSELoss(Reduction)); } @@ -36,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MSELoss(Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs index bbdb9b02..3f02ad8f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelMargin.cs @@ -25,7 +25,7 @@ public class MultiLabelMargin /// Creates a multi-label margin loss (MultiLabelMarginLoss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MultiLabelMarginLoss(Reduction)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MultiLabelMarginLoss(Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs index 428d0dc4..58b2d1c7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiLabelSoftMargin.cs @@ -53,7 +53,7 @@ public string WeightXml /// Creates a multi-label soft margin loss (MultiLabelSoftMarginLoss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MultiLabelSoftMarginLoss(Weight, Reduction)); } @@ -64,8 +64,8 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MultiLabelSoftMarginLoss(Weight, Reduction)); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMargin.cs similarity index 92% rename from src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs rename to src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMargin.cs index 0c6e866c..c67ac114 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiClassMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/MultiMargin.cs @@ -15,7 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// [Description("Creates a multi-class margin loss (MultiMarginLoss) module.")] [TypeConverter(typeof(TensorOperatorConverter))] -public class MultiClassMargin : IScalarTypeProvider +public class MultiMargin : IScalarTypeProvider { /// /// The p parameter, can be either 1 or 2. @@ -65,7 +65,7 @@ public string WeightXml /// Creates a multi-class margin loss (MultiMarginLoss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MultiMarginLoss(P, Margin, Weight, Reduction)); } @@ -76,7 +76,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MultiMarginLoss(P, Margin, Weight, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs index 742187bd..eb65260d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/NegativeLogLikelihood.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// [Description("Creates a negative log likelihood (NLL) loss module.")] [TypeConverter(typeof(TensorOperatorConverter))] +[DisplayName("NLL")] public class NegativeLogLikelihood : IScalarTypeProvider { /// @@ -53,7 +54,7 @@ public string WeightXml /// Creates a negative log likelihood (NLL) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(NLLLoss(Weight, Reduction)); } @@ -64,7 +65,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => NLLLoss(Weight, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs index be310bdf..65af3083 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/PoissonNegativeLogLikelihood.cs @@ -13,6 +13,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Loss; /// See for more information. /// [Description("Creates a poisson negative log likelihood (PoissonNLL) loss module.")] +[DisplayName("PoissonNLL")] public class PoissonNegativeLogLikelihood { /// @@ -43,7 +44,7 @@ public class PoissonNegativeLogLikelihood /// Creates a poisson negative log likelihood (PoissonNLL) loss module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(PoissonNLLLoss(LogInput, Full, Eps, Reduction)); } @@ -54,7 +55,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => PoissonNLLLoss(LogInput, Full, Eps, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs index e3cf02fe..f4b3b238 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/SmoothL1.cs @@ -31,7 +31,7 @@ public class SmoothL1 /// Creates a smooth L1 loss (SmoothL1Loss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(SmoothL1Loss(Reduction, Beta)); } @@ -42,7 +42,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => SmoothL1Loss(Reduction, Beta)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs index e0973470..211b74e1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/SoftMargin.cs @@ -25,7 +25,7 @@ public class SoftMargin /// Creates a soft margin loss (SoftMarginLoss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(SoftMarginLoss(Reduction)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => SoftMarginLoss(Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs b/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs index 3b1e6e70..e9403d15 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Loss/TripletMargin.cs @@ -49,7 +49,7 @@ public class TripletMargin /// Creates a triplet margin loss (TripletMarginLoss) module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(TripletMarginLoss(Margin, P, Eps, Swap, Reduction)); } @@ -60,7 +60,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => TripletMarginLoss(Margin, P, Eps, Swap, Reduction)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs index 8197c6ab..452e36aa 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs @@ -18,7 +18,7 @@ namespace Bonsai.ML.Torch.NeuralNets; [XmlInclude(typeof(Loss.MarginRanking))] [XmlInclude(typeof(Loss.MeanAbsoluteError))] [XmlInclude(typeof(Loss.MeanSquaredError))] -[XmlInclude(typeof(Loss.MultiClassMargin))] +[XmlInclude(typeof(Loss.MultiMargin))] [XmlInclude(typeof(Loss.MultiLabelMargin))] [XmlInclude(typeof(Loss.MultiLabelSoftMargin))] [XmlInclude(typeof(Loss.NegativeLogLikelihood))] diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs index 1e6e301f..2cfb1a11 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm1d.cs @@ -63,7 +63,7 @@ public class BatchNorm1d /// Creates a BatchNorm1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } @@ -74,7 +74,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => BatchNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs index ef7f0496..26cfd3f4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm2d.cs @@ -63,7 +63,7 @@ public class BatchNorm2d /// Creates a BatchNorm2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } @@ -74,7 +74,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => BatchNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs index 88df1447..07e960a7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/BatchNorm3d.cs @@ -63,7 +63,7 @@ public class BatchNorm3d /// Creates a BatchNorm3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } @@ -74,7 +74,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => BatchNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs index 52ae8d51..74bc73e0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/GroupNorm.cs @@ -57,7 +57,7 @@ public class GroupNorm /// Creates a GroupNorm module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); } @@ -68,7 +68,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => GroupNorm(NumGroups, NumChannels, Eps, Affine, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs index b5b44a4c..d18a2f27 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm1d.cs @@ -63,7 +63,7 @@ public class InstanceNorm1d /// Creates an InstanceNorm1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } @@ -74,7 +74,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => InstanceNorm1d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs index 22cbb7da..64c6d287 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm2d.cs @@ -63,7 +63,7 @@ public class InstanceNorm2d /// Creates an InstanceNorm2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } @@ -74,7 +74,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => InstanceNorm2d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs index afe9bbe1..b6af2cfc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/InstanceNorm3d.cs @@ -63,7 +63,7 @@ public class InstanceNorm3d /// Creates an InstanceNorm3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } @@ -74,7 +74,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => InstanceNorm3d(Features, Eps, Momentum, Affine, TrackRunningStats, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs index c8c176f4..795830af 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LayerNorm.cs @@ -58,7 +58,7 @@ public class LayerNorm /// Creates a LayerNorm module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); } @@ -69,7 +69,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LayerNorm(NormalizedShape, Eps, ElementwiseAffine, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs index c1ab17c1..81500870 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Normalization/LocalResponseNorm.cs @@ -43,7 +43,7 @@ public class LocalResponseNorm /// Creates a LocalResponseNorm module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LocalResponseNorm(Size, Alpha, Beta, K)); } @@ -54,7 +54,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LocalResponseNorm(Size, Alpha, Beta, K)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs index a88fc783..12b1b7d3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs @@ -1,6 +1,10 @@ using System; using System.ComponentModel; +using System.Linq; using System.Reactive.Linq; +using System.Collections.Generic; +using System.Linq.Expressions; +using Bonsai.Expressions; using static TorchSharp.torch; using static TorchSharp.torch.optim.lr_scheduler; @@ -19,7 +23,7 @@ public class OptimizationStep /// /// /// - public IObservable Process(IObservable source) + public IObservable Process(IObservable source) where T : optim.Optimizer { return source.Do(input => input.step()); } @@ -33,4 +37,4 @@ public IObservable Process(IObservable source) { return source.Do(input => input.step()); } -} \ No newline at end of file +} diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs index 38dc3f50..d529e57f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adadelta.cs @@ -53,7 +53,7 @@ public class Adadelta /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => Adadelta(parameters, LearningRate, Rho, Eps, WeightDecay, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs index 39e265f8..6e8ff601 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adagrad.cs @@ -53,7 +53,7 @@ public class Adagrad /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => Adagrad(parameters, LearningRate, LearningRateDecay, WeightDecay, InitialAccumulatorValue, Eps)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs index 5086a58f..5e2327f7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adam.cs @@ -65,7 +65,7 @@ public class Adam /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => Adam(parameters, LearningRate, BetaGradient, BetaSquaredGradient, Eps, WeightDecay, Amsgrad, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs index d34e819d..044ba766 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AdamW.cs @@ -65,7 +65,7 @@ public class AdamW /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => AdamW(parameters, LearningRate, BetaGradient, BetaSquaredGradient, Eps, WeightDecay, Amsgrad, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs index 3870bdc6..2b1726e1 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Adamax.cs @@ -53,7 +53,7 @@ public class Adamax /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => Adamax(parameters, LearningRate, BetaGradient, BetaSquaredGradient, Eps, WeightDecay)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs index 6a69b73b..6719f9f6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/AveragedStochasticGradientDescent.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// See for more information. /// [Description("Creates an averaged stochastic gradient descent (ASGD) optimizer.")] +[DisplayName("ASGD")] public class AveragedStochasticGradientDescent { /// @@ -59,7 +60,7 @@ public class AveragedStochasticGradientDescent /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => ASGD(parameters, LearningRate, Lambda, Alpha, T0, WeightDecay, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs index 0c1b152b..4446615c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/Lbfgs.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// See for more information. /// [Description("Creates a limited-memory Broyden-Fletcher-Goldfarb-Shanno (LBFGS) optimizer.")] +[DisplayName("LBFGS")] public class Lbfgs { /// @@ -59,7 +60,7 @@ public class Lbfgs /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => LBFGS(parameters, LearningRate, MaxIter, MaxEval, ToleranceGrad, ToleranceChange, HistorySize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs index aa484ce5..4584fe9d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/ResilientBackpropagation.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// See for more information. /// [Description("Creates a resilient backpropagation (Rprop) optimizer.")] +[DisplayName("Rprop")] public class ResilientBackpropagation { /// @@ -59,7 +60,7 @@ public class ResilientBackpropagation /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => Rprop(parameters, LearningRate, EtaMinus, EtaPlus, MinStep, MaxStep, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs index ed4c3710..70681e89 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/RootMeanSquarePropagation.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// See for more information. /// [Description("Creates a root mean square propagation (RMSProp) optimizer.")] +[DisplayName("RMSProp")] public class RootMeanSquarePropagation { /// @@ -65,7 +66,7 @@ public class RootMeanSquarePropagation /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => RMSProp(parameters, LearningRate, Alpha, Eps, WeightDecay, Momentum, Centered, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs index 39f98fbc..1416d244 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Optimizer/StochasticGradientDescent.cs @@ -15,6 +15,7 @@ namespace Bonsai.ML.Torch.NeuralNets.Optimizer; /// See for more information. /// [Description("Creates a stochastic gradient descent (SGD) optimizer.")] +[DisplayName("SGD")] public class StochasticGradientDescent { /// @@ -59,7 +60,7 @@ public class StochasticGradientDescent /// /// /// - public IObservable Process(IObservable source) where T : IEnumerable + public IObservable Process(IObservable source) where T : IEnumerable { return source.Select(parameters => SGD(parameters, LearningRate, Momentum, Dampening, WeightDecay, Nesterov, Maximize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs index 64f1cd78..104ce716 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad1d.cs @@ -32,7 +32,7 @@ public class ConstantPad1d /// Creates a 1D constant padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ConstantPad1d(PaddingSize, Value)); } @@ -43,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ConstantPad1d(PaddingSize, Value)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs index b3346845..47246637 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad2d.cs @@ -32,7 +32,7 @@ public class ConstantPad2d /// Creates a 2D constant padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ConstantPad2d(PaddingSize, Value)); } @@ -43,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ConstantPad2d(PaddingSize, Value)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs index d576d4ca..d22557ec 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ConstantPad3d.cs @@ -32,7 +32,7 @@ public class ConstantPad3d /// Creates a 3D constant padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ConstantPad3d(PaddingSize, Value)); } @@ -43,7 +43,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ConstantPad3d(PaddingSize, Value)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs index c15400b3..225087fd 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad1d.cs @@ -26,7 +26,7 @@ public class ReflectionPad1d /// Creates a 1D reflection padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReflectionPad1d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReflectionPad1d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs index 478bfa39..da3f6a14 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad2d.cs @@ -26,7 +26,7 @@ public class ReflectionPad2d /// Creates a 2D reflection padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReflectionPad2d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReflectionPad2d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs index 8e5b38eb..c0677d1d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReflectionPad3d.cs @@ -26,7 +26,7 @@ public class ReflectionPad3d /// Creates a 3D reflection padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReflectionPad3d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReflectionPad3d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs index 5d09c938..fe530720 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad1d.cs @@ -26,7 +26,7 @@ public class ReplicationPad1d /// Creates a 1D replication padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReplicationPad1d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReplicationPad1d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs index ae83b409..d434cb2c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad2d.cs @@ -26,7 +26,7 @@ public class ReplicationPad2d /// Creates a 2D replication padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReplicationPad2d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReplicationPad2d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs index 151c3b9b..9f4f9ef5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ReplicationPad3d.cs @@ -26,7 +26,7 @@ public class ReplicationPad3d /// Creates a 3D replication padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ReplicationPad3d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ReplicationPad3d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs index 00120e23..fb1f46cc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Padding/ZeroPad2d.cs @@ -26,7 +26,7 @@ public class ZeroPad2d /// Creates a 2D zero padding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ZeroPad2d(PaddingSize)); } @@ -37,7 +37,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ZeroPad2d(PaddingSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs index a91692e4..4f1b0b88 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool1d.cs @@ -28,7 +28,7 @@ public class AdaptiveAvgPool1d /// /// Creates an AdaptiveAvgPool1d module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AdaptiveAvgPool1d(OutputSize)); } @@ -39,7 +39,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AdaptiveAvgPool1d(OutputSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs index c21963d5..361c7b2c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool2d.cs @@ -29,7 +29,7 @@ public class AdaptiveAvgPool2d /// /// Creates an AdaptiveAvgPool2d module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AdaptiveAvgPool2d(OutputSize)); } @@ -40,7 +40,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AdaptiveAvgPool2d(OutputSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs index 991068bc..ac088fb7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveAvgPool3d.cs @@ -25,7 +25,7 @@ public class AdaptiveAvgPool3d /// /// Creates an AdaptiveAvgPool3d module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AdaptiveAvgPool3d(OutputSize)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AdaptiveAvgPool3d(OutputSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs index ceea2f4d..473d056f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool1d.cs @@ -24,7 +24,7 @@ public class AdaptiveMaxPool1d /// /// Creates an AdaptiveMaxPool1d module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AdaptiveMaxPool1d(OutputSize)); } @@ -35,7 +35,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AdaptiveMaxPool1d(OutputSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs index 9d9a530f..93dfa349 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool2d.cs @@ -29,7 +29,7 @@ public class AdaptiveMaxPool2d /// /// Creates an AdaptiveMaxPool2d module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AdaptiveMaxPool2d(OutputSize)); } @@ -40,7 +40,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AdaptiveMaxPool2d(OutputSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs index ce8fa8e7..e30547bb 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AdaptiveMaxPool3d.cs @@ -29,7 +29,7 @@ public class AdaptiveMaxPool3d /// /// Creates an AdaptiveMaxPool3d module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AdaptiveMaxPool3d(OutputSize)); } @@ -40,7 +40,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AdaptiveMaxPool3d(OutputSize)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs index 68916f90..0c765dba 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool1d.cs @@ -53,7 +53,7 @@ public class AvgPool1d /// Creates an AvgPool1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); } @@ -64,7 +64,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AvgPool1d(KernelSize, Stride, Padding, CeilMode, CountIncludePad)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs index 30f9c2f2..845a726d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool2d.cs @@ -62,7 +62,7 @@ public class AvgPool2d /// Creates an AvgPool2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AvgPool2d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); } @@ -73,7 +73,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AvgPool2d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs index 069b098c..f90d1fb3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/AvgPool3d.cs @@ -62,7 +62,7 @@ public class AvgPool3d /// Creates an AvgPool3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(AvgPool3d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); } @@ -73,7 +73,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => AvgPool3d(KernelSize, Stride, Padding, CeilMode, CountIncludePad, DivisorOverride)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs index dca58095..51670afe 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool2d.cs @@ -40,7 +40,7 @@ public class FractionalMaxPool2d /// Creates a FractionalMaxPool2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); } @@ -51,7 +51,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => FractionalMaxPool2d(KernelSize, OutputSize, OutputRatio)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs index 63ef2e86..3b7bd131 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/FractionalMaxPool3d.cs @@ -40,7 +40,7 @@ public class FractionalMaxPool3d /// Creates a FractionalMaxPool3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)); } @@ -51,7 +51,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => FractionalMaxPool3d(KernelSize, OutputSize, OutputRatio)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs index bc9caec3..b32bc3e3 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool1d.cs @@ -47,7 +47,7 @@ public class LPPool1d /// Creates a LPPool1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LPPool1d(Norm, KernelSize, Stride, CeilMode)); } @@ -58,7 +58,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LPPool1d(Norm, KernelSize, Stride, CeilMode)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs index 75d5a9cc..fc235998 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/LPPool2d.cs @@ -49,7 +49,7 @@ public class LPPool2d /// Creates a LPPool2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LPPool2d(Norm, KernelSize, Stride, CeilMode)); } @@ -60,7 +60,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LPPool2d(Norm, KernelSize, Stride, CeilMode)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs index e810ce94..c137e2ed 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool1d.cs @@ -53,7 +53,7 @@ public class MaxPool1d /// Creates a MaxPool1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); } @@ -64,7 +64,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MaxPool1d(KernelSize, Stride, Padding, Dilation, CeilMode)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs index 4d51d77a..373e86c4 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool2d.cs @@ -53,7 +53,7 @@ public class MaxPool2d /// Creates a MaxPool2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); } @@ -64,7 +64,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MaxPool2d(KernelSize, Stride, Padding, Dilation, CeilMode)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs index 641f902d..fb0796ab 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxPool3d.cs @@ -53,7 +53,7 @@ public class MaxPool3d /// Creates a MaxPool3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); } @@ -64,7 +64,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MaxPool3d(KernelSize, Stride, Padding, Dilation, CeilMode)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs index 25ffef76..293a4eee 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool1d.cs @@ -41,7 +41,7 @@ public class MaxUnpool1d /// Creates a MaxUnpool1d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MaxUnpool1d(KernelSize, Stride, Padding)); } @@ -52,7 +52,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MaxUnpool1d(KernelSize, Stride, Padding)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs index d02cd3ce..8ff1eaad 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool2d.cs @@ -44,7 +44,7 @@ public class MaxUnpool2d /// Creates a MaxUnpool2d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MaxUnpool2d(KernelSize, Stride, Padding)); } @@ -55,7 +55,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MaxUnpool2d(KernelSize, Stride, Padding)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs index 83adc4fb..5a63d783 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Pooling/MaxUnpool3d.cs @@ -44,7 +44,7 @@ public class MaxUnpool3d /// Creates a MaxUnpool3d module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(MaxUnpool3d(KernelSize, Stride, Padding)); } @@ -55,7 +55,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => MaxUnpool3d(KernelSize, Stride, Padding)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs index 210e1e2e..8aaeee87 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnit.cs @@ -75,7 +75,7 @@ public class GatedRecurrentUnit /// Creates a GRU module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } @@ -86,7 +86,7 @@ public class GatedRecurrentUnit /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => GRU(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs index 56677038..846a5325 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/GatedRecurrentUnitCell.cs @@ -51,7 +51,7 @@ public class GatedRecurrentUnitCell /// Creates a GRUCell module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(GRUCell(InputSize, HiddenSize, Bias, Device, Type)); } @@ -62,7 +62,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => GRUCell(InputSize, HiddenSize, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs index 7386e8b6..43166c0c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemory.cs @@ -75,7 +75,7 @@ public class LongShortTermMemory /// Creates an LSTM module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } @@ -86,7 +86,7 @@ public class LongShortTermMemory /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LSTM(InputSize, HiddenSize, NumLayers, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs index 7611b7cd..0ce7ce24 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/LongShortTermMemoryCell.cs @@ -51,7 +51,7 @@ public class LongShortTermMemoryCell /// Creates an LSTMCell module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); } @@ -62,7 +62,7 @@ public class LongShortTermMemoryCell /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => LSTMCell(InputSize, HiddenSize, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs index 772ccd90..e73c5ee0 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetwork.cs @@ -81,7 +81,7 @@ public class RecurrentNeuralNetwork /// Creates an RNN module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } @@ -92,7 +92,7 @@ public class RecurrentNeuralNetwork /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => RNN(InputSize, HiddenSize, NumLayers, NonLinearity, Bias, BatchFirst, Dropout, Bidirectional, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs index 8ae3e317..0cfc972d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Recurrent/RecurrentNeuralNetworkCell.cs @@ -57,7 +57,7 @@ public class RecurrentNeuralNetworkCell /// Creates an RNNCell module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); } @@ -68,7 +68,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => RNNCell(InputSize, HiddenSize, NonLinearity, Bias, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs index ba822c5a..362c8c3e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Shuffle/ChannelShuffle.cs @@ -25,7 +25,7 @@ public class ChannelShuffle /// Creates a channel shuffle module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(ChannelShuffle(Groups)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => ChannelShuffle(Groups)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs index f9ee39ce..3a4e5d83 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/Embedding.cs @@ -75,7 +75,7 @@ public class Embedding /// Creates an embedding module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Embedding(NumEmbeddings, EmbeddingDims, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } @@ -86,7 +86,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Embedding(NumEmbeddings, EmbeddingDims, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs index a8c6ea56..4dfe806f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBag.cs @@ -88,7 +88,7 @@ public class EmbeddingBag /// Creates an embedding bag module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); } @@ -99,7 +99,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => EmbeddingBag(NumEmbeddings, EmbeddingDims, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs index 3a7b0dd7..0de43006 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingBagFromPretrained.cs @@ -103,7 +103,7 @@ public string EmbeddingsXml /// Creates an embedding bag module from pretrained weights. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); } @@ -114,7 +114,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => EmbeddingBag_from_pretrained(Embeddings, Freeze, MaxNorm, NormType, ScaleGradByFreq, Mode, Sparse, IncludeLastOffset, PaddingIndex, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs index 1af4eaee..b081d99e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Sparse/EmbeddingFromPretrained.cs @@ -90,7 +90,7 @@ public string EmbeddingsXml /// Creates an embedding module from pretrained weights. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Embedding_from_pretrained(Embeddings, Freeze, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } @@ -101,7 +101,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Embedding_from_pretrained(Embeddings, Freeze, PaddingIdx, MaxNorm, NormType, ScaleGradByFreq, Sparse, Device, Type)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs index 489db872..1823fc7a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/Transformer.cs @@ -61,7 +61,7 @@ public class Transformer /// Creates a Transformer module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(nn.Transformer(DimModel, NumHeads, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); } @@ -72,7 +72,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => nn.Transformer(DimModel, NumHeads, NumEncoderLayers, NumDecoderLayers, DimFeedforward, Dropout, Activation)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs index c3adb41f..c04e19db 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoder.cs @@ -23,18 +23,11 @@ public class TransformerDecoder public long NumLayers { get; set; } /// - /// The transformer decoder layer to be stacked. - /// - [XmlIgnore] - [Description("The transformer decoder layer to be stacked.")] - public TorchSharp.Modules.TransformerDecoderLayer TransformerDecoderLayer { get; private set; } - - /// - /// Creates a TransformerDecoder module. + /// Creates a TransformerDecoder module from the input TransformerDecoderLayer. /// /// - public IObservable> Process() + public IObservable Process(IObservable source) { - return Observable.Return(TransformerDecoder(TransformerDecoderLayer, NumLayers)); + return source.Select(input => TransformerDecoder(input, NumLayers)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs index 491f9454..c4be092d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerDecoderLayer.cs @@ -49,7 +49,7 @@ public class TransformerDecoderLayer /// Creates a TransformerDecoderLayer module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(TransformerDecoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); } @@ -60,7 +60,7 @@ public IObservable /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => TransformerDecoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs index 384a5585..0573b0b7 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoder.cs @@ -22,19 +22,12 @@ public class TransformerEncoder [Description("The number of sub-encoder layers in the encoder.")] public long NumLayers { get; set; } - /// - /// The transformer encoder layer to be stacked. - /// - [XmlIgnore] - [Description("The transformer encoder layer to be stacked.")] - public TorchSharp.Modules.TransformerEncoderLayer TransformerEncoderLayer { get; private set; } - /// /// Creates a TransformerEncoder module from the input TransformerEncoderLayer. /// /// - public IObservable> Process() + public IObservable Process(IObservable source) { - return Observable.Return(TransformerEncoder(TransformerEncoderLayer, NumLayers)); + return source.Select(input => TransformerEncoder(input, NumLayers)); } } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs index 26394e7e..f5e432ef 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Transformer/TransformerEncoderLayer.cs @@ -49,7 +49,7 @@ public class TransformerEncoderLayer /// Creates a TransformerEncoderLayer module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(TransformerEncoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); } @@ -60,7 +60,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => TransformerEncoderLayer(DimModel, NumHeads, DimFeedforward, Dropout, Activation)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs index 07824ded..7239cc26 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelShuffle.cs @@ -24,7 +24,7 @@ public class PixelShuffle /// /// Creates a pixel shuffle module. /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(PixelShuffle(UpscaleFactor)); } @@ -35,7 +35,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => PixelShuffle(UpscaleFactor)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs index 31e27cd8..3139616d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/PixelUnshuffle.cs @@ -25,7 +25,7 @@ public class PixelUnshuffle /// Creates a pixel unshuffle module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(PixelUnshuffle(DownscaleFactor)); } @@ -36,7 +36,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => PixelUnshuffle(DownscaleFactor)); } diff --git a/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs b/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs index b26d40fe..b1e0bda2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Vision/Upsample.cs @@ -59,7 +59,7 @@ public class Upsample /// Creates an Upsample module. /// /// - public IObservable> Process() + public IObservable Process() { return Observable.Return(Upsample(Size, ScaleFactor, Mode, AlignCorners, RecomputeScaleFactor)); } @@ -70,7 +70,7 @@ public IObservable> Process() /// /// /// - public IObservable> Process(IObservable source) + public IObservable Process(IObservable source) { return source.Select(_ => Upsample(Size, ScaleFactor, Mode, AlignCorners, RecomputeScaleFactor)); } From aa457e5721476b1216653caf6d0ed0e982a915c3 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 29 Jan 2026 22:43:21 +0000 Subject: [PATCH 26/30] Refactored save module into a builder class and renamed to save module state --- src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs | 203 ------------------ .../NeuralNets/SaveModuleStateBuilder.cs | 83 +++++++ 2 files changed, 83 insertions(+), 203 deletions(-) delete mode 100644 src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs create mode 100644 src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs deleted file mode 100644 index 69435968..00000000 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModule.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.ComponentModel; -using System.Reactive.Linq; -using System.Xml.Serialization; -using TorchSharp; -using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.jit; - -namespace Bonsai.ML.Torch.NeuralNets; - -/// -/// Represents an operator that saves a module's state to a file. -/// -[Combinator] -[ResetCombinator] -[Description("Saves a module's state to a file.")] -[WorkflowElementCategory(ElementCategory.Sink)] -public class SaveModule -{ - /// - /// The path to save the module's state. - /// - [Description("The path to save the module's state.")] - [Editor("Bonsai.Design.OpenFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] - public string ModulePath { get; set; } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - public IObservable Process(IObservable source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input module's state to the specified file path. - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - input.save(ModulePath); - }); - } - - /// - /// Saves the input scripted module's state to the specified file path. - /// - /// - /// - public IObservable Process(IObservable source) - { - return source.Do(input => - { - save(input, ModulePath); - }); - } - - /// - /// Saves the input scripted module to the specified file path. - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - save(input, ModulePath); - }); - } - - /// - /// Saves the input scripted module to the specified file path. - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - save(input, ModulePath); - }); - } - - /// - /// Saves the input scripted module to the specified file path. - /// - /// - /// - /// - /// - /// - public IObservable> Process(IObservable> source) - { - return source.Do(input => - { - save(input, ModulePath); - }); - } -} \ No newline at end of file diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs new file mode 100644 index 00000000..20571dd9 --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs @@ -0,0 +1,83 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Reflection; +using System.Xml.Serialization; +using TorchSharp; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; +using static TorchSharp.torch.jit; +using Bonsai.Expressions; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that saves a module's state to a file. +/// +[Combinator] +[Description("Saves a module's state to a file.")] +[WorkflowElementCategory(ElementCategory.Sink)] +public class SaveModuleStateBuilder : SingleArgumentExpressionBuilder +{ + /// + /// The path where the module's state will be stored. + /// + [Description("The path where the module's state will be stored.")] + [Editor("Bonsai.Design.SaveFileNameEditor, Bonsai.Design", DesignTypes.UITypeEditor)] + public string ModulePath { get; set; } + + /// + public override Expression Build(IEnumerable args) + { + var inputArg = args.First().Type.GetGenericArguments()[0]; + bool isInputModule = false; + for (var t = inputArg; t != null && t != typeof(object); t = t.BaseType) + { + if (t.GetType() == typeof(nn.Module) || t.IsGenericType && t.GetGenericTypeDefinition().Name.StartsWith("Module`")) + { + isInputModule = true; + break; + } + } + + if (!isInputModule) + throw new InvalidOperationException("The SaveModuleState operator requires a Module type as input."); + + var selectMethod = typeof(Observable) + .GetMethods() + .First(m => + m.Name == nameof(Observable.Select) && + m.IsGenericMethodDefinition && + m.GetParameters().Length == 2); + + var moduleParameter = Expression.Parameter(inputArg, "m"); + + var moduleSaveMethods = typeof(nn.Module) + .GetMethods(BindingFlags.Instance | BindingFlags.Public) + .Where(m => + { + var parameters = m.GetParameters(); + return m.Name == "save" && parameters.Length == 2 && parameters[0].ParameterType == typeof(string) && parameters[1].ParameterType == typeof(IList); + }); + + var moduleSaveMethod = moduleSaveMethods.First(); + + List moduleSaveArgs = + [ + Expression.Constant(ModulePath, typeof(string)), + Expression.Constant(null, typeof(IList)) + ]; + + var saveCall = Expression.Call(moduleParameter, moduleSaveMethod, moduleSaveArgs); + + var convertSaveCall = Expression.Convert(saveCall, inputArg); + var lambda = Expression.Lambda(convertSaveCall, moduleParameter); + + var genericSelectMethod = selectMethod.MakeGenericMethod(inputArg, inputArg); + + return Expression.Call(genericSelectMethod, args.First(), lambda); + } +} From 18d8107f0d9c499d27ec1a42bc6690548933982e Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Thu, 29 Jan 2026 23:42:03 +0000 Subject: [PATCH 27/30] Added operator to merge derived modules into a common module base class for feeding sequential container --- .../NeuralNets/MergeModules.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Bonsai.ML.Torch/NeuralNets/MergeModules.cs diff --git a/src/Bonsai.ML.Torch/NeuralNets/MergeModules.cs b/src/Bonsai.ML.Torch/NeuralNets/MergeModules.cs new file mode 100644 index 00000000..1807287a --- /dev/null +++ b/src/Bonsai.ML.Torch/NeuralNets/MergeModules.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using System.Linq; +using System.Collections.Generic; +using static TorchSharp.torch; +using static TorchSharp.torch.nn; + +namespace Bonsai.ML.Torch.NeuralNets; + +/// +/// Represents an operator that merges derived modules into a collection of base module types. +/// +[Combinator] +[Description("Merges derived modules into a collection of base module types.")] +[WorkflowElementCategory(ElementCategory.Combinator)] +public class MergeModules +{ + /// + /// Merges the input sequences of modules and casts them to the base module class. + /// + /// + /// + public IObservable>> Process(params IObservable[] sources) + { + return Observable + .Concat(sources.Select(source => + source.Take(1))) + .OfType>() + .ToList() + .Select(modules => modules.AsEnumerable()); + } +} From 291d6a10253c784c930ee193da093c863bb5e059 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Mon, 2 Feb 2026 14:34:01 +0000 Subject: [PATCH 28/30] Updated base module builder class to set argument range --- .../NeuralNets/ActivationFunctionBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs | 7 +++---- .../NeuralNets/ConvolutionModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs | 5 +---- .../NeuralNets/LearningRateSchedulerBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs | 8 ++++---- src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs | 5 ++++- .../NeuralNets/NormalizationModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs | 6 +----- src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs | 4 ---- src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs | 5 +---- .../NeuralNets/TransformerModuleBuilder.cs | 5 +---- src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs | 5 +---- 22 files changed, 29 insertions(+), 86 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs index 619e42fb..6c2bdf5a 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ActivationFunctionBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -41,9 +41,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class ActivationFunctionBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "ActivationFunction"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs index 65e69e6a..cbd23287 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ContainerBuilder.cs @@ -1,6 +1,5 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; - namespace Bonsai.ML.Torch.NeuralNets; /// @@ -10,11 +9,11 @@ namespace Bonsai.ML.Torch.NeuralNets; [DefaultProperty(nameof(ContainerModule))] [Combinator] [Description("Creates a sequential container for torch modules.")] -[WorkflowElementCategory(ElementCategory.Source)] +[WorkflowElementCategory(ElementCategory.Transform)] public class ContainerBuilder : ModuleCombinatorBuilder, INamedElement { /// - public override Range ArgumentRange => Range.Create(0, 1); + public override Range ArgumentRange => Range.Create(1, 1); /// internal override string BuilderName => "Container"; diff --git a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs index d570804b..ec9e4ae5 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ConvolutionModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -20,9 +20,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class ConvolutionModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "ConvolutionModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs index f0779b7a..aae8f651 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/DistanceModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -14,9 +14,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class DistanceModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "DistanceModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs index b448651c..b111af08 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/DropoutModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -18,9 +18,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class DropoutModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "DropoutModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs index 64134556..d5ef301c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/FlattenModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -14,9 +14,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class FlattenModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "FlattenModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs index 83268749..95c4c489 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LearningRateSchedulerBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -21,9 +21,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Transform)] public class LearningRateSchedulerBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "LearningRateScheduler"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs index b148ac40..5d9df3a8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LinearModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -15,9 +15,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class LinearModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "LinearModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs index 7351e49f..cba653b8 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LoadModuleStateBuilder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; @@ -69,9 +69,9 @@ public override Expression Build(IEnumerable args) List moduleLoadArgs = [ Expression.Constant(ModulePath, typeof(string)), - Expression.Constant(true), // strict loading - Expression.Constant(null, typeof(IList)), // exclude list - Expression.Constant(null, typeof(Dictionary)) // exclude dictionary + Expression.Constant(true), + Expression.Constant(null, typeof(IList)), + Expression.Constant(null, typeof(Dictionary)) ]; var loadCall = Expression.Call(moduleParameter, moduleLoadMethod, moduleLoadArgs); diff --git a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs index 452e36aa..d7ec3604 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/LossBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -32,9 +32,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class LossBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "Loss"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs index 311be66c..c0696ba6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ModuleCombinatorBuilder.cs @@ -1,4 +1,4 @@ -using Bonsai.Expressions; +using Bonsai.Expressions; using System; using System.ComponentModel; using System.Linq; @@ -20,6 +20,9 @@ public abstract class ModuleCombinatorBuilder : ExpressionBuilder, ICustomTypeDe internal object Module { get; set; } + /// + public override Range ArgumentRange => Range.Create(0, 1); + /// public override Expression Build(IEnumerable arguments) { diff --git a/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs index 8da81a75..e925f7fc 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/NormalizationModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -21,9 +21,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class NormalizationModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "NormalizationModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs index 12b1b7d3..77e6377f 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizationStep.cs @@ -1,10 +1,6 @@ -using System; +using System; using System.ComponentModel; -using System.Linq; using System.Reactive.Linq; -using System.Collections.Generic; -using System.Linq.Expressions; -using Bonsai.Expressions; using static TorchSharp.torch; using static TorchSharp.torch.optim.lr_scheduler; diff --git a/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs index 716bd1de..59498d3d 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/OptimizerBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -22,9 +22,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Transform)] public class OptimizerBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "Optimizer"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs index 31cd6d18..b6b43f15 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/PaddingModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -22,9 +22,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class PaddingModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "PaddingModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs index 89483de1..bd0e9d65 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/PoolingModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -31,9 +31,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class PoolingModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "PoolingModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs index 00dcdc96..58fa5bae 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/RecurrentModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -18,9 +18,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class RecurrentModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "RecurrentModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs index 20571dd9..ce7dfb02 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SaveModuleStateBuilder.cs @@ -5,11 +5,7 @@ using System.ComponentModel; using System.Reactive.Linq; using System.Reflection; -using System.Xml.Serialization; -using TorchSharp; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.jit; using Bonsai.Expressions; namespace Bonsai.ML.Torch.NeuralNets; diff --git a/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs index 286d389d..4dd00f7e 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ShuffleModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -13,9 +13,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class ShuffleModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "ShuffleModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs index 3cbb2e47..9f8179f6 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/SparseModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -16,9 +16,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class SparseModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "SparseModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs index 64504b34..d99c2af2 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/TransformerModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -17,9 +17,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class TransformerModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "TransformerModule"; /// diff --git a/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs b/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs index 56f0e725..fb584f3b 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/VisionModuleBuilder.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using System.Xml.Serialization; namespace Bonsai.ML.Torch.NeuralNets; @@ -15,9 +15,6 @@ namespace Bonsai.ML.Torch.NeuralNets; [WorkflowElementCategory(ElementCategory.Source)] public class VisionModuleBuilder : ModuleCombinatorBuilder, INamedElement { - /// - public override Range ArgumentRange => Range.Create(0, 1); - internal override string BuilderName => "VisionModule"; /// From f2cc9bfbe543a4ecddbe843a7831fc02eb9856e0 Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Mon, 2 Feb 2026 14:34:50 +0000 Subject: [PATCH 29/30] Updated to support correct sink behavior --- src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs b/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs index b0e872dd..2eb76410 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/ZeroGradient.cs @@ -1,15 +1,7 @@ -using System; -using System.Linq; +using System; using System.ComponentModel; -using System.Reactive; -using System.Reactive.Disposables; using System.Reactive.Linq; -using System.Xml.Serialization; using static TorchSharp.torch; -using static TorchSharp.torch.nn; -using static TorchSharp.torch.optim; -using static TorchSharp.torch.optim.lr_scheduler; -using System.Threading.Tasks; namespace Bonsai.ML.Torch.NeuralNets; @@ -26,8 +18,8 @@ public class ZeroGradient /// /// /// - public IObservable Process(IObservable source) + public IObservable Process(IObservable source) where T : optim.Optimizer { return source.Do(input => input.zero_grad()); } -} \ No newline at end of file +} From a11b3de900a689a263f907294fcf098fa68862fd Mon Sep 17 00:00:00 2001 From: ncguilbeault Date: Mon, 2 Feb 2026 14:35:25 +0000 Subject: [PATCH 30/30] Fixed sequential container to correctly receive input from `MergeModules` --- src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs index 2a44f23c..5bf4676c 100644 --- a/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs +++ b/src/Bonsai.ML.Torch/NeuralNets/Container/Sequential.cs @@ -1,9 +1,10 @@ -using System; +using System; using System.ComponentModel; using System.Reactive.Linq; using static TorchSharp.torch; using static TorchSharp.torch.nn; using System.Linq; +using System.Collections.Generic; namespace Bonsai.ML.Torch.NeuralNets.Container; @@ -20,7 +21,7 @@ public class Sequential /// Creates a sequential container from the input modules. /// /// - public IObservable Process(IObservable source) where T : Module + public IObservable Process(IObservable source) where T : IEnumerable> { return source.Select(modules => Sequential(modules)); }