-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataLoader.cs
More file actions
160 lines (142 loc) · 5.37 KB
/
DataLoader.cs
File metadata and controls
160 lines (142 loc) · 5.37 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
namespace TorchSharpDataLoaderExample
{
using System.Collections;
using static TorchSharp.torch;
/// <summary>
/// This class makes easier to create batch. Data set must implement Dataset interface
/// </summary>
public class DataLoader : IEnumerable<Dictionary<string, Tensor>>, IDisposable
{
private Dataset dataset;
private int batchSize;
private bool shuffle;
private Device device;
private IEnumerable<long> shuffler;
/// <summary>
/// Pytorch style dataloader
/// </summary>
/// <param name="dataset">Dataset for create batch</param>
/// <param name="batchSize">Size of batch</param>
/// <param name="device">device for output tensor</param>
/// <param name="shuffler">Shuffler for dataloader</param>
public DataLoader(Dataset dataset, int batchSize, IEnumerable<long> shuffler, Device device = null)
{
this.dataset = dataset;
this.batchSize = batchSize;
this.shuffle = true;
this.device = device ?? CPU;
this.shuffler = shuffler;
}
/// <summary>
/// Pytorch style dataloader
/// </summary>
/// <param name="dataset">Dataset for create batch</param>
/// <param name="batchSize">Size of batch</param>
/// <param name="shuffle">true if shuffle dataset, false for not</param>
/// <param name="device">device for output tensor</param>
/// <param name="seed">Seed for generating shuffle</param>
public DataLoader(Dataset dataset, int batchSize, bool shuffle = false, Device device = null, int? seed = null)
{
this.dataset = dataset;
this.batchSize = batchSize;
this.shuffle = shuffle;
this.device = device ?? CPU;
this.shuffler = seed is null
? new FisherYatesShuffler(dataset.Count)
: new FisherYatesShuffler(dataset.Count, seed);
}
/// <summary>
/// Generate enumerator
/// </summary>
/// <returns>Enumerator for batch</returns>
public IEnumerator<Dictionary<string, Tensor>> GetEnumerator() =>
new DataLoaderEnumerator(dataset, batchSize, shuffle, device, shuffler);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Size of batch
/// </summary>
public long Count => (dataset.Count - 1) / batchSize + 1;
private class DataLoaderEnumerator : IEnumerator<Dictionary<string, Tensor>>
{
private Dataset dataset;
private int batchSize;
private Device device;
private bool shuffle;
private IEnumerable<long> shuffleEnumerable;
private IEnumerator<long> shuffler;
private long currentVal = 0;
public DataLoaderEnumerator(Dataset dataset, int batchSize, bool shuffle, Device device,
IEnumerable<long> shuffleEnumerable)
{
this.dataset = dataset;
this.batchSize = batchSize;
this.device = device;
this.shuffle = shuffle;
this.shuffleEnumerable = shuffleEnumerable;
Reset();
}
private bool MoveNextValue()
{
if (shuffle)
{
if (!shuffler.MoveNext()) return false;
currentVal = shuffler.Current;
return true;
}
else
{
currentVal++;
return currentVal < dataset.Count;
}
}
/// <summary>
/// Get next batch
/// </summary>
/// <returns>true if batch created, false if batch has finished</returns>
public bool MoveNext()
{
DisposeCurrent();
if (!MoveNextValue()) return false;
List<Dictionary<string, Tensor>> dic = new();
dic.Add(dataset.GetTensor(currentVal));
for (var i = 1; i < batchSize; i++)
{
if (!MoveNextValue()) break;
dic.Add(dataset.GetTensor(currentVal));
}
Current = new();
foreach (var x in dic[0].Keys)
Current[x] = cat(dic.Select(k => k[x].unsqueeze(0)).ToArray(), 0).to(device);
return true;
}
/// <summary>
/// Reset enumerator
/// </summary>
public void Reset()
{
DisposeCurrent();
if (shuffle) shuffler = shuffleEnumerable.GetEnumerator();
currentVal = -1;
}
/// <summary>
/// Current tensor
/// </summary>
public Dictionary<string, Tensor> Current { get; private set; }
object IEnumerator.Current => Current;
public void Dispose()
{
DisposeCurrent();
}
private void DisposeCurrent()
{
if (Current is null) return;
foreach (var x in Current.Values)
x.Dispose();
}
}
public void Dispose()
{
dataset.Dispose();
}
}
}