forked from dotnet/TorchSharp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRandomOrder.cs
56 lines (51 loc) · 1.74 KB
/
RandomOrder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using static TorchSharp.torch;
namespace TorchSharp
{
public static partial class torchvision
{
internal class RandomOrder : IDisposable, ITransform
{
public RandomOrder(ITransform[] transforms)
{
this.transforms = transforms;
}
public void Dispose()
{
foreach (var t in transforms) {
if (t is IDisposable) {
((IDisposable)t).Dispose();
}
}
}
public Tensor call(Tensor input)
{
var rng = new Random();
foreach (var t in transforms.OrderBy(t => rng.NextDouble())) {
input = t.call(input);
}
return input;
}
private IList<ITransform> transforms;
}
public static partial class transforms
{
/// <summary>
/// Apply a list of transformations in a random order.
/// </summary>
/// <param name="transforms">A list of transforms to apply.</param>
/// <remarks>
/// This transform uses the .NET Random API, not the Torch RNG.
/// Each invocation of 'forward()' will randomize the order in which transforms
/// are applied.
/// </remarks>
static public ITransform RandomOrder(params ITransform[] transforms)
{
return new RandomOrder(transforms);
}
}
}
}