You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity ML-Agents新增观测后Sensor Shape不匹配问题求助

解决ML-Agents观测数不匹配错误

问题背景

基于排球项目修改Unity ML-Agents Agent时,新增7个队友位置相关观测(原11个,现总18个),修改后的CollectObservations代码如下:

public override void CollectObservations(VectorSensor sensor)
{
    // Agent rotation (1 float)
    sensor.AddObservation(this.transform.rotation.y);

    // Vector from agent to ball (direction to ball) (3 floats)
    Vector3 toBall = new Vector3((ballRb.transform.position.x - this.transform.position.x)*agentRot, 
    (ballRb.transform.position.y - this.transform.position.y),
    (ballRb.transform.position.z - this.transform.position.z)*agentRot);

    sensor.AddObservation(toBall.normalized);

    // Distance from the ball (1 float)
    sensor.AddObservation(toBall.magnitude);

    // Agent velocity (3 floats)
    sensor.AddObservation(agentRb.velocity);

    // Ball velocity (3 floats)
    sensor.AddObservation(ballRb.velocity.y);
    sensor.AddObservation(ballRb.velocity.z*agentRot);
    sensor.AddObservation(ballRb.velocity.x*agentRot);

    // Vector from agent to teammate (direction to teammate) (3 floats)
    Vector3 toMate = new Vector3((mateRb.transform.position.x - this.transform.position.x)*agentRot, 
    (mateRb.transform.position.y - this.transform.position.y),
    (mateRb.transform.position.z - this.transform.position.z)*agentRot);

    sensor.AddObservation(toMate.normalized);

    // Distance from the mate (1 float)
    sensor.AddObservation(toMate.magnitude);

    // Mate velocity (3 floats)
    sensor.AddObservation(mateRb.velocity.y);
    sensor.AddObservation(mateRb.velocity.z*agentRot);
    sensor.AddObservation(mateRb.velocity.x*agentRot);
}

运行后出现以下错误和警告:

Sensor shapes must match. [18] != [11]
UnityEngine.Debug:AssertFormat (bool,string,object[])
Unity.MLAgents.Sensors.SensorShapeValidator:ValidateSensors (System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Sensors/SensorShapeValidator.cs:43)
Unity.MLAgents.RpcCommunicator:PutObservations (string,Unity.MLAgents.AgentInfo,System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Communicator/RpcCommunicator.cs:319)
Unity.MLAgents.Policies.RemotePolicy:RequestDecision (Unity.MLAgents.AgentInfo,System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Policies/RemotePolicy.cs:46)
Unity.MLAgents.Agent:SendInfoToBrain () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Agent.cs:1096)
Unity.MLAgents.Agent:SendInfo () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Agent.cs:1323)
Unity.MLAgents.Academy:EnvironmentStep () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Academy.cs:573)
Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Academy.cs:43)

More observations (18) made than vector observation size (11). The observations will be truncated.
UnityEngine.Debug:LogWarningFormat (string,object[])
Unity.MLAgents.Sensors.VectorSensor:Write (Unity.MLAgents.Sensors.ObservationWriter) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Sensors/VectorSensor.cs:47)
Unity.MLAgents.GrpcExtensions:GetObservationProto (Unity.MLAgents.Sensors.ISensor,Unity.MLAgents.Sensors.ObservationWriter) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Communicator/GrpcExtensions.cs:399)
Unity.MLAgents.RpcCommunicator:PutObservations (string,Unity.MLAgents.AgentInfo,System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Communicator/RpcCommunicator.cs:330)
Unity.MLAgents.Policies.RemotePolicy:RequestDecision (Unity.MLAgents.AgentInfo,System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Policies/RemotePolicy.cs:46)
Unity.MLAgents.Agent:SendInfoToBrain () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Agent.cs:1096)
Unity.MLAgents.Agent:SendInfo () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Agent.cs:1323)
Unity.MLAgents.Academy:EnvironmentStep () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Academy.cs:573)
Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Academy.cs:43)

错误原因

ML-Agents中Agent的Vector Observation Size配置仍为原项目的11,但实际代码中收集了18个观测值,导致传感器形状验证失败,多余的观测被截断。

解决步骤

  1. 在Unity编辑器中选中你的Agent GameObject
  2. 在Inspector面板找到Behavior Parameters组件
  3. 展开Vector Observation选项,将Space Size的值从11修改为18(与实际收集的观测总数一致)
  4. 保存场景后重新运行项目

额外注意事项

  • 若已训练过模型,修改观测维度后需要重新训练,旧模型因输入维度不兼容无法继续使用
  • 确保场景中所有同类型的Agent都同步修改该参数,避免出现观测维度不一致的问题

内容的提问来源于stack exchange,提问作者Filip Byberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 21:14:58