Class ObservableAttribute
Specify that a field or property should be used to generate observations for an Agent. For each field or property that uses ObservableAttribute, a corresponding ISensor will be created during Agent initialization, and this sensor will read the values during training and inference.
Namespace: Unity.MLAgents.Sensors.Reflection
Syntax
public class ObservableAttribute : Attribute
Remarks
ObservableAttribute is intended to make initial setup of an Agent easier. Because it uses reflection to read the values of fields and properties at runtime, this may be much slower than reading the values directly. If the performance of ObservableAttribute is an issue, you can get the same functionality by overriding CollectObservations(VectorSensor) or creating a custom ISensor implementation to read the values without reflection.
Note that you do not need to adjust the VectorObservationSize in BrainParameters when adding ObservableAttribute to fields or properties.
Examples
This sample class will produce two observations, one for the m_Health field, and one for the HealthPercent property.
using Unity.MLAgents;
using Unity.MLAgents.Sensors.Reflection;
public class MyAgent : Agent
{
[Observable]
int m_Health;
[Observable]
float HealthPercent
{
get => return 100.0f * m_Health / float(m_MaxHealth);
}
}
Constructors
ObservableAttribute(String, Int32)
ObservableAttribute constructor.
Declaration
public ObservableAttribute(string name = null, int numStackedObservations = 1)
Parameters
Type | Name | Description |
---|---|---|
String | name | Optional override for the sensor name. Note that all sensors for an Agent must have a unique name. |
Int32 | numStackedObservations | Number of frames to concatenate observations from. |