-
Notifications
You must be signed in to change notification settings - Fork 4.4k
WIP Made initial changes to enable dimension properties and added attention module #4763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
66501bf
1596d45
d54ea91
cd49289
3cddfac
1268e9a
428763b
5e907c3
4c6e28d
ce0e837
1b416f2
e9319a6
0338e70
979bf58
7c5a9ae
c39955f
618e981
dde2cb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
|
||
namespace Unity.MLAgents.Sensors | ||
{ | ||
public class BufferSensor : ISensor, IDimensionPropertiesSensor | ||
{ | ||
private int m_MaxNumObs; | ||
private int m_ObsSize; | ||
float[] m_ObservationBuffer; | ||
int m_CurrentNumObservables; | ||
public BufferSensor(int maxNumberObs, int obsSize) | ||
{ | ||
m_MaxNumObs = maxNumberObs; | ||
m_ObsSize = obsSize; | ||
m_ObservationBuffer = new float[m_ObsSize * m_MaxNumObs]; | ||
m_CurrentNumObservables = 0; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public int[] GetObservationShape() | ||
{ | ||
return new int[] { m_MaxNumObs, m_ObsSize }; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public DimensionProperty[] GetDimensionProperties() | ||
{ | ||
return new DimensionProperty[]{ | ||
DimensionProperty.VariableSize, | ||
DimensionProperty.None | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Appends an observation to the buffer. If the buffer is full (maximum number | ||
/// of observation is reached) the observation will be ignored. the length of | ||
/// the provided observation array must be equal to the observation size of | ||
/// the buffer sensor. | ||
/// </summary> | ||
/// <param name="obs"> The float array observation</param> | ||
public void AppendObservation(float[] obs) | ||
{ | ||
if (m_CurrentNumObservables >= m_MaxNumObs) | ||
{ | ||
return; | ||
} | ||
for (int i = 0; i < obs.Length; i++) | ||
{ | ||
m_ObservationBuffer[m_CurrentNumObservables * m_ObsSize + i] = obs[i]; | ||
} | ||
m_CurrentNumObservables++; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public int Write(ObservationWriter writer) | ||
{ | ||
for (int i = 0; i < m_ObsSize * m_MaxNumObs; i++) | ||
{ | ||
writer[i] = m_ObservationBuffer[i]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is implicitly zero padded, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the assumption is that if all the floats in an obs are 0, then the observation is padded. This padding will be ignored by the trainers, but still fed as input during inference / training. |
||
} | ||
return m_ObsSize * m_MaxNumObs; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual byte[] GetCompressedObservation() | ||
{ | ||
return null; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Update() | ||
{ | ||
Reset(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Reset() | ||
{ | ||
m_CurrentNumObservables = 0; | ||
Array.Clear(m_ObservationBuffer, 0, m_ObservationBuffer.Length); | ||
} | ||
|
||
public SensorCompressionType GetCompressionType() | ||
{ | ||
return SensorCompressionType.None; | ||
} | ||
|
||
public string GetName() | ||
{ | ||
return "BufferSensor"; | ||
} | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using UnityEngine; | ||
|
||
namespace Unity.MLAgents.Sensors | ||
{ | ||
|
||
/// <summary> | ||
/// A component for BufferSensor. | ||
/// </summary> | ||
[AddComponentMenu("ML Agents/Buffer Sensor", (int)MenuGroup.Sensors)] | ||
public class BufferSensorComponent : SensorComponent | ||
{ | ||
public int ObservableSize; | ||
public int MaxNumObservables; | ||
private BufferSensor m_Sensor; | ||
|
||
/// <inheritdoc/> | ||
public override ISensor CreateSensor() | ||
{ | ||
m_Sensor = new BufferSensor(MaxNumObservables, ObservableSize); | ||
return m_Sensor; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override int[] GetObservationShape() | ||
{ | ||
return new[] { MaxNumObservables, ObservableSize }; | ||
} | ||
|
||
/// <summary> | ||
/// Appends an observation to the buffer. If the buffer is full (maximum number | ||
/// of observation is reached) the observation will be ignored. the length of | ||
/// the provided observation array must be equal to the observation size of | ||
/// the buffer sensor. | ||
/// </summary> | ||
/// <param name="obs"> The float array observation</param> | ||
public void AppendObservation(float[] obs) | ||
{ | ||
m_Sensor.AppendObservation(obs); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace Unity.MLAgents.Sensors | ||
{ | ||
|
||
/// <summary> | ||
/// The Dimension property flags of the observations | ||
/// </summary> | ||
[System.Flags] | ||
public enum DimensionProperty | ||
{ | ||
/// <summary> | ||
/// No properties specified. | ||
/// </summary> | ||
Unspecified = 0, | ||
|
||
/// <summary> | ||
/// No Property of the observation in that dimension. Observation can be processed with | ||
/// fully connected networks. | ||
/// </summary> | ||
None = 1, | ||
|
||
/// <summary> | ||
/// Means it is suitable to do a convolution in this dimension. | ||
/// </summary> | ||
TranslationalEquivariance = 2, | ||
|
||
/// <summary> | ||
/// Means that there can be a variable number of observations in this dimension. | ||
/// The observations are unordered. | ||
/// </summary> | ||
VariableSize = 4, | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Sensor interface for sensors with special dimension properties. | ||
/// </summary> | ||
public interface IDimensionPropertiesSensor | ||
{ | ||
/// <summary> | ||
/// Returns the array containing the properties of each dimensions of the | ||
/// observation. The length of the array must be equal to the rank of the | ||
/// observation tensor. | ||
/// </summary> | ||
/// <returns>The array of DimensionProperty</returns> | ||
DimensionProperty[] GetDimensionProperties(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.