spinup/algos/pytorch/ppo_lstm/core_lstm.py
def split_trajectories_episodes(trajectory_tensors, input_data):
    """
    Split trajectories by episode. The return values are episode-wise trajectories. This function split the 2048 trajectories
    into almost 7-10 trajectories, each one is a episode. For the values, the episodic trajectory is appended one in the end.
    """
    parallel_rollouts = input_data["parallel_rollouts"]
    epochs = input_data["epochs"]
    states_episodes, actions_episodes, action_probabilities_episodes = [], [], []
    rewards_episodes, terminal_rewards_episodes, terminals_episodes, values_episodes = [], [], [], []
    policy_hidden_episodes, policy_cell_episodes, critic_hidden_episodes, critic_cell_episodes = [], [], [], []
    len_episodes = []
    trajectory_episodes = {key: [] for key in trajectory_tensors.keys()}
    for i in range(parallel_rollouts):
        terminals_tmp = trajectory_tensors["terminals"].clone()
        terminals_tmp[0, i] = 1
        terminals_tmp[-1, i] = 1
        split_points = (terminals_tmp[:, i] == 1).nonzero() + 1

        split_lens = split_points[1:] - split_points[:-1]
        split_lens[0] += 1

        len_episode = [split_len.item() for split_len in split_lens]
        len_episodes += len_episode
        for key, value in trajectory_tensors.items():
            # Value includes additional step.
            if key == "values":
                value_split = list(torch.split(value[:, i], len_episode[:-1] + [len_episode[-1] + 1]))
                # Append extra 0 to values to represent no future reward.
                for j in range(len(value_split) - 1):
                    value_split[j] = torch.cat((value_split[j], torch.zeros(1)))
                trajectory_episodes[key] += value_split
            else:
                trajectory_episodes[key] += torch.split(value[:, i], len_episode)
    return trajectory_episodes, len_episodes

    Split trajectories by episode. The return values are episode-wise trajectories. This function split the 2048 trajectories
    into almost 7-10 trajectories, each one is a episode. For the values, the episodic trajectory is appended one in the end.
    
Split trajectories by episode. The return values are episode-wise trajectories. This function split the 2048 trajectories
into almost 7-10 trajectories, each one is a episode. For the values, the episodic trajectory is appended one in the end.
Split trajectories by episode . The return values are episode - wise trajectories . This function split the 2048 trajectories into almost 7 - 10 trajectories each one is a episode . For the values the episodic trajectory is appended one in the end .
def split_trajectories_episodes(trajectory_tensors, input_data):
    parallel_rollouts = input_data["parallel_rollouts"]
    epochs = input_data["epochs"]
    states_episodes, actions_episodes, action_probabilities_episodes = [], [], []
    rewards_episodes, terminal_rewards_episodes, terminals_episodes, values_episodes = [], [], [], []
    policy_hidden_episodes, policy_cell_episodes, critic_hidden_episodes, critic_cell_episodes = [], [], [], []
    len_episodes = []
    trajectory_episodes = {key: [] for key in trajectory_tensors.keys()}
    for i in range(parallel_rollouts):
        terminals_tmp = trajectory_tensors["terminals"].clone()
        terminals_tmp[0, i] = 1
        terminals_tmp[-1, i] = 1
        split_points = (terminals_tmp[:, i] == 1).nonzero() + 1
        split_lens = split_points[1:] - split_points[:-1]
        split_lens[0] += 1
        len_episode = [split_len.item() for split_len in split_lens]
        len_episodes += len_episode
        for key, value in trajectory_tensors.items():
            if key == "values":
                value_split = list(torch.split(value[:, i], len_episode[:-1] + [len_episode[-1] + 1]))
                for j in range(len(value_split) - 1):
                    value_split[j] = torch.cat((value_split[j], torch.zeros(1)))
                trajectory_episodes[key] += value_split
            else:
                trajectory_episodes[key] += torch.split(value[:, i], len_episode)
    return trajectory_episodes, len_episodes
def split_trajectories_episodes ( trajectory_tensors , input_data ) : parallel_rollouts = input_data [ "parallel_rollouts" ] epochs = input_data [ "epochs" ] states_episodes , actions_episodes , action_probabilities_episodes = [ ] , [ ] , [ ] rewards_episodes , terminal_rewards_episodes , terminals_episodes , values_episodes = [ ] , [ ] , [ ] , [ ] policy_hidden_episodes , policy_cell_episodes , critic_hidden_episodes , critic_cell_episodes = [ ] , [ ] , [ ] , [ ] len_episodes = [ ] trajectory_episodes = { key : [ ] for key in trajectory_tensors . keys ( ) } for i in range ( parallel_rollouts ) : terminals_tmp = trajectory_tensors [ "terminals" ] . clone ( ) terminals_tmp [ 0 , i ] = 1 terminals_tmp [ - 1 , i ] = 1 split_points = ( terminals_tmp [ : , i ] == 1 ) . nonzero ( ) + 1 split_lens = split_points [ 1 : ] - split_points [ : - 1 ] split_lens [ 0 ] += 1 len_episode = [ split_len . item ( ) for split_len in split_lens ] len_episodes += len_episode for key , value in trajectory_tensors . items ( ) : if key == "values" : value_split = list ( torch . split ( value [ : , i ] , len_episode [ : - 1 ] + [ len_episode [ - 1 ] + 1 ] ) ) for j in range ( len ( value_split ) - 1 ) : value_split [ j ] = torch . cat ( ( value_split [ j ] , torch . zeros ( 1 ) ) ) trajectory_episodes [ key ] += value_split else : trajectory_episodes [ key ] += torch . split ( value [ : , i ] , len_episode ) return trajectory_episodes , len_episodes
Split trajectories by episode.
Split trajectories by episode .
"""
    Split trajectories by episode. The return values are episode-wise trajectories. This function split the 2048 trajectories
    into almost 7-10 trajectories, each one is a episode. For the values, the episodic trajectory is appended one in the end.
    """ # Value includes additional step. # Append extra 0 to values to represent no future reward.
trajectory_tensors
input_data