Skip to content

Commit

Permalink
Merge pull request #501 from NiklasGustafsson/bugs
Browse files Browse the repository at this point in the history
Fixed bugs #499 and #500
  • Loading branch information
NiklasGustafsson committed Feb 3, 2022
2 parents 5e2800e + f5e281c commit 3fc452d
Show file tree
Hide file tree
Showing 52 changed files with 1,830 additions and 318 deletions.
20 changes: 18 additions & 2 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

Releases, starting with 9/2/2021, are listed with the most recent release at the top.

## NuGet Version 0.96.0

__API Changes:__

__NOTE__: This release contains breaking changes.

'Module.named_parameters()', 'parameters()', 'named_modules()', 'named_children()' all return IEnumerable instances instead of arrays.
Adding weight and bias properties to the RNN modules.
Lower-cased names: Module.Train --> Module.train and Module.Eval --> Module.eval

__Fixed Bugs:__

#500 BatchNorm1d throws exception during eval with batch size of 1
#499 Setting Linear.weight is not reflected in 'parameters()'
#496 Wrong output shape of torch.nn.Conv2d with 2d stride overload

## NuGet Version 0.95.4

__API Changes:__
Expand Down Expand Up @@ -151,15 +167,15 @@ __Fixed Bugs:__

__Added Features:__

```
'''
torch.nn.MultiHeadAttention
torch.linalg.cond
torch.linalg.cholesky_ex
torch.linalg.inv_ex
torch.amax/amin
torch.matrix_exp
torch.distributions.* (about half the namespace)
```
'''

__API Changes:__

Expand Down
4 changes: 2 additions & 2 deletions build/BranchInfo.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<MajorVersion>0</MajorVersion>
<MinorVersion>95</MinorVersion>
<PatchVersion>4</PatchVersion>
<MinorVersion>96</MinorVersion>
<PatchVersion>0</PatchVersion>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Examples.Utils/Examples.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.3.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Examples/AdversarialExampleGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal static void Main(string[] args)
}

model.to((Device)device);
model.Eval();
model.eval();

var epsilons = new double[] { 0, 0.05, 0.1, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50 };

Expand Down
4 changes: 1 addition & 3 deletions src/Examples/AlexNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public AlexNet(string name, int numClasses, torch.Device device = null) : base(n
("l3", Linear(4096, numClasses))
);

register_module("features", features);
register_module("avg", avgPool);
register_module("classify", classifier);
RegisterComponents();

if (device != null && device.type == DeviceType.CUDA)
this.to(device);
Expand Down
4 changes: 2 additions & 2 deletions src/Examples/CIFAR10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal static void Main(string[] args)
long batchSize,
long size)
{
model.Train();
model.train();

int batchId = 1;
long total = 0;
Expand Down Expand Up @@ -200,7 +200,7 @@ internal static void Main(string[] args)
IEnumerable<(Tensor, Tensor)> dataLoader,
long size)
{
model.Eval();
model.eval();

double testLoss = 0;
long correct = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.3.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="SkiaSharp" Version="2.80.2" />
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Examples/MNIST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public override Tensor forward(Tensor input)
long batchSize,
long size)
{
model.Train();
model.train();

int batchId = 1;

Expand Down Expand Up @@ -206,7 +206,7 @@ public override Tensor forward(Tensor input)
IEnumerable<(Tensor, Tensor)> dataLoader,
long size)
{
model.Eval();
model.eval();

double testLoss = 0;
int correct = 0;
Expand Down
1 change: 0 additions & 1 deletion src/Examples/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"profiles": {
"Examples": {
"commandName": "Project",
"commandLineArgs": "VGG11 16 1800",
"nativeDebugging": true
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Examples/SequenceToSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal static void Main(string[] args)

private static void train(int epoch, Tensor train_data, TransformerModel model, Loss criterion, int bptt, int ntokens, torch.optim.Optimizer optimizer)
{
model.Train();
model.train();

using (var d = torch.NewDisposeScope()) {

Expand All @@ -126,7 +126,7 @@ private static void train(int epoch, Tensor train_data, TransformerModel model,
using (var output = model.forward(data, src_mask)) {
var loss = criterion(output.view(-1, ntokens), targets);
loss.backward();
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5);
torch.nn.utils.clip_grad_norm_(model.parameters().ToArray(), 0.5);
optimizer.step();

total_loss += loss.to(torch.CPU).item<float>();
Expand All @@ -145,7 +145,7 @@ private static void train(int epoch, Tensor train_data, TransformerModel model,

private static double evaluate(Tensor eval_data, TransformerModel model, Loss criterion, int bptt, int ntokens, torch.optim.Optimizer optimizer)
{
model.Eval();
model.eval();

using (var d = torch.NewDisposeScope()) {

Expand Down
6 changes: 3 additions & 3 deletions src/Examples/TextClassification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal static void Main(string[] args)

static void train(int epoch, IEnumerable<(Tensor, Tensor, Tensor)> train_data, TextClassificationModel model, Loss criterion, torch.optim.Optimizer optimizer)
{
model.Train();
model.train();

double total_acc = 0.0;
long total_count = 0;
Expand All @@ -123,7 +123,7 @@ static void train(int epoch, IEnumerable<(Tensor, Tensor, Tensor)> train_data, T

var loss = criterion(predicted_labels, labels);
loss.backward();
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5);
torch.nn.utils.clip_grad_norm_(model.parameters().ToArray(), 0.5);
optimizer.step();

total_acc += (predicted_labels.argmax(1) == labels).sum().to(torch.CPU).item<long>();
Expand All @@ -141,7 +141,7 @@ static void train(int epoch, IEnumerable<(Tensor, Tensor, Tensor)> train_data, T

static double evaluate(IEnumerable<(Tensor, Tensor, Tensor)> test_data, TextClassificationModel model, Loss criterion)
{
model.Eval();
model.eval();

double total_acc = 0.0;
long total_count = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Examples/AdversarialExampleGeneration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ let run epochs =

model.``to``(device) |> ignore

model.Eval()
model.eval()

let epsilons = [| 0.0; 0.05; 0.1; 0.15; 0.20; 0.25; 0.30; 0.35; 0.40; 0.45; 0.50|]

Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Examples/AlexNet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ let loss x y = functional.nll_loss().Invoke(x,y)

let train (model:Model) (optimizer:Optimizer) (dataLoader: CIFARReader) epoch =

model.Train()
model.train()

let size = dataLoader.Size

Expand Down Expand Up @@ -125,7 +125,7 @@ let train (model:Model) (optimizer:Optimizer) (dataLoader: CIFARReader) epoch =
end

let test (model:Model) (dataLoader:CIFARReader) =
model.Eval()
model.eval()

let sz = float32 dataLoader.Size

Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Examples/FSharp.Examples.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.3.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Examples/MNIST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Model(name,device:torch.Device) as this =
let loss x y = functional.nll_loss(reduction=Reduction.Mean).Invoke(x,y)

let train (model:Model) (optimizer:Optimizer) (dataLoader: MNISTReader) epoch =
model.Train()
model.train()

let size = dataLoader.Size
let batchSize = dataLoader.BatchSize
Expand All @@ -114,7 +114,7 @@ let train (model:Model) (optimizer:Optimizer) (dataLoader: MNISTReader) epoch =
batchID <- batchID + 1

let test (model:Model) (dataLoader:MNISTReader) =
model.Eval()
model.eval()

let sz = float32 dataLoader.Size

Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Examples/SequenceToSequence.fs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let get_batch (source:torch.Tensor) (index:int64) =

let train epoch (model:TransformerModel) (optimizer:Optimizer) (trainData:torch.Tensor) ntokens =

model.Train()
model.train()

use d = torch.NewDisposeScope()

Expand Down Expand Up @@ -183,7 +183,7 @@ let train epoch (model:TransformerModel) (optimizer:Optimizer) (trainData:torch.

let evaluate (model:TransformerModel) (evalData:torch.Tensor) ntokens =

model.Eval()
model.eval()

use d = torch.NewDisposeScope()

Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Examples/TextClassification.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type TextClassificationModel(vocabSize, embedDim, nClasses, device:torch.Device)

let train epoch (trainData:IEnumerable<torch.Tensor*torch.Tensor*torch.Tensor>) (model:TextClassificationModel) (optimizer:torch.optim.Optimizer) =

model.Train()
model.train()

let mutable total_acc = 0.0
let mutable total_count = 0L
Expand Down Expand Up @@ -103,7 +103,7 @@ let train epoch (trainData:IEnumerable<torch.Tensor*torch.Tensor*torch.Tensor>)

let evaluate (testData:IEnumerable<torch.Tensor*torch.Tensor*torch.Tensor>) (model:TextClassificationModel) =

model.Eval()
model.eval()

let mutable total_acc = 0.0
let mutable total_count = 0L
Expand Down
2 changes: 1 addition & 1 deletion src/Native/LibTorchSharp/THSModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void THSNN_Module_register_module(const NNModule module, const char* name, const
void THSNN_Module_register_parameter(const NNModule module, const char* name, const Tensor tensor, bool requires_grad)
{
CATCH(
(*module)->register_parameter(name, *tensor, requires_grad);
(*module)->register_parameter(name, (tensor == nullptr) ? at::Tensor() : *tensor, requires_grad);
);
}

Expand Down
Loading

0 comments on commit 3fc452d

Please sign in to comment.