private StateBase getData(Phase phase, int nAction) { GetDataArgs args = m_brain.getDataArgs(phase, 0, nAction, true); m_icallback.OnGetData(args); return(args.State); }
private StateBase getData(int nAction) { GetDataArgs args = m_brain.getDataArgs(nAction); m_icallback.OnGetData(args); return(args.State); }
private StateBase getData(Phase phase, int nAction, int nIdx) { GetDataArgs args = m_brain.getDataArgs(phase, nAction); m_icallback.OnGetData(args); args.State.Data.Index = nIdx; return(args.State); }
public async void GetData(GetDataArgs getDataArgs) { var data = await getDataArgs.M.GetDataAsync(getDataArgs._Uri); foreach (var item in data.Dataset.Data) { DataItems items = DataItems.GetItems(item); view.DataGridItems = items; } }
protected override bool getData(GetDataArgs e) { Tuple <State, double, bool> state = null; if (e.Reset) { if (m_firststate != null) { state = m_firststate; m_firststate = null; } else { state = m_igym.Reset(); } } if (e.Action >= 0) { state = m_igym.Step(e.Action); } bool bIsOpen = (m_nUiId >= 0) ? true : false; int nDataLen = 0; SimpleDatum stateData = state.Item1.GetData(false, out nDataLen); e.State = new StateBase(m_igym.GetActionSpace().Count()); e.State.Reward = 0; e.State.Data = stateData; e.State.Done = state.Item3; e.State.IsValid = true; if (m_sw.Elapsed.TotalMilliseconds > 1000) { int nMax = (int)GetProperty("GlobalMaxIterations"); int nIteration = (int)GetProperty("GlobalIteration"); double dfPct = (nMax == 0) ? 0 : (double)nIteration / (double)nMax; e.OutputLog.Progress = dfPct; e.OutputLog.WriteLine("(" + dfPct.ToString("P") + ") Global Iteration #" + nIteration.ToString()); m_sw.Restart(); } return(true); }
protected override bool getData(GetDataArgs e) { Tuple <State, double, bool> state = null; if (e.Reset) { state = m_igym.Reset(); } if (e.Action >= 0) { state = m_igym.Step(e.Action); } bool bIsOpen = (m_nUiId >= 0) ? true : false; Tuple <Bitmap, SimpleDatum> data = m_igym.Render(bIsOpen, 512, 512, true); int nDataLen = 0; SimpleDatum stateData = state.Item1.GetData(false, out nDataLen); Observation obs = new Observation(data.Item1, ImageData.GetImage(data.Item2), m_igym.RequiresDisplayImage, stateData.RealData, state.Item2, state.Item3); e.State = new StateBase(m_igym.GetActionSpace().Count()); e.State.Reward = obs.Reward; e.State.Data = data.Item2; e.State.Done = obs.Done; e.State.IsValid = true; if (m_gymui != null && m_nUiId >= 0) { m_gymui.Render(m_nUiId, obs); Thread.Sleep(m_igym.UiDelay); } if (m_sw.Elapsed.TotalMilliseconds > 1000) { double dfPct = (GlobalEpisodeMax == 0) ? 0 : (double)GlobalEpisodeCount / (double)GlobalEpisodeMax; e.OutputLog.Progress = dfPct; e.OutputLog.WriteLine("(" + dfPct.ToString("P") + ") Global Episode #" + GlobalEpisodeCount.ToString() + " Global Reward = " + GlobalRewards.ToString() + " Exploration Rate = " + ExplorationRate.ToString("P") + " Optimal Selection Rate = " + OptimalSelectionRate.ToString("P")); m_sw.Restart(); } return(true); }
public float[] Run(int nN) { try { Stopwatch sw = new Stopwatch(); float[] rgPredictions = new float[nN]; sw.Start(); m_bIsDataReal = true; if (m_rgVocabulary != null) { m_bIsDataReal = m_rgVocabulary.IsDataReal; } m_mycaffe.Log.Enable = false; if (m_bIsDataReal && !m_bUsePreloadData) { string strSolverErr = ""; int nLookahead = 1; if (m_nSolverSequenceLength >= 0 && m_nSolverSequenceLength < m_nSequenceLength) { nLookahead = m_nSequenceLength - m_nSolverSequenceLength; } rgPredictions = new float[nN * 2 * nLookahead]; for (int i = 0; i < nN; i++) { GetDataArgs e = getDataArgs(Phase.RUN, 0, 0, true); m_icallback.OnGetData(e); int nExpectedCount = m_blobData.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, e.State.Data.ItemCount, strSolverErr + "The size of the data received ('" + e.State.Data.ItemCount.ToString() + "') does mot match the expected data count of '" + nExpectedCount.ToString() + "'!"); m_blobData.mutable_cpu_data = e.State.Data.GetData <T>(); if (m_blobLabel != null) { nExpectedCount = m_blobLabel.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, e.State.Label.ItemCount, strSolverErr + "The size of the label received ('" + e.State.Label.ItemCount.ToString() + "') does not match the expected label count of '" + nExpectedCount.ToString() + "'!"); m_blobLabel.mutable_cpu_data = e.State.Label.GetData <T>(); } double dfLoss; BlobCollection <T> colResults = m_net.Forward(out dfLoss); Blob <T> blobOutput = colResults[0]; if (m_blobOutput != null) { blobOutput = m_blobOutput; } float[] rgResults = Utility.ConvertVecF <T>(blobOutput.update_cpu_data()); for (int j = nLookahead; j > 0; j--) { float fPrediction = getLastPrediction(rgResults, m_rgVocabulary, j); int nIdx = e.State.Label.ItemCount - j; float fActual = (float)e.State.Label.GetDataAtF(nIdx); int nIdx0 = ((nLookahead - j) * nN * 2); int nIdx1 = nIdx0 + nN; if (m_dfScale != 1.0 && m_dfScale > 0) { fActual /= (float)m_dfScale; } if (m_rgVocabulary == null || m_bDisableVocabulary) { if (m_dfScale != 1.0 && m_dfScale > 0) { fPrediction /= (float)m_dfScale; } rgPredictions[nIdx0 + i] = fPrediction; rgPredictions[nIdx1 + i] = fActual; } else { rgPredictions[nIdx0 + i] = (float)m_rgVocabulary.GetValueAt((int)fPrediction, true); rgPredictions[nIdx1 + i] = (float)m_rgVocabulary.GetValueAt((int)fActual, true); } } if (sw.Elapsed.TotalMilliseconds > 1000) { double dfPct = (double)i / (double)nN; m_mycaffe.Log.Enable = true; m_mycaffe.Log.Progress = dfPct; m_mycaffe.Log.WriteLine("Running at " + dfPct.ToString("P") + " complete..."); m_mycaffe.Log.Enable = false; sw.Restart(); } if (m_mycaffe.CancelEvent.WaitOne(0)) { break; } } } else { int nIdx = 0; List <T> rgInput = rgInput = getInitialInput(m_bIsDataReal); for (int i = 0; i < nN; i++) { T[] rgInputVector = new T[m_blobData.count()]; for (int j = 0; j < m_nSequenceLength; j++) { // The batch is filled with 0 except for the first sequence which is the one we want to use for prediction. nIdx = j * m_nBatchSize; rgInputVector[nIdx] = rgInput[j]; } m_blobData.mutable_cpu_data = rgInputVector; double dfLoss; BlobCollection <T> colResults = m_net.Forward(out dfLoss); Blob <T> blobOutput = colResults[0]; if (m_blobOutput != null) { blobOutput = m_blobOutput; } float[] rgResults = Utility.ConvertVecF <T>(blobOutput.update_cpu_data()); float fPrediction = getLastPrediction(rgResults, m_rgVocabulary, 1); //Add the new prediction and discard the oldest one rgInput.Add((T)Convert.ChangeType(fPrediction, typeof(T))); rgInput.RemoveAt(0); if (m_rgVocabulary == null || m_bDisableVocabulary) { rgPredictions[i] = fPrediction; } else { rgPredictions[i] = (float)m_rgVocabulary.GetValueAt((int)fPrediction); } if (sw.Elapsed.TotalMilliseconds > 1000) { double dfPct = (double)i / (double)nN; m_mycaffe.Log.Enable = true; m_mycaffe.Log.Progress = dfPct; m_mycaffe.Log.WriteLine("Running at " + dfPct.ToString("P") + " complete..."); m_mycaffe.Log.Enable = false; sw.Restart(); } if (m_mycaffe.CancelEvent.WaitOne(0)) { break; } } } return(rgPredictions); } catch (Exception excpt) { throw excpt; } finally { m_mycaffe.Log.Enable = true; } }
public void FeedNet(bool bTrain) { bool bFound; int nIdx; Phase phase = (bTrain) ? Phase.TRAIN : Phase.TEST; // Real Data if (m_bIsDataReal) { if (m_bUsePreloadData) { double[] rgdfData = (bTrain) ? m_rgdfTrainData : m_rgdfTestData; // Re-order the data according to caffe input specification for LSTM layer. for (int i = 0; i < m_nBatchSize; i++) { int nCurrentValIdx = m_random.Next(rgdfData.Length - m_nSequenceLength - 1); for (int j = 0; j < m_nSequenceLength; j++) { // Feed the net with input data and labels (clips are always the same) double dfData = rgdfData[nCurrentValIdx + j]; // Labels are the same with an offset of +1 double dfLabel = rgdfData[nCurrentValIdx + j + 1]; // predict next value float fDataIdx = findIndex(dfData, out bFound); float fLabelIdx = findIndex(dfLabel, out bFound); // LSTM: Create input data, the data must be in the order // seq1_val1, seq2_val1, ..., seqBatch_Size_val1, seq1_val2, seq2_val2, ..., seqBatch_Size_valSequence_Length if (m_lstmType == LayerParameter.LayerType.LSTM) { nIdx = m_nBatchSize * j + i; } // LSTM_SIMPLE: Create input data, the data must be in the order // seq1_val1, seq1_val2, ..., seq1_valBatchSize, seq2_val1, seq2_val2, ..., seqSequenceLength_valBatchSize else { nIdx = i * m_nBatchSize + j; } m_rgDataInput[nIdx] = (T)Convert.ChangeType(fDataIdx, typeof(T)); if (m_nSequenceLengthLabel == (m_nSequenceLength * m_nBatchSize) || j == m_nSequenceLength - 1) { m_rgLabelInput[nIdx] = (T)Convert.ChangeType(fLabelIdx, typeof(T)); } } } m_blobData.mutable_cpu_data = m_rgDataInput; m_blobLabel.mutable_cpu_data = m_rgLabelInput; } else { m_mycaffe.Log.CHECK_EQ(m_nBatchSize, m_nThreads, "The 'Threads' setting of " + m_nThreads.ToString() + " must match the batch size = " + m_nBatchSize.ToString() + "!"); List <GetDataArgs> rgDataArgs = new List <GetDataArgs>(); if (m_nBatchSize == 1) { GetDataArgs e = getDataArgs(phase, 0, 0, true, m_nBatchSize); m_icallback.OnGetData(e); rgDataArgs.Add(e); } else { for (int i = 0; i < m_nBatchSize; i++) { rgDataArgs.Add(getDataArgs(phase, i, 0, true, m_nBatchSize)); } if (!m_dataPool.Run(rgDataArgs)) { m_mycaffe.Log.FAIL("Data Time Out - Failed to collect all data to build the RNN batch!"); } } double[] rgData = rgDataArgs[0].State.Data.GetData <double>(); double[] rgLabel = rgDataArgs[0].State.Label.GetData <double>(); double[] rgClip = rgDataArgs[0].State.Clip.GetData <double>(); int nDataLen = rgData.Length; int nLabelLen = rgLabel.Length; int nClipLen = rgClip.Length; int nDataItem = nDataLen / nLabelLen; if (m_nBatchSize > 1) { rgData = new double[nDataLen * m_nBatchSize]; rgLabel = new double[nLabelLen * m_nBatchSize]; rgClip = new double[nClipLen * m_nBatchSize]; for (int i = 0; i < m_nBatchSize; i++) { for (int j = 0; j < m_nSequenceLength; j++) { // LSTM: Create input data, the data must be in the order // seq1_val1, seq2_val1, ..., seqBatch_Size_val1, seq1_val2, seq2_val2, ..., seqBatch_Size_valSequence_Length if (m_lstmType == LayerParameter.LayerType.LSTM) { nIdx = m_nBatchSize * j + i; } // LSTM_SIMPLE: Create input data, the data must be in the order // seq1_val1, seq1_val2, ..., seq1_valBatchSize, seq2_val1, seq2_val2, ..., seqSequenceLength_valBatchSize else { nIdx = i * m_nBatchSize + j; } Array.Copy(rgDataArgs[i].State.Data.GetData <double>(), 0, rgData, nIdx * nDataItem, nDataItem); rgLabel[nIdx] = rgDataArgs[i].State.Label.GetDataAtD(j); rgClip[nIdx] = rgDataArgs[i].State.Clip.GetDataAtD(j); } } } string strSolverErr = ""; if (m_nSolverSequenceLength >= 0 && m_nSolverSequenceLength != m_nSequenceLength) { strSolverErr = "The solver parameter 'SequenceLength' length of " + m_nSolverSequenceLength.ToString() + " must match the model sequence length of " + m_nSequenceLength.ToString() + ". "; } int nExpectedCount = m_blobData.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, rgData.Length, strSolverErr + "The size of the data received ('" + rgData.Length.ToString() + "') does mot match the expected data count of '" + nExpectedCount.ToString() + "'!"); m_blobData.mutable_cpu_data = Utility.ConvertVec <T>(rgData); nExpectedCount = m_blobLabel.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, rgLabel.Length, strSolverErr + "The size of the label received ('" + rgLabel.Length.ToString() + "') does not match the expected label count of '" + nExpectedCount.ToString() + "'!"); m_blobLabel.mutable_cpu_data = Utility.ConvertVec <T>(rgLabel); nExpectedCount = m_blobClip.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, rgClip.Length, strSolverErr + "The size of the clip received ('" + rgClip.Length.ToString() + "') does not match the expected clip count of '" + nExpectedCount.ToString() + "'!"); m_blobClip.mutable_cpu_data = Utility.ConvertVec <T>(rgClip); } } // Byte Data (uses a vocabulary if available) else { byte[] rgData = (bTrain) ? m_rgTrainData : m_rgTestData; // Create input data, the data must be in the order // seq1_char1, seq2_char1, ..., seqBatch_Size_char1, seq1_char2, seq2_char2, ..., seqBatch_Size_charSequence_Length // As seq1_charSequence_Length == seq2_charSequence_Length-1 == seq3_charSequence_Length-2 == ... we can perform block copy for efficientcy. // Labels are the same with an offset of +1 // Re-order the data according to caffe input specification for LSTM layer. for (int i = 0; i < m_nBatchSize; i++) { int nCurrentCharIdx = m_random.Next(rgData.Length - m_nSequenceLength - 2); for (int j = 0; j < m_nSequenceLength; j++) { // Feed the net with input data and labels (clips are always the same) byte bData = rgData[nCurrentCharIdx + j]; // Labels are the same with an offset of +1 byte bLabel = rgData[nCurrentCharIdx + j + 1]; // predict next character float fDataIdx = findIndex(bData, out bFound); float fLabelIdx = findIndex(bLabel, out bFound); // LSTM: Create input data, the data must be in the order // seq1_val1, seq2_val1, ..., seqBatch_Size_val1, seq1_val2, seq2_val2, ..., seqBatch_Size_valSequence_Length if (m_lstmType == LayerParameter.LayerType.LSTM) { nIdx = m_nBatchSize * j + i; } // LSTM_SIMPLE: Create input data, the data must be in the order // seq1_val1, seq1_val2, ..., seq1_valBatchSize, seq2_val1, seq2_val2, ..., seqSequenceLength_valBatchSize else { nIdx = i * m_nBatchSize + j; } m_rgDataInput[nIdx] = (T)Convert.ChangeType(fDataIdx, typeof(T)); if (m_nSequenceLengthLabel == (m_nSequenceLength * m_nBatchSize) || j == m_nSequenceLength - 1) { m_rgLabelInput[nIdx] = (T)Convert.ChangeType(fLabelIdx, typeof(T)); } } } m_blobData.mutable_cpu_data = m_rgDataInput; m_blobLabel.mutable_cpu_data = m_rgLabelInput; } }
public void Run(GetDataArgs args) { m_args = args; m_evtRun.Set(); }
public void FeedNet(bool bTrain) { bool bFound; int nIdx; // Real Data if (m_bIsDataReal) { if (m_bUsePreloadData) { double[] rgdfData = (bTrain) ? m_rgdfTrainData : m_rgdfTestData; // Re-order the data according to caffe input specification for LSTM layer. for (int i = 0; i < m_nBatchSize; i++) { int nCurrentValIdx = m_random.Next(rgdfData.Length - m_nSequenceLength - 1); for (int j = 0; j < m_nSequenceLength; j++) { // Feed the net with input data and labels (clips are always the same) double dfData = rgdfData[nCurrentValIdx + j]; // Labels are the same with an offset of +1 double dfLabel = rgdfData[nCurrentValIdx + j + 1]; // predict next value float fDataIdx = findIndex(dfData, out bFound); float fLabelIdx = findIndex(dfLabel, out bFound); // LSTM: Create input data, the data must be in the order // seq1_val1, seq2_val1, ..., seqBatch_Size_val1, seq1_val2, seq2_val2, ..., seqBatch_Size_valSequence_Length if (m_lstmType == LayerParameter.LayerType.LSTM) { nIdx = m_nBatchSize * j + i; } // LSTM_SIMPLE: Create input data, the data must be in the order // seq1_val1, seq1_val2, ..., seq1_valBatchSize, seq2_val1, seq2_val2, ..., seqSequenceLength_valBatchSize else { nIdx = i * m_nBatchSize + j; } m_rgDataInput[nIdx] = (T)Convert.ChangeType(fDataIdx, typeof(T)); if (m_nSequenceLengthLabel == (m_nSequenceLength * m_nBatchSize) || j == m_nSequenceLength - 1) { m_rgLabelInput[nIdx] = (T)Convert.ChangeType(fLabelIdx, typeof(T)); } } } m_blobData.mutable_cpu_data = m_rgDataInput; m_blobLabel.mutable_cpu_data = m_rgLabelInput; } else { GetDataArgs e = getDataArgs(0, true); m_icallback.OnGetData(e); string strSolverErr = ""; if (m_nSolverSequenceLength >= 0 && m_nSolverSequenceLength != m_nSequenceLength) { strSolverErr = "The solver parameter 'SequenceLength' length of " + m_nSolverSequenceLength.ToString() + " must match the model sequence length of " + m_nSequenceLength.ToString() + ". "; } int nExpectedCount = m_blobData.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, e.State.Data.ItemCount, strSolverErr + "The size of the data received ('" + e.State.Data.ItemCount.ToString() + "') does mot match the expected data count of '" + nExpectedCount.ToString() + "'!"); m_blobData.mutable_cpu_data = Utility.ConvertVec <T>(e.State.Data.RealData); nExpectedCount = m_blobLabel.count(); m_mycaffe.Log.CHECK_EQ(nExpectedCount, e.State.Label.ItemCount, strSolverErr + "The size of the label received ('" + e.State.Label.ItemCount.ToString() + "') does not match the expected label count of '" + nExpectedCount.ToString() + "'!"); m_blobLabel.mutable_cpu_data = Utility.ConvertVec <T>(e.State.Label.RealData); } } // Byte Data (uses a vocabulary if available) else { byte[] rgData = (bTrain) ? m_rgTrainData : m_rgTestData; // Create input data, the data must be in the order // seq1_char1, seq2_char1, ..., seqBatch_Size_char1, seq1_char2, seq2_char2, ..., seqBatch_Size_charSequence_Length // As seq1_charSequence_Length == seq2_charSequence_Length-1 == seq3_charSequence_Length-2 == ... we can perform block copy for efficientcy. // Labels are the same with an offset of +1 // Re-order the data according to caffe input specification for LSTM layer. for (int i = 0; i < m_nBatchSize; i++) { int nCurrentCharIdx = m_random.Next(rgData.Length - m_nSequenceLength - 1); for (int j = 0; j < m_nSequenceLength; j++) { // Feed the net with input data and labels (clips are always the same) byte bData = rgData[nCurrentCharIdx + j]; // Labels are the same with an offset of +1 byte bLabel = rgData[nCurrentCharIdx + j + 1]; // predict next character float fDataIdx = findIndex(bData, out bFound); float fLabelIdx = findIndex(bLabel, out bFound); // LSTM: Create input data, the data must be in the order // seq1_val1, seq2_val1, ..., seqBatch_Size_val1, seq1_val2, seq2_val2, ..., seqBatch_Size_valSequence_Length if (m_lstmType == LayerParameter.LayerType.LSTM) { nIdx = m_nBatchSize * j + i; } // LSTM_SIMPLE: Create input data, the data must be in the order // seq1_val1, seq1_val2, ..., seq1_valBatchSize, seq2_val1, seq2_val2, ..., seqSequenceLength_valBatchSize else { nIdx = i * m_nBatchSize + j; } m_rgDataInput[nIdx] = (T)Convert.ChangeType(fDataIdx, typeof(T)); if (m_nSequenceLengthLabel == (m_nSequenceLength * m_nBatchSize) || j == m_nSequenceLength - 1) { m_rgLabelInput[nIdx] = (T)Convert.ChangeType(fLabelIdx, typeof(T)); } } } m_blobData.mutable_cpu_data = m_rgDataInput; m_blobLabel.mutable_cpu_data = m_rgLabelInput; } }