Skip to content
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

made a better error if a placeholder is missing or if a placeholder is … #27

Merged
merged 2 commits into from
Sep 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public enum tensorType
public string[] ObservationPlaceholderName;
/// Modify only in inspector : Name of the action node
public string ActionPlaceholderName = "action";
#if ENABLE_TENSORFLOW
#if ENABLE_TENSORFLOW
TFGraph graph;
TFSession session;
bool hasRecurrent;
Expand All @@ -62,7 +62,7 @@ public enum tensorType
float[,] inputState;
List<float[,,,]> observationMatrixList;
float[,] inputOldMemories;
#endif
#endif

/// Reference to the brain that uses this CoreBrainInternal
public Brain brain;
Expand Down Expand Up @@ -190,13 +190,22 @@ public void DecideAction()

foreach (TensorFlowAgentPlaceholder placeholder in graphPlaceholders)
{
if (placeholder.valueType == TensorFlowAgentPlaceholder.tensorType.FloatingPoint)
try
{
runner.AddInput(graph[graphScope + placeholder.name][0], new float[] { Random.Range(placeholder.minValue, placeholder.maxValue) });
if (placeholder.valueType == TensorFlowAgentPlaceholder.tensorType.FloatingPoint)
{
runner.AddInput(graph[graphScope + placeholder.name][0], new float[] { Random.Range(placeholder.minValue, placeholder.maxValue) });
}
else if (placeholder.valueType == TensorFlowAgentPlaceholder.tensorType.Integer)
{
runner.AddInput(graph[graphScope + placeholder.name][0], new int[] { Random.Range((int)placeholder.minValue, (int)placeholder.maxValue + 1) });
}
}
else if (placeholder.valueType == TensorFlowAgentPlaceholder.tensorType.Integer)
catch
{
runner.AddInput(graph[graphScope + placeholder.name][0], new int[] { Random.Range((int)placeholder.minValue, (int)placeholder.maxValue + 1) });
throw new UnityAgentsException(string.Format(@"One of the Tensorflow placeholder cound nout be found.
In brain {0}, there are no {1} placeholder named {2}.",
brain.gameObject.name, placeholder.valueType.ToString(), graphScope + placeholder.name));
}
}

Expand All @@ -212,6 +221,26 @@ public void DecideAction()
runner.AddInput(graph[graphScope + ObservationPlaceholderName[obs_number]][0], observationMatrixList[obs_number]);
}

TFTensor[] networkOutput;
try
{
networkOutput = runner.Run();
}
catch (TFException e)
{
string errorMessage = e.Message;
try
{
errorMessage = string.Format(@"The tensorflow graph needs an input for {0} of type {1}",
e.Message.Split(new string[]{ "Node: " }, 0)[1].Split('=')[0],
e.Message.Split(new string[]{ "dtype=" }, 0)[1].Split(',')[0]);
}
finally
{
throw new UnityAgentsException(errorMessage);
}

}

// Create the recurrent tensor
if (hasRecurrent)
Expand All @@ -220,7 +249,7 @@ public void DecideAction()

runner.AddInput(graph[graphScope + RecurrentInPlaceholderName][0], inputOldMemories);
runner.Fetch(graph[graphScope + RecurrentOutPlaceholderName][0]);
float[,] recurrent_tensor = runner.Run()[1].GetValue() as float[,];
float[,] recurrent_tensor = networkOutput[1].GetValue() as float[,];

int i = 0;
foreach (int k in agentKeys)
Expand All @@ -241,7 +270,7 @@ public void DecideAction()

if (brain.brainParameters.actionSpaceType == StateType.continuous)
{
float[,] output = runner.Run()[0].GetValue() as float[,];
float[,] output = networkOutput[0].GetValue() as float[,];
int i = 0;
foreach (int k in agentKeys)
{
Expand All @@ -256,7 +285,7 @@ public void DecideAction()
}
else if (brain.brainParameters.actionSpaceType == StateType.discrete)
{
long[,] output = runner.Run()[0].GetValue() as long[,];
long[,] output = networkOutput[0].GetValue() as long[,];
int i = 0;
foreach (int k in agentKeys)
{
Expand Down