MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
PrioritizedMemoryCollection.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
9{
14 {
15 float m_fAlpha;
16 double m_fMaxPriority = 1.0f;
17 int m_nItCapacity = 1;
18 MemoryCollection m_mem;
19 SumSegmentTree m_ItSum;
20 MinSegmentTree m_ItMin;
21
27 public PrioritizedMemoryCollection(int nMax, float fAlpha)
28 {
29 m_mem = new MemoryCollection(nMax);
30 m_fAlpha = fAlpha;
31
32 while (m_nItCapacity < nMax)
33 {
34 m_nItCapacity *= 2;
35 }
36
37 m_ItSum = new SumSegmentTree(m_nItCapacity);
38 m_ItMin = new MinSegmentTree(m_nItCapacity);
39 }
40
44 public void CleanUp()
45 {
46 }
47
51 public int Count
52 {
53 get { return m_mem.Count; }
54 }
55
60 public void Add(MemoryItem m)
61 {
62 int nIdx = m_mem.NextIndex;
63 m_mem.Add(m);
64
65 int nVal = (int)Math.Pow(m_fMaxPriority, m_fAlpha);
66 m_ItSum[nIdx] = nVal;
67 m_ItMin[nIdx] = nVal;
68 }
69
70 private int[] getSamplesProportional(CryptoRandom random, int nCount)
71 {
72 int[] rgIdx = new int[nCount];
73
74 for (int i = 0; i < nCount; i++)
75 {
76 double dfRand = random.NextDouble();
77 double dfSum1 = m_ItSum.sum(0, m_mem.Count - 1);
78 double dfMass = dfRand * dfSum1;
79 int nIdx = m_ItSum.find_prefixsum_idx((float)dfMass);
80 rgIdx[i] = nIdx;
81 }
82
83 return rgIdx;
84 }
85
93 public MemoryCollection GetSamples(CryptoRandom random, int nCount, double dfBeta)
94 {
95 int[] rgIdx = getSamplesProportional(random, nCount);
96 double[] rgfWeights = new double[nCount];
97 double fSum = m_ItSum.sum();
98 double fMin = m_ItMin.min();
99 double fPMin = fMin / fSum;
100 double fMaxWeight = (float)Math.Pow(fPMin * m_mem.Count, -dfBeta);
101 MemoryCollection mem = new MemoryCollection(nCount);
102
103 for (int i = 0; i < rgIdx.Length; i++)
104 {
105 int nIdx = rgIdx[i];
106 double fItSum = m_ItSum[nIdx];
107 double fPSample = fItSum / fSum;
108 double fWeight = Math.Pow(fPSample * m_mem.Count, -dfBeta);
109 rgfWeights[i] = fWeight / fMaxWeight;
110
111 mem.Add(m_mem[nIdx]);
112 }
113
114 mem.Indexes = rgIdx;
115 mem.Priorities = rgfWeights;
116
117 return mem;
118 }
119
127 public void Update(MemoryCollection rgSamples)
128 {
129 int[] rgIdx = rgSamples.Indexes;
130 double[] rgfPriorities = rgSamples.Priorities;
131
132 if (rgIdx.Length != rgfPriorities.Length)
133 throw new Exception("The index and priority arrays must have the same length.");
134
135 for (int i = 0; i < rgIdx.Length; i++)
136 {
137 int nIdx = rgIdx[i];
138 double fPriority = rgfPriorities[i];
139
140 if (fPriority <= 0)
141 throw new Exception("The priority at index '" + i.ToString() + "' is zero!");
142
143 if (nIdx < 0 || nIdx >= m_mem.Count)
144 throw new Exception("The index at index '" + i.ToString() + "' is out of range!");
145
146 double fNewPriority = Math.Pow(fPriority, m_fAlpha);
147 m_ItSum[nIdx] = fNewPriority;
148 m_ItMin[nIdx] = fNewPriority;
149 m_fMaxPriority = Math.Max(m_fMaxPriority, fPriority);
150 }
151 }
152 }
153}
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
Definition: CryptoRandom.cs:14
double NextDouble()
Returns a random double within the range .
Definition: CryptoRandom.cs:83
The memory collection stores a set of memory items.
double[] Priorities
Get/set the priorities associated with the collection (if any).
int Count
Returns the current count of items.
int NextIndex
Returns the next index.
int[] Indexes
Get/set the indexes associated with the collection (if any).
virtual void Add(MemoryItem item)
Adds a new memory item to the array of items and if at capacity, removes an item.
The MemoryItem stores the information about a given cycle.
The MinSegmentTree performs a reduction over the array and returns the minimum value.
Definition: SegmentTree.cs:236
double min(int nStart=0, int? nEnd=null)
Returns the minimum element in the array.
Definition: SegmentTree.cs:252
The PrioritizedMemoryCollection provides a sampling based on prioritizations.
PrioritizedMemoryCollection(int nMax, float fAlpha)
The constructor.
void Add(MemoryItem m)
Add a new item to the collection.
MemoryCollection GetSamples(CryptoRandom random, int nCount, double dfBeta)
Return a batch of items.
void Update(MemoryCollection rgSamples)
Update the priorities of sampled transitions.
int Count
Returns the number of items in the collection.
The SumSegmentTree provides a sum reduction of the items within the array.
Definition: SegmentTree.cs:176
int find_prefixsum_idx(double fPrefixSum)
Finds the highest indes 'i' in the array such that sum(arr[0] + arr[1] + ... + arr[i-1]) less than or...
Definition: SegmentTree.cs:205
double sum(int nStart=0, int? nEnd=null)
Returns arr[start] + ... + arr[end]
Definition: SegmentTree.cs:192
The IMemoryCollection interface is implemented by all memory collection types.
Definition: Interfaces.cs:37
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12