Public Class encodertrain Public Shared Sub Train(runId As String, cleanDataRoot As…
- User
Public Class encoder_train
codePublic Shared Sub Train(runId As String, cleanDataRoot As String, modelsDir As String, umapEvery As Integer, saveEvery As Integer, backupEvery As Integer, visEvery As Integer, forceRestart As Boolean, visdomServer As String, noVisdom As Boolean) '····省略代码···· ' 设置运行前向传递和损失计算的设备(电脑上没装CUDA,所以第二个选项跳过) Dim device As Device = If(TorchSharp.torch.cuda.is_available(), Device.CUDA, Device.CPU) Dim lossDevice As Device = Device.CPU '' 创建模型和优化器 Dim model As SpeakerEncoder = New SpeakerEncoder(device, lossDevice) Dim optimizer As TorchSharp.torch.OptimizerOptions = TorchSharp.torch.optim.Adam(model.Parameters(), learning_rate_init) '····省略代码···· End SubEnd Class
Public Class SpeakerEncoder Private ReadOnly _device As DeviceDescriptor Private ReadOnly _lossDevice As DeviceDescriptor Private ReadOnly _intTensorConverter As IValueConverter(Of Integer) Private ReadOnly _lossFn As CrossEntropyLoss
codePublic Property TensorSimilarityWeight As TorchSharp.NN.Parameter Public Property TensorSimilarityBias As TorchSharp.NN.Parameter Public Property Lstm As TorchSharp.NN.Recurrent.LSTM Public Property Linear As TorchSharp.NN.Linear Public Property Relu As TorchSharp.NN.Activation.ReLU Public Sub New(device As DeviceDescriptor, lossDevice As DeviceDescriptor) _device = device _lossDevice = lossDevice _intTensorConverter = ValueConverters.Int64ToFloat.Create() ' 定义神经网络结构 Lstm = New TorchSharp.NN.Recurrent.LSTM(New TorchSharp.NN.Recurrent.LSTMOptions(_device, inputSize:=mel_n_channels, hiddenSize:=model_hidden_size, numLayers:=model_num_layers, batchFirst:=True)) Linear = New TorchSharp.NN.Linear(New LinearOptions(_device, inFeatures:=model_hidden_size, outFeatures:=model_embedding_size)) Relu = New TorchSharp.NN.Activation.ReLU(_device) ' 初始化余弦相似度缩放参数 TensorSimilarityWeight = New TorchSharp.NN.Parameter(New Microsoft.ML.OnnxRuntime.Tensors.DenseTensor()) TensorSimilarityBias = New TorchSharp.NN.Parameter(New Microsoft.ML.OnnxRuntime.Tensors.DenseTensor(Of Single)(_lossDevice, New Integer() {1}, New Single() {-5.0F})) ' 初始化损失函数(这里假设有一个自定义的CrossEntropyLoss类,适用于.NET环境) _lossFn = New CrossEntropyLoss(_lossDevice) End Sub - Assistant
The core issue you're facing is due to using a custom implementation of
ReadOnlySpan(Of T)which does not allow indexing. Normally,ReadOnlySpan(Of T)is a struct in .NET that represents a contiguous region of arbitrary memory and does support indexing directly. However, in the code you shared,ReadOnlySpan(Of T)is implemented as a custom class without an indexer, which leads to the inability to use the indexing operation .