in rela/transition.cc [160:202]
RNNTransition RNNTransition::makeBatch(
const std::vector<RNNTransition>& transitions, const std::string& device) {
std::vector<TensorDict> obsVec;
// TensorVecDict h0Vec;
std::vector<TensorDict> actionVec;
std::vector<torch::Tensor> rewardVec;
std::vector<torch::Tensor> terminalVec;
std::vector<torch::Tensor> bootstrapVec;
std::vector<torch::Tensor> seqLenVec;
for (size_t i = 0; i < transitions.size(); i++) {
obsVec.push_back(transitions[i].obs);
// utils::tensorVecDictAppend(h0Vec, transitions[i].h0);
actionVec.push_back(transitions[i].action);
rewardVec.push_back(transitions[i].reward);
terminalVec.push_back(transitions[i].terminal);
bootstrapVec.push_back(transitions[i].bootstrap);
seqLenVec.push_back(transitions[i].seqLen);
}
RNNTransition batch;
batch.obs = tensor_dict::stack(obsVec, 1);
// batch.h0 = tensor_dict::stack(h0Vec, 1); // 1 is batch for rnn hid
batch.action = tensor_dict::stack(actionVec, 1);
batch.reward = torch::stack(rewardVec, 1);
batch.terminal = torch::stack(terminalVec, 1);
batch.bootstrap = torch::stack(bootstrapVec, 1);
batch.seqLen = torch::stack(seqLenVec, 0); //.squeeze(1);
if (device != "cpu") {
auto d = torch::Device(device);
auto toDevice = [&](const torch::Tensor& t) { return t.to(d); };
batch.obs = tensor_dict::apply(batch.obs, toDevice);
// batch.h0 = tensor_dict::apply(batch.h0, toDevice);
batch.action = tensor_dict::apply(batch.action, toDevice);
batch.reward = batch.reward.to(d);
batch.terminal = batch.terminal.to(d);
batch.bootstrap = batch.bootstrap.to(d);
batch.seqLen = batch.seqLen.to(d);
}
return batch;
}