<commit_before>#!/usr/bin/env python3

import argparse
import os
import sys
import shutil
from subprocess import Popen, PIPE

import numpy as np
import numpy.random as npr

pythonCmd = 'python3'
rlDir = os.path.dirname(os.path.realpath(__file__))
plotSrc = os.path.join(rlDir, 'src', 'plot.py')
mainSrc = os.path.join(rlDir, 'src', 'main.py')

all_algs = ['DDPG', 'NAF', 'ICNN']

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('task', type=str,
                        choices=['InvertedPendulum', 'InvertedDoublePendulum',
                            'Reacher', 'HalfCheetah', 'Swimmer', 'Hopper',
                                 'Walker2d', 'Ant', 'Humanoid', 'HumanoidStandup'],
                        help='(Every task is currently v1.)')
    parser.add_argument('--alg', type=str, choices=all_algs)
    parser.add_argument('--nSamples', type=int, default=10)
    parser.add_argument('--save', type=str)
    parser.add_argument('--overwrite', action='store_true')
    parser.add_argument('--analyze', action='store_true')

    args = parser.parse_args()

    allDir = args.save or os.path.join('output.random-search', args.task)
    if os.path.exists(allDir):
        if args.overwrite:
            shutil.rmtree(allDir)
    os.makedirs(allDir, exist_ok=True)

    if args.analyze:
        analyze(args, allDir)
    else:
        algs = [args.alg] if args.alg is not None else all_algs
        for alg in algs:
            runAlg(args, alg, allDir)

def runAlg(args, alg, allDir):
    algDir = os.path.join(allDir, alg)

    np.random.seed(0)
    for i in range(args.nSamples):
        hp_alg = {
            'reward_k': 10.**npr.uniform(-4, 1),
            'l2norm': 10.**npr.uniform(-10, -2),
            'pl2norm': 10.**npr.uniform(-10, -2),
            'rate': 10.**npr.uniform(-4, -1),
            'prate': 10.**npr.uniform(-4, -1),
            'outheta': np.maximum(1e-8, npr.normal(loc=0.15, scale=0.1)),
            'ousigma': np.maximum(1e-8, npr.normal(loc=0.1, scale=0.05)),
            'lrelu': 10.**npr.uniform(-3, -1),
            'naf_bn': bool(npr.binomial(1, 0.5)),
            'icnn_bn': bool(npr.binomial(1, 0.5))
        }
        if hp_alg['l2norm'] < 1e-8: hp_alg['l2norm'] = 0.
        if hp_alg['pl2norm'] < 1e-8: hp_alg['pl2norm'] = 0.

        runExp(args, alg, algDir, i, hp_alg)
        analyze(args, allDir)

def analyze(args, allDir):
    with open(os.path.join(allDir, 'analysis.txt'), 'w') as f:
        f.write("Format: [experiment, max test loss val (idx)]\n")
        for alg in all_algs:
            algDir = os.path.join(allDir, alg)
            if os.path.exists(algDir):
                f.write("\n=== {} ===\n".format(alg))
                for exp in sorted(os.listdir(algDir)):
                    expDir = os.path.join(algDir, exp)
                    testLoss = np.loadtxt(os.path.join(expDir, 'test.log'))
                    vals = testLoss[:,1]
                    maxVal, maxValI = vals.max(), vals.argmax()
                    timestep = testLoss[maxValI,0]
                    f.write('  + Experiment {}: Max test loss of {} at timestep {}\n'.format(exp, maxVal, timestep))

def runExp(args, alg, algDir, expNum, hp_alg):
    hp = hyperparams[args.task]
    expDir = os.path.join(algDir, str(expNum))
<commit_msg>RL random search: Zero pad experiment directory.<commit_after>#!/usr/bin/env python3

import argparse
import os
import sys
import shutil
from subprocess import Popen, PIPE

import numpy as np
import numpy.random as npr

pythonCmd = 'python3'
rlDir = os.path.dirname(os.path.realpath(__file__))
plotSrc = os.path.join(rlDir, 'src', 'plot.py')
mainSrc = os.path.join(rlDir, 'src', 'main.py')

all_algs = ['DDPG', 'NAF', 'ICNN']

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('task', type=str,
                        choices=['InvertedPendulum', 'InvertedDoublePendulum',
                            'Reacher', 'HalfCheetah', 'Swimmer', 'Hopper',
                                 'Walker2d', 'Ant', 'Humanoid', 'HumanoidStandup'],
                        help='(Every task is currently v1.)')
    parser.add_argument('--alg', type=str, choices=all_algs)
    parser.add_argument('--nSamples', type=int, default=10)
    parser.add_argument('--save', type=str)
    parser.add_argument('--overwrite', action='store_true')
    parser.add_argument('--analyze', action='store_true')

    args = parser.parse_args()

    allDir = args.save or os.path.join('output.random-search', args.task)
    if os.path.exists(allDir):
        if args.overwrite:
            shutil.rmtree(allDir)
    os.makedirs(allDir, exist_ok=True)

    if args.analyze:
        analyze(args, allDir)
    else:
        algs = [args.alg] if args.alg is not None else all_algs
        for alg in algs:
            runAlg(args, alg, allDir)

def runAlg(args, alg, allDir):
    algDir = os.path.join(allDir, alg)

    np.random.seed(0)
    for i in range(args.nSamples):
        hp_alg = {
            'reward_k': 10.**npr.uniform(-4, 1),
            'l2norm': 10.**npr.uniform(-10, -2),
            'pl2norm': 10.**npr.uniform(-10, -2),
            'rate': 10.**npr.uniform(-4, -1),
            'prate': 10.**npr.uniform(-4, -1),
            'outheta': np.maximum(1e-8, npr.normal(loc=0.15, scale=0.1)),
            'ousigma': np.maximum(1e-8, npr.normal(loc=0.1, scale=0.05)),
            'lrelu': 10.**npr.uniform(-3, -1),
            'naf_bn': bool(npr.binomial(1, 0.5)),
            'icnn_bn': bool(npr.binomial(1, 0.5))
        }
        if hp_alg['l2norm'] < 1e-8: hp_alg['l2norm'] = 0.
        if hp_alg['pl2norm'] < 1e-8: hp_alg['pl2norm'] = 0.

        runExp(args, alg, algDir, i, hp_alg)
        analyze(args, allDir)

def analyze(args, allDir):
    with open(os.path.join(allDir, 'analysis.txt'), 'w') as f:
        f.write("Format: [experiment, max test loss val (idx)]\n")
        for alg in all_algs:
            algDir = os.path.join(allDir, alg)
            if os.path.exists(algDir):
                f.write("\n=== {} ===\n".format(alg))
                for exp in sorted(os.listdir(algDir)):
                    expDir = os.path.join(algDir, exp)
                    testLoss = np.loadtxt(os.path.join(expDir, 'test.log'))
                    vals = testLoss[:,1]
                    maxVal, maxValI = vals.max(), vals.argmax()
                    timestep = testLoss[maxValI,0]
                    f.write('  + Experiment {}: Max test loss of {} at timestep {}\n'.format(exp, maxVal, timestep))

def runExp(args, alg, algDir, expNum, hp_alg):
    hp = hyperparams[args.task]
    expDir = os.path.join(algDir, str(expNum).zfill(3))

{'fraction_non_alphanumeric': 0.16350210970464135, 'fraction_numerical': 0.02034358047016275, 'mean_word_length': 2.3003480855295875, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '894764', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2332, 'n_words': 478}