Clear Filters
Clear Filters

how to construct a batch for multiple trajectories in neural network

4 views (last 30 days)
If we have more than one trajectory and want to construct a batch for all these trajectories what will be the possible way to use all the trajectroies for the batch in neural network?
e.g this will work for multiple trajectories?
function [x0, targets] = createMiniBatch(numTimesteps,numTimesPerObs,miniBatchSize,X)
% e.g: numTimesteps=20;numTimesPerObs=5; miniBatchSize=10;
% Create batches of trajectories.
s = randperm(numTimesteps - numTimesPerObs, miniBatchSize);
x0 = dlarray(X(:, s));
targets = zeros([size(X,1) miniBatchSize numTimesPerObs]);
for i = 1:miniBatchSize
targets(:, i, 1:numTimesPerObs) = X(:, s(i) + 1:(s(i) + numTimesPerObs));
end
end
or there is another way for batch for multiple trajectroies

Accepted Answer

Shubham
Shubham on 6 Feb 2024
Hi Muhammad,
The function you provided seems to be designed for creating mini-batches from a single long trajectory X, where X is a matrix with each column representing a timestep. The function selects random starting points s and creates input sequences x0 and target sequences targets for training a neural network.
However, if you have multiple trajectories and you want to create mini-batches that include samples from all these trajectories, you need to modify the function to handle a cell array of trajectories or a 3D array where each slice along the third dimension represents a different trajectory.
function [x0, targets] = createMiniBatch(numTimesteps, numTimesPerObs, miniBatchSize, trajectories)
% e.g: numTimesteps=20; numTimesPerObs=5; miniBatchSize=10;
% trajectories is a cell array where each cell contains a matrix for a trajectory
% Calculate the total number of trajectories
numTrajectories = numel(trajectories);
% Preallocate arrays for inputs and targets
inputSize = size(trajectories{1}, 1); % Assuming all trajectories have the same number of features
x0 = dlarray(zeros(inputSize, miniBatchSize));
targets = zeros(inputSize, miniBatchSize, numTimesPerObs);
for i = 1:miniBatchSize
% Randomly select a trajectory
trajIndex = randi(numTrajectories);
% Randomly select a starting point within the trajectory
maxStartIndex = size(trajectories{trajIndex}, 2) - numTimesPerObs;
startIndex = randi(maxStartIndex);
% Extract the sequence of data for the current mini-batch
x0(:, i) = dlarray(trajectories{trajIndex}(:, startIndex));
targets(:, i, :) = trajectories{trajIndex}(:, startIndex+1:startIndex+numTimesPerObs);
end
end
In this modified function:
  • trajectories is a cell array where each cell contains a matrix for a trajectory.
  • The function randomly selects a trajectory and then randomly selects a starting point within that trajectory for each sample in the mini-batch.
This approach allows you to create mini-batches that contain sequences from different trajectories, which can be beneficial for training neural networks on more varied data.
Remember to ensure that all trajectories have enough timesteps (size(trajectories{trajIndex}, 2) > numTimesPerObs) to avoid indexing errors. If your trajectories have different numbers of features or timesteps, you may need to further modify the function to accommodate these differences.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!