'Convert time-major batch of episodes to batch-major list of episodes.'

def convert_from_batched_episodes(self, initial_state, observations, actions, rewards, terminated, pads):

    rewards = np.array(rewards)
    pads = np.array(pads)
    observations = [np.array(obs) for obs in observations]
    actions = [np.array(act) for act in actions]
    total_rewards = np.sum((rewards * (1 - pads)), axis=0)
    total_length = np.sum((1 - pads), axis=0).astype('int32')
    episodes = []
    num_episodes = rewards.shape[1]
    for i in xrange(num_episodes):
        length = total_length[i]
        ep_initial = initial_state[i]
        ep_obs = [obs[:length, i, ...] for obs in observations]
        ep_act = [act[:(length + 1), i, ...] for act in actions]
        ep_rewards = rewards[:length, i]
        episodes.append([ep_initial, ep_obs, ep_act, ep_rewards, terminated[i]])
    return episodes
