public static void ResizeTensor(TensorProxy tensor, int batch, ITensorAllocator allocator) { if (tensor.shape[0] == batch && tensor.data != null && tensor.data.batch == batch) { return; } tensor.data?.Dispose(); tensor.shape[0] = batch; if (tensor.shape.Length == 4) { tensor.data = allocator.Alloc( new TensorShape( batch, (int)tensor.shape[1], (int)tensor.shape[2], (int)tensor.shape[3])); } else { tensor.data = allocator.Alloc( new TensorShape( batch, (int)tensor.shape[tensor.shape.Length - 1])); } }
public void Apply(TensorProxy tensorProxy, IEnumerable <AgentIdActionPair> actions) { //var tensorDataProbabilities = tensorProxy.Data as float[,]; var idActionPairList = actions as List <AgentIdActionPair> ?? actions.ToList(); var batchSize = idActionPairList.Count; var actionValues = new float[batchSize, m_ActionSize.Length]; var startActionIndices = Utilities.CumSum(m_ActionSize); for (var actionIndex = 0; actionIndex < m_ActionSize.Length; actionIndex++) { var nBranchAction = m_ActionSize[actionIndex]; var actionProbs = new TensorProxy() { valueType = TensorProxy.TensorType.FloatingPoint, shape = new long[] { batchSize, nBranchAction }, data = m_Allocator.Alloc(new TensorShape(batchSize, nBranchAction)) }; for (var batchIndex = 0; batchIndex < batchSize; batchIndex++) { for (var branchActionIndex = 0; branchActionIndex < nBranchAction; branchActionIndex++) { actionProbs.data[batchIndex, branchActionIndex] = tensorProxy.data[batchIndex, startActionIndices[actionIndex] + branchActionIndex]; } } var outputTensor = new TensorProxy() { valueType = TensorProxy.TensorType.FloatingPoint, shape = new long[] { batchSize, 1 }, data = m_Allocator.Alloc(new TensorShape(batchSize, 1)) }; Eval(actionProbs, outputTensor, m_Multinomial); for (var ii = 0; ii < batchSize; ii++) { actionValues[ii, actionIndex] = outputTensor.data[ii, 0]; } actionProbs.data.Dispose(); outputTensor.data.Dispose(); } var agentIndex = 0; foreach (var idActionPair in idActionPairList) { var actionVal = new float[m_ActionSize.Length]; for (var j = 0; j < m_ActionSize.Length; j++) { actionVal[j] = actionValues[agentIndex, j]; } idActionPair.action.Invoke(new AgentAction { vectorActions = actionVal }); agentIndex++; } }
public void Apply(TensorProxy tensorProxy, Dictionary <Agent, AgentInfo> agentInfo) { //var tensorDataProbabilities = tensorProxy.Data as float[,]; var batchSize = agentInfo.Keys.Count; var actions = new float[batchSize, m_ActionSize.Length]; var startActionIndices = Utilities.CumSum(m_ActionSize); for (var actionIndex = 0; actionIndex < m_ActionSize.Length; actionIndex++) { var nBranchAction = m_ActionSize[actionIndex]; var actionProbs = new TensorProxy() { valueType = TensorProxy.TensorType.FloatingPoint, shape = new long[] { batchSize, nBranchAction }, data = m_Allocator.Alloc(new TensorShape(batchSize, nBranchAction)) }; for (var batchIndex = 0; batchIndex < batchSize; batchIndex++) { for (var branchActionIndex = 0; branchActionIndex < nBranchAction; branchActionIndex++) { actionProbs.data[batchIndex, branchActionIndex] = tensorProxy.data[batchIndex, startActionIndices[actionIndex] + branchActionIndex]; } } var outputTensor = new TensorProxy() { valueType = TensorProxy.TensorType.FloatingPoint, shape = new long[] { batchSize, 1 }, data = m_Allocator.Alloc(new TensorShape(batchSize, 1)) }; Eval(actionProbs, outputTensor, m_Multinomial); for (var ii = 0; ii < batchSize; ii++) { actions[ii, actionIndex] = outputTensor.data[ii, 0]; } actionProbs.data.Dispose(); outputTensor.data.Dispose(); } var agentIndex = 0; foreach (var agent in agentInfo.Keys) { var action = new float[m_ActionSize.Length]; for (var j = 0; j < m_ActionSize.Length; j++) { action[j] = actions[agentIndex, j]; } agent.UpdateVectorAction(action); agentIndex++; } }
public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable <AgentInfoSensorsPair> infos) { tensorProxy.shape = new long[0]; tensorProxy.data?.Dispose(); tensorProxy.data = m_Allocator.Alloc(new TensorShape(1, 1)); tensorProxy.data[0] = 1; }
public void Generate(TensorProxy tensorProxy, int batchSize, Dictionary <Agent, AgentInfo> agentInfo) { tensorProxy.shape = new long[0]; tensorProxy.data?.Dispose(); tensorProxy.data = m_Allocator.Alloc(new TensorShape(1, 1)); tensorProxy.data[0] = 1; }
// @TODO: choose approach to handle case when tensors after Flatten/Reshape are written into OR taken ownership of // 1) owns data, copy on PrepareCacheForAccess() and PinForWrite() // 2) always copy data in Flatten()/Reshape(), remove from Tensor interface // 2) always copy data in Flatten()/Reshape(), implement ICloneable for GPU ITensorData /// <summary> /// Create a flattened copy of the current Tensor ie of shape [B,1,1,H*W*CH] /// </summary> public Tensor Flatten() { var newShape = shape.Flatten(); Tensor copy; if (m_TensorAllocator != null) { copy = m_TensorAllocator.Alloc(newShape, m_TensorOnDevice); } else { copy = new Tensor(newShape, m_TensorOnDevice); } copy.name = $"flatten of {name}"; copy.m_Cache = m_Cache; copy.m_CacheIsDirty = m_CacheIsDirty; return(copy); }
// @TODO: choose approach to handle case when tensors after Flatten/Reshape are written into OR taken ownership of // 1) owns data, copy on PrepareCacheForAccess() and PinForWrite() // 2) always copy data in Flatten()/Reshape(), remove from Tensor interface // 2) always copy data in Flatten()/Reshape(), implement ICloneable for GPU ITensorData private Tensor ShallowCopy(TensorShape newShape, string newName) { Tensor copy; if (m_TensorAllocator != null) { copy = m_TensorAllocator.Alloc(newShape, m_TensorOnDevice); } else { copy = new Tensor(newShape, m_TensorOnDevice); } copy.name = newName; copy.m_Cache = m_Cache; copy.m_CacheIsDirty = m_CacheIsDirty; return(copy); }
public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable <Agent> agents) { tensorProxy.data?.Dispose(); tensorProxy.data = m_Allocator.Alloc(new TensorShape(1, 1)); tensorProxy.data[0] = batchSize; }
public void Generate(TensorProxy tensorProxy, int batchSize, IList <AgentInfoSensorsPair> infos) { tensorProxy.data?.Dispose(); tensorProxy.data = m_Allocator.Alloc(new TensorShape(1, 1)); tensorProxy.data[0] = batchSize; }
public void Apply(TensorProxy tensorProxy, IEnumerable <int> actionIds, Dictionary <int, float[]> lastActions) { //var tensorDataProbabilities = tensorProxy.Data as float[,]; var idActionPairList = actionIds as List <int> ?? actionIds.ToList(); var batchSize = idActionPairList.Count; var actionValues = new float[batchSize, m_ActionSize.Length]; var startActionIndices = Utilities.CumSum(m_ActionSize); for (var actionIndex = 0; actionIndex < m_ActionSize.Length; actionIndex++) { var nBranchAction = m_ActionSize[actionIndex]; var actionProbs = new TensorProxy() { valueType = TensorProxy.TensorType.FloatingPoint, shape = new long[] { batchSize, nBranchAction }, data = m_Allocator.Alloc(new TensorShape(batchSize, nBranchAction)) }; for (var batchIndex = 0; batchIndex < batchSize; batchIndex++) { for (var branchActionIndex = 0; branchActionIndex < nBranchAction; branchActionIndex++) { actionProbs.data[batchIndex, branchActionIndex] = tensorProxy.data[batchIndex, startActionIndices[actionIndex] + branchActionIndex]; } } var outputTensor = new TensorProxy() { valueType = TensorProxy.TensorType.FloatingPoint, shape = new long[] { batchSize, 1 }, data = m_Allocator.Alloc(new TensorShape(batchSize, 1)) }; Eval(actionProbs, outputTensor, m_Multinomial); for (var ii = 0; ii < batchSize; ii++) { actionValues[ii, actionIndex] = outputTensor.data[ii, 0]; } actionProbs.data.Dispose(); outputTensor.data.Dispose(); } var agentIndex = 0; foreach (int agentId in actionIds) { if (lastActions.ContainsKey(agentId)) { var actionVal = lastActions[agentId]; if (actionVal == null) { actionVal = new float[m_ActionSize.Length]; lastActions[agentId] = actionVal; } for (var j = 0; j < m_ActionSize.Length; j++) { actionVal[j] = actionValues[agentIndex, j]; } } agentIndex++; } }
public void Generate(TensorProxy tensorProxy, int batchSize, Dictionary <Agent, AgentInfo> agentInfo) { tensorProxy.Data = _allocator.Alloc(new TensorShape(1, 1)); tensorProxy.Data[0] = batchSize; }