def __init__(self, reddit, app_key=None, app_secret=None, scope=None,<EOL>refreshable=None, configfile=DEFAULT_CONFIG, print_log=False,<EOL>server_mode=None):

self.r = reddit<EOL>self.server = None<EOL>self._print = print_log<EOL>self.log = get_logger()<EOL>self.configfile = configfile<EOL>self.config = configparser.ConfigParser()<EOL>needMigration = None<EOL>try:<EOL><INDENT>self.config.read(configfile)<EOL>if len(self.config.sections()) == <NUM_LIT:0>:<EOL><INDENT>if os.path.isfile("<STR_LIT>"):<EOL><INDENT>needMigration = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>raise FNFError("<STR_LIT>" + configfile + "<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>except configparser.MissingSectionHeaderError:<EOL><INDENT>needMigration = configfile<EOL><DEDENT>if needMigration is not None:<EOL><INDENT>self._migrate_config(needMigration, configfile)<EOL>self.config.read(configfile)<EOL><DEDENT>if not self.config.has_section(CONFIGKEY_SERVER_MODE[<NUM_LIT:0>]):<EOL><INDENT>self.config.add_section(CONFIGKEY_SERVER_MODE[<NUM_LIT:0>])<EOL><DEDENT>try:<EOL><INDENT>self._get_value(CONFIGKEY_SERVER_MODE)<EOL><DEDENT>except KeyError:<EOL><INDENT>self.config.set(CONFIGKEY_SERVER_MODE[<NUM_LIT:0>], CONFIGKEY_SERVER_MODE[<NUM_LIT:1>], str(False))<EOL><DEDENT>if app_key is not None:<EOL><INDENT>self.config.set(CONFIGKEY_APP_KEY[<NUM_LIT:0>], CONFIGKEY_APP_KEY[<NUM_LIT:1>], str(app_key))<EOL><DEDENT>if app_secret is not None:<EOL><INDENT>self.config.set(CONFIGKEY_APP_SECRET[<NUM_LIT:0>], CONFIGKEY_APP_SECRET[<NUM_LIT:1>], str(app_secret))<EOL><DEDENT>if scope is not None:<EOL><INDENT>self.config.set(CONFIGKEY_SCOPE[<NUM_LIT:0>], CONFIGKEY_SCOPE[<NUM_LIT:1>], str(scope))<EOL><DEDENT>if refreshable is not None:<EOL><INDENT>self.config.set(CONFIGKEY_REFRESHABLE[<NUM_LIT:0>], CONFIGKEY_REFRESHABLE[<NUM_LIT:1>], str(refreshable))<EOL><DEDENT>if server_mode is not None:<EOL><INDENT>self.config.set(CONFIGKEY_SERVER_MODE[<NUM_LIT:0>], CONFIGKEY_SERVER_MODE[<NUM_LIT:1>], str(server_mode))<EOL><DEDENT>global SERVER_URL, SERVER_PORT, SERVER_REDIRECT_PATH, SERVER_LINK_PATH<EOL>SERVER_URL = self._get_value(CONFIGKEY_SERVER_URL, exception_default=SERVER_URL)<EOL>SERVER_PORT = self._get_value(CONFIGKEY_SERVER_PORT, int, exception_default=SERVER_PORT)<EOL>SERVER_REDIRECT_PATH = self._get_value(CONFIGKEY_SERVER_REDIRECT_PATH, exception_default=SERVER_REDIRECT_PATH)<EOL>SERVER_LINK_PATH = self._get_value(CONFIGKEY_SERVER_LINK_PATH, exception_default=SERVER_LINK_PATH)<EOL>self._set_app_info()<EOL>self.refresh()<EOL>self.set_access_credentials()<EOL>

Create a new instance. The app info can also be read from a config file.


Set the app info (id & secret) read from the config file on the Reddit object


def _get_value(self, key, func=None, split_val=None, as_boolean=False,<EOL>exception_default=None):

try:<EOL><INDENT>if as_boolean:<EOL><INDENT>return self.config.getboolean(key[<NUM_LIT:0>], key[<NUM_LIT:1>])<EOL><DEDENT>value = self.config.get(key[<NUM_LIT:0>], key[<NUM_LIT:1>])<EOL>if split_val is not None:<EOL><INDENT>value = value.split(split_val)<EOL><DEDENT>if func is not None:<EOL><INDENT>return func(value)<EOL><DEDENT>return value<EOL><DEDENT>except (KeyError, configparser.NoSectionError, configparser.NoOptionError) as e:<EOL><INDENT>if exception_default is not None:<EOL><INDENT>return exception_default<EOL><DEDENT>raise KeyError(e)<EOL><DEDENT>

Helper method to get a value from the config


if not self.config.has_section(key[<NUM_LIT:0>]):<EOL><INDENT>self.config.add_section(key[<NUM_LIT:0>])<EOL><DEDENT>self.config.set(key[<NUM_LIT:0>], key[<NUM_LIT:1>], str(value))<EOL>with open(self.configfile, "<STR_LIT:w>") as f:<EOL><INDENT>self.config.write(f)<EOL><DEDENT>

Change the value of the given key in the given file to the given value


def _migrate_config(self, oldname=DEFAULT_CONFIG, newname=DEFAULT_CONFIG):

self._log("<STR_LIT>"<EOL>"<STR_LIT>", logging.WARNING)<EOL>with open(oldname, "<STR_LIT:r>") as old:<EOL><INDENT>with open(newname, "<STR_LIT:w>") as new:<EOL><INDENT>new.write("<STR_LIT>")<EOL>new.write(old.read())<EOL><DEDENT><DEDENT>

Migrates the old config file format to the new one


server_address = (SERVER_URL, SERVER_PORT)<EOL>self.server = HTTPServer(server_address, OAuth2UtilRequestHandler)<EOL>self.server.response_code = None<EOL>self.server.authorize_url = authorize_url<EOL>t = Thread(target=self.server.serve_forever)<EOL>t.daemon = True<EOL>t.start()<EOL>

Start the webserver that will receive the code


while not self.server.response_code:<EOL><INDENT>time.sleep(<NUM_LIT:2>)<EOL><DEDENT>time.sleep(<NUM_LIT:5>)<EOL>self.server.shutdown()<EOL>

Wait until the user accepted or rejected the request


if not self.r.has_oauth_app_info:<EOL><INDENT>self._log('<STR_LIT>', logging.ERROR)<EOL>raise AttributeError('<STR_LIT>')<EOL><DEDENT>url = self.r.get_authorize_url('<STR_LIT>',<EOL>self._get_value(CONFIGKEY_SCOPE, set, split_val='<STR_LIT:U+002C>'),<EOL>self._get_value(CONFIGKEY_REFRESHABLE, as_boolean=True))<EOL>self._start_webserver(url)<EOL>if not self._get_value(CONFIGKEY_SERVER_MODE, as_boolean=True):<EOL><INDENT>webbrowser.open(url)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>.format(SERVER_URL, SERVER_PORT, SERVER_LINK_PATH))<EOL><DEDENT>self._wait_for_response()<EOL>try:<EOL><INDENT>access_information = self.r.get_access_information(<EOL>self.server.response_code)<EOL><DEDENT>except praw.errors.OAuthException:<EOL><INDENT>self._log("<STR_LIT>", logging.ERROR)<EOL>raise<EOL><DEDENT>self._change_value(CONFIGKEY_TOKEN, access_information["<STR_LIT>"])<EOL>self._change_value(CONFIGKEY_REFRESH_TOKEN, access_information["<STR_LIT>"])<EOL>self._change_value(CONFIGKEY_VALID_UNTIL, time.time() + TOKEN_VALID_DURATION)<EOL>

Request new access information from reddit using the built in webserver


Check whether the tokens are set and request new ones if not


self._print = not self._print<EOL>if self._print:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>


if _retry >= <NUM_LIT:5>:<EOL><INDENT>raise ConnectionAbortedError('<STR_LIT>')<EOL><DEDENT>self._check_token_present()<EOL>try:<EOL><INDENT>self.r.set_access_credentials(self._get_value(CONFIGKEY_SCOPE, set, split_val="<STR_LIT:U+002C>"),<EOL>self._get_value(CONFIGKEY_TOKEN),<EOL>self._get_value(CONFIGKEY_REFRESH_TOKEN))<EOL><DEDENT>except (praw.errors.OAuthInvalidToken, praw.errors.HTTPException) as e:<EOL><INDENT>self._log("<STR_LIT>")<EOL>self._get_new_access_information()<EOL><DEDENT>

Set the token on the Reddit Object again


def refresh(self, force=False, _retry=<NUM_LIT:0>):

if _retry >= <NUM_LIT:5>:<EOL><INDENT>raise ConnectionAbortedError('<STR_LIT>')<EOL><DEDENT>self._check_token_present()<EOL>if time.time() > self._get_value(CONFIGKEY_VALID_UNTIL, float, exception_default=<NUM_LIT:0>) - REFRESH_MARGIN:<EOL><INDENT>self.config.read(self.configfile)<EOL>if time.time() < self._get_value(CONFIGKEY_VALID_UNTIL, float, exception_default=<NUM_LIT:0>) - REFRESH_MARGIN:<EOL><INDENT>self._log("<STR_LIT>")<EOL>self.set_access_credentials()<EOL><DEDENT><DEDENT>if force or time.time() > self._get_value(CONFIGKEY_VALID_UNTIL, float, exception_default=<NUM_LIT:0>) - REFRESH_MARGIN:<EOL><INDENT>self._log("<STR_LIT>")<EOL>try:<EOL><INDENT>new_token = self.r.refresh_access_information(self._get_value(CONFIGKEY_REFRESH_TOKEN))<EOL>self._change_value(CONFIGKEY_TOKEN, new_token["<STR_LIT>"])<EOL>self._change_value(CONFIGKEY_VALID_UNTIL, time.time() + TOKEN_VALID_DURATION)<EOL>self.set_access_credentials()<EOL><DEDENT>except (praw.errors.OAuthInvalidToken, praw.errors.HTTPException) as e:<EOL><INDENT>self._log("<STR_LIT>")<EOL>self._get_new_access_information()<EOL><DEDENT><DEDENT>

Check if the token is still valid and requests a new if it is not
valid anymore

Call this method before a call to praw
if there might have passed more than one hour

force: if true, a new token will be retrieved no matter what


cls = self.__class__<EOL>if cls._executor is None:<EOL><INDENT>cls._executor = ThreadPoolExecutor(max_workers)<EOL><DEDENT>return cls._executor<EOL>


cls = self.__class__<EOL>if cls._client is None:<EOL><INDENT>kwargs = {}<EOL>if self.tls_config:<EOL><INDENT>kwargs['<STR_LIT>'] = docker.tls.TLSConfig(**self.tls_config)<EOL><DEDENT>kwargs.update(kwargs_from_env())<EOL>client = docker.APIClient(version='<STR_LIT>', **kwargs)<EOL>cls._client = client<EOL><DEDENT>return cls._client<EOL>


if self.tls_cert and self.tls_key:<EOL><INDENT>return (self.tls_cert, self.tls_key)<EOL><DEDENT>return None<EOL>

A tuple consisting of the TLS client certificate and key if they
        have been provided, otherwise None.


if hasattr(self, "<STR_LIT>") and self.server_name:<EOL><INDENT>server_name = self.server_name<EOL><DEDENT>else:<EOL><INDENT>server_name = <NUM_LIT:1><EOL><DEDENT>return "<STR_LIT>".format(self.service_prefix,<EOL>self.service_owner,<EOL>server_name<EOL>)<EOL>

Service name inside the Docker Swarm

service_suffix should be a numerical value unique for user
{service_prefix}-{service_owner}-{service_suffix}


return []<EOL>

it's called in traitlets. It's a special method name.
        Don't inherit any env from the parent process


def _docker(self, method, *args, **kwargs):

m = getattr(self.client, method)<EOL>return m(*args, **kwargs)<EOL>

wrapper for calling docker methods

        to be passed to ThreadPoolExecutor


def docker(self, method, *args, **kwargs):

return self.executor.submit(self._docker, method, *args, **kwargs)<EOL>

Call a docker method in a background thread

        returns a Future


service = yield self.get_service()<EOL>if not service:<EOL><INDENT>self.log.warn("<STR_LIT>")<EOL>return <NUM_LIT:0><EOL><DEDENT>task_filter = {'<STR_LIT>': service['<STR_LIT>']['<STR_LIT:Name>']}<EOL>tasks = yield self.docker(<EOL>'<STR_LIT>', task_filter<EOL>)<EOL>running_task = None<EOL>for task in tasks:<EOL><INDENT>task_state = task['<STR_LIT>']['<STR_LIT>']<EOL>self.log.debug(<EOL>"<STR_LIT>",<EOL>task['<STR_LIT>'][:<NUM_LIT:7>],<EOL>self.service_id[:<NUM_LIT:7>],<EOL>pformat(task_state),<EOL>)<EOL>if task_state == '<STR_LIT>':<EOL><INDENT>running_task = task<EOL><DEDENT><DEDENT>if running_task is not None:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>

Check for a task state like `docker service ps id`


<EOL>if self.use_user_options:<EOL><INDENT>user_options = self.user_options<EOL><DEDENT>else:<EOL><INDENT>user_options = {}<EOL><DEDENT>self.log.warn("<STR_LIT>".format(user_options))<EOL>service = yield self.get_service()<EOL>if service is None:<EOL><INDENT>if '<STR_LIT:name>' in user_options:<EOL><INDENT>self.server_name = user_options['<STR_LIT:name>']<EOL><DEDENT>if hasattr(self, '<STR_LIT>') and self.container_spec is not None:<EOL><INDENT>container_spec = dict(**self.container_spec)<EOL><DEDENT>elif user_options == {}:<EOL><INDENT>raise("<STR_LIT>")<EOL><DEDENT>container_spec.update(user_options.get('<STR_LIT>', {}))<EOL>container_spec['<STR_LIT>'] = []<EOL>for mount in self.container_spec['<STR_LIT>']:<EOL><INDENT>m = dict(**mount)<EOL>if '<STR_LIT:source>' in m:<EOL><INDENT>m['<STR_LIT:source>'] = m['<STR_LIT:source>'].format(<EOL>username=self.service_owner)<EOL><DEDENT>if '<STR_LIT>' in m:<EOL><INDENT>device = m['<STR_LIT>']['<STR_LIT>']['<STR_LIT>'].format(<EOL>username=self.service_owner<EOL>)<EOL>m['<STR_LIT>']['<STR_LIT>']['<STR_LIT>'] = device<EOL>m['<STR_LIT>'] = docker.types.DriverConfig(<EOL>**m['<STR_LIT>'])<EOL><DEDENT>container_spec['<STR_LIT>'].append(docker.types.Mount(**m))<EOL><DEDENT>container_spec['<STR_LIT>'] = self.get_env()<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>resource_spec = self.resource_spec<EOL><DEDENT>resource_spec.update(user_options.get('<STR_LIT>', {}))<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>networks = self.networks<EOL><DEDENT>if user_options.get('<STR_LIT>') is not None:<EOL><INDENT>networks = user_options.get('<STR_LIT>')<EOL><DEDENT>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>placement = self.placement<EOL><DEDENT>if user_options.get('<STR_LIT>') is not None:<EOL><INDENT>placement = user_options.get('<STR_LIT>')<EOL><DEDENT>image = container_spec['<STR_LIT>']<EOL>del container_spec['<STR_LIT>']<EOL>container_spec = docker.types.ContainerSpec(<EOL>image, **container_spec)<EOL>resources = docker.types.Resources(**resource_spec)<EOL>task_spec = {'<STR_LIT>': container_spec,<EOL>'<STR_LIT>': resources,<EOL>'<STR_LIT>': placement<EOL>}<EOL>task_tmpl = docker.types.TaskTemplate(**task_spec)<EOL>resp = yield self.docker('<STR_LIT>',<EOL>task_tmpl,<EOL>name=self.service_name,<EOL>networks=networks)<EOL>self.service_id = resp['<STR_LIT>']<EOL>self.log.info(<EOL>"<STR_LIT>",<EOL>self.service_name, self.service_id[:<NUM_LIT:7>], image)<EOL><DEDENT>else:<EOL><INDENT>self.log.info(<EOL>"<STR_LIT>",<EOL>self.service_name, self.service_id[:<NUM_LIT:7>])<EOL>envs = service['<STR_LIT>']['<STR_LIT>']['<STR_LIT>']['<STR_LIT>']<EOL>for line in envs:<EOL><INDENT>if line.startswith('<STR_LIT>'):<EOL><INDENT>self.api_token = line.split('<STR_LIT:=>', <NUM_LIT:1>)[<NUM_LIT:1>]<EOL>break<EOL><DEDENT><DEDENT><DEDENT>ip = self.service_name<EOL>port = self.service_port<EOL>return (ip, port)<EOL>

Start the single-user server in a docker service.
        You can specify the params for the service through jupyterhub_config.py
        or using the user_options


Stop and remove the service

        Consider using stop/start when Docker adds support


Returns the unique id of the extension that can
be used to activate it via a command line option.


Calculates the salt for the file name encryption
based on the given absolute file path.


def pad(data_to_pad, block_size, style='<STR_LIT>'):

padding_len = block_size-len(data_to_pad)%block_size<EOL>if style == '<STR_LIT>':<EOL><INDENT>padding = bchr(padding_len)*padding_len<EOL><DEDENT>elif style == '<STR_LIT>':<EOL><INDENT>padding = bchr(<NUM_LIT:0>)*(padding_len-<NUM_LIT:1>) + bchr(padding_len)<EOL><DEDENT>elif style == '<STR_LIT>':<EOL><INDENT>padding = bchr(<NUM_LIT>) + bchr(<NUM_LIT:0>)*(padding_len-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return data_to_pad + padding<EOL>

Apply standard padding.

    :Parameters:
      data_to_pad : byte string
        The data that needs to be padded.
      block_size : integer
        The block boundary to use for padding. The output length is guaranteed
        to be a multiple of ``block_size``.
      style : string
        Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
    :Return:
      The original data with the appropriate padding added at the end.


def unpad(padded_data, block_size, style='<STR_LIT>'):

pdata_len = len(padded_data)<EOL>if pdata_len % block_size:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if style in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>padding_len = bord(padded_data[-<NUM_LIT:1>])<EOL>if padding_len<<NUM_LIT:1> or padding_len>min(block_size, pdata_len):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if style == '<STR_LIT>':<EOL><INDENT>if padded_data[-padding_len:]!=bchr(padding_len)*padding_len:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if padded_data[-padding_len:-<NUM_LIT:1>]!=bchr(<NUM_LIT:0>)*(padding_len-<NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>elif style == '<STR_LIT>':<EOL><INDENT>padding_len = pdata_len - padded_data.rfind(bchr(<NUM_LIT>))<EOL>if padding_len<<NUM_LIT:1> or padding_len>min(block_size, pdata_len):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if padding_len><NUM_LIT:1> and padded_data[<NUM_LIT:1>-padding_len:]!=bchr(<NUM_LIT:0>)*(padding_len-<NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return padded_data[:-padding_len]<EOL>

Remove standard padding.

    :Parameters:
      padded_data : byte string
        A piece of data with padding that needs to be stripped.
      block_size : integer
        The block boundary to use for padding. The input length
        must be a multiple of ``block_size``.
      style : string
        Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
    :Return:
        Data without padding.
    :Raises ValueError:
        if the padding is incorrect.


self.A = A<EOL>

Initialize solver

Args:
    A (sparse matrix): The matrix to solve


if not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self._Ainv = self.Solver(self.A)<EOL><DEDENT>return self._Ainv<EOL>


if not hasattr(self, '<STR_LIT>'):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>return self._A<EOL>


if isinstance(rhs, scipy.sparse.spmatrix):<EOL><INDENT>def qIter(qs):<EOL><INDENT>for j in range(qs.shape[<NUM_LIT:1>]):<EOL><INDENT>qi = qs.getcol(j).toarray().ravel()<EOL>yield qi<EOL><DEDENT>return<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def qIter(qs):<EOL><INDENT>for j in range(qs.shape[<NUM_LIT:1>]):<EOL><INDENT>qi = qs[:, j]<EOL>yield qi<EOL><DEDENT>return<EOL><DEDENT><DEDENT>result = np.empty(rhs.shape, dtype=np.complex128)<EOL>for i, q in enumerate(qIter(rhs)):<EOL><INDENT>result[:, i] = self._solve(q)<EOL><DEDENT>return result<EOL>

Carries out the action of solving for wavefields.

Args:
    rhs (sparse matrix): Right-hand side vector(s)

Returns:
    np.ndarray: Wavefields


if getattr(self, '<STR_LIT>', None) is None:<EOL><INDENT>self._Ainv = self.Solver(self.A, <NUM_LIT>)<EOL>self._Ainv.run_pardiso(<NUM_LIT:12>)<EOL><DEDENT>return self._Ainv<EOL>


def random_matrix(rows, cols, mean=<NUM_LIT:0>, std=<NUM_LIT:1>, sparsity=<NUM_LIT:0>, radius=<NUM_LIT:0>, diagonal=<NUM_LIT:0>, rng=None):

if rng is None or isinstance(rng, int):<EOL><INDENT>rng = np.random.RandomState(rng)<EOL><DEDENT>arr = mean + std * rng.randn(rows, cols)<EOL>if <NUM_LIT:1> > sparsity > <NUM_LIT:0>:<EOL><INDENT>k = min(rows, cols)<EOL>mask = rng.binomial(n=<NUM_LIT:1>, p=<NUM_LIT:1> - sparsity, size=(rows, cols)).astype(bool)<EOL>mask[:k, :k] |= np.eye(k).astype(bool)<EOL>arr *= mask<EOL><DEDENT>if radius > <NUM_LIT:0>:<EOL><INDENT>u, s, vT = np.linalg.svd(arr, full_matrices=False)<EOL>arr = np.dot(np.dot(u, np.diag(radius * s / abs(s[<NUM_LIT:0>]))), vT)<EOL><DEDENT>if diagonal != <NUM_LIT:0>:<EOL><INDENT>arr = diagonal * np.eye(max(rows, cols))[:rows, :cols]<EOL><DEDENT>return arr.astype(FLOAT)<EOL>

Create a matrix of randomly-initialized weights.

    Parameters
    ----------
    rows : int
        Number of rows of the weight matrix -- equivalently, the number of
        "input" units that the weight matrix connects.
    cols : int
        Number of columns of the weight matrix -- equivalently, the number
        of "output" units that the weight matrix connects.
    mean : float, optional
        Draw initial weight values from a normal with this mean. Defaults to 0.
    std : float, optional
        Draw initial weight values from a normal with this standard deviation.
        Defaults to 1.
    sparsity : float in (0, 1), optional
        If given, ensure that the given fraction of the weight matrix is
        set to zero. Defaults to 0, meaning all weights are nonzero.
    radius : float, optional
        If given, rescale the initial weights to have this spectral radius.
        No scaling is performed by default.
    diagonal : float, optional
        If nonzero, create a matrix containing all zeros except for this value
        along the diagonal. If nonzero, other arguments (except for rows and
        cols) will be ignored.
    rng : :class:`numpy.random.RandomState` or int, optional
        A random number generator, or an integer seed for a random number
        generator. If not provided, the random number generator will be created
        with an automatically chosen seed.

    Returns
    -------
    matrix : numpy array
        An array containing random values. These often represent the weights
        connecting each "input" unit to each "output" unit in a layer.


def random_vector(size, mean=<NUM_LIT:0>, std=<NUM_LIT:1>, rng=None):

if rng is None or isinstance(rng, int):<EOL><INDENT>rng = np.random.RandomState(rng)<EOL><DEDENT>return (mean + std * rng.randn(size)).astype(FLOAT)<EOL>

Create a vector of randomly-initialized values.

    Parameters
    ----------
    size : int
        Length of vecctor to create.
    mean : float, optional
        Mean value for initial vector values. Defaults to 0.
    std : float, optional
        Standard deviation for initial vector values. Defaults to 1.
    rng : :class:`numpy.random.RandomState` or int, optional
        A random number generator, or an integer seed for a random number
        generator. If not provided, the random number generator will be created
        with an automatically chosen seed.

    Returns
    -------
    vector : numpy array
        An array containing random values. This often represents the bias for a
        layer of computation units.


if isinstance(patterns, basestring):<EOL><INDENT>patterns = (patterns, )<EOL><DEDENT>if isinstance(outputs, dict):<EOL><INDENT>outputs = outputs.items()<EOL><DEDENT>for name, expr in outputs:<EOL><INDENT>for pattern in patterns:<EOL><INDENT>if fnmatch.fnmatch(name, pattern):<EOL><INDENT>yield name, expr<EOL>break<EOL><DEDENT><DEDENT><DEDENT>

Get the outputs from a network that match a pattern.

    Parameters
    ----------
    outputs : dict or sequence of (str, theano expression)
        Output expressions to filter for matches. If this is a dictionary, its
        ``items()`` will be processed for matches.
    patterns : sequence of str
        A sequence of glob-style patterns to match against. Any parameter
        matching any pattern in this sequence will be included in the match.

    Yields
    ------
    matches : pair of str, theano expression
        Generates a sequence of (name, expression) pairs. The name is the name
        of the output that matched, and the expression is the symbolic output in
        the network graph.


if isinstance(patterns, basestring):<EOL><INDENT>patterns = (patterns, )<EOL><DEDENT>for layer in layers:<EOL><INDENT>for param in layer.params:<EOL><INDENT>name = param.name<EOL>for pattern in patterns:<EOL><INDENT>if fnmatch.fnmatch(name, pattern):<EOL><INDENT>yield name, param<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

Get the parameters from a network that match a pattern.

    Parameters
    ----------
    layers : list of :class:`theanets.layers.Layer`
        A list of network layers to retrieve parameters from.
    patterns : sequence of str
        A sequence of glob-style patterns to match against. Any parameter
        matching any pattern in this sequence will be included in the match.

    Yields
    ------
    matches : pair of str, theano expression
        Generates a sequence of (name, expression) pairs. The name is the name
        of the parameter that matched, and the expression represents the
        parameter symbolically.


global _detailed_callsite<EOL>_detailed_callsite = True<EOL>


now = datetime.datetime.now()<EOL>module = '<STR_LIT>'<EOL>if _detailed_callsite:<EOL><INDENT>caller = inspect.stack()[<NUM_LIT:1>]<EOL>parts = caller.filename.replace('<STR_LIT>', '<STR_LIT>').split('<STR_LIT:/>')<EOL>module = '<STR_LIT>'.format(<EOL>'<STR_LIT:.>'.join(parts[parts.index('<STR_LIT>')+<NUM_LIT:1>:]), caller.lineno)<EOL><DEDENT>click.echo('<STR_LIT:U+0020>'.join((<EOL>click.style(now.strftime('<STR_LIT>'), fg='<STR_LIT>'),<EOL>click.style(now.strftime('<STR_LIT>'), fg='<STR_LIT>'),<EOL>click.style(module, fg='<STR_LIT>'),<EOL>msg.format(*args, **kwargs),<EOL>)))<EOL>

Log a message to the console.

    Parameters
    ----------
    msg : str
        A string to display on the console. This can contain {}-style
        formatting commands; the remaining positional and keyword arguments
        will be used to fill them in.


def encode(self, x, layer=None, sample=False, **kwargs):

enc = self.feed_forward(x, **kwargs)[self._find_output(layer)]<EOL>if sample:<EOL><INDENT>return np.random.binomial(n=<NUM_LIT:1>, p=enc).astype(np.uint8)<EOL><DEDENT>return enc<EOL>

Encode a dataset using the hidden layer activations of our network.

        Parameters
        ----------
        x : ndarray
            A dataset to encode. Rows of this dataset capture individual data
            points, while columns represent the variables in each data point.

        layer : str, optional
            The name of the hidden layer output to use. By default, we use
            the "middle" hidden layer---for example, for a 4,2,4 or 4,3,2,3,4
            autoencoder, we use the layer with size 2.

        sample : bool, optional
            If True, then draw a sample using the hidden activations as
            independent Bernoulli probabilities for the encoded data. This
            assumes the hidden layer has a logistic sigmoid activation function.

        Returns
        -------
        ndarray :
            The given dataset, encoded by the appropriate hidden layer
            activation.


def decode(self, z, layer=None, **kwargs):

key = self._find_output(layer)<EOL>if key not in self._functions:<EOL><INDENT>regs = regularizers.from_kwargs(self, **kwargs)<EOL>outputs, updates = self.build_graph(regs)<EOL>self._functions[key] = theano.function(<EOL>[outputs[key]],<EOL>[outputs[self.layers[-<NUM_LIT:1>].output_name]],<EOL>updates=updates)<EOL><DEDENT>return self._functions[key](z)[<NUM_LIT:0>]<EOL>

Decode an encoded dataset by computing the output layer activation.

        Parameters
        ----------
        z : ndarray
            A matrix containing encoded data from this autoencoder.
        layer : int or str or :class:`Layer <layers.Layer>`, optional
            The index or name of the hidden layer that was used to encode `z`.

        Returns
        -------
        decoded : ndarray
            The decoded dataset.


if layer is None:<EOL><INDENT>layer = len(self.layers) // <NUM_LIT:2><EOL><DEDENT>if isinstance(layer, int):<EOL><INDENT>layer = self.layers[layer]<EOL><DEDENT>if isinstance(layer, util.basestring):<EOL><INDENT>try:<EOL><INDENT>layer = [l for l in self.layers if l.name == layer][<NUM_LIT:0>]<EOL><DEDENT>except IndexError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if isinstance(layer, layers.Layer):<EOL><INDENT>layer = layer.output_name<EOL><DEDENT>return layer<EOL>

Find a layer output name for the given layer specifier.

        Parameters
        ----------
        layer : None, int, str, or :class:`theanets.layers.Layer`
            A layer specification. If this is None, the "middle" layer in the
            network will be used (i.e., the layer at the middle index in the
            list of network layers). If this is an integer, the corresponding
            layer in the network's layer list will be used. If this is a string,
            the layer with the corresponding name will be returned.

        Returns
        -------
        name : str
            The fully-scoped output name for the desired layer.


return super(Autoencoder, self).score(x, x, w=w, **kwargs)<EOL>

Compute R^2 coefficient of determination for a given input.

        Parameters
        ----------
        x : ndarray (num-examples, num-inputs)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        r2 : float
            The R^2 correlation between the prediction of this netork and its
            input. This can serve as one measure of the information loss of the
            autoencoder.


monitors = super(Classifier, self).monitors(**kwargs)<EOL>regs = regularizers.from_kwargs(self, **kwargs)<EOL>outputs, _ = self.build_graph(regs)<EOL>return monitors + [('<STR_LIT>', self.losses[<NUM_LIT:0>].accuracy(outputs))]<EOL>

Return expressions that should be computed to monitor training.

        Returns
        -------
        monitors : list of (name, expression) pairs
            A list of named monitor expressions to compute for this network.


outputs = self.feed_forward(x, **kwargs)<EOL>return outputs[self.layers[-<NUM_LIT:1>].output_name].argmax(axis=-<NUM_LIT:1>)<EOL>

Compute a greedy classification for the given set of data.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to classify. Examples are given as the
            rows in this array.

        Returns
        -------
        k : ndarray (num-examples, )
            A vector of class index values, one per row of input data.


return self.feed_forward(x, **kwargs)[self.layers[-<NUM_LIT:1>].output_name]<EOL>

Compute class posterior probabilities for the given set of data.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to predict. Examples are given as the
            rows in this array.

        Returns
        -------
        p : ndarray (num-examples, num-classes)
            An array of class posterior probability values, one per row of input
            data.


return self.feed_forward(x, **kwargs)[self.layers[-<NUM_LIT:1>].full_name('<STR_LIT>')]<EOL>

Compute the logit values that underlie the softmax output.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to classify. Examples are given as the
            rows in this array.

        Returns
        -------
        l : ndarray (num-examples, num-classes)
            An array of posterior class logit values, one row of logit values
            per row of input data.


def score(self, x, y, w=None, **kwargs):

eq = y == self.predict(x, **kwargs)<EOL>if w is not None:<EOL><INDENT>return (w * eq).sum() / w.sum()<EOL><DEDENT>return eq.mean()<EOL>

Compute the mean accuracy on a set of labeled data.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to classify. Examples are given as the
            rows in this array.
        y : ndarray (num-examples, )
            A vector of integer class labels, one for each row of input data.
        w : ndarray (num-examples, )
            A vector of weights, one for each row of input data.

        Returns
        -------
        score : float
            The (possibly weighted) mean accuracy of the model on the data.


if '<STR_LIT>' in kwargs:<EOL><INDENT>regs = kwargs['<STR_LIT>']<EOL>if isinstance(regs, (tuple, list)):<EOL><INDENT>return regs<EOL><DEDENT>if isinstance(regs, dict):<EOL><INDENT>kwargs.update(regs)<EOL><DEDENT><DEDENT>regs = []<EOL>rng = kwargs.get('<STR_LIT>', <NUM_LIT>)<EOL>def pattern(ls):<EOL><INDENT>return tuple(l.output_name for l in ls)<EOL><DEDENT>inputs = pattern([l for l in graph.layers if isinstance(l, layers.Input)])<EOL>hiddens = pattern(graph.layers[<NUM_LIT:1>:-<NUM_LIT:1>])<EOL>outputs = pattern([graph.layers[-<NUM_LIT:1>]])<EOL>spec = {inputs: kwargs.get('<STR_LIT>', <NUM_LIT:0>),<EOL>hiddens: kwargs.get('<STR_LIT>', <NUM_LIT:0>),<EOL>outputs: kwargs.get('<STR_LIT>', <NUM_LIT:0>)}<EOL>spec.update(kwargs.get('<STR_LIT>', {}))<EOL>for pattern, w in spec.items():<EOL><INDENT>if w:<EOL><INDENT>regs.append(BernoulliDropout(pattern=pattern, weight=w, rng=rng))<EOL><DEDENT><DEDENT>spec = {inputs: kwargs.get('<STR_LIT>', <NUM_LIT:0>),<EOL>hiddens: kwargs.get('<STR_LIT>', <NUM_LIT:0>),<EOL>outputs: kwargs.get('<STR_LIT>', <NUM_LIT:0>)}<EOL>spec.update(kwargs.get('<STR_LIT>', {}))<EOL>for pattern, w in spec.items():<EOL><INDENT>if w:<EOL><INDENT>regs.append(GaussianNoise(pattern=pattern, weight=w, rng=rng))<EOL><DEDENT><DEDENT>for key, value in kwargs.items():<EOL><INDENT>if Regularizer.is_registered(key):<EOL><INDENT>if not isinstance(value, dict):<EOL><INDENT>value = dict(weight=value)<EOL><DEDENT>regs.append(Regularizer.build(key, **value))<EOL><DEDENT><DEDENT>return regs<EOL>

Construct common regularizers from a set of keyword arguments.

    Keyword arguments not listed below will be passed to
    :func:`Regularizer.build` if they specify the name of a registered
    :class:`Regularizer`.

    Parameters
    ----------
    graph : :class:`theanets.graph.Network`
        A network graph to regularize.

    regularizers : dict or tuple/list of :class:`Regularizer`, optional
        If this is a list or a tuple, the contents of the list will be returned
        as the regularizers. This is to permit custom lists of regularizers to
        be passed easily.

        If this is a dict, its contents will be added to the other keyword
        arguments passed in.

    rng : int or theano RandomStreams, optional
        If an integer is provided, it will be used to seed the random number
        generators for the dropout or noise regularizers. If a theano
        RandomStreams object is provided, it will be used directly. Defaults to
        13.

    input_dropout : float, optional
        Apply dropout to input layers in the network graph, with this dropout
        rate. Defaults to 0 (no dropout).

    hidden_dropout : float, optional
        Apply dropout to hidden layers in the network graph, with this dropout
        rate. Defaults to 0 (no dropout).

    output_dropout : float, optional
        Apply dropout to the output layer in the network graph, with this
        dropout rate. Defaults to 0 (no dropout).

    input_noise : float, optional
        Apply noise to input layers in the network graph, with this standard
        deviation. Defaults to 0 (no noise).

    hidden_noise : float, optional
        Apply noise to hidden layers in the network graph, with this standard
        deviation. Defaults to 0 (no noise).

    output_noise : float, optional
        Apply noise to the output layer in the network graph, with this
        standard deviation. Defaults to 0 (no noise).

    Returns
    -------
    regs : list of :class:`Regularizer`
        A list of regularizers to apply to the given network graph.


util.log('<STR_LIT>'<EOL>'<STR_LIT>', self)<EOL>

Log some diagnostic info about this regularizer.


pass<EOL>

Modify the outputs of a particular layer in the computation graph.

        Parameters
        ----------
        outputs : dict of Theano expressions
            A map from string output names to the corresponding Theano
            expression. This dictionary contains the fully-scoped name of all
            outputs from a single layer in the computation graph.

            This map is mutable, so any changes that the regularizer makes will
            be retained when the caller regains control.

        Notes
        -----

        This method is applied during graph-construction time to change the
        behavior of one or more layer outputs. For example, the
        :class:`BernoulliDropout` class replaces matching outputs with an
        expression containing "masked" outputs, where some elements are randomly
        set to zero each time the expression is evaluated.

        Any regularizer that needs to modify the structure of the computation
        graph should implement this method.


return <NUM_LIT:0.><EOL>

Compute a scalar term to add to the loss function for a model.

        Parameters
        ----------
        layers : list of :class:`theanets.layers.Layer`
            A list of the layers in the model being regularized.
        outputs : dict of Theano expressions
            A dictionary mapping string expression names to their corresponding
            Theano expressions in the computation graph. This dictionary
            contains the fully-scoped name of every layer output in the graph.


util.log('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>', self)<EOL>


def itertrain(self, train, valid=None, **kwargs):

for monitors in downhill.build(<EOL>algo=self.algo,<EOL>loss=self.network.loss(**kwargs),<EOL>updates=self.network.updates(**kwargs),<EOL>monitors=self.network.monitors(**kwargs),<EOL>inputs=self.network.variables,<EOL>params=self.network.params,<EOL>monitor_gradients=kwargs.get('<STR_LIT>', False),<EOL>).iterate(train, valid=valid, **kwargs):<EOL><INDENT>yield monitors<EOL><DEDENT>

Train a model using a training and validation set.

        This method yields a series of monitor values to the caller. After every
        iteration, a pair of monitor dictionaries is generated: one evaluated on
        the training dataset, and another evaluated on the validation dataset.
        The validation monitors might not be updated during every training
        iteration; in this case, the most recent validation monitors will be
        yielded along with the training monitors.

        Parameters
        ----------
        train : :class:`Dataset <theanets.dataset.Dataset>`
            A set of training data for computing updates to model parameters.
        valid : :class:`Dataset <theanets.dataset.Dataset>`
            A set of validation data for computing monitor values and
            determining when the loss has stopped improving.

        Yields
        ------
        training : dict
            A dictionary mapping monitor names to values, evaluated on the
            training dataset.
        validation : dict
            A dictionary containing monitor values evaluated on the validation
            dataset.


@staticmethod<EOL><INDENT>def reservoir(xs, n, rng):<DEDENT>

pool = []<EOL>for i, x in enumerate(xs):<EOL><INDENT>if len(pool) < n:<EOL><INDENT>pool.append(x / np.linalg.norm(x))<EOL>continue<EOL><DEDENT>j = rng.randint(i + <NUM_LIT:1>)<EOL>if j < n:<EOL><INDENT>pool[j] = x / np.linalg.norm(x)<EOL><DEDENT><DEDENT>L = len(pool)<EOL>S = np.std(pool, axis=<NUM_LIT:0>)<EOL>while len(pool) < n:<EOL><INDENT>x = pool[rng.randint(L)]<EOL>pool.append(x + S * rng.randn(*x.shape))<EOL><DEDENT>return np.array(pool, dtype=pool[<NUM_LIT:0>].dtype)<EOL>

Select a random sample of n items from xs.


ifci = itertools.chain.from_iterable<EOL>def first(x):<EOL><INDENT>return x[<NUM_LIT:0>] if isinstance(x, (tuple, list)) else x<EOL><DEDENT>def last(x):<EOL><INDENT>return x[-<NUM_LIT:1>] if isinstance(x, (tuple, list)) else x<EOL><DEDENT>odim = idim = None<EOL>for t in train:<EOL><INDENT>idim = first(t).shape[-<NUM_LIT:1>]<EOL>odim = last(t).shape[-<NUM_LIT:1>]<EOL><DEDENT>rng = kwargs.get('<STR_LIT>')<EOL>if rng is None or isinstance(rng, int):<EOL><INDENT>rng = np.random.RandomState(rng)<EOL><DEDENT>samples = ifci(last(t) for t in train)<EOL>for param in self.network.layers[-<NUM_LIT:1>].params:<EOL><INDENT>shape = param.get_value(borrow=True).shape<EOL>if len(shape) == <NUM_LIT:2> and shape[<NUM_LIT:1>] == odim:<EOL><INDENT>arr = np.vstack(SampleTrainer.reservoir(samples, shape[<NUM_LIT:0>], rng))<EOL>util.log('<STR_LIT>', param.name, shape)<EOL>param.set_value(arr / np.sqrt((arr * arr).sum(axis=<NUM_LIT:1>))[:, None])<EOL><DEDENT><DEDENT>samples = ifci(first(t) for t in train)<EOL>for layer in self.network.layers:<EOL><INDENT>for param in layer.params:<EOL><INDENT>shape = param.get_value(borrow=True).shape<EOL>if len(shape) == <NUM_LIT:2> and shape[<NUM_LIT:0>] == idim:<EOL><INDENT>arr = np.vstack(SampleTrainer.reservoir(samples, shape[<NUM_LIT:1>], rng)).T<EOL>util.log('<STR_LIT>', param.name, shape)<EOL>param.set_value(arr / np.sqrt((arr * arr).sum(axis=<NUM_LIT:0>)))<EOL>samples = ifci(self.network.feed_forward(<EOL>first(t))[i-<NUM_LIT:1>] for t in train)<EOL><DEDENT><DEDENT><DEDENT>yield dict(loss=<NUM_LIT:0>), dict(loss=<NUM_LIT:0>)<EOL>


net = self.network<EOL>original = list(net.layers)<EOL>output_name = original[-<NUM_LIT:1>].output_name<EOL>tied = any(isinstance(l, layers.Tied) for l in original)<EOL>L = <NUM_LIT:1> + len(original) // <NUM_LIT:2> if tied else len(original) - <NUM_LIT:1><EOL>for i in range(<NUM_LIT:1>, L):<EOL><INDENT>tail = []<EOL>if i == L - <NUM_LIT:1>:<EOL><INDENT>net.layers = original<EOL><DEDENT>elif tied:<EOL><INDENT>net.layers = original[:i+<NUM_LIT:1>]<EOL>for j in range(i):<EOL><INDENT>prev = tail[-<NUM_LIT:1>] if tail else net.layers[-<NUM_LIT:1>]<EOL>tail.append(layers.Layer.build(<EOL>'<STR_LIT>', partner=original[i-j].name, inputs=prev.name))<EOL><DEDENT>net.layers = original[:i+<NUM_LIT:1>] + tail<EOL><DEDENT>else:<EOL><INDENT>tail.append(layers.Layer.build(<EOL>'<STR_LIT>',<EOL>name='<STR_LIT>',<EOL>inputs=original[i].output_name,<EOL>size=original[-<NUM_LIT:1>].output_size,<EOL>activation=original[-<NUM_LIT:1>].kwargs['<STR_LIT>']))<EOL>net.layers = original[:i+<NUM_LIT:1>] + tail<EOL><DEDENT>util.log('<STR_LIT>',<EOL>'<STR_LIT>'.join(l.name for l in net.layers))<EOL>[l.bind(net, initialize=False) for l in net.layers]<EOL>[l.setup() for l in tail]<EOL>net.losses[<NUM_LIT:0>].output_name = net.layers[-<NUM_LIT:1>].output_name<EOL>trainer = DownhillTrainer(self.algo, net)<EOL>for monitors in trainer.itertrain(train, valid, **kwargs):<EOL><INDENT>yield monitors<EOL><DEDENT><DEDENT>net.layers = original<EOL>net.losses[<NUM_LIT:0>].output_name = output_name<EOL>


from . import feedforward<EOL>original_layer_names = set(l.name for l in self.network.layers[:-<NUM_LIT:1>])<EOL>layers_ = list(l.to_spec() for l in self.network.layers[:-<NUM_LIT:1>])<EOL>for i, l in enumerate(layers_[::-<NUM_LIT:1>][:-<NUM_LIT:2>]):<EOL><INDENT>layers_.append(dict(<EOL>form='<STR_LIT>', partner=l['<STR_LIT:name>'], activation=l['<STR_LIT>']))<EOL><DEDENT>layers_.append(dict(<EOL>form='<STR_LIT>', partner=layers_[<NUM_LIT:1>]['<STR_LIT:name>'], activation='<STR_LIT>'))<EOL>util.log('<STR_LIT>')<EOL>ae = feedforward.Autoencoder(layers=layers_)<EOL>pre = SupervisedPretrainer(self.algo, ae)<EOL>for monitors in pre.itertrain(train, valid, **kwargs):<EOL><INDENT>yield monitors<EOL><DEDENT>for param in ae.params:<EOL><INDENT>l, p = param.name.split('<STR_LIT:.>')<EOL>if l in original_layer_names:<EOL><INDENT>util.log('<STR_LIT>', param.name)<EOL>self.network.find(l, p).set_value(param.get_value())<EOL><DEDENT><DEDENT>util.log('<STR_LIT>')<EOL>


if isinstance(name, Activation):<EOL><INDENT>return name<EOL><DEDENT>if '<STR_LIT:+>' in name:<EOL><INDENT>return functools.reduce(<EOL>Compose, (build(n, layer, **kwargs) for n in name.split('<STR_LIT:+>')))<EOL><DEDENT>act = COMMON.get(name)<EOL>if act is not None:<EOL><INDENT>act.name = name<EOL>act.params = []<EOL>return act<EOL><DEDENT>if name.lower().startswith('<STR_LIT>') and '<STR_LIT::>' in name:<EOL><INDENT>name, pieces = name.split('<STR_LIT::>', <NUM_LIT:1>)<EOL>kwargs['<STR_LIT>'] = int(pieces)<EOL><DEDENT>kwargs['<STR_LIT:name>'] = name<EOL>kwargs['<STR_LIT>'] = layer<EOL>return Activation.build(name, **kwargs)<EOL>

Construct an activation function by name.

    Parameters
    ----------
    name : str or :class:`Activation`
        The name of the type of activation function to build, or an
        already-created instance of an activation function.
    layer : :class:`theanets.layers.Layer`
        The layer to which this activation will be applied.
    kwargs : dict
        Additional named arguments to pass to the activation constructor.

    Returns
    -------
    activation : :class:`Activation`
        A neural network activation function instance.


raise NotImplementedError<EOL>

Compute a symbolic expression for this activation function.

        Parameters
        ----------
        x : Theano expression
            A Theano expression representing the input to this activation
            function.

        Returns
        -------
        y : Theano expression
            A Theano expression representing the output from this activation
            function.


return self._params + getattr(self.activate, '<STR_LIT>', [])<EOL>


if len(self._input_shapes) != <NUM_LIT:1>:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'<EOL>.format(self.name, self._input_shapes))<EOL><DEDENT>return list(self._input_shapes)[<NUM_LIT:0>]<EOL>

Name of layer input (for layers with one input).


return self._input_shapes[self.input_name]<EOL>

Shape of layer input (for layers with one input).


shape = self.input_shape<EOL>if shape is None:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'.format(self.name))<EOL><DEDENT>return shape[-<NUM_LIT:1>]<EOL>

Size of layer input (for layers with one input).


return self.full_name('<STR_LIT>')<EOL>

Full name of the default output for this layer.


return self._output_shapes['<STR_LIT>']<EOL>

Shape of default output from this layer.


shape = self.output_shape<EOL>if shape is None:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'.format(self.name))<EOL><DEDENT>return shape[-<NUM_LIT:1>]<EOL>

Number of "neurons" in this layer's default output.


return '<STR_LIT>'.format(self.name, name)<EOL>

Return a fully-scoped name for the given layer output.

        Parameters
        ----------
        name : str
            Name of an output for this layer.

        Returns
        -------
        scoped : str
            A fully-scoped name for the given output from this layer.


outputs, updates = self.transform(inputs)<EOL>if isinstance(outputs, dict):<EOL><INDENT>outputs = sorted(outputs.items())<EOL><DEDENT>if isinstance(outputs, (TT.TensorVariable, SS.SparseVariable)):<EOL><INDENT>outputs = [('<STR_LIT>', outputs)]<EOL><DEDENT>outs = {self.full_name(name): expr for name, expr in outputs}<EOL>return outs, updates<EOL>

Create Theano variables representing the outputs of this layer.

        Parameters
        ----------
        inputs : dict of Theano expressions
            Symbolic inputs to this layer, given as a dictionary mapping string
            names to Theano expressions. Each string key should be of the form
            "{layer_name}:{output_name}" and refers to a specific output from
            a specific layer in the graph.

        Returns
        -------
        outputs : dict
            A dictionary mapping names to Theano expressions for the outputs
            from this layer.
        updates : sequence of (parameter, expression) tuples
            Updates that should be performed by a Theano function that computes
            something using this layer.


raise NotImplementedError<EOL>

Transform the inputs for this layer into an output for the layer.

        Parameters
        ----------
        inputs : dict of Theano expressions
            Symbolic inputs to this layer, given as a dictionary mapping string
            names to Theano expressions. See :func:`Layer.connect`.

        Returns
        -------
        output : Theano expression
            The output for this layer is the same as the input.
        updates : list
            An empty updates list.


def bind(self, graph, reset=True, initialize=True):

if reset:<EOL><INDENT>for k in self._input_shapes:<EOL><INDENT>self._input_shapes[k] = None<EOL><DEDENT>for k in self._output_shapes:<EOL><INDENT>self._output_shapes[k] = None<EOL><DEDENT><DEDENT>self.resolve_inputs(graph.layers)<EOL>self.resolve_outputs()<EOL>self.activate = activations.build(<EOL>self.kwargs.get('<STR_LIT>', '<STR_LIT:relu>'), self)<EOL>if initialize:<EOL><INDENT>self.setup()<EOL><DEDENT>self.log()<EOL>

Bind this layer into a computation graph.

        This method is a wrapper for performing common initialization tasks. It
        calls :func:`resolve`, :func:`setup`, and :func:`log`.

        Parameters
        ----------
        graph : :class:`Network <theanets.graph.Network>`
            A computation network in which this layer is to be bound.
        reset : bool, optional
            If ``True`` (the default), reset the resolved layers for this layer.
        initialize : bool, optional
            If ``True`` (the default), initialize the parameters for this layer
            by calling :func:`setup`.

        Raises
        ------
        theanets.util.ConfigurationError :
            If an input cannot be resolved.


resolved = {}<EOL>for name, shape in self._input_shapes.items():<EOL><INDENT>if shape is None:<EOL><INDENT>name, shape = self._resolve_shape(name, layers)<EOL><DEDENT>resolved[name] = shape<EOL><DEDENT>self._input_shapes = resolved<EOL>

Resolve the names of inputs for this layer into shape tuples.

        Parameters
        ----------
        layers : list of :class:`Layer`
            A list of the layers that are available for resolving inputs.

        Raises
        ------
        theanets.util.ConfigurationError :
            If an input cannot be resolved.


input_shape = None<EOL>for i, shape in enumerate(self._input_shapes.values()):<EOL><INDENT>if i == <NUM_LIT:0>:<EOL><INDENT>input_shape = shape<EOL><DEDENT>if len(input_shape) != len(shape) or any(<EOL>a is not None and b is not None and a != b<EOL>for a, b in zip(input_shape[:-<NUM_LIT:1>], shape[:-<NUM_LIT:1>])):<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'<EOL>.format(self.name, self._input_shapes))<EOL><DEDENT><DEDENT>size = self.kwargs.get('<STR_LIT:size>')<EOL>shape = self.kwargs.get('<STR_LIT>')<EOL>if shape is not None:<EOL><INDENT>pass<EOL><DEDENT>elif size is not None:<EOL><INDENT>shape = tuple(input_shape[:-<NUM_LIT:1>]) + (size, )<EOL><DEDENT>else:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'.format(self.name))<EOL><DEDENT>self._output_shapes['<STR_LIT>'] = shape<EOL>

Resolve the names of outputs for this layer into shape tuples.


pass<EOL>

Set up the parameters and initial values for this layer.


inputs = '<STR_LIT:U+002CU+0020>'.join('<STR_LIT>'.format(*ns) for ns in self._input_shapes.items())<EOL>util.log('<STR_LIT>',<EOL>self, getattr(self.activate, '<STR_LIT:name>', self.activate), inputs)<EOL>util.log('<STR_LIT>', self.log_params())<EOL>


total = <NUM_LIT:0><EOL>for p in self.params:<EOL><INDENT>shape = p.get_value().shape<EOL>util.log('<STR_LIT>', p.name, shape)<EOL>total += np.prod(shape)<EOL><DEDENT>return total<EOL>

Log information about this layer's parameters.


if '<STR_LIT:{>' not in string:<EOL><INDENT>string = '<STR_LIT>' + string<EOL><DEDENT>return string.format(self.name)<EOL>

Helper method to format our name into a string.


matches = [l for l in layers if name.split('<STR_LIT::>')[<NUM_LIT:0>] == l.name]<EOL>if len(matches) != <NUM_LIT:1>:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'<EOL>.format(self.name, name, [l.name for l in layers]))<EOL><DEDENT>name = name if '<STR_LIT::>' in name else matches[<NUM_LIT:0>].output_name<EOL>return name, matches[<NUM_LIT:0>]._output_shapes[name.split('<STR_LIT::>')[<NUM_LIT:1>]]<EOL>

Given a list of layers, find the layer output with the given name.

        Parameters
        ----------
        name : str
            Name of a layer to resolve.
        layers : list of :class:`theanets.layers.base.Layer`
            A list of layers to search in.

        Raises
        ------
        util.ConfigurationError :
            If there is no such layer, or if there are more than one.

        Returns
        -------
        name : str
            The fully-scoped name of the desired output.
        shape : tuple of None and/or int
            The shape of the named output.


name = self._fmt(str(key))<EOL>for i, p in enumerate(self._params):<EOL><INDENT>if key == i or name == p.name:<EOL><INDENT>return p<EOL><DEDENT><DEDENT>raise KeyError(key)<EOL>

Get a shared variable for a parameter by name.

        Parameters
        ----------
        key : str or int
            The name of the parameter to look up, or the index of the parameter
            in our parameter list. These are both dependent on the
            implementation of the layer.

        Returns
        -------
        param : shared variable
            A shared variable containing values for the given parameter.

        Raises
        ------
        KeyError
            If a param with the given name does not exist.


def add_weights(self, name, nin, nout, mean=<NUM_LIT:0>, std=<NUM_LIT:0>, sparsity=<NUM_LIT:0>, diagonal=<NUM_LIT:0>):

glorot = <NUM_LIT:1> / np.sqrt(nin + nout)<EOL>m = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', mean))<EOL>s = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', std or glorot))<EOL>p = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', sparsity))<EOL>d = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', diagonal))<EOL>self._params.append(theano.shared(<EOL>util.random_matrix(nin, nout, mean=m, std=s, sparsity=p,<EOL>diagonal=d, rng=self.rng),<EOL>name=self._fmt(name)))<EOL>

Helper method to create a new weight matrix.

        Parameters
        ----------
        name : str
            Name of the parameter to add.
        nin : int
            Size of "input" for this weight matrix.
        nout : int
            Size of "output" for this weight matrix.
        mean : float, optional
            Mean value for randomly-initialized weights. Defaults to 0.
        std : float, optional
            Standard deviation of initial matrix values. Defaults to
            :math:`1 / sqrt(n_i + n_o)`.
        sparsity : float, optional
            Fraction of weights to be set to zero. Defaults to 0.
        diagonal : float, optional
            Initialize weights to a matrix of zeros with this value along the
            diagonal. Defaults to None, which initializes all weights randomly.


def add_bias(self, name, size, mean=<NUM_LIT:0>, std=<NUM_LIT:1>):

mean = self.kwargs.get('<STR_LIT>'.format(name), mean)<EOL>std = self.kwargs.get('<STR_LIT>'.format(name), std)<EOL>self._params.append(theano.shared(<EOL>util.random_vector(size, mean, std, rng=self.rng),<EOL>name=self._fmt(name)))<EOL>

Helper method to create a new bias vector.

        Parameters
        ----------
        name : str
            Name of the parameter to add.
        size : int
            Size of the bias vector.
        mean : float, optional
            Mean value for randomly-initialized biases. Defaults to 0.
        std : float, optional
            Standard deviation for randomly-initialized biases. Defaults to 1.


spec = dict(**self.kwargs)<EOL>spec.update(<EOL>form=self.__class__.__name__.lower(),<EOL>name=self.name,<EOL>activation=self.kwargs.get('<STR_LIT>', '<STR_LIT:relu>'),<EOL>)<EOL>return spec<EOL>

Create a specification dictionary for this layer.

        Returns
        -------
        spec : dict
            A dictionary specifying the configuration of this layer.


def add_conv_weights(self, name, mean=<NUM_LIT:0>, std=None, sparsity=<NUM_LIT:0>):

nin = self.input_size<EOL>nout = self.output_size<EOL>mean = self.kwargs.get(<EOL>'<STR_LIT>'.format(name),<EOL>self.kwargs.get('<STR_LIT>', mean))<EOL>std = self.kwargs.get(<EOL>'<STR_LIT>'.format(name),<EOL>self.kwargs.get('<STR_LIT>', std or <NUM_LIT:1> / np.sqrt(nin + nout)))<EOL>sparsity = self.kwargs.get(<EOL>'<STR_LIT>'.format(name),<EOL>self.kwargs.get('<STR_LIT>', sparsity))<EOL>arr = np.zeros((nout, nin) + self.filter_size, util.FLOAT)<EOL>for r in range(self.filter_size[<NUM_LIT:0>]):<EOL><INDENT>for c in range(self.filter_size[<NUM_LIT:1>]):<EOL><INDENT>arr[:, :, r, c] = util.random_matrix(<EOL>nout, nin, mean, std, sparsity=sparsity, rng=self.rng)<EOL><DEDENT><DEDENT>self._params.append(theano.shared(arr, name=self._fmt(name)))<EOL>

Add a convolutional weight array to this layer's parameters.

        Parameters
        ----------
        name : str
            Name of the parameter to add.
        mean : float, optional
            Mean value for randomly-initialized weights. Defaults to 0.
        std : float, optional
            Standard deviation of initial matrix values. Defaults to
            :math:`1 / sqrt(n_i + n_o)`.
        sparsity : float, optional
            Fraction of weights to set to zero. Defaults to 0.


def add_weights(self, name, nin, nout, mean=<NUM_LIT:0>, std=<NUM_LIT:0>, sparsity=<NUM_LIT:0>, radius=<NUM_LIT:0>,<EOL>diagonal=<NUM_LIT:0>):

glorot = <NUM_LIT:1> / np.sqrt(nin + nout)<EOL>mean = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', mean))<EOL>std = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', std or glorot))<EOL>s = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', sparsity))<EOL>r = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', radius))<EOL>d = self.kwargs.get(<EOL>'<STR_LIT>'.format(name), self.kwargs.get('<STR_LIT>', diagonal))<EOL>if nin == self.output_size and nout % nin == <NUM_LIT:0>:<EOL><INDENT>arr = np.concatenate([<EOL>util.random_matrix(nin, nin, mean, std, sparsity=s, radius=r,<EOL>diagonal=d, rng=self.rng)<EOL>for _ in range(nout // nin)], axis=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>arr = util.random_matrix(nin, nout, mean, std, sparsity=s, rng=self.rng)<EOL><DEDENT>self._params.append(theano.shared(arr, name=self._fmt(name)))<EOL>

Helper method to create a new weight matrix.

        Parameters
        ----------
        name : str
            Name of parameter to define.
        nin : int, optional
            Size of "input" for this weight matrix. Defaults to self.nin.
        nout : int, optional
            Size of "output" for this weight matrix. Defaults to self.nout.
        mean : float, optional
            Mean of initial matrix values. Defaults to 0.
        std : float, optional
            Standard deviation of initial matrix values. Defaults to
            :math:`1 / sqrt(n_i + n_o)`.
        sparsity : float, optional
            Fraction of weights to set randomly to zero. Defaults to 0.
        radius : float, optional
            If nonzero, rescale initial weights to have this spectral radius.
            Defaults to 0.


def _scan(self, inputs, outputs, name='<STR_LIT>', step=None, constants=None):

init = []<EOL>for i, x in enumerate(outputs):<EOL><INDENT>ndim = getattr(x, '<STR_LIT>', -<NUM_LIT:1>)<EOL>if x is None or isinstance(x, dict) or ndim > <NUM_LIT:0>:<EOL><INDENT>init.append(x)<EOL>continue<EOL><DEDENT>if isinstance(x, int) or ndim == <NUM_LIT:0>:<EOL><INDENT>init.append(TT.repeat(theano.shared(<EOL>np.zeros((<NUM_LIT:1>, self.output_size), util.FLOAT),<EOL>name=self._fmt('<STR_LIT>'.format(i))), x, axis=<NUM_LIT:0>))<EOL>continue<EOL><DEDENT>raise ValueError('<STR_LIT>'.format(x))<EOL><DEDENT>return theano.scan(<EOL>step or self._step,<EOL>name=self._fmt(name),<EOL>sequences=inputs,<EOL>outputs_info=init,<EOL>non_sequences=constants,<EOL>go_backwards='<STR_LIT>' in self.kwargs.get('<STR_LIT>', '<STR_LIT>').lower(),<EOL>truncate_gradient=self.kwargs.get('<STR_LIT>', -<NUM_LIT:1>),<EOL>)<EOL>

Helper method for defining a basic loop in theano.

        Parameters
        ----------
        inputs : sequence of theano expressions
            Inputs to the scan operation.
        outputs : sequence of output specifiers
            Specifiers for the outputs of the scan operation. This should be a
            sequence containing:
            - None for values that are output by the scan but not tapped as
              inputs,
            - an integer or theano scalar (``ndim == 0``) indicating the batch
              size for initial zero state,
            - a theano tensor variable (``ndim > 0``) containing initial state
              data, or
            - a dictionary containing a full output specifier. See
              ``outputs_info`` in the Theano documentation for ``scan``.
        name : str, optional
            Name of the scan variable to create. Defaults to ``'scan'``.
        step : callable, optional
            The callable to apply in the loop. Defaults to :func:`self._step`.
        constants : sequence of tensor, optional
            A sequence of parameters, if any, needed by the step function.

        Returns
        -------
        output(s) : theano expression(s)
            Theano expression(s) representing output(s) from the scan.
        updates : sequence of update tuples
            A sequence of updates to apply inside a theano function.


def _create_rates(self, dist='<STR_LIT>', size=None, eps=<NUM_LIT>):

if size is None:<EOL><INDENT>size = self.output_size<EOL><DEDENT>if dist == '<STR_LIT>':<EOL><INDENT>z = np.random.uniform(eps, <NUM_LIT:1> - eps, size=size).astype(util.FLOAT)<EOL>return theano.shared(z, name=self._fmt('<STR_LIT>'))<EOL><DEDENT>if dist == '<STR_LIT>':<EOL><INDENT>z = np.random.uniform(-<NUM_LIT:6>, -eps, size=size).astype(util.FLOAT)<EOL>return theano.shared(np.exp(z), name=self._fmt('<STR_LIT>'))<EOL><DEDENT>return None<EOL>

Create a rate parameter (usually for a recurrent network layer).

        Parameters
        ----------
        dist : {'uniform', 'log'}, optional
            Distribution of rate values. Defaults to ``'uniform'``.
        size : int, optional
            Number of rates to create. Defaults to ``self.output_size``.
        eps : float, optional
            A "buffer" preventing rate values from getting too close to 0 or 1.
            Defaults to 1e-4.

        Returns
        -------
        rates : theano shared or None
            A vector of rate parameters for certain types of recurrent layers.


return self.network.train(*args, **kwargs)<EOL>

Train the network until the trainer converges.

        All arguments are passed to :func:`train
        <theanets.graph.Network.itertrain>`.

        Returns
        -------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.


return self.network.itertrain(*args, **kwargs)<EOL>

Train the network iteratively.

        All arguments are passed to :func:`itertrain
        <theanets.graph.Network.itertrain>`.

        Yields
        ------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.


self.network.save(path)<EOL>

Save the current network to a pickle file on disk.

        Parameters
        ----------
        path : str
            Location of the file to save the network.


self.network = graph.Network.load(path)<EOL>return self.network<EOL>

Load a saved network from a pickle file on disk.

        This method sets the ``network`` attribute of the experiment to the
        loaded network model.

        Parameters
        ----------
        filename : str
            Load the keyword arguments and parameters of a network from a pickle
            file at the named path. If this name ends in ".gz" then the input
            will automatically be gunzipped; otherwise the input will be treated
            as a "raw" pickle.

        Returns
        -------
        network : :class:`Network <graph.Network>`
            A newly-constructed network, with topology and parameters loaded
            from the given pickle file.


def add_layer(self, layer=None, **kwargs):

<EOL>if isinstance(layer, layers.Layer):<EOL><INDENT>self.layers.append(layer)<EOL>return<EOL><DEDENT>form = kwargs.pop('<STR_LIT>', '<STR_LIT>' if self.layers else '<STR_LIT:input>').lower()<EOL>if isinstance(layer, util.basestring):<EOL><INDENT>if not layers.Layer.is_registered(layer):<EOL><INDENT>raise util.ConfigurationError('<STR_LIT>'.format(layer))<EOL><DEDENT>form = layer<EOL>layer = None<EOL><DEDENT>if isinstance(layer, (tuple, list)) and all(isinstance(x, int) for x in layer):<EOL><INDENT>kwargs['<STR_LIT>'] = tuple(layer)<EOL>layer = None<EOL><DEDENT>if isinstance(layer, (tuple, list)):<EOL><INDENT>for el in layer:<EOL><INDENT>if isinstance(el, util.basestring) and layers.Layer.is_registered(el):<EOL><INDENT>form = el<EOL><DEDENT>elif isinstance(el, util.basestring):<EOL><INDENT>kwargs['<STR_LIT>'] = el<EOL><DEDENT>elif isinstance(el, int):<EOL><INDENT>if '<STR_LIT:size>' in kwargs:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'.format(kwargs))<EOL><DEDENT>kwargs['<STR_LIT:size>'] = el<EOL><DEDENT><DEDENT>layer = None<EOL><DEDENT>if isinstance(layer, dict):<EOL><INDENT>for key, value in layer.items():<EOL><INDENT>if key == '<STR_LIT>':<EOL><INDENT>form = value.lower()<EOL><DEDENT>else:<EOL><INDENT>kwargs[key] = value<EOL><DEDENT><DEDENT>layer = None<EOL><DEDENT>if '<STR_LIT>' not in kwargs and '<STR_LIT:size>' not in kwargs and isinstance(layer, int):<EOL><INDENT>kwargs['<STR_LIT:size>'] = layer<EOL><DEDENT>if form == '<STR_LIT:input>' and '<STR_LIT>' not in kwargs:<EOL><INDENT>kwargs.setdefault('<STR_LIT>', self.INPUT_NDIM)<EOL><DEDENT>if form != '<STR_LIT:input>':<EOL><INDENT>kwargs.setdefault('<STR_LIT>', self.layers[-<NUM_LIT:1>].output_name)<EOL>kwargs.setdefault('<STR_LIT>', self._rng)<EOL><DEDENT>if form.lower() == '<STR_LIT>' and '<STR_LIT>' not in kwargs:<EOL><INDENT>tied = <NUM_LIT:1><EOL>partner = None<EOL>for l in self.layers[::-<NUM_LIT:1>]:<EOL><INDENT>tied += <NUM_LIT:1> if isinstance(l, layers.Tied) else -<NUM_LIT:1><EOL>if tied == <NUM_LIT:0>:<EOL><INDENT>partner = l.name<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'.format(kwargs))<EOL><DEDENT>kwargs['<STR_LIT>'] = partner<EOL><DEDENT>layer = layers.Layer.build(form, **kwargs)<EOL>if isinstance(layer, layers.Input):<EOL><INDENT>if any(layer.name == i.name for i in self.inputs):<EOL><INDENT>raise util.ConfigurationError(<EOL>'<STR_LIT>'.format(layer.name))<EOL><DEDENT><DEDENT>self.layers.append(layer)<EOL>

Add a :ref:`layer <layers>` to our network graph.

        Parameters
        ----------
        layer : int, tuple, dict, or :class:`Layer <theanets.layers.base.Layer>`
            A value specifying the layer to add. For more information, please
            see :ref:`guide-creating-specifying-layers`.


def add_loss(self, loss=None, **kwargs):

if isinstance(loss, losses.Loss):<EOL><INDENT>self.losses.append(loss)<EOL>return<EOL><DEDENT>form = loss or '<STR_LIT>'<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>form = kwargs.pop('<STR_LIT>').lower()<EOL><DEDENT>kw = dict(target=self.INPUT_NDIM, output_name=self.layers[-<NUM_LIT:1>].output_name)<EOL>kw.update(kwargs)<EOL>if isinstance(loss, dict):<EOL><INDENT>loss = dict(loss)<EOL>if '<STR_LIT>' in loss:<EOL><INDENT>form = loss.pop('<STR_LIT>').lower()<EOL><DEDENT>kw.update(loss)<EOL><DEDENT>self.losses.append(losses.Loss.build(form, **kw))<EOL>

Add a :ref:`loss function <losses>` to the model.

        Parameters
        ----------
        loss : str, dict, or :class:`theanets.losses.Loss`
            A loss function to add. If this is a Loss instance, it will be added
            immediately. If this is a string, it names a loss function to build
            and add. If it is a dictionary, it should contain a ``'form'`` key
            whose string value names the loss function to add. Other arguments
            will be passed to :func:`theanets.losses.Loss.build`.


self.losses = []<EOL>self.add_loss(*args, **kwargs)<EOL>

Clear the current loss functions from the network and add a new one.

        All parameters and keyword arguments are passed to :func:`add_loss`
        after clearing the current losses.


def itertrain(self, train, valid=None, algo='<STR_LIT>', subalgo='<STR_LIT>',<EOL>save_every=<NUM_LIT:0>, save_progress=None, **kwargs):

if '<STR_LIT>' not in kwargs:<EOL><INDENT>kwargs['<STR_LIT>'] = self._rng<EOL><DEDENT>def create_dataset(data, **kwargs):<EOL><INDENT>name = kwargs.get('<STR_LIT:name>', '<STR_LIT>')<EOL>s = '<STR_LIT>'.format(name)<EOL>return downhill.Dataset(<EOL>data,<EOL>name=name,<EOL>batch_size=kwargs.get('<STR_LIT>', <NUM_LIT:32>),<EOL>iteration_size=kwargs.get('<STR_LIT>', kwargs.get(s)),<EOL>axis=kwargs.get('<STR_LIT>', <NUM_LIT:0>),<EOL>rng=kwargs['<STR_LIT>'])<EOL><DEDENT>if valid is None:<EOL><INDENT>valid = train<EOL><DEDENT>if not isinstance(valid, downhill.Dataset):<EOL><INDENT>valid = create_dataset(valid, name='<STR_LIT>', **kwargs)<EOL><DEDENT>if not isinstance(train, downhill.Dataset):<EOL><INDENT>train = create_dataset(train, name='<STR_LIT:train>', **kwargs)<EOL><DEDENT>if '<STR_LIT>' in kwargs:<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>',<EOL>DeprecationWarning)<EOL>algo = kwargs.pop('<STR_LIT>')<EOL>if isinstance(algo, (list, tuple)):<EOL><INDENT>algo = algo[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>if isinstance(algo, util.basestring):<EOL><INDENT>algo = algo.lower()<EOL>if algo == '<STR_LIT>':<EOL><INDENT>algo = trainer.SampleTrainer(self)<EOL><DEDENT>elif algo.startswith('<STR_LIT>') or algo.startswith('<STR_LIT>'):<EOL><INDENT>algo = trainer.SupervisedPretrainer(subalgo, self)<EOL><DEDENT>elif algo.startswith('<STR_LIT>') or algo.startswith('<STR_LIT>'):<EOL><INDENT>algo = trainer.UnsupervisedPretrainer(subalgo, self)<EOL><DEDENT>else:<EOL><INDENT>algo = trainer.DownhillTrainer(algo, self)<EOL><DEDENT><DEDENT>def needs_saving(elapsed, iteration):<EOL><INDENT>if save_progress is None:<EOL><INDENT>return False<EOL><DEDENT>if isinstance(save_every, float):<EOL><INDENT>return elapsed > <NUM_LIT> * save_every<EOL><DEDENT>if isinstance(save_every, int):<EOL><INDENT>return iteration % save_every == <NUM_LIT:0><EOL><DEDENT>return False<EOL><DEDENT>start = time.time()<EOL>for i, monitors in enumerate(algo.itertrain(train, valid, **kwargs)):<EOL><INDENT>yield monitors<EOL>now = time.time()<EOL>if i and needs_saving(now - start, i):<EOL><INDENT>filename_or_handle = save_progress<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>filename_or_handle = save_progress.format(int(now))<EOL><DEDENT>self.save(filename_or_handle)<EOL>start = now<EOL><DEDENT><DEDENT>

Train our network, one batch at a time.

        This method yields a series of ``(train, valid)`` monitor pairs. The
        ``train`` value is a dictionary mapping names to monitor values
        evaluated on the training dataset. The ``valid`` value is also a
        dictionary mapping names to values, but these values are evaluated on
        the validation dataset.

        Because validation might not occur every training iteration, the
        validation monitors might be repeated for multiple training iterations.
        It is probably most helpful to think of the validation monitors as being
        the "most recent" values that have been computed.

        After training completes, the network attribute of this class will
        contain the trained network parameters.

        Parameters
        ----------
        train : :class:`Dataset <downhill.dataset.Dataset>` or list
            A dataset to use when training the network. If this is a
            ``downhill.Dataset`` instance, it will be used directly as the
            training datset. If it is a list of numpy arrays or a list of
            callables, it will be converted to a ``downhill.Dataset`` and then
            used as the training set.
        valid : :class:`Dataset <downhill.dataset.Dataset>` or list, optional
            If this is provided, it will be used as a validation dataset. If not
            provided, the training set will be used for validation. (This is not
            recommended!)
        algo : str, optional
            An optimization algorithm to use for training our network. If not
            provided, :class:`RMSProp <downhill.adaptive.RMSProp>` will be used.
        subalgo : str, optional
            An optimization algorithm to use for a trainer that requires a
            "sub-algorithm," sugh as an unsupervised pretrainer. Defaults to
            :class:`RMSProp <downhill.adaptive.RMSProp>`.
        save_every : int or float, optional
            If this is nonzero and ``save_progress`` is not None, then the model
            being trained will be saved periodically. If this is a float, it is
            treated as a number of minutes to wait between savings. If it is an
            int, it is treated as the number of training epochs to wait between
            savings. Defaults to 0.
        save_progress : str or file handle, optional
            If this is not None, and ``save_progress`` is nonzero, then save the
            model periodically during training. This parameter gives either (a)
            the full path of a file to save the model, or (b) a file-like object
            where the model should be saved. If it is a string and the given
            name contains a "{}" format specifier, it will be filled with the
            integer Unix timestamp at the time the model is saved. Defaults to
            None, which does not save models.

        Yields
        ------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.


monitors = None<EOL>for monitors in self.itertrain(*args, **kwargs):<EOL><INDENT>pass<EOL><DEDENT>return monitors<EOL>

Train the network until the trainer converges.

        All arguments are passed to :func:`itertrain`.

        Returns
        -------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.


def add(s):<EOL><INDENT>h.update(str(s).encode('<STR_LIT:utf-8>'))<EOL><DEDENT>h = hashlib.md5()<EOL>for l in self.layers:<EOL><INDENT>add('<STR_LIT>'.format(l.__class__.__name__, l.name, l.output_shape))<EOL><DEDENT>for l in self.losses:<EOL><INDENT>add('<STR_LIT>'.format(l.__class__.__name__, l.weight))<EOL><DEDENT>for r in regularizers:<EOL><INDENT>add('<STR_LIT>'.format(r.__class__.__name__, r.weight, r.pattern))<EOL><DEDENT>return h.hexdigest()<EOL>

Construct a string key for representing a computation graph.

        This key will be unique for a given (a) network topology, (b) set of
        losses, and (c) set of regularizers.

        Returns
        -------
        key : str
            A hash representing the computation graph for the current network.


key = self._hash(regularizers)<EOL>if key not in self._graphs:<EOL><INDENT>util.log('<STR_LIT>')<EOL>for loss in self.losses:<EOL><INDENT>loss.log()<EOL><DEDENT>for reg in regularizers:<EOL><INDENT>reg.log()<EOL><DEDENT>outputs = {}<EOL>updates = []<EOL>for layer in self.layers:<EOL><INDENT>out, upd = layer.connect(outputs)<EOL>for reg in regularizers:<EOL><INDENT>reg.modify_graph(out)<EOL><DEDENT>outputs.update(out)<EOL>updates.extend(upd)<EOL><DEDENT>self._graphs[key] = outputs, updates<EOL><DEDENT>return self._graphs[key]<EOL>

Connect the layers in this network to form a computation graph.

        Parameters
        ----------
        regularizers : list of :class:`theanets.regularizers.Regularizer`
            A list of the regularizers to apply while building the computation
            graph.

        Returns
        -------
        outputs : list of Theano variables
            A list of expressions giving the output of each layer in the graph.
        updates : list of update tuples
            A list of updates that should be performed by a Theano function that
            computes something using this graph.


return [l.input for l in self.layers if isinstance(l, layers.Input)]<EOL>

A list of Theano variables for feedforward computations.


result = self.inputs<EOL>seen = set(i.name for i in result)<EOL>for loss in self.losses:<EOL><INDENT>for v in loss.variables:<EOL><INDENT>if v.name not in seen:<EOL><INDENT>result.append(v)<EOL>seen.add(v.name)<EOL><DEDENT><DEDENT><DEDENT>return result<EOL>

A list of Theano variables for loss computations.


return [p for l in self.layers for p in l.params]<EOL>

A list of the learnable Theano parameters for this network.


for i, layer in enumerate(self.layers):<EOL><INDENT>if which == i or which == layer.name:<EOL><INDENT>return layer.find(param)<EOL><DEDENT><DEDENT>raise KeyError(which)<EOL>

Get a parameter from a layer in the network.

        Parameters
        ----------
        which : int or str
            The layer that owns the parameter to return.

            If this is an integer, then 0 refers to the input layer, 1 refers
            to the first hidden layer, 2 to the second, and so on.

            If this is a string, the layer with the corresponding name, if any,
            will be used.

        param : int or str
            Name of the parameter to retrieve from the specified layer, or its
            index in the parameter list of the layer.

        Raises
        ------
        KeyError
            If there is no such layer, or if there is no such parameter in the
            specified layer.

        Returns
        -------
        param : Theano shared variable
            A shared parameter variable from the indicated layer.


regs = regularizers.from_kwargs(self, **kwargs)<EOL>key = self._hash(regs)<EOL>if key not in self._functions:<EOL><INDENT>outputs, updates = self.build_graph(regs)<EOL>labels, exprs = list(outputs.keys()), list(outputs.values())<EOL>util.log('<STR_LIT>')<EOL>self._functions[key] = (labels, theano.function(<EOL>self.inputs, exprs, updates=updates))<EOL><DEDENT>labels, f = self._functions[key]<EOL>return dict(zip(labels, f(x)))<EOL>

Compute a forward pass of all layers from the given input.

        All keyword arguments are passed directly to :func:`build_graph`.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        layers : list of ndarray (num-examples, num-units)
            The activation values of each layer in the the network when given
            input `x`. For each of the hidden layers, an array is returned
            containing one row per input example; the columns of each array
            correspond to units in the respective layer. The "output" of the
            network is the last element of this list.


return self.feed_forward(x, **kwargs)[self.layers[-<NUM_LIT:1>].output_name]<EOL>

Compute a forward pass of the inputs, returning the network output.

        All keyword arguments end up being passed to :func:`build_graph`.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        y : ndarray (num-examples, num-variables)
            Returns the values of the network output units when given input `x`.
            Rows in this array correspond to examples, and columns to output
            variables.


u = y - self.predict(x, **kwargs)<EOL>v = y - y.mean()<EOL>if w is None:<EOL><INDENT>w = np.ones_like(u)<EOL><DEDENT>return <NUM_LIT:1> - (w * u * u).sum() / (w * v * v).sum()<EOL>

Compute R^2 coefficient of determination for a given labeled input.

        Parameters
        ----------
        x : ndarray (num-examples, num-inputs)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.
        y : ndarray (num-examples, num-outputs)
            An array containing expected target data for the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        r2 : float
            The R^2 correlation between the prediction of this netork and its
            target output.


if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>opener = gzip.open if filename_or_handle.lower().endswith('<STR_LIT>') else open<EOL>handle = opener(filename_or_handle, '<STR_LIT:wb>')<EOL><DEDENT>else:<EOL><INDENT>handle = filename_or_handle<EOL><DEDENT>pickle.dump(self, handle, -<NUM_LIT:1>)<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>handle.close()<EOL><DEDENT>util.log('<STR_LIT>', filename_or_handle)<EOL>

Save the state of this network to a pickle file on disk.

        Parameters
        ----------
        filename_or_handle : str or file handle
            Save the state of this network to a pickle file. If this parameter
            is a string, it names the file where the pickle will be saved. If it
            is a file-like object, this object will be used for writing the
            pickle. If the filename ends in ".gz" then the output will
            automatically be gzipped.


assert not isinstance(cls, Network),'<STR_LIT>'<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>opener = gzip.open if filename_or_handle.lower().endswith('<STR_LIT>') else open<EOL>handle = opener(filename_or_handle, '<STR_LIT:rb>')<EOL><DEDENT>else:<EOL><INDENT>handle = filename_or_handle<EOL><DEDENT>model = pickle.load(handle)<EOL>if isinstance(filename_or_handle, util.basestring):<EOL><INDENT>handle.close()<EOL><DEDENT>util.log('<STR_LIT>', filename_or_handle)<EOL>return model<EOL>

Load a saved network from disk.

        Parameters
        ----------
        filename_or_handle : str or file handle
            Load the state of this network from a pickle file. If this parameter
            is a string, it names the file where the pickle will be saved. If it
            is a file-like object, this object will be used for reading the
            pickle. If the filename ends in ".gz" then the output will
            automatically be gunzipped.


regs = regularizers.from_kwargs(self, **kwargs)<EOL>outputs, _ = self.build_graph(regs)<EOL>return sum(l.weight * l(outputs) for l in self.losses) +sum(r.weight * r.loss(self.layers, outputs) for r in regs)<EOL>

Return a variable representing the regularized loss for this network.

        The regularized loss includes both the :ref:`loss computation <losses>`
        for the network as well as any :ref:`regularizers <regularizers>` that
        are in place.

        Keyword arguments are passed directly to
        :func:`theanets.regularizers.from_kwargs`.

        Returns
        -------
        loss : Theano expression
            A Theano expression representing the loss of this network.


regs = regularizers.from_kwargs(self, **kwargs)<EOL>outputs, _ = self.build_graph(regs)<EOL>monitors = [('<STR_LIT>', self.losses[<NUM_LIT:0>](outputs))]<EOL>def matching(pattern):<EOL><INDENT>'''<STR_LIT>'''<EOL>for name, expr in util.outputs_matching(outputs, pattern):<EOL><INDENT>yield name, expr<EOL><DEDENT>for name, expr in util.params_matching(self.layers, pattern):<EOL><INDENT>yield name, expr<EOL><DEDENT><DEDENT>def parse_levels(levels):<EOL><INDENT>'''<STR_LIT>'''<EOL>if isinstance(levels, dict):<EOL><INDENT>levels = levels.items()<EOL><DEDENT>if isinstance(levels, (int, float)):<EOL><INDENT>levels = [levels]<EOL><DEDENT>for level in levels:<EOL><INDENT>if isinstance(level, (tuple, list)):<EOL><INDENT>label, call = level<EOL>yield '<STR_LIT>'.format(label), call<EOL><DEDENT>if isinstance(level, (int, float)):<EOL><INDENT>def call(expr):<EOL><INDENT>return (expr < level).mean()<EOL><DEDENT>yield '<STR_LIT>'.format(level), call<EOL><DEDENT><DEDENT><DEDENT>inputs = kwargs.get('<STR_LIT>', {})<EOL>if isinstance(inputs, dict):<EOL><INDENT>inputs = inputs.items()<EOL><DEDENT>for pattern, levels in inputs:<EOL><INDENT>for name, expr in matching(pattern):<EOL><INDENT>for key, value in parse_levels(levels):<EOL><INDENT>monitors.append(('<STR_LIT>'.format(name, key), value(expr)))<EOL><DEDENT><DEDENT><DEDENT>return monitors<EOL>

Return expressions that should be computed to monitor training.

        Returns
        -------
        monitors : list of (name, expression) pairs
            A list of named monitor expressions to compute for this network.


regs = regularizers.from_kwargs(self, **kwargs)<EOL>_, updates = self.build_graph(regs)<EOL>return updates<EOL>

Return expressions to run as updates during network training.

        Returns
        -------
        updates : list of (parameter, expression) pairs
            A list of named parameter update expressions for this network.


result = [self._target]<EOL>if self._weights is not None:<EOL><INDENT>result.append(self._weights)<EOL><DEDENT>return result<EOL>

A list of Theano variables used in this loss.


util.log('<STR_LIT>'<EOL>'<STR_LIT>', self)<EOL>

Log some diagnostic info about this loss.


raise NotImplementedError<EOL>

Construct the computation graph for this loss function.

        Parameters
        ----------
        outputs : dict of Theano expressions
            A dictionary mapping network output names to Theano expressions
            representing the outputs of a computation graph.

        Returns
        -------
        loss : Theano expression
            The values of the loss given the network output.


err = outputs[self.output_name] - self._target<EOL>if self._weights is not None:<EOL><INDENT>return (self._weights * err * err).sum() / self._weights.sum()<EOL><DEDENT>return (err * err).mean()<EOL>


err = outputs[self.output_name] - self._target<EOL>if self._weights is not None:<EOL><INDENT>return abs(self._weights * err).sum() / self._weights.sum()<EOL><DEDENT>return abs(err).mean()<EOL>


<EOL>mean = outputs[self.mean_name]<EOL>covar = outputs[self.covar_name]<EOL>prec = <NUM_LIT:1> / (abs(covar) + self.covar_eps)  <EOL>eta = mean * prec<EOL>logpi = TT.cast(mean.shape[-<NUM_LIT:1>] * np.log(<NUM_LIT:2> * np.pi), '<STR_LIT>')<EOL>logdet = TT.log(prec.sum(axis=-<NUM_LIT:1>))<EOL>const = logpi - logdet + (eta * prec * eta).sum(axis=-<NUM_LIT:1>)<EOL>squared = (self._target * prec * self._target).sum(axis=-<NUM_LIT:1>)<EOL>nll = <NUM_LIT:0.5> * (const + squared) - (eta * self._target).sum(axis=-<NUM_LIT:1>)<EOL>return nll.mean()<EOL>


output = outputs[self.output_name]<EOL>xx = self.kernel(self._target, self._target)<EOL>xy = self.kernel(self._target, output)<EOL>yy = self.kernel(output, output)<EOL>return xx.mean() - <NUM_LIT:2> * xy.mean() + yy.mean()<EOL>


output = outputs[self.output_name]<EOL>eps = <NUM_LIT><EOL>t = TT.clip(self._target, eps, <NUM_LIT:1> - eps)<EOL>kl = t * TT.log(t / TT.clip(output, eps, <NUM_LIT:1> - eps))<EOL>if self._weights is not None:<EOL><INDENT>return abs(self._weights * kl).sum() / self._weights.sum()<EOL><DEDENT>return abs(kl).mean()<EOL>


output = outputs[self.output_name]<EOL>k = output.shape[-<NUM_LIT:1>]<EOL>n = TT.prod(output.shape) // k<EOL>prob = output.reshape((n, k))[TT.arange(n), self._target.reshape((n, ))]<EOL>nlp = -TT.log(TT.clip(prob, <NUM_LIT>, <NUM_LIT:1>))<EOL>if self._weights is not None:<EOL><INDENT>return (self._weights.reshape((n, )) * nlp).sum() / self._weights.sum()<EOL><DEDENT>return nlp.mean()<EOL>


output = outputs[self.output_name]<EOL>predict = TT.argmax(output, axis=-<NUM_LIT:1>)<EOL>correct = TT.eq(predict, self._target)<EOL>acc = correct.mean()<EOL>if self._weights is not None:<EOL><INDENT>acc = (self._weights * correct).sum() / self._weights.sum()<EOL><DEDENT>return acc<EOL>

Build a Theano expression for computing the accuracy of graph output.

        Parameters
        ----------
        outputs : dict of Theano expressions
            A dictionary mapping network output names to Theano expressions
            representing the outputs of a computation graph.

        Returns
        -------
        acc : Theano expression
            A Theano expression representing the accuracy of the output compared
            to the target data.


output = outputs[self.output_name]<EOL>k = output.shape[-<NUM_LIT:1>]<EOL>n = TT.prod(output.shape) // k<EOL>output = output.reshape((n, k))<EOL>true = output[TT.arange(n), self._target.reshape((n, ))]<EOL>err = TT.maximum(<NUM_LIT:0>, (output - true[:, None]).max(axis=-<NUM_LIT:1>))<EOL>if self._weights is not None:<EOL><INDENT>return (self._weights.reshape((n, )) * err).sum() / self._weights.sum()<EOL><DEDENT>return err.mean()<EOL>


def batches(arrays, steps=<NUM_LIT:100>, batch_size=<NUM_LIT:64>, rng=None):

assert batch_size >= <NUM_LIT:2>, '<STR_LIT>'<EOL>assert isinstance(arrays, (tuple, list)), '<STR_LIT>'<EOL>if rng is None or isinstance(rng, int):<EOL><INDENT>rng = np.random.RandomState(rng)<EOL><DEDENT>def sample():<EOL><INDENT>xs = [np.zeros((batch_size, steps, a.shape[<NUM_LIT:1>]), a.dtype) for a in arrays]<EOL>for i in range(batch_size):<EOL><INDENT>j = rng.randint(len(arrays[<NUM_LIT:0>]) - steps)<EOL>for x, a in zip(xs, arrays):<EOL><INDENT>x[i] = a[j:j+steps]<EOL><DEDENT><DEDENT>return xs<EOL><DEDENT>return sample<EOL>

Create a callable that generates samples from a dataset.

    Parameters
    ----------
    arrays : list of ndarray (time-steps, data-dimensions)
        Arrays of data. Rows in these arrays are assumed to correspond to time
        steps, and columns to variables. Multiple arrays can be given; in such
        a case, these arrays usually correspond to [input, output]---for
        example, for a recurrent regression problem---or [input, output,
        weights]---for a weighted regression or classification problem.
    steps : int, optional
        Generate samples of this many time steps. Defaults to 100.
    batch_size : int, optional
        Generate this many samples per call. Defaults to 64. This must match the
        batch_size parameter that was used when creating the recurrent network
        that will process the data.
    rng : :class:`numpy.random.RandomState` or int, optional
        A random number generator, or an integer seed for a random number
        generator. If not provided, the random number generator will be created
        with an automatically chosen seed.

    Returns
    -------
    callable :
        A callable that can be used inside a dataset for training a recurrent
        network.


return list(self._fwd_index.get(c, <NUM_LIT:0>) for c in txt)<EOL>

Encode a text string by replacing characters with alphabet index.

        Parameters
        ----------
        txt : str
            A string to encode.

        Returns
        -------
        classes : list of int
            A sequence of alphabet index values corresponding to the given text.


return '<STR_LIT>'.join(self._rev_index[c] for c in enc)<EOL>

Encode a text string by replacing characters with alphabet index.

        Parameters
        ----------
        classes : list of int
            A sequence of alphabet index values to convert to text.

        Returns
        -------
        txt : str
            A string containing corresponding characters from the alphabet.


def classifier_batches(self, steps, batch_size, rng=None):

assert batch_size >= <NUM_LIT:2>, '<STR_LIT>'<EOL>if rng is None or isinstance(rng, int):<EOL><INDENT>rng = np.random.RandomState(rng)<EOL><DEDENT>T = np.arange(steps)<EOL>def batch():<EOL><INDENT>inputs = np.zeros((batch_size, steps, <NUM_LIT:1> + len(self.alpha)), '<STR_LIT:f>')<EOL>outputs = np.zeros((batch_size, steps), '<STR_LIT:i>')<EOL>for b in range(batch_size):<EOL><INDENT>offset = rng.randint(len(self.text) - steps - <NUM_LIT:1>)<EOL>enc = self.encode(self.text[offset:offset + steps + <NUM_LIT:1>])<EOL>inputs[b, T, enc[:-<NUM_LIT:1>]] = <NUM_LIT:1><EOL>outputs[b, T] = enc[<NUM_LIT:1>:]<EOL><DEDENT>return [inputs, outputs]<EOL><DEDENT>return batch<EOL>

Create a callable that returns a batch of training data.

        Parameters
        ----------
        steps : int
            Number of time steps in each batch.
        batch_size : int
            Number of training examples per batch.
        rng : :class:`numpy.random.RandomState` or int, optional
            A random number generator, or an integer seed for a random number
            generator. If not provided, the random number generator will be
            created with an automatically chosen seed.

        Returns
        -------
        batch : callable
            A callable that, when called, returns a batch of data that can be
            used to train a classifier model.


def predict_sequence(self, labels, steps, streams=<NUM_LIT:1>, rng=None):

if rng is None or isinstance(rng, int):<EOL><INDENT>rng = np.random.RandomState(rng)<EOL><DEDENT>offset = len(labels)<EOL>batch = max(<NUM_LIT:2>, streams)<EOL>inputs = np.zeros((batch, offset + steps, self.layers[<NUM_LIT:0>].output_size), '<STR_LIT:f>')<EOL>inputs[:, np.arange(offset), labels] = <NUM_LIT:1><EOL>for i in range(offset, offset + steps):<EOL><INDENT>chars = []<EOL>for pdf in self.predict_proba(inputs[:i])[:, -<NUM_LIT:1>]:<EOL><INDENT>try:<EOL><INDENT>c = rng.multinomial(<NUM_LIT:1>, pdf).argmax(axis=-<NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>c = pdf.argmax(axis=-<NUM_LIT:1>)<EOL><DEDENT>chars.append(int(c))<EOL><DEDENT>inputs[np.arange(batch), i, chars] = <NUM_LIT:1><EOL>yield chars[<NUM_LIT:0>] if streams == <NUM_LIT:1> else chars<EOL><DEDENT>

Draw a sequential sample of class labels from this network.

        Parameters
        ----------
        labels : list of int
            A list of integer class labels to get the classifier started.
        steps : int
            The number of time steps to sample.
        streams : int, optional
            Number of parallel streams to sample from the model. Defaults to 1.
        rng : :class:`numpy.random.RandomState` or int, optional
            A random number generator, or an integer seed for a random number
            generator. If not provided, the random number generator will be
            created with an automatically chosen seed.

        Yields
        ------
        label(s) : int or list of int
            Yields at each time step an integer class label sampled sequentially
            from the model. If the number of requested streams is greater than
            1, this will be a list containing the corresponding number of class
            labels.


def batch_at(features, labels, seq_begins, seq_lengths):

length = seq_lengths.max()<EOL>feat = np.zeros((BATCH_SIZE, length, features.shape[-<NUM_LIT:1>]), '<STR_LIT:f>')<EOL>labl = np.zeros((BATCH_SIZE, length), '<STR_LIT:i>')<EOL>mask = np.zeros((BATCH_SIZE, length), '<STR_LIT:f>')<EOL>for b, (begin, length) in enumerate(zip(seq_begins, seq_lengths)):<EOL><INDENT>feat[b, :length] = features[begin:begin+length]<EOL>labl[b, :length] = labels[begin:begin+length]<EOL>mask[b, :length] = <NUM_LIT:1><EOL><DEDENT>return [feat, labl, mask]<EOL>

Extract a single batch of data to pass to the model being trained.

    Parameters
    ----------
    features, labels : ndarray
        Arrays of the input features and target labels.
    seq_begins : ndarray
        Array of the start offsets of the speech segments to include.
    seq_lengths : ndarray
        Array of the lengths of the speech segments to include in the batch.

    Returns
    -------
    features, labels, mask : ndarrays
        A triple of arrays for training a network. The first element contains
        input features, the second contains target labels, and the third
        contains a "mask" consisting of ones where there is valid data and zeros
        everywhere else.


seq_lengths = dataset.variables['<STR_LIT>'].data<EOL>seq_begins = np.concatenate(([<NUM_LIT:0>], np.cumsum(seq_lengths)[:-<NUM_LIT:1>]))<EOL>def sample():<EOL><INDENT>chosen = np.random.choice(<EOL>list(range(len(seq_lengths))), BATCH_SIZE, replace=False)<EOL>return batch_at(dataset.variables['<STR_LIT>'].data,<EOL>dataset.variables['<STR_LIT>'].data,<EOL>seq_begins[chosen],<EOL>seq_lengths[chosen])<EOL><DEDENT>return sample<EOL>

Returns a callable that chooses sequences from netcdf data.


return dict(form='<STR_LIT>', worker='<STR_LIT>', size=n)<EOL>

Helper for building a bidirectional LSTM layer with n cells.


fn = os.path.join(DATASETS, dataset)<EOL>dn = os.path.dirname(fn)<EOL>if not os.path.exists(dn):<EOL><INDENT>print('<STR_LIT>', dn)<EOL>os.makedirs(dn)<EOL><DEDENT>if not os.path.exists(fn):<EOL><INDENT>if sys.version_info < (<NUM_LIT:3>, ):<EOL><INDENT>urllib.urlretrieve(url, fn)<EOL><DEDENT>else:<EOL><INDENT>urllib.request.urlretrieve(url, fn)<EOL><DEDENT><DEDENT>return fn<EOL>

Find the location of a dataset on disk, downloading if needed.


fn = find('<STR_LIT>', '<STR_LIT>')<EOL>h = gzip.open(fn, '<STR_LIT:rb>')<EOL>if sys.version_info < (<NUM_LIT:3>, ):<EOL><INDENT>(timg, tlab), (vimg, vlab), (simg, slab) = pickle.load(h)<EOL><DEDENT>else:<EOL><INDENT>(timg, tlab), (vimg, vlab), (simg, slab) = pickle.load(h, encoding='<STR_LIT>')<EOL><DEDENT>h.close()<EOL>if not flatten:<EOL><INDENT>timg = timg.reshape((-<NUM_LIT:1>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>))<EOL>vimg = vimg.reshape((-<NUM_LIT:1>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>))<EOL>simg = simg.reshape((-<NUM_LIT:1>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>))<EOL><DEDENT>if labels:<EOL><INDENT>return ((timg, tlab.astype('<STR_LIT:i>')),<EOL>(vimg, vlab.astype('<STR_LIT:i>')),<EOL>(simg, slab.astype('<STR_LIT:i>')))<EOL><DEDENT>return (timg, ), (vimg, ), (simg, )<EOL>


def extract(name):<EOL><INDENT>print('<STR_LIT>'.format(name))<EOL>h = tar.extractfile(name)<EOL>if sys.version_info < (<NUM_LIT:3>, ):<EOL><INDENT>d = pickle.load(h)<EOL><DEDENT>else:<EOL><INDENT>d = pickle.load(h, encoding='<STR_LIT>')<EOL>for k in list(d):<EOL><INDENT>d[k.decode('<STR_LIT:utf8>')] = d[k]<EOL><DEDENT><DEDENT>h.close()<EOL>img = d['<STR_LIT:data>'].reshape(<EOL>(-<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:32>, <NUM_LIT:32>)).transpose((<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:1>)).astype('<STR_LIT:f>') / <NUM_LIT> - <NUM_LIT:1><EOL>if flatten:<EOL><INDENT>img = img.reshape((-<NUM_LIT:1>, <NUM_LIT:32> * <NUM_LIT:32> * <NUM_LIT:3>))<EOL><DEDENT>d['<STR_LIT:data>'] = img<EOL>return d<EOL><DEDENT>fn = find('<STR_LIT>', '<STR_LIT>')<EOL>tar = tarfile.open(fn)<EOL>imgs = []<EOL>labs = []<EOL>for i in range(<NUM_LIT:1>, <NUM_LIT:6>):<EOL><INDENT>d = extract('<STR_LIT>'.format(i))<EOL>imgs.extend(d['<STR_LIT:data>'])<EOL>labs.extend(d['<STR_LIT>'])<EOL><DEDENT>timg = np.asarray(imgs[:<NUM_LIT>])<EOL>tlab = np.asarray(labs[:<NUM_LIT>], '<STR_LIT:i>')<EOL>vimg = np.asarray(imgs[<NUM_LIT>:])<EOL>vlab = np.asarray(labs[<NUM_LIT>:], '<STR_LIT:i>')<EOL>d = extract('<STR_LIT>')<EOL>simg = d['<STR_LIT:data>']<EOL>slab = d['<STR_LIT>']<EOL>tar.close()<EOL>if labels:<EOL><INDENT>return (timg, tlab), (vimg, vlab), (simg, slab)<EOL><DEDENT>return (timg, ), (vimg, ), (simg, )<EOL>