<commit_before>import net
import sim
import edb

__doc__ = """Training loop. Should be made executable from the terminal/commandline with a config file."""

# TODO Argparse


def fit(models, env, edb, config, verbose=True):
    """
    :type: model: list or tuple
    :param models: Tuple of (Q-Network, Target-Network).

    :type env: sim.Simulator
    :param env: Environment object.

    :type edb: edb.ExperienceDB
    :param edb: Experience database.

    :type config: dict or str
    :param config: Configuration dictionary or path to a YAML configuration file.

    :type verbose: str
    :param verbose: quack quack.
    """

    # Make print function
    def _print(msg):
        if verbose:
            print(msg)

    # Read config
    if isinstance(config, str):
        config = readconfigfile(config)

    # Read networks
    model, targetmodel = models

    # # Check models
    assert hasattr(targetmodel, 'baggage'), "Target model must have baggage with target parameter update function."
    assert "updatetargetparams" in targetmodel.baggage.keys(), "Target parameter update function 'updatetargetparams' " \
                                                               "must be in targetmodel's baggage."
    assert model.savedir is not None, "Model must have a place to save parameters."

    _print("[+] Tests passed.")

    # Prepare loop variables
    # Episode counter
    episodecount = 0
    # Iteration counter
    itercount = -1
    # Epoch counter
    gamecount = 0

    # Begin loop
    while True:
        # Update loop variables
        itercount += 1

        # Check if loop must be broken
        if itercount > config['maxitercount']:
            _print("[-] Maximum number of iterations reached, aborting training...")
            break

        if gamecount > config['maxgamecount']:
            _print("[-] Maximum number of epochs reached, aborting training...")
            break

        # Start new game
        env.newgame()
        gamecount += 1
        isterminal = False

        _print("| New Game |")

        # Sample initial state from environment
        state = env.getstate()

        while not isterminal:
            # ----- [GET XP] -----
            # Sample from network
            Q = model.classifier(state)
            # Q is a tensor which can be squeezed to a vector, which can then be argmax-ed to get an action.
            action = Q.squeeze().argmax()
            # See what the environment thinks about this action
            reward, newstate, isterminal = env.getresponse(action)
            # Log to experience database
            edb.log(state, action, reward, newstate, isterminal)
            # Set state to newstate
            state = newstate

            # ----- [LEARN FROM XP] -----
            # Fetch new batch from experience database
            x, yt = edb.batcherbatcher(targetnetwork=targetmodel, gamma=config['gamma'],
                                       batchsize=config['batchsize'])
            # Train on batch
            out = model.classifiertrainer(x, yt)
            # Update target network parameters
            targetmodel.baggage.updatetargetparams(params=model.params, decay=config['targetnetworkparamdecay'])

            echomsg = "| Cost: {C} || Loss: {L} |".format(C=out['C'], L=out['L'])
            _print(echomsg)

        # Check if the game was won
        gamewon = env.getreward() == 1.
        _print("| [{}] Game {} won. |".format(*{True: ('+', 'was'), False: ('-', 'was not')}[gamewon]))

        # Save parameters if required
        if itercount % config['saveevery'] == 0:
            model.save('--iter-{}-routine'.format(itercount))
            targetmodel.save('--iter-{}-routine'.format(itercount))

    return model
<commit_msg>- hotfix<commit_after>import net
import sim
import edb

__doc__ = """Training loop. Should be made executable from the terminal/commandline with a config file."""

# TODO Argparse


def fit(models, env, edb, config, verbose=True):
    """
    :type: model: list or tuple
    :param models: Tuple of (Q-Network, Target-Network).

    :type env: sim.Simulator
    :param env: Environment object.

    :type edb: edb.ExperienceDB
    :param edb: Experience database.

    :type config: dict or str
    :param config: Configuration dictionary or path to a YAML configuration file.

    :type verbose: str
    :param verbose: quack quack.
    """

    # Make print function
    def _print(msg):
        if verbose:
            print(msg)

    # Read config
    if isinstance(config, str):
        config = readconfigfile(config)

    # Read networks
    model, targetmodel = models

    # # Check models
    assert hasattr(targetmodel, 'baggage'), "Target model must have baggage with target parameter update function."
    assert "updatetargetparams" in targetmodel.baggage.keys(), "Target parameter update function 'updatetargetparams' " \
                                                               "must be in targetmodel's baggage."
    assert model.savedir is not None, "Model must have a place to save parameters."

    _print("[+] Tests passed.")

    # Prepare loop variables
    # Episode counter
    episodecount = 0
    # Iteration counter
    itercount = -1
    # Epoch counter
    gamecount = 0

    # Begin loop
    while True:
        # Update loop variables
        itercount += 1

        # Check if loop must be broken
        if itercount > config['maxitercount']:
            _print("[-] Maximum number of iterations reached, aborting training...")
            break

        if gamecount > config['maxgamecount']:
            _print("[-] Maximum number of epochs reached, aborting training...")
            break

        # Start new game
        env.newgame()
        gamecount += 1
        isterminal = False

        _print("| New Game |")

        # Sample initial state from environment
        state = env.getstate()

        while not isterminal:
            # ----- [GET XP] -----
            # Sample from network
            Q = model.classifier(state)
            # Q is a tensor which can be squeezed to a vector, which can then be argmax-ed to get an action.
            action = Q.squeeze().argmax()
            # See what the environment thinks about this action
            reward, newstate, isterminal = env.getresponse(action)
            # Log to experience database
            edb.log(state, action, reward, newstate, isterminal)
            # Set state to newstate
            state = newstate

            # ----- [LEARN FROM XP] -----
            # Fetch new batch from experience database
            x, yt = edb.batcherbatcher(targetnetwork=targetmodel, gamma=config['gamma'],
                                       batchsize=config['batchsize'])
            # Train on batch
            out = model.classifiertrainer(x, yt)
            # Update target network parameters
            targetmodel.baggage["updatetargetparams"](params=model.params, decay=config['targetnetworkparamdecay'])

            echomsg = "| Cost: {C} || Loss: {L} |".format(C=out['C'], L=out['L'])
            _print(echomsg)

        # Check if the game was won
        gamewon = env.getreward() == 1.
        _print("| [{}] Game {} won. |".format(*{True: ('+', 'was'), False: ('-', 'was not')}[gamewon]))

        # Save parameters if required
        if itercount % config['saveevery'] == 0:
            model.save('--iter-{}-routine'.format(itercount))
            targetmodel.save('--iter-{}-routine'.format(itercount))

    return model

{'fraction_non_alphanumeric': 0.10603242094073877, 'fraction_numerical': 0.0018602179112410311, 'mean_word_length': 2.0697389885807502, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1006997', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1978, 'n_words': 671}