def shear(<EOL>x, intensity=<NUM_LIT:0.1>, is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>,<EOL>order=<NUM_LIT:1><EOL>):

if is_random:<EOL><INDENT>shear = np.random.uniform(-intensity, intensity)<EOL><DEDENT>else:<EOL><INDENT>shear = intensity<EOL><DEDENT>shear_matrix = np.array([[<NUM_LIT:1>, -np.sin(shear), <NUM_LIT:0>], [<NUM_LIT:0>, np.cos(shear), <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = x.shape[row_index], x.shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(shear_matrix, h, w)<EOL>x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)<EOL>return x<EOL>

Shear an image randomly or non-randomly.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    intensity : float
        Percentage of shear, usually -0.5 ~ 0.5 (is_random==True), 0 ~ 0.5 (is_random==False),
        you can have a quick try by shear(X, 1).
    is_random : boolean
        If True, randomly shear. Default is False.
    row_index col_index and channel_index : int
        Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
    fill_mode : str
        Method to fill missing pixel, default `nearest`, more options `constant`, `reflect` or `wrap`, see and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
    cval : float
        Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0.
    order : int
        The order of interpolation. The order has to be in the range 0-5. See ``tl.prepro.affine_transform`` and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__

    Returns
    -------
    numpy.array
        A processed image.

    References
    -----------
    - `Affine transformation <https://uk.mathworks.com/discovery/affine-transformation.html>`__


if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>x, y, w, h = coord<EOL>x_center = x + w / <NUM_LIT><EOL>y_center = y + h / <NUM_LIT><EOL>return [x_center, y_center, w, h]<EOL>

Convert one coordinate [x, y, w, h] to [x_center, y_center, w, h].
    It is the reverse process of ``obj_box_coord_centroid_to_upleft``.

    Parameters
    ------------
    coord : list of 4 int/float
        One coordinate.

    Returns
    -------
    list of 4 numbers
        New bounding box.


def rotation_multi(<EOL>x, rg=<NUM_LIT:20>, is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>, order=<NUM_LIT:1><EOL>):

if is_random:<EOL><INDENT>theta = np.pi / <NUM_LIT> * np.random.uniform(-rg, rg)<EOL><DEDENT>else:<EOL><INDENT>theta = np.pi / <NUM_LIT> * rg<EOL><DEDENT>rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), <NUM_LIT:0>], [np.sin(theta), np.cos(theta), <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = x[<NUM_LIT:0>].shape[row_index], x[<NUM_LIT:0>].shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)<EOL>results = []<EOL>for data in x:<EOL><INDENT>results.append(affine_transform(data, transform_matrix, channel_index, fill_mode, cval, order))<EOL><DEDENT>return np.asarray(results)<EOL>

Rotate multiple images with the same arguments, randomly or non-randomly.
    Usually be used for image segmentation which x=[X, Y], X and Y should be matched.

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.rotation``.

    Returns
    -------
    numpy.array
        A list of processed images.

    Examples
    --------
    >>> x, y --> [row, col, 1]  greyscale
    >>> x, y = tl.prepro.rotation_multi([x, y], rg=90, is_random=False)


mask = disk(radius)<EOL>x = _erosion(x, selem=mask)<EOL>return x<EOL>

Return greyscale morphological erosion of an image,
    see `skimage.morphology.erosion <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.erosion>`__.

    Parameters
    -----------
    x : 2D array
        A greyscale image.
    radius : int
        For the radius of mask.

    Returns
    -------
    numpy.array
        A processed greyscale image.


def affine_transform_cv2(x, transform_matrix, flags=None, border_mode='<STR_LIT>'):

rows, cols = x.shape[<NUM_LIT:0>], x.shape[<NUM_LIT:1>]<EOL>if flags is None:<EOL><INDENT>flags = cv2.INTER_AREA<EOL><DEDENT>if border_mode is '<STR_LIT>':<EOL><INDENT>border_mode = cv2.BORDER_CONSTANT<EOL><DEDENT>elif border_mode is '<STR_LIT>':<EOL><INDENT>border_mode = cv2.BORDER_REPLICATE<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>return cv2.warpAffine(x, transform_matrix[<NUM_LIT:0>:<NUM_LIT:2>,:],(cols,rows), flags=flags, borderMode=border_mode)<EOL>

Return transformed images by given an affine matrix in OpenCV format (x is width). (Powered by OpenCV2, faster than ``tl.prepro.affine_transform``)

    Parameters
    ----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    transform_matrix : numpy.array
        A transform matrix, OpenCV format.
    border_mode : str
        - `constant`, pad the image with a constant value (i.e. black or 0)
        - `replicate`, the row or column at the very edge of the original is replicated to the extra border.

    Examples
    --------
    >>> M_shear = tl.prepro.affine_shear_matrix(intensity=0.2, is_random=False)
    >>> M_zoom = tl.prepro.affine_zoom_matrix(zoom_range=0.8)
    >>> M_combined = M_shear.dot(M_zoom)
    >>> result = tl.prepro.affine_transform_cv2(image, M_combined)


def rotation(<EOL>x, rg=<NUM_LIT:20>, is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>, order=<NUM_LIT:1><EOL>):

if is_random:<EOL><INDENT>theta = np.pi / <NUM_LIT> * np.random.uniform(-rg, rg)<EOL><DEDENT>else:<EOL><INDENT>theta = np.pi / <NUM_LIT> * rg<EOL><DEDENT>rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), <NUM_LIT:0>], [np.sin(theta), np.cos(theta), <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = x.shape[row_index], x.shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)<EOL>x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)<EOL>return x<EOL>

Rotate an image randomly or non-randomly.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    rg : int or float
        Degree to rotate, usually 0 ~ 180.
    is_random : boolean
        If True, randomly rotate. Default is False
    row_index col_index and channel_index : int
        Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
    fill_mode : str
        Method to fill missing pixel, default `nearest`, more options `constant`, `reflect` or `wrap`, see `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
    cval : float
        Value used for points outside the boundaries of the input if mode=`constant`. Default is 0.0
    order : int
        The order of interpolation. The order has to be in the range 0-5. See ``tl.prepro.affine_transform`` and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__

    Returns
    -------
    numpy.array
        A processed image.

    Examples
    ---------
    >>> x --> [row, col, 1]
    >>> x = tl.prepro.rotation(x, rg=40, is_random=False)
    >>> tl.vis.save_image(x, 'im.png')


def imresize(x, size=None, interp='<STR_LIT>', mode=None):

if size is None:<EOL><INDENT>size = [<NUM_LIT:100>, <NUM_LIT:100>]<EOL><DEDENT>if x.shape[-<NUM_LIT:1>] == <NUM_LIT:1>:<EOL><INDENT>x = scipy.misc.imresize(x[:, :, <NUM_LIT:0>], size, interp=interp, mode=mode)<EOL>return x[:, :, np.newaxis]<EOL><DEDENT>else:<EOL><INDENT>return scipy.misc.imresize(x, size, interp=interp, mode=mode)<EOL><DEDENT>

Resize an image by given output size and method.

    Warning, this function will rescale the value to [0, 255].

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    size : list of 2 int or None
        For height and width.
    interp : str
        Interpolation method for re-sizing (`nearest`, `lanczos`, `bilinear`, `bicubic` (default) or `cubic`).
    mode : str
        The PIL image mode (`P`, `L`, etc.) to convert image before resizing.

    Returns
    -------
    numpy.array
        A processed image.

    References
    ------------
    - `scipy.misc.imresize <https://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.imresize.html>`__


def array_to_img(x, dim_ordering=(<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>), scale=True):

<EOL>x = x.transpose(dim_ordering)<EOL>if scale:<EOL><INDENT>x += max(-np.min(x), <NUM_LIT:0>)<EOL>x_max = np.max(x)<EOL>if x_max != <NUM_LIT:0>:<EOL><INDENT>x = x / x_max<EOL><DEDENT>x *= <NUM_LIT:255><EOL><DEDENT>if x.shape[<NUM_LIT:2>] == <NUM_LIT:3>:<EOL><INDENT>return PIL.Image.fromarray(x.astype('<STR_LIT>'), '<STR_LIT>')<EOL><DEDENT>elif x.shape[<NUM_LIT:2>] == <NUM_LIT:1>:<EOL><INDENT>return PIL.Image.fromarray(x[:, :, <NUM_LIT:0>].astype('<STR_LIT>'), '<STR_LIT:L>')<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>', x.shape[<NUM_LIT:2>])<EOL><DEDENT>

Converts a numpy array to PIL image object (uint8 format).

    Parameters
    ----------
    x : numpy.array
        An image with dimension of 3 and channels of 1 or 3.
    dim_ordering : tuple of 3 int
        Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
    scale : boolean
        If True, converts image to [0, 255] from any range of value like [-1, 2]. Default is True.

    Returns
    -------
    PIL.image
        An image.

    References
    -----------
    `PIL Image.fromarray <http://pillow.readthedocs.io/en/3.1.x/reference/Image.html?highlight=fromarray>`__


def brightness(x, gamma=<NUM_LIT:1>, gain=<NUM_LIT:1>, is_random=False):

if is_random:<EOL><INDENT>gamma = np.random.uniform(<NUM_LIT:1> - gamma, <NUM_LIT:1> + gamma)<EOL><DEDENT>x = exposure.adjust_gamma(x, gamma, gain)<EOL>return x<EOL>

Change the brightness of a single image, randomly or non-randomly.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    gamma : float
        Non negative real number. Default value is 1.
            - Small than 1 means brighter.
            - If `is_random` is True, gamma in a range of (1-gamma, 1+gamma).
    gain : float
        The constant multiplier. Default value is 1.
    is_random : boolean
        If True, randomly change brightness. Default is False.

    Returns
    -------
    numpy.array
        A processed image.

    References
    -----------
    - `skimage.exposure.adjust_gamma <http://scikit-image.org/docs/dev/api/skimage.exposure.html>`__
    - `chinese blog <http://www.cnblogs.com/denny402/p/5124402.html>`__


if len(x.shape) == <NUM_LIT:3>:<EOL><INDENT>if x.shape[-<NUM_LIT:1>] == <NUM_LIT:3>:  <EOL><INDENT>img_size = x.shape<EOL>mask = np.random.binomial(n=<NUM_LIT:1>, p=keep, size=x.shape[:-<NUM_LIT:1>])<EOL>for i in range(<NUM_LIT:3>):<EOL><INDENT>x[:, :, i] = np.multiply(x[:, :, i], mask)<EOL><DEDENT><DEDENT>elif x.shape[-<NUM_LIT:1>] == <NUM_LIT:1>:  <EOL><INDENT>img_size = x.shape<EOL>x = np.multiply(x, np.random.binomial(n=<NUM_LIT:1>, p=keep, size=img_size))<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>".format(x.shape))<EOL><DEDENT><DEDENT>elif len(x.shape) == <NUM_LIT:2> or <NUM_LIT:1>:  <EOL><INDENT>img_size = x.shape<EOL>x = np.multiply(x, np.random.binomial(n=<NUM_LIT:1>, p=keep, size=img_size))<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>".format(x.shape))<EOL><DEDENT>return x<EOL>

Randomly set some pixels to zero by a given keeping probability.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] or [row, col].
    keep : float
        The keeping probability (0, 1), the lower more values will be set to zero.

    Returns
    -------
    numpy.array
        A processed image.


def keypoint_resize_random_crop(image, annos, mask=None, size=(<NUM_LIT>, <NUM_LIT>)):

if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>def resize_image(image, annos, mask, target_width, target_height):<EOL><INDENT>"""<STR_LIT>"""<EOL>y, x, _ = np.shape(image)<EOL>ratio_y = target_height / y<EOL>ratio_x = target_width / x<EOL>new_joints = []<EOL>for people in annos:<EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < <NUM_LIT:0> or keypoints[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>pts = (int(keypoints[<NUM_LIT:0>] * ratio_x + <NUM_LIT:0.5>), int(keypoints[<NUM_LIT:1>] * ratio_y + <NUM_LIT:0.5>))<EOL>if pts[<NUM_LIT:0>] > target_width - <NUM_LIT:1> or pts[<NUM_LIT:1>] > target_height - <NUM_LIT:1>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL>new_image = cv2.resize(image, (target_width, target_height), interpolation=cv2.INTER_AREA)<EOL>if mask is not None:<EOL><INDENT>new_mask = cv2.resize(mask, (target_width, target_height), interpolation=cv2.INTER_AREA)<EOL>return new_image, annos, new_mask<EOL><DEDENT>else:<EOL><INDENT>return new_image, annos, None<EOL><DEDENT><DEDENT>_target_height = size[<NUM_LIT:0>]<EOL>_target_width = size[<NUM_LIT:1>]<EOL>if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>height, width, _ = np.shape(image)<EOL>if height <= width:<EOL><INDENT>ratio = _target_height / height<EOL>new_width = int(ratio * width)<EOL>if height == width:<EOL><INDENT>new_width = _target_height<EOL><DEDENT>image, annos, mask = resize_image(image, annos, mask, new_width, _target_height)<EOL>if new_width > _target_width:<EOL><INDENT>crop_range_x = np.random.randint(<NUM_LIT:0>, new_width - _target_width)<EOL><DEDENT>else:<EOL><INDENT>crop_range_x = <NUM_LIT:0><EOL><DEDENT>image = image[:, crop_range_x:crop_range_x + _target_width, :]<EOL>if mask is not None:<EOL><INDENT>mask = mask[:, crop_range_x:crop_range_x + _target_width]<EOL><DEDENT>new_joints = []<EOL>for people in annos:<EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < -<NUM_LIT:10> or keypoints[<NUM_LIT:1>] < -<NUM_LIT:10>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>top = crop_range_x + _target_width - <NUM_LIT:1><EOL>if keypoints[<NUM_LIT:0>] >= crop_range_x and keypoints[<NUM_LIT:0>] <= top:<EOL><INDENT>pts = (int(keypoints[<NUM_LIT:0>] - crop_range_x), int(keypoints[<NUM_LIT:1>]))<EOL><DEDENT>else:<EOL><INDENT>pts = (-<NUM_LIT:1000>, -<NUM_LIT:1000>)<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL><DEDENT>if height > width:<EOL><INDENT>ratio = _target_width / width<EOL>new_height = int(ratio * height)<EOL>image, annos, mask = resize_image(image, annos, mask, _target_width, new_height)<EOL>if new_height > _target_height:<EOL><INDENT>crop_range_y = np.random.randint(<NUM_LIT:0>, new_height - _target_height)<EOL><DEDENT>else:<EOL><INDENT>crop_range_y = <NUM_LIT:0><EOL><DEDENT>image = image[crop_range_y:crop_range_y + _target_width, :, :]<EOL>if mask is not None:<EOL><INDENT>mask = mask[crop_range_y:crop_range_y + _target_width, :]<EOL><DEDENT>new_joints = []<EOL>for people in annos:  <EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < <NUM_LIT:0> or keypoints[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>bot = crop_range_y + _target_height - <NUM_LIT:1><EOL>if keypoints[<NUM_LIT:1>] >= crop_range_y and keypoints[<NUM_LIT:1>] <= bot:<EOL><INDENT>pts = (int(keypoints[<NUM_LIT:0>]), int(keypoints[<NUM_LIT:1>] - crop_range_y))<EOL><DEDENT>else:<EOL><INDENT>pts = (-<NUM_LIT:1000>, -<NUM_LIT:1000>)<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL><DEDENT>if mask is not None:<EOL><INDENT>return image, annos, mask<EOL><DEDENT>else:<EOL><INDENT>return image, annos, None<EOL><DEDENT>

Reszie the image to make either its width or height equals to the given sizes.
    Then randomly crop image without influence scales.
    Resize the image match with the minimum size before cropping, this API will change the zoom scale of object.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    size : tuple of int
        The size (height, width) of returned image.

    Returns
    ----------
    preprocessed image, annos, mask


def shear2(<EOL>x, shear=(<NUM_LIT:0.1>, <NUM_LIT:0.1>), is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>,<EOL>order=<NUM_LIT:1><EOL>):

if len(shear) != <NUM_LIT:2>:<EOL><INDENT>raise AssertionError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if isinstance(shear, tuple):<EOL><INDENT>shear = list(shear)<EOL><DEDENT>if is_random:<EOL><INDENT>shear[<NUM_LIT:0>] = np.random.uniform(-shear[<NUM_LIT:0>], shear[<NUM_LIT:0>])<EOL>shear[<NUM_LIT:1>] = np.random.uniform(-shear[<NUM_LIT:1>], shear[<NUM_LIT:1>])<EOL><DEDENT>shear_matrix = np.array([[<NUM_LIT:1>, shear[<NUM_LIT:0>], <NUM_LIT:0>],[shear[<NUM_LIT:1>], <NUM_LIT:1>, <NUM_LIT:0>],[<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = x.shape[row_index], x.shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(shear_matrix, h, w)<EOL>x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)<EOL>return x<EOL>

Shear an image randomly or non-randomly.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    shear : tuple of two floats
        Percentage of shear for height and width direction (0, 1).
    is_random : boolean
        If True, randomly shear. Default is False.
    row_index col_index and channel_index : int
        Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
    fill_mode : str
        Method to fill missing pixel, default `nearest`, more options `constant`, `reflect` or `wrap`, see `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
    cval : float
        Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0.
    order : int
        The order of interpolation. The order has to be in the range 0-5. See ``tl.prepro.affine_transform`` and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__

    Returns
    -------
    numpy.array
        A processed image.

    References
    -----------
    - `Affine transformation <https://uk.mathworks.com/discovery/affine-transformation.html>`__


def save_model(self, network=None, model_name='<STR_LIT>', **kwargs):

kwargs.update({'<STR_LIT>': model_name})<EOL>self._fill_project_info(kwargs)  <EOL>params = network.get_all_params()<EOL>s = time.time()<EOL>kwargs.update({'<STR_LIT>': network.all_graphs, '<STR_LIT:time>': datetime.utcnow()})<EOL>try:<EOL><INDENT>params_id = self.model_fs.put(self._serialization(params))<EOL>kwargs.update({'<STR_LIT>': params_id, '<STR_LIT:time>': datetime.utcnow()})<EOL>self.db.Model.insert_one(kwargs)<EOL>print("<STR_LIT>".format(round(time.time() - s, <NUM_LIT:2>)))<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>

Save model architecture and parameters into database, timestamp will be added automatically.

        Parameters
        ----------
        network : TensorLayer layer
            TensorLayer layer instance.
        model_name : str
            The name/key of model.
        kwargs : other events
            Other events, such as name, accuracy, loss, step number and etc (optinal).

        Examples
        ---------
        Save model architecture and parameters into database.
        >>> db.save_model(net, accuracy=0.8, loss=2.3, name='second_model')

        Load one model with parameters from database (run this in other script)
        >>> net = db.find_top_model(sess=sess, accuracy=0.8, loss=2.3)

        Find and load the latest model.
        >>> net = db.find_top_model(sess=sess, sort=[("time", pymongo.DESCENDING)])
        >>> net = db.find_top_model(sess=sess, sort=[("time", -1)])

        Find and load the oldest model.
        >>> net = db.find_top_model(sess=sess, sort=[("time", pymongo.ASCENDING)])
        >>> net = db.find_top_model(sess=sess, sort=[("time", 1)])

        Get model information
        >>> net._accuracy
        ... 0.8

        Returns
        ---------
        boolean : True for success, False for fail.


return self._s<EOL>


return args.update({'<STR_LIT>': self.project_name})<EOL>

Fill in project_name for all studies, architectures and parameters.


def run_top_task(self, task_name=None, sort=None, **kwargs):

if not isinstance(task_name, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:status>': '<STR_LIT>'})<EOL>task = self.db.Task.find_one_and_update(kwargs, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}}, sort=sort)<EOL>try:<EOL><INDENT>if task is None:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name, sort))<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name, sort))<EOL><DEDENT>_datetime = task['<STR_LIT:time>']<EOL>_script = task['<STR_LIT>']<EOL>_id = task['<STR_LIT>']<EOL>_hyper_parameters = task['<STR_LIT>']<EOL>_saved_result_keys = task['<STR_LIT>']<EOL>logging.info("<STR_LIT>")<EOL>for key in _hyper_parameters:<EOL><INDENT>globals()[key] = _hyper_parameters[key]<EOL>logging.info("<STR_LIT>".format(key, _hyper_parameters[key]))<EOL><DEDENT>s = time.time()<EOL>logging.info("<STR_LIT>".format(task_name, sort, _datetime))<EOL>_script = _script.decode('<STR_LIT:utf-8>')<EOL>with tf.Graph().as_default():  <EOL><INDENT>exec(_script, globals())<EOL><DEDENT>_ = self.db.Task.find_one_and_update({'<STR_LIT>': _id}, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}})<EOL>__result = {}<EOL>for _key in _saved_result_keys:<EOL><INDENT>logging.info("<STR_LIT>".format(_key, globals()[_key], type(globals()[_key])))<EOL>__result.update({"<STR_LIT:%s>" % _key: globals()[_key]})<EOL><DEDENT>_ = self.db.Task.find_one_and_update(<EOL>{<EOL>'<STR_LIT>': _id<EOL>}, {'<STR_LIT>': {<EOL>'<STR_LIT:result>': __result<EOL>}}, return_document=pymongo.ReturnDocument.AFTER<EOL>)<EOL>logging.info(<EOL>"<STR_LIT>".<EOL>format(task_name, sort, _datetime,<EOL>time.time() - s)<EOL>)<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>logging.info("<STR_LIT>")<EOL>_ = self.db.Task.find_one_and_update({'<STR_LIT>': _id}, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}})<EOL>return False<EOL><DEDENT>

Finds and runs a pending task that in the first of the sorting list.

        Parameters
        -----------
        task_name : str
            The task name.
        sort : List of tuple
            PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details.
        kwargs : other parameters
            Users customized parameters such as description, version number.

        Examples
        ---------
        Monitors the database and pull tasks to run
        >>> while True:
        >>>     print("waiting task from distributor")
        >>>     db.run_top_task(task_name='mnist', sort=[("time", -1)])
        >>>     time.sleep(1)

        Returns
        --------
        boolean : True for success, False for fail.


self._fill_project_info(kwargs)<EOL>self.db.Dataset.delete_many(kwargs)<EOL>logging.info("<STR_LIT>")<EOL>

Delete datasets.

        Parameters
        -----------
        kwargs : logging information
            Find items to delete, leave it empty to delete all log.


self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:time>': datetime.utcnow()})<EOL>_result = self.db.TrainLog.insert_one(kwargs)<EOL>_log = self._print_dict(kwargs)<EOL>logging.info("<STR_LIT>" + _log)<EOL>

Saves the training log, timestamp will be added automatically.

        Parameters
        -----------
        kwargs : logging information
            Events, such as accuracy, loss, step number and etc.

        Examples
        ---------
        >>> db.save_training_log(accuracy=0.33, loss=0.98)


return pickle.loads(ps)<EOL>


def save_dataset(self, dataset=None, dataset_name=None, **kwargs):

self._fill_project_info(kwargs)<EOL>if dataset_name is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>kwargs.update({'<STR_LIT>': dataset_name})<EOL>s = time.time()<EOL>try:<EOL><INDENT>dataset_id = self.dataset_fs.put(self._serialization(dataset))<EOL>kwargs.update({'<STR_LIT>': dataset_id, '<STR_LIT:time>': datetime.utcnow()})<EOL>self.db.Dataset.insert_one(kwargs)<EOL>print("<STR_LIT>".format(round(time.time() - s, <NUM_LIT:2>)))<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>

Saves one dataset into database, timestamp will be added automatically.

        Parameters
        ----------
        dataset : any type
            The dataset you want to store.
        dataset_name : str
            The name of dataset.
        kwargs : other events
            Other events, such as description, author and etc (optinal).

        Examples
        ----------
        Save dataset
        >>> db.save_dataset([X_train, y_train, X_test, y_test], 'mnist', description='this is a tutorial')

        Get dataset
        >>> dataset = db.find_top_dataset('mnist')

        Returns
        ---------
        boolean : Return True if save success, otherwise, return False.


def create_task(self, task_name=None, script=None, hyper_parameters=None, saved_result_keys=None, **kwargs):

if not isinstance(task_name, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if not isinstance(script, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if hyper_parameters is None:<EOL><INDENT>hyper_parameters = {}<EOL><DEDENT>if saved_result_keys is None:<EOL><INDENT>saved_result_keys = []<EOL><DEDENT>self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:time>': datetime.utcnow()})<EOL>kwargs.update({'<STR_LIT>': hyper_parameters})<EOL>kwargs.update({'<STR_LIT>': saved_result_keys})<EOL>_script = open(script, '<STR_LIT:rb>').read()<EOL>kwargs.update({'<STR_LIT:status>': '<STR_LIT>', '<STR_LIT>': _script, '<STR_LIT:result>': {}})<EOL>self.db.Task.insert_one(kwargs)<EOL>logging.info("<STR_LIT>".format(task_name, script))<EOL>

Uploads a task to the database, timestamp will be added automatically.

        Parameters
        -----------
        task_name : str
            The task name.
        script : str
            File name of the python script.
        hyper_parameters : dictionary
            The hyper parameters pass into the script.
        saved_result_keys : list of str
            The keys of the task results to keep in the database when the task finishes.
        kwargs : other parameters
            Users customized parameters such as description, version number.

        Examples
        -----------
        Uploads a task
        >>> db.create_task(task_name='mnist', script='example/tutorial_mnist_simple.py', description='simple tutorial')

        Finds and runs the latest task
        >>> db.run_top_task(sess=sess, sort=[("time", pymongo.DESCENDING)])
        >>> db.run_top_task(sess=sess, sort=[("time", -1)])

        Finds and runs the oldest task
        >>> db.run_top_task(sess=sess, sort=[("time", pymongo.ASCENDING)])
        >>> db.run_top_task(sess=sess, sort=[("time", 1)])


with tf.get_default_graph().gradient_override_map({"<STR_LIT>": "<STR_LIT>"}):<EOL><INDENT>return tf.sign(x, name='<STR_LIT>')<EOL><DEDENT>

Sign function.

    Clip and binarize tensor using the straight through estimator (STE) for the gradient, usually be used for
    quantizing values in `Binarized Neural Networks`: https://arxiv.org/abs/1602.02830.

    Parameters
    ----------
    x : Tensor
        input.

    Examples
    --------
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.lrelu(x, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013)`
       http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf

    - `BinaryNet: Training Deep Neural Networks with Weights and Activations Constrained to +1 or -1, Courbariaux et al. (2016)`
       https://arxiv.org/abs/1602.02830


@deprecated(date="<STR_LIT>", instructions="<STR_LIT>")<EOL>def leaky_relu(x, alpha=<NUM_LIT>, name="<STR_LIT>"):

if not (<NUM_LIT:0> < alpha <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>with tf.name_scope(name, "<STR_LIT>") as name_scope:<EOL><INDENT>x = tf.convert_to_tensor(x, name="<STR_LIT>")<EOL>return tf.maximum(x, alpha * x, name=name_scope)<EOL><DEDENT>

leaky_relu can be used through its shortcut: :func:`tl.act.lrelu`.

    This function is a modified version of ReLU, introducing a nonzero gradient for negative input. Introduced by the paper:
    `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__

    The function return the following results:
      - When x < 0: ``f(x) = alpha_low * x``.
      - When x >= 0: ``f(x) = x``.

    Parameters
    ----------
    x : Tensor
        Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
    alpha : float
        Slope.
    name : str
        The function name (optional).

    Examples
    --------
    >>> import tensorlayer as tl
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.lrelu(x, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__


def leaky_twice_relu6(x, alpha_low=<NUM_LIT>, alpha_high=<NUM_LIT>, name="<STR_LIT>"):

if not isinstance(alpha_high, tf.Tensor) and not (<NUM_LIT:0> < alpha_high <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(alpha_low, tf.Tensor) and not (<NUM_LIT:0> < alpha_low <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>with tf.name_scope(name, "<STR_LIT>") as name_scope:<EOL><INDENT>x = tf.convert_to_tensor(x, name="<STR_LIT>")<EOL>x_is_above_0 = tf.minimum(x, <NUM_LIT:6> * (<NUM_LIT:1> - alpha_high) + alpha_high * x)<EOL>x_is_below_0 = tf.minimum(alpha_low * x, <NUM_LIT:0>)<EOL>return tf.maximum(x_is_above_0, x_is_below_0, name=name_scope)<EOL><DEDENT>

:func:`leaky_twice_relu6` can be used through its shortcut: :func:`:func:`tl.act.ltrelu6`.

    This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
    `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__

    This activation function also follows the behaviour of the activation function :func:`tf.nn.relu6` introduced by the following paper:
    `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__

    This function push further the logic by adding `leaky` behaviour both below zero and above six.

    The function return the following results:
      - When x < 0: ``f(x) = alpha_low * x``.
      - When x in [0, 6]: ``f(x) = x``.
      - When x > 6: ``f(x) = 6 + (alpha_high * (x-6))``.

    Parameters
    ----------
    x : Tensor
        Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
    alpha_low : float
        Slope for x < 0: ``f(x) = alpha_low * x``.
    alpha_high : float
        Slope for x < 6: ``f(x) = 6 (alpha_high * (x-6))``.
    name : str
        The function name (optional).

    Examples
    --------
    >>> import tensorlayer as tl
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.leaky_twice_relu6(x, 0.2, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
    - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__


def discount_episode_rewards(rewards=None, gamma=<NUM_LIT>, mode=<NUM_LIT:0>):

if rewards is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>discounted_r = np.zeros_like(rewards, dtype=np.float32)<EOL>running_add = <NUM_LIT:0><EOL>for t in reversed(xrange(<NUM_LIT:0>, rewards.size)):<EOL><INDENT>if mode == <NUM_LIT:0>:<EOL><INDENT>if rewards[t] != <NUM_LIT:0>: running_add = <NUM_LIT:0><EOL><DEDENT>running_add = running_add * gamma + rewards[t]<EOL>discounted_r[t] = running_add<EOL><DEDENT>return discounted_r<EOL>

Take 1D float array of rewards and compute discounted rewards for an
    episode. When encount a non-zero value, consider as the end a of an episode.

    Parameters
    ----------
    rewards : list
        List of rewards
    gamma : float
        Discounted factor
    mode : int
        Mode for computing the discount rewards.
            - If mode == 0, reset the discount process when encount a non-zero reward (Ping-pong game).
            - If mode == 1, would not reset the discount process.

    Returns
    --------
    list of float
        The discounted rewards.

    Examples
    ----------
    >>> rewards = np.asarray([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1])
    >>> gamma = 0.9
    >>> discount_rewards = tl.rein.discount_episode_rewards(rewards, gamma)
    >>> print(discount_rewards)
    [ 0.72899997  0.81        0.89999998  1.          0.72899997  0.81
    0.89999998  1.          0.72899997  0.81        0.89999998  1.        ]
    >>> discount_rewards = tl.rein.discount_episode_rewards(rewards, gamma, mode=1)
    >>> print(discount_rewards)
    [ 1.52110755  1.69011939  1.87791049  2.08656716  1.20729685  1.34144104
    1.49048996  1.65610003  0.72899997  0.81        0.89999998  1.        ]


def choice_action_by_probs(probs=(<NUM_LIT:0.5>, <NUM_LIT:0.5>), action_list=None):

if action_list is None:<EOL><INDENT>n_action = len(probs)<EOL>action_list = np.arange(n_action)<EOL><DEDENT>else:<EOL><INDENT>if len(action_list) != len(probs):<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>return np.random.choice(action_list, p=probs)<EOL>

Choice and return an an action by given the action probability distribution.

    Parameters
    ------------
    probs : list of float.
        The probability distribution of all actions.
    action_list : None or a list of int or others
        A list of action in integer, string or others. If None, returns an integer range between 0 and len(probs)-1.

    Returns
    --------
    float int or str
        The chosen action.

    Examples
    ----------
    >>> for _ in range(5):
    >>>     a = choice_action_by_probs([0.2, 0.4, 0.4])
    >>>     print(a)
    0
    1
    1
    2
    1
    >>> for _ in range(3):
    >>>     a = choice_action_by_probs([0.5, 0.5], ['a', 'b'])
    >>>     print(a)
    a
    b
    b


def draw_boxes_and_labels_to_image(<EOL>image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None<EOL>):

if len(coords) != len(classes):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if len(scores) > <NUM_LIT:0> and len(scores) != len(classes):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>image = image.copy()<EOL>imh, imw = image.shape[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>thick = int((imh + imw) // <NUM_LIT>)<EOL>for i, _v in enumerate(coords):<EOL><INDENT>if is_center:<EOL><INDENT>x, y, x2, y2 = tl.prepro.obj_box_coord_centroid_to_upleft_butright(coords[i])<EOL><DEDENT>else:<EOL><INDENT>x, y, x2, y2 = coords[i]<EOL><DEDENT>if is_rescale:  <EOL><INDENT>x, y, x2, y2 = tl.prepro.obj_box_coord_scale_to_pixelunit([x, y, x2, y2], (imh, imw))<EOL><DEDENT>cv2.rectangle(<EOL>image,<EOL>(int(x), int(y)),<EOL>(int(x2), int(y2)),  <EOL>[<NUM_LIT:0>, <NUM_LIT:255>, <NUM_LIT:0>],<EOL>thick<EOL>)<EOL>cv2.putText(<EOL>image,<EOL>classes_list[classes[i]] + (("<STR_LIT>" % (scores[i])) if (len(scores) != <NUM_LIT:0>) else "<STR_LIT:U+0020>"),<EOL>(int(x), int(y)),  <EOL><NUM_LIT:0>,<EOL><NUM_LIT> * imh,  <EOL>[<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>],  <EOL>int(thick / <NUM_LIT:2>) + <NUM_LIT:1><EOL>)  <EOL><DEDENT>if save_name is not None:<EOL><INDENT>save_image(image, save_name)<EOL><DEDENT>return image<EOL>

Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``.

    Parameters
    -----------
    image : numpy.array
        The RGB image [height, width, channel].
    classes : list of int
        A list of class ID (int).
    coords : list of int
        A list of list for coordinates.
            - Should be [x, y, x2, y2] (up-left and botton-right format)
            - If [x_center, y_center, w, h] (set is_center to True).
    scores : list of float
        A list of score (float). (Optional)
    classes_list : list of str
        for converting ID to string on image.
    is_center : boolean
        Whether the coordinates is [x_center, y_center, w, h]
            - If coordinates are [x_center, y_center, w, h], set it to True for converting it to [x, y, x2, y2] (up-left and botton-right) internally.
            - If coordinates are [x1, x2, y1, y2], set it to False.
    is_rescale : boolean
        Whether to rescale the coordinates from pixel-unit format to ratio format.
            - If True, the input coordinates are the portion of width and high, this API will scale the coordinates to pixel unit internally.
            - If False, feed the coordinates with pixel unit format.
    save_name : None or str
        The name of image file (i.e. image.png), if None, not to save image.

    Returns
    -------
    numpy.array
        The saved image.

    References
    -----------
    - OpenCV rectangle and putText.
    - `scikit-image <http://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.rectangle>`__.


def draw_weights(W=None, second=<NUM_LIT:10>, saveable=True, shape=None, name='<STR_LIT>', fig_idx=<NUM_LIT>):

if shape is None:<EOL><INDENT>shape = [<NUM_LIT>, <NUM_LIT>]<EOL><DEDENT>import matplotlib.pyplot as plt<EOL>if saveable is False:<EOL><INDENT>plt.ion()<EOL><DEDENT>fig = plt.figure(fig_idx)  <EOL>n_units = W.shape[<NUM_LIT:1>]<EOL>num_r = int(np.sqrt(n_units))  <EOL>num_c = int(np.ceil(n_units / num_r))<EOL>count = int(<NUM_LIT:1>)<EOL>for _row in range(<NUM_LIT:1>, num_r + <NUM_LIT:1>):<EOL><INDENT>for _col in range(<NUM_LIT:1>, num_c + <NUM_LIT:1>):<EOL><INDENT>if count > n_units:<EOL><INDENT>break<EOL><DEDENT>fig.add_subplot(num_r, num_c, count)<EOL>feature = W[:, count - <NUM_LIT:1>] / np.sqrt((W[:, count - <NUM_LIT:1>]**<NUM_LIT:2>).sum())<EOL>plt.imshow(<EOL>np.reshape(feature, (shape[<NUM_LIT:0>], shape[<NUM_LIT:1>])), cmap='<STR_LIT>', interpolation="<STR_LIT>"<EOL>)  <EOL>plt.gca().xaxis.set_major_locator(plt.NullLocator())  <EOL>plt.gca().yaxis.set_major_locator(plt.NullLocator())<EOL>count = count + <NUM_LIT:1><EOL><DEDENT><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT>

Visualize every columns of the weight matrix to a group of Greyscale img.

    Parameters
    ----------
    W : numpy.array
        The weight matrix
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    shape : a list with 2 int or None
        The shape of feature image, MNIST is [28, 80].
    name : a string
        A name to save the image, if saveable is True.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> tl.visualize.draw_weights(network.all_params[0].eval(), second=10, saveable=True, name='weight_of_1st_layer', fig_idx=2012)


def save_images(images, size, image_path='<STR_LIT>'):

if len(images.shape) == <NUM_LIT:3>:  <EOL><INDENT>images = images[:, :, :, np.newaxis]<EOL><DEDENT>def merge(images, size):<EOL><INDENT>h, w = images.shape[<NUM_LIT:1>], images.shape[<NUM_LIT:2>]<EOL>img = np.zeros((h * size[<NUM_LIT:0>], w * size[<NUM_LIT:1>], <NUM_LIT:3>), dtype=images.dtype)<EOL>for idx, image in enumerate(images):<EOL><INDENT>i = idx % size[<NUM_LIT:1>]<EOL>j = idx // size[<NUM_LIT:1>]<EOL>img[j * h:j * h + h, i * w:i * w + w, :] = image<EOL><DEDENT>return img<EOL><DEDENT>def imsave(images, size, path):<EOL><INDENT>if np.max(images) <= <NUM_LIT:1> and (-<NUM_LIT:1> <= np.min(images) < <NUM_LIT:0>):<EOL><INDENT>images = ((images + <NUM_LIT:1>) * <NUM_LIT>).astype(np.uint8)<EOL><DEDENT>elif np.max(images) <= <NUM_LIT:1> and np.min(images) >= <NUM_LIT:0>:<EOL><INDENT>images = (images * <NUM_LIT:255>).astype(np.uint8)<EOL><DEDENT>return imageio.imwrite(path, merge(images, size))<EOL><DEDENT>if len(images) > size[<NUM_LIT:0>] * size[<NUM_LIT:1>]:<EOL><INDENT>raise AssertionError("<STR_LIT>".format(len(images)))<EOL><DEDENT>return imsave(images, size, image_path)<EOL>

Save multiple images into one single image.

    Parameters
    -----------
    images : numpy array
        (batch, w, h, c)
    size : list of 2 ints
        row and column number.
        number of images should be equal or less than size[0] * size[1]
    image_path : str
        save path

    Examples
    ---------
    >>> import numpy as np
    >>> import tensorlayer as tl
    >>> images = np.random.rand(64, 100, 100, 3)
    >>> tl.visualize.save_images(images, [8, 8], 'temp.png')


def tsne_embedding(embeddings, reverse_dictionary, plot_only=<NUM_LIT>, second=<NUM_LIT:5>, saveable=False, name='<STR_LIT>', fig_idx=<NUM_LIT>):

import matplotlib.pyplot as plt<EOL>def plot_with_labels(low_dim_embs, labels, figsize=(<NUM_LIT>, <NUM_LIT>), second=<NUM_LIT:5>, saveable=True, name='<STR_LIT>', fig_idx=<NUM_LIT>):<EOL><INDENT>if low_dim_embs.shape[<NUM_LIT:0>] < len(labels):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if saveable is False:<EOL><INDENT>plt.ion()<EOL>plt.figure(fig_idx)<EOL><DEDENT>plt.figure(figsize=figsize)  <EOL>for i, label in enumerate(labels):<EOL><INDENT>x, y = low_dim_embs[i, :]<EOL>plt.scatter(x, y)<EOL>plt.annotate(label, xy=(x, y), xytext=(<NUM_LIT:5>, <NUM_LIT:2>), textcoords='<STR_LIT>', ha='<STR_LIT:right>', va='<STR_LIT>')<EOL><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>from sklearn.manifold import TSNE<EOL>from six.moves import xrange<EOL>tsne = TSNE(perplexity=<NUM_LIT:30>, n_components=<NUM_LIT:2>, init='<STR_LIT>', n_iter=<NUM_LIT>)<EOL>low_dim_embs = tsne.fit_transform(embeddings[:plot_only, :])<EOL>labels = [reverse_dictionary[i] for i in xrange(plot_only)]<EOL>plot_with_labels(low_dim_embs, labels, second=second, saveable=saveable, name=name, fig_idx=fig_idx)<EOL><DEDENT>except ImportError:<EOL><INDENT>_err = "<STR_LIT>"<EOL>tl.logging.error(_err)<EOL>raise ImportError(_err)<EOL><DEDENT>

Visualize the embeddings by using t-SNE.

    Parameters
    ----------
    embeddings : numpy.array
        The embedding matrix.
    reverse_dictionary : dictionary
        id_to_word, mapping id to unique word.
    plot_only : int
        The number of examples to plot, choice the most common words.
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> see 'tutorial_word2vec_basic.py'
    >>> final_embeddings = normalized_embeddings.eval()
    >>> tl.visualize.tsne_embedding(final_embeddings, labels, reverse_dictionary,
    ...                   plot_only=500, second=5, saveable=False, name='tsne')


def CNN2d(CNN=None, second=<NUM_LIT:10>, saveable=True, name='<STR_LIT>', fig_idx=<NUM_LIT>):

import matplotlib.pyplot as plt<EOL>n_mask = CNN.shape[<NUM_LIT:3>]<EOL>n_row = CNN.shape[<NUM_LIT:0>]<EOL>n_col = CNN.shape[<NUM_LIT:1>]<EOL>n_color = CNN.shape[<NUM_LIT:2>]<EOL>row = int(np.sqrt(n_mask))<EOL>col = int(np.ceil(n_mask / row))<EOL>plt.ion()  <EOL>fig = plt.figure(fig_idx)<EOL>count = <NUM_LIT:1><EOL>for _ir in range(<NUM_LIT:1>, row + <NUM_LIT:1>):<EOL><INDENT>for _ic in range(<NUM_LIT:1>, col + <NUM_LIT:1>):<EOL><INDENT>if count > n_mask:<EOL><INDENT>break<EOL><DEDENT>fig.add_subplot(col, row, count)<EOL>if n_color == <NUM_LIT:1>:<EOL><INDENT>plt.imshow(np.reshape(CNN[:, :, :, count - <NUM_LIT:1>], (n_row, n_col)), cmap='<STR_LIT>', interpolation="<STR_LIT>")<EOL><DEDENT>elif n_color == <NUM_LIT:3>:<EOL><INDENT>plt.imshow(<EOL>np.reshape(CNN[:, :, :, count - <NUM_LIT:1>], (n_row, n_col, n_color)), cmap='<STR_LIT>', interpolation="<STR_LIT>"<EOL>)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>plt.gca().xaxis.set_major_locator(plt.NullLocator())  <EOL>plt.gca().yaxis.set_major_locator(plt.NullLocator())<EOL>count = count + <NUM_LIT:1><EOL><DEDENT><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT>

Display a group of RGB or Greyscale CNN masks.

    Parameters
    ----------
    CNN : numpy.array
        The image. e.g: 64 5x5 RGB images can be (5, 5, 3, 64).
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    fig_idx : int
        The matplotlib figure index.

    Examples
    --------
    >>> tl.visualize.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_mnist', fig_idx=2012)


def build_words_dataset(words=None, vocabulary_size=<NUM_LIT>, printable=True, unk_key='<STR_LIT>'):

if words is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>count = [[unk_key, -<NUM_LIT:1>]]<EOL>count.extend(collections.Counter(words).most_common(vocabulary_size - <NUM_LIT:1>))<EOL>dictionary = dict()<EOL>for word, _ in count:<EOL><INDENT>dictionary[word] = len(dictionary)<EOL><DEDENT>data = list()<EOL>unk_count = <NUM_LIT:0><EOL>for word in words:<EOL><INDENT>if word in dictionary:<EOL><INDENT>index = dictionary[word]<EOL><DEDENT>else:<EOL><INDENT>index = <NUM_LIT:0>  <EOL>unk_count += <NUM_LIT:1><EOL><DEDENT>data.append(index)<EOL><DEDENT>count[<NUM_LIT:0>][<NUM_LIT:1>] = unk_count<EOL>reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))<EOL>if printable:<EOL><INDENT>tl.logging.info('<STR_LIT>' % len(collections.Counter(words).keys()))<EOL>tl.logging.info('<STR_LIT>'.format(vocabulary_size))<EOL><DEDENT>if len(collections.Counter(words).keys()) < vocabulary_size:<EOL><INDENT>raise Exception(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>return data, count, dictionary, reverse_dictionary<EOL>

Build the words dictionary and replace rare words with 'UNK' token.
    The most common word has the smallest integer id.

    Parameters
    ----------
    words : list of str or byte
        The context in list format. You may need to do preprocessing on the words, such as lower case, remove marks etc.
    vocabulary_size : int
        The maximum vocabulary size, limiting the vocabulary size. Then the script replaces rare words with 'UNK' token.
    printable : boolean
        Whether to print the read vocabulary size of the given words.
    unk_key : str
        Represent the unknown words.

    Returns
    --------
    data : list of int
        The context in a list of ID.
    count : list of tuple and list
        Pair words and IDs.
            - count[0] is a list : the number of rare words
            - count[1:] are tuples : the number of occurrence of each word
            - e.g. [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)]
    dictionary : dictionary
        It is `word_to_id` that maps word to ID.
    reverse_dictionary : a dictionary
        It is `id_to_word` that maps ID to word.

    Examples
    --------
    >>> words = tl.files.load_matt_mahoney_text8_dataset()
    >>> vocabulary_size = 50000
    >>> data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size)

    References
    -----------------
    - `tensorflow/examples/tutorials/word2vec/word2vec_basic.py <https://github.com/tensorflow/tensorflow/blob/r0.7/tensorflow/examples/tutorials/word2vec/word2vec_basic.py>`__


if word_id >= len(self.reverse_vocab):<EOL><INDENT>return self.reverse_vocab[self.unk_id]<EOL><DEDENT>else:<EOL><INDENT>return self.reverse_vocab[word_id]<EOL><DEDENT>

Returns the word string of an integer word id.


if gfile.Exists(vocabulary_path):<EOL><INDENT>rev_vocab = []<EOL>with gfile.GFile(vocabulary_path, mode="<STR_LIT:rb>") as f:<EOL><INDENT>rev_vocab.extend(f.readlines())<EOL><DEDENT>rev_vocab = [tf.compat.as_bytes(line.strip()) for line in rev_vocab]<EOL>vocab = dict([(x, y) for (y, x) in enumerate(rev_vocab)])<EOL>return vocab, rev_vocab<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>", vocabulary_path)<EOL><DEDENT>

Initialize vocabulary from file, return the `word_to_id` (dictionary)
    and `id_to_word` (list).

    We assume the vocabulary is stored one-item-per-line, so a file will result in a vocabulary {"dog": 0, "cat": 1}, and this function will also return the reversed-vocabulary ["dog", "cat"].

    Parameters
    -----------
    vocabulary_path : str
        Path to the file containing the vocabulary.

    Returns
    --------
    vocab : dictionary
        a dictionary that maps word to ID.
    rev_vocab : list of int
        a list that maps ID to word.

    Examples
    ---------
    >>> Assume 'test' contains
    dog
    cat
    bird
    >>> vocab, rev_vocab = tl.nlp.initialize_vocabulary("test")
    >>> print(vocab)
    >>> {b'cat': 1, b'dog': 0, b'bird': 2}
    >>> print(rev_vocab)
    >>> [b'dog', b'cat', b'bird']

    Raises
    -------
    ValueError : if the provided vocabulary_path does not exist.


def data_to_token_ids(<EOL>data_path, target_path, vocabulary_path, tokenizer=None, normalize_digits=True, UNK_ID=<NUM_LIT:3>,<EOL>_DIGIT_RE=re.compile(br"<STR_LIT>")<EOL>):

if not gfile.Exists(target_path):<EOL><INDENT>tl.logging.info("<STR_LIT>" % data_path)<EOL>vocab, _ = initialize_vocabulary(vocabulary_path)<EOL>with gfile.GFile(data_path, mode="<STR_LIT:rb>") as data_file:<EOL><INDENT>with gfile.GFile(target_path, mode="<STR_LIT:w>") as tokens_file:<EOL><INDENT>counter = <NUM_LIT:0><EOL>for line in data_file:<EOL><INDENT>counter += <NUM_LIT:1><EOL>if counter % <NUM_LIT> == <NUM_LIT:0>:<EOL><INDENT>tl.logging.info("<STR_LIT>" % counter)<EOL><DEDENT>token_ids = sentence_to_token_ids(<EOL>line, vocab, tokenizer, normalize_digits, UNK_ID=UNK_ID, _DIGIT_RE=_DIGIT_RE<EOL>)<EOL>tokens_file.write("<STR_LIT:U+0020>".join([str(tok) for tok in token_ids]) + "<STR_LIT:\n>")<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>tl.logging.info("<STR_LIT>" % target_path)<EOL><DEDENT>

Tokenize data file and turn into token-ids using given vocabulary file.

    This function loads data line-by-line from data_path, calls the above
    sentence_to_token_ids, and saves the result to target_path. See comment
    for sentence_to_token_ids on the details of token-ids format.

    Parameters
    -----------
    data_path : str
        Path to the data file in one-sentence-per-line format.
    target_path : str
        Path where the file with token-ids will be created.
    vocabulary_path : str
        Path to the vocabulary file.
    tokenizer : function
        A function to use to tokenize each sentence. If None, ``basic_tokenizer`` will be used.
    normalize_digits : boolean
        If true, all digits are replaced by 0.

    References
    ----------
    - Code from ``/tensorflow/models/rnn/translation/data_utils.py``


if a is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>b = np.copy(a)<EOL>try:<EOL><INDENT>if temperature == <NUM_LIT:1>:<EOL><INDENT>return np.argmax(np.random.multinomial(<NUM_LIT:1>, a, <NUM_LIT:1>))<EOL><DEDENT>if temperature is None:<EOL><INDENT>return np.argmax(a)<EOL><DEDENT>else:<EOL><INDENT>a = np.log(a) / temperature<EOL>a = np.exp(a) / np.sum(np.exp(a))<EOL>return np.argmax(np.random.multinomial(<NUM_LIT:1>, a, <NUM_LIT:1>))<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>message = "<STR_LIT>"<EOL>warnings.warn(message, Warning)<EOL>return np.argmax(np.random.multinomial(<NUM_LIT:1>, b, <NUM_LIT:1>))<EOL><DEDENT>

Sample an index from a probability array.

    Parameters
    ----------
    a : list of float
        List of probabilities.
    temperature : float or None
        The higher the more uniform. When a = [0.1, 0.2, 0.7],
            - temperature = 0.7, the distribution will be sharpen [0.05048273,  0.13588945,  0.81362782]
            - temperature = 1.0, the distribution will be the same [0.1,    0.2,    0.7]
            - temperature = 1.5, the distribution will be filtered [0.16008435,  0.25411807,  0.58579758]
            - If None, it will be ``np.argmax(a)``

    Notes
    ------
    - No matter what is the temperature and input list, the sum of all probabilities will be one. Even if input list = [1, 100, 200], the sum of all probabilities will still be one.
    - For large vocabulary size, choice a higher temperature or ``tl.nlp.sample_top`` to avoid error.


<EOL>counter = collections.Counter(data)<EOL>count_pairs = sorted(counter.items(), key=lambda x: (-x[<NUM_LIT:1>], x[<NUM_LIT:0>]))<EOL>words, _ = list(zip(*count_pairs))<EOL>word_to_id = dict(zip(words, range(len(words))))<EOL>return word_to_id<EOL>

Build vocabulary.

    Given the context in list format.
    Return the vocabulary, which is a dictionary for word to id.
    e.g. {'campbell': 2587, 'atlantic': 2247, 'aoun': 6746 .... }

    Parameters
    ----------
    data : list of str
        The context in list format

    Returns
    --------
    dictionary
        that maps word to unique ID. e.g. {'campbell': 2587, 'atlantic': 2247, 'aoun': 6746 .... }

    References
    ---------------
    - `tensorflow.models.rnn.ptb.reader <https://github.com/tensorflow/tensorflow/tree/master/tensorflow/models/rnn/ptb>`_

    Examples
    --------
    >>> data_path = os.getcwd() + '/simple-examples/data'
    >>> train_path = os.path.join(data_path, "ptb.train.txt")
    >>> word_to_id = build_vocab(read_txt_words(train_path))


def words_to_word_ids(data=None, word_to_id=None, unk_key='<STR_LIT>'):

if data is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if word_to_id is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>word_ids = []<EOL>for word in data:<EOL><INDENT>if word_to_id.get(word) is not None:<EOL><INDENT>word_ids.append(word_to_id[word])<EOL><DEDENT>else:<EOL><INDENT>word_ids.append(word_to_id[unk_key])<EOL><DEDENT><DEDENT>return word_ids<EOL>

Convert a list of string (words) to IDs.

    Parameters
    ----------
    data : list of string or byte
        The context in list format
    word_to_id : a dictionary
        that maps word to ID.
    unk_key : str
        Represent the unknown words.

    Returns
    --------
    list of int
        A list of IDs to represent the context.

    Examples
    --------
    >>> words = tl.files.load_matt_mahoney_text8_dataset()
    >>> vocabulary_size = 50000
    >>> data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True)
    >>> context = [b'hello', b'how', b'are', b'you']
    >>> ids = tl.nlp.words_to_word_ids(words, dictionary)
    >>> context = tl.nlp.word_ids_to_words(ids, reverse_dictionary)
    >>> print(ids)
    [6434, 311, 26, 207]
    >>> print(context)
    [b'hello', b'how', b'are', b'you']

    References
    ---------------
    - `tensorflow.models.rnn.ptb.reader <https://github.com/tensorflow/tensorflow/tree/master/tensorflow/models/rnn/ptb>`__


def sentence_to_token_ids(<EOL>sentence, vocabulary, tokenizer=None, normalize_digits=True, UNK_ID=<NUM_LIT:3>, _DIGIT_RE=re.compile(br"<STR_LIT>")<EOL>):

if tokenizer:<EOL><INDENT>words = tokenizer(sentence)<EOL><DEDENT>else:<EOL><INDENT>words = basic_tokenizer(sentence)<EOL><DEDENT>if not normalize_digits:<EOL><INDENT>return [vocabulary.get(w, UNK_ID) for w in words]<EOL><DEDENT>return [vocabulary.get(re.sub(_DIGIT_RE, b"<STR_LIT:0>", w), UNK_ID) for w in words]<EOL>

Convert a string to list of integers representing token-ids.

    For example, a sentence "I have a dog" may become tokenized into
    ["I", "have", "a", "dog"] and with vocabulary {"I": 1, "have": 2,
    "a": 4, "dog": 7"} this function will return [1, 2, 4, 7].

    Parameters
    -----------
    sentence : tensorflow.python.platform.gfile.GFile Object
        The sentence in bytes format to convert to token-ids, see ``basic_tokenizer()`` and ``data_to_token_ids()``.
    vocabulary : dictionary
        Mmapping tokens to integers.
    tokenizer : function
        A function to use to tokenize each sentence. If None, ``basic_tokenizer`` will be used.
    normalize_digits : boolean
        If true, all digits are replaced by 0.

    Returns
    --------
    list of int
        The token-ids for the sentence.


with open(filename, "<STR_LIT:r>") as f:<EOL><INDENT>words = f.read()<EOL>return words<EOL><DEDENT>

Read context from file without any preprocessing.

    Parameters
    ----------
    filename : str
        A file path (like .txt file)

    Returns
    --------
    str
        The context in a string.


self._vocab = vocab<EOL>self._unk_id = unk_id<EOL>


return [id_to_word[i] for i in data]<EOL>

Convert a list of integer to strings (words).

    Parameters
    ----------
    data : list of int
        The context in list format.
    id_to_word : dictionary
        a dictionary that maps ID to word.

    Returns
    --------
    list of str
        A list of string or byte to represent the context.

    Examples
    ---------
    >>> see ``tl.nlp.words_to_word_ids``


def create_vocabulary(<EOL>vocabulary_path, data_path, max_vocabulary_size, tokenizer=None, normalize_digits=True,<EOL>_DIGIT_RE=re.compile(br"<STR_LIT>"), _START_VOCAB=None<EOL>):

if _START_VOCAB is None:<EOL><INDENT>_START_VOCAB = [b"<STR_LIT>", b"<STR_LIT>", b"<STR_LIT>", b"<STR_LIT>"]<EOL><DEDENT>if not gfile.Exists(vocabulary_path):<EOL><INDENT>tl.logging.info("<STR_LIT>" % (vocabulary_path, data_path))<EOL>vocab = {}<EOL>with gfile.GFile(data_path, mode="<STR_LIT:rb>") as f:<EOL><INDENT>counter = <NUM_LIT:0><EOL>for line in f:<EOL><INDENT>counter += <NUM_LIT:1><EOL>if counter % <NUM_LIT> == <NUM_LIT:0>:<EOL><INDENT>tl.logging.info("<STR_LIT>" % counter)<EOL><DEDENT>tokens = tokenizer(line) if tokenizer else basic_tokenizer(line)<EOL>for w in tokens:<EOL><INDENT>word = re.sub(_DIGIT_RE, b"<STR_LIT:0>", w) if normalize_digits else w<EOL>if word in vocab:<EOL><INDENT>vocab[word] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>vocab[word] = <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>vocab_list = _START_VOCAB + sorted(vocab, key=vocab.get, reverse=True)<EOL>if len(vocab_list) > max_vocabulary_size:<EOL><INDENT>vocab_list = vocab_list[:max_vocabulary_size]<EOL><DEDENT>with gfile.GFile(vocabulary_path, mode="<STR_LIT:wb>") as vocab_file:<EOL><INDENT>for w in vocab_list:<EOL><INDENT>vocab_file.write(w + b"<STR_LIT:\n>")<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>tl.logging.info("<STR_LIT>" % (vocabulary_path, data_path))<EOL><DEDENT>

r"""Create vocabulary file (if it does not exist yet) from data file.

    Data file is assumed to contain one sentence per line. Each sentence is
    tokenized and digits are normalized (if normalize_digits is set).
    Vocabulary contains the most-frequent tokens up to max_vocabulary_size.
    We write it to vocabulary_path in a one-token-per-line format, so that later
    token in the first line gets id=0, second line gets id=1, and so on.

    Parameters
    -----------
    vocabulary_path : str
        Path where the vocabulary will be created.
    data_path : str
        Data file that will be used to create vocabulary.
    max_vocabulary_size : int
        Limit on the size of the created vocabulary.
    tokenizer : function
        A function to use to tokenize each data sentence. If None, basic_tokenizer will be used.
    normalize_digits : boolean
        If true, all digits are replaced by `0`.
    _DIGIT_RE : regular expression function
        Default is ``re.compile(br"\d")``.
    _START_VOCAB : list of str
        The pad, go, eos and unk token, default is ``[b"_PAD", b"_GO", b"_EOS", b"_UNK"]``.

    References
    ----------
    - Code from ``/tensorflow/models/rnn/translation/data_utils.py``


if not docstring:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>lines = docstring.expandtabs().splitlines()<EOL>indent = sys.maxsize<EOL>for line in lines[<NUM_LIT:1>:]:<EOL><INDENT>stripped = line.lstrip()<EOL>if stripped:<EOL><INDENT>indent = min(indent, len(line) - len(stripped))<EOL><DEDENT><DEDENT>trimmed = [lines[<NUM_LIT:0>].strip()]<EOL>if indent < sys.maxsize:<EOL><INDENT>for line in lines[<NUM_LIT:1>:]:<EOL><INDENT>trimmed.append(line[indent:].rstrip())<EOL><DEDENT><DEDENT>while trimmed and not trimmed[-<NUM_LIT:1>]:<EOL><INDENT>trimmed.pop()<EOL><DEDENT>while trimmed and not trimmed[<NUM_LIT:0>]:<EOL><INDENT>trimmed.pop(<NUM_LIT:0>)<EOL><DEDENT>return '<STR_LIT:\n>'.join(trimmed)<EOL>

Normalizes the docstring.

    Replaces tabs with spaces, removes leading and trailing blanks lines, and
    removes any indentation.

    Copied from PEP-257:
    https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation

    Args:
        docstring: the docstring to normalize

    Returns:
        The normalized docstring


def _add_notice_to_docstring(doc, no_doc_str, notice):

if not doc:<EOL><INDENT>lines = [no_doc_str]<EOL><DEDENT>else:<EOL><INDENT>lines = _normalize_docstring(doc).splitlines()<EOL><DEDENT>notice = ['<STR_LIT>'] + notice<EOL>if len(lines) > <NUM_LIT:1>:<EOL><INDENT>if lines[<NUM_LIT:1>].strip():<EOL><INDENT>notice.append('<STR_LIT>')<EOL><DEDENT>lines[<NUM_LIT:1>:<NUM_LIT:1>] = notice<EOL><DEDENT>else:<EOL><INDENT>lines += notice<EOL><DEDENT>return '<STR_LIT:\n>'.join(lines)<EOL>

Adds a deprecation notice to a docstring.


def _add_deprecated_function_notice_to_docstring(doc, date, instructions):

if instructions:<EOL><INDENT>deprecation_message = """<STR_LIT>""" % (('<STR_LIT>' if date is None else ('<STR_LIT>' % date)), instructions)<EOL><DEDENT>else:<EOL><INDENT>deprecation_message = """<STR_LIT>""" % (('<STR_LIT>' if date is None else ('<STR_LIT>' % date)))<EOL><DEDENT>main_text = [deprecation_message]<EOL>return _add_notice_to_docstring(doc, '<STR_LIT>', main_text)<EOL>

Adds a deprecation notice to a docstring for deprecated functions.


def seq_minibatches2(inputs, targets, batch_size, num_steps):

if len(inputs) != len(targets):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>data_len = len(inputs)<EOL>batch_len = data_len // batch_size<EOL>data = np.zeros((batch_size, batch_len) + inputs.shape[<NUM_LIT:1>:], dtype=inputs.dtype)<EOL>data2 = np.zeros([batch_size, batch_len])<EOL>for i in range(batch_size):<EOL><INDENT>data[i] = inputs[batch_len * i:batch_len * (i + <NUM_LIT:1>)]<EOL>data2[i] = targets[batch_len * i:batch_len * (i + <NUM_LIT:1>)]<EOL><DEDENT>epoch_size = (batch_len - <NUM_LIT:1>) // num_steps<EOL>if epoch_size == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>for i in range(epoch_size):<EOL><INDENT>x = data[:, i * num_steps:(i + <NUM_LIT:1>) * num_steps]<EOL>x2 = data2[:, i * num_steps:(i + <NUM_LIT:1>) * num_steps]<EOL>yield (x, x2)<EOL><DEDENT>

Generate a generator that iterates on two list of words. Yields (Returns) the source contexts and
    the target context by the given batch_size and num_steps (sequence_length).
    In TensorFlow's tutorial, this generates the `batch_size` pointers into the raw PTB data, and allows minibatch iteration along these pointers.

    Parameters
    ----------
    inputs : list of data
        The context in list format; note that context usually be represented by splitting by space, and then convert to unique word IDs.
    targets : list of data
        The context in list format; note that context usually be represented by splitting by space, and then convert to unique word IDs.
    batch_size : int
        The batch size.
    num_steps : int
        The number of unrolls. i.e. sequence length

    Yields
    ------
    Pairs of the batched data, each a matrix of shape [batch_size, num_steps].

    Raises
    ------
    ValueError : if batch_size or num_steps are too high.

    Examples
    --------
    >>> X = [i for i in range(20)]
    >>> Y = [i for i in range(20,40)]
    >>> for batch in tl.iterate.seq_minibatches2(X, Y, batch_size=2, num_steps=3):
    ...     x, y = batch
    ...     print(x, y)

    [[  0.   1.   2.]
    [ 10.  11.  12.]]
    [[ 20.  21.  22.]
    [ 30.  31.  32.]]

    [[  3.   4.   5.]
    [ 13.  14.  15.]]
    [[ 23.  24.  25.]
    [ 33.  34.  35.]]

    [[  6.   7.   8.]
    [ 16.  17.  18.]]
    [[ 26.  27.  28.]
    [ 36.  37.  38.]]

    Notes
    -----
    - Hint, if the input data are images, you can modify the source code `data = np.zeros([batch_size, batch_len)` to `data = np.zeros([batch_size, batch_len, inputs.shape[1], inputs.shape[2], inputs.shape[3]])`.


def minibatches(inputs=None, targets=None, batch_size=None, allow_dynamic_batch_size=False, shuffle=False):

if len(inputs) != len(targets):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if shuffle:<EOL><INDENT>indices = np.arange(len(inputs))<EOL>np.random.shuffle(indices)<EOL><DEDENT>for start_idx in range(<NUM_LIT:0>, len(inputs), batch_size):<EOL><INDENT>end_idx = start_idx + batch_size<EOL>if end_idx > len(inputs):<EOL><INDENT>if allow_dynamic_batch_size:<EOL><INDENT>end_idx = len(inputs)<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if shuffle:<EOL><INDENT>excerpt = indices[start_idx:end_idx]<EOL><DEDENT>else:<EOL><INDENT>excerpt = slice(start_idx, end_idx)<EOL><DEDENT>if (isinstance(inputs, list) or isinstance(targets, list)) and (shuffle ==True):<EOL><INDENT>yield [inputs[i] for i in excerpt], [targets[i] for i in excerpt]<EOL><DEDENT>else:<EOL><INDENT>yield inputs[excerpt], targets[excerpt]<EOL><DEDENT><DEDENT>

Generate a generator that input a group of example in numpy.array and
    their labels, return the examples and labels by the given batch size.

    Parameters
    ----------
    inputs : numpy.array
        The input features, every row is a example.
    targets : numpy.array
        The labels of inputs, every row is a example.
    batch_size : int
        The batch size.
    allow_dynamic_batch_size: boolean
        Allow the use of the last data batch in case the number of examples is not a multiple of batch_size, this may result in unexpected behaviour if other functions expect a fixed-sized batch-size.
    shuffle : boolean
        Indicating whether to use a shuffling queue, shuffle the dataset before return.

    Examples
    --------
    >>> X = np.asarray([['a','a'], ['b','b'], ['c','c'], ['d','d'], ['e','e'], ['f','f']])
    >>> y = np.asarray([0,1,2,3,4,5])
    >>> for batch in tl.iterate.minibatches(inputs=X, targets=y, batch_size=2, shuffle=False):
    >>>     print(batch)
    (array([['a', 'a'], ['b', 'b']], dtype='<U1'), array([0, 1]))
    (array([['c', 'c'], ['d', 'd']], dtype='<U1'), array([2, 3]))
    (array([['e', 'e'], ['f', 'f']], dtype='<U1'), array([4, 5]))

    Notes
    -----
    If you have two inputs and one label and want to shuffle them together, e.g. X1 (1000, 100), X2 (1000, 80) and Y (1000, 1), you can stack them together (`np.hstack((X1, X2))`)
    into (1000, 180) and feed to ``inputs``. After getting a batch, you can split it back into X1 and X2.


def alphas(shape, alpha_value, name=None):

with ops.name_scope(name, "<STR_LIT>", [shape]) as name:<EOL><INDENT>alpha_tensor = convert_to_tensor(alpha_value)<EOL>alpha_dtype = dtypes.as_dtype(alpha_tensor.dtype).base_dtype<EOL>if not isinstance(shape, ops.Tensor):<EOL><INDENT>try:<EOL><INDENT>shape = constant_op._tensor_shape_tensor_conversion_function(tensor_shape.TensorShape(shape))<EOL><DEDENT>except (TypeError, ValueError):<EOL><INDENT>shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)<EOL><DEDENT><DEDENT>if not shape._shape_tuple():<EOL><INDENT>shape = reshape(shape, [-<NUM_LIT:1>])  <EOL><DEDENT>try:<EOL><INDENT>output = constant(alpha_value, shape=shape, dtype=alpha_dtype, name=name)<EOL><DEDENT>except (TypeError, ValueError):<EOL><INDENT>output = fill(shape, constant(alpha_value, dtype=alpha_dtype), name=name)<EOL><DEDENT>if output.dtype.base_dtype != alpha_dtype:<EOL><INDENT>raise AssertionError("<STR_LIT>" % (output.dtype.base_dtype, alpha_dtype))<EOL><DEDENT>return output<EOL><DEDENT>

Creates a tensor with all elements set to `alpha_value`.
    This operation returns a tensor of type `dtype` with shape `shape` and all
    elements set to alpha.

    Parameters
    ----------
    shape: A list of integers, a tuple of integers, or a 1-D `Tensor` of type `int32`.
        The shape of the desired tensor
    alpha_value: `float32`, `float64`, `int8`, `uint8`, `int16`, `uint16`, int32`, `int64`
        The value used to fill the resulting `Tensor`.
    name: str
        A name for the operation (optional).

    Returns
    -------
    A `Tensor` with all elements set to alpha.

    Examples
    --------
    >>> tl.alphas([2, 3], tf.int32)  # [[alpha, alpha, alpha], [alpha, alpha, alpha]]


def cross_entropy(output, target, name=None):

if name is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=target, logits=output), name=name)<EOL>

Softmax cross-entropy operation, returns the TensorFlow expression of cross-entropy for two distributions,
    it implements softmax internally. See ``tf.nn.sparse_softmax_cross_entropy_with_logits``.

    Parameters
    ----------
    output : Tensor
        A batch of distribution with shape: [batch_size, num of classes].
    target : Tensor
        A batch of index with shape: [batch_size, ].
    name : string
        Name of this loss.

    Examples
    --------
    >>> ce = tl.cost.cross_entropy(y_logits, y_target_logits, 'my_loss')

    References
    -----------
    - About cross-entropy: `<https://en.wikipedia.org/wiki/Cross_entropy>`__.
    - The code is borrowed from: `<https://en.wikipedia.org/wiki/Cross_entropy>`__.


if isinstance(scale, numbers.Integral):<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if isinstance(scale, numbers.Real):<EOL><INDENT>if scale < <NUM_LIT:0.>:<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if scale >= <NUM_LIT:1.>:<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if scale == <NUM_LIT:0.>:<EOL><INDENT>tl.logging.info('<STR_LIT>')<EOL>return lambda _, name=None: None<EOL><DEDENT><DEDENT>def li(weights):<EOL><INDENT>"""<STR_LIT>"""<EOL>with tf.name_scope('<STR_LIT>') as scope:<EOL><INDENT>my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='<STR_LIT>')<EOL>standard_ops_fn = standard_ops.multiply<EOL>return standard_ops_fn(<EOL>my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), <NUM_LIT:1>))),<EOL>name=scope<EOL>)<EOL><DEDENT><DEDENT>return li<EOL>

Li regularization removes the neurons of previous layer. The `i` represents `inputs`.
    Returns a function that can be used to apply group li regularization to weights.
    The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.

    Parameters
    ----------
    scale : float
        A scalar multiplier `Tensor`. 0.0 disables the regularizer.
    scope: str
        An optional scope name for this function.

    Returns
    --------
    A function with signature `li(weights, name=None)` that apply Li regularization.

    Raises
    ------
    ValueError : if scale is outside of the range [0.0, 1.0] or if scale is not a float.


def cross_entropy_seq(logits, target_seqs, batch_size=None):

sequence_loss_by_example_fn = tf.contrib.legacy_seq2seq.sequence_loss_by_example<EOL>loss = sequence_loss_by_example_fn(<EOL>[logits], [tf.reshape(target_seqs, [-<NUM_LIT:1>])], [tf.ones_like(tf.reshape(target_seqs, [-<NUM_LIT:1>]), dtype=tf.float32)]<EOL>)<EOL>cost = tf.reduce_sum(loss)  <EOL>if batch_size is not None:<EOL><INDENT>cost = cost / batch_size<EOL><DEDENT>return cost<EOL>

Returns the expression of cross-entropy of two sequences, implement
    softmax internally. Normally be used for fixed length RNN outputs, see `PTB example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_ptb_lstm_state_is_tuple.py>`__.

    Parameters
    ----------
    logits : Tensor
        2D tensor with shape of `[batch_size * n_steps, n_classes]`.
    target_seqs : Tensor
        The target sequence, 2D tensor `[batch_size, n_steps]`, if the number of step is dynamic, please use ``tl.cost.cross_entropy_seq_with_mask`` instead.
    batch_size : None or int.
        Whether to divide the cost by batch size.
            - If integer, the return cost will be divided by `batch_size`.
            - If None (default), the return cost will not be divided by anything.

    Examples
    --------
    >>> see `PTB example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_ptb_lstm_state_is_tuple.py>`__.for more details
    >>> input_data = tf.placeholder(tf.int32, [batch_size, n_steps])
    >>> targets = tf.placeholder(tf.int32, [batch_size, n_steps])
    >>> # build the network
    >>> print(net.outputs)
    (batch_size * n_steps, n_classes)
    >>> cost = tl.cost.cross_entropy_seq(network.outputs, targets)


def normalized_mean_square_error(output, target, name="<STR_LIT>"):

<EOL>if output.get_shape().ndims == <NUM_LIT:2>:  <EOL><INDENT>nmse_a = tf.sqrt(tf.reduce_sum(tf.squared_difference(output, target), axis=<NUM_LIT:1>))<EOL>nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=<NUM_LIT:1>))<EOL><DEDENT>elif output.get_shape().ndims == <NUM_LIT:3>:  <EOL><INDENT>nmse_a = tf.sqrt(tf.reduce_sum(tf.squared_difference(output, target), axis=[<NUM_LIT:1>, <NUM_LIT:2>]))<EOL>nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=[<NUM_LIT:1>, <NUM_LIT:2>]))<EOL><DEDENT>elif output.get_shape().ndims == <NUM_LIT:4>:  <EOL><INDENT>nmse_a = tf.sqrt(tf.reduce_sum(tf.squared_difference(output, target), axis=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>]))<EOL>nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=[<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>]))<EOL><DEDENT>nmse = tf.reduce_mean(nmse_a / nmse_b, name=name)<EOL>return nmse<EOL>

Return the TensorFlow expression of normalized mean-square-error of two distributions.

    Parameters
    ----------
    output : Tensor
        2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, height, width] or [batch_size, height, width, channel].
    target : Tensor
        The target distribution, format the same with `output`.
    name : str
        An optional name to attach to this function.


if isinstance(scale, numbers.Integral):<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if isinstance(scale, numbers.Real):<EOL><INDENT>if scale < <NUM_LIT:0.>:<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if scale == <NUM_LIT:0.>:<EOL><INDENT>tl.logging.info('<STR_LIT>')<EOL>return lambda _, name=None: None<EOL><DEDENT><DEDENT>def mn_o(weights, name='<STR_LIT>'):<EOL><INDENT>"""<STR_LIT>"""<EOL>with tf.name_scope(name) as scope:<EOL><INDENT>my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='<STR_LIT>')<EOL>if tf.__version__ <= '<STR_LIT>':<EOL><INDENT>standard_ops_fn = standard_ops.mul<EOL><DEDENT>else:<EOL><INDENT>standard_ops_fn = standard_ops.multiply<EOL><DEDENT>return standard_ops_fn(<EOL>my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), <NUM_LIT:0>)), name=scope<EOL>)<EOL><DEDENT><DEDENT>return mn_o<EOL>

Max-norm output regularization removes the neurons of current layer.
    Returns a function that can be used to apply max-norm regularization to each column of weight matrix.
    The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.

    Parameters
    ----------
    scale : float
        A scalar multiplier `Tensor`. 0.0 disables the regularizer.

    Returns
    ---------
    A function with signature `mn_o(weights, name=None)` that apply Lo regularization.

    Raises
    ---------
    ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.


if isinstance(scale, numbers.Integral):<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if isinstance(scale, numbers.Real):<EOL><INDENT>if scale < <NUM_LIT:0.>:<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if scale >= <NUM_LIT:1.>:<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if scale == <NUM_LIT:0.>:<EOL><INDENT>tl.logging.info('<STR_LIT>')<EOL>return lambda _, name=None: None<EOL><DEDENT><DEDENT>def lo(weights, name='<STR_LIT>'):<EOL><INDENT>"""<STR_LIT>"""<EOL>with tf.name_scope(name) as scope:<EOL><INDENT>my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='<STR_LIT>')<EOL>standard_ops_fn = standard_ops.multiply<EOL>return standard_ops_fn(<EOL>my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), <NUM_LIT:0>))),<EOL>name=scope<EOL>)<EOL><DEDENT><DEDENT>return lo<EOL>

Lo regularization removes the neurons of current layer. The `o` represents `outputs`
    Returns a function that can be used to apply group lo regularization to weights.
    The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.

    Parameters
    ----------
    scale : float
        A scalar multiplier `Tensor`. 0.0 disables the regularizer.

    Returns
    -------
    A function with signature `lo(weights, name=None)` that apply Lo regularization.

    Raises
    ------
    ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.


def sigmoid_cross_entropy(output, target, name=None):

return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output), name=name)<EOL>

Sigmoid cross-entropy operation, see ``tf.nn.sigmoid_cross_entropy_with_logits``.

    Parameters
    ----------
    output : Tensor
        A batch of distribution with shape: [batch_size, num of classes].
    target : Tensor
        A batch of index with shape: [batch_size, ].
    name : string
        Name of this loss.


def cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details=False, name=None):

targets = tf.reshape(target_seqs, [-<NUM_LIT:1>])  <EOL>weights = tf.to_float(tf.reshape(input_mask, [-<NUM_LIT:1>]))  <EOL>losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets, name=name) * weights<EOL>loss = tf.divide(<EOL>tf.reduce_sum(losses),  <EOL>tf.reduce_sum(weights),<EOL>name="<STR_LIT>"<EOL>)<EOL>if return_details:<EOL><INDENT>return loss, losses, weights, targets<EOL><DEDENT>else:<EOL><INDENT>return loss<EOL><DEDENT>

Returns the expression of cross-entropy of two sequences, implement
    softmax internally. Normally be used for Dynamic RNN with Synced sequence input and output.

    Parameters
    -----------
    logits : Tensor
        2D tensor with shape of [batch_size * ?, n_classes], `?` means dynamic IDs for each example.
        - Can be get from `DynamicRNNLayer` by setting ``return_seq_2d`` to `True`.
    target_seqs : Tensor
        int of tensor, like word ID. [batch_size, ?], `?` means dynamic IDs for each example.
    input_mask : Tensor
        The mask to compute loss, it has the same size with `target_seqs`, normally 0 or 1.
    return_details : boolean
        Whether to return detailed losses.
            - If False (default), only returns the loss.
            - If True, returns the loss, losses, weights and targets (see source code).

    Examples
    --------
    >>> batch_size = 64
    >>> vocab_size = 10000
    >>> embedding_size = 256
    >>> input_seqs = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="input")
    >>> target_seqs = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="target")
    >>> input_mask = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="mask")
    >>> net = tl.layers.EmbeddingInputlayer(
    ...         inputs = input_seqs,
    ...         vocabulary_size = vocab_size,
    ...         embedding_size = embedding_size,
    ...         name = 'seq_embedding')
    >>> net = tl.layers.DynamicRNNLayer(net,
    ...         cell_fn = tf.contrib.rnn.BasicLSTMCell,
    ...         n_hidden = embedding_size,
    ...         dropout = (0.7 if is_train else None),
    ...         sequence_length = tl.layers.retrieve_seq_length_op2(input_seqs),
    ...         return_seq_2d = True,
    ...         name = 'dynamicrnn')
    >>> print(net.outputs)
    (?, 256)
    >>> net = tl.layers.DenseLayer(net, n_units=vocab_size, name="output")
    >>> print(net.outputs)
    (?, 10000)
    >>> loss = tl.cost.cross_entropy_seq_with_mask(net.outputs, target_seqs, input_mask)


if isinstance(scale, numbers.Integral):<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if isinstance(scale, numbers.Real):<EOL><INDENT>if scale < <NUM_LIT:0.>:<EOL><INDENT>raise ValueError('<STR_LIT>' % scale)<EOL><DEDENT>if scale == <NUM_LIT:0.>:<EOL><INDENT>tl.logging.info('<STR_LIT>')<EOL>return lambda _, name=None: None<EOL><DEDENT><DEDENT>def mn_i(weights, name='<STR_LIT>'):<EOL><INDENT>"""<STR_LIT>"""<EOL>with tf.name_scope(name) as scope:<EOL><INDENT>my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='<STR_LIT>')<EOL>if tf.__version__ <= '<STR_LIT>':<EOL><INDENT>standard_ops_fn = standard_ops.mul<EOL><DEDENT>else:<EOL><INDENT>standard_ops_fn = standard_ops.multiply<EOL><DEDENT>return standard_ops_fn(<EOL>my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), <NUM_LIT:1>)), name=scope<EOL>)<EOL><DEDENT><DEDENT>return mn_i<EOL>

Max-norm input regularization removes the neurons of previous layer.
    Returns a function that can be used to apply max-norm regularization to each row of weight matrix.
    The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.

    Parameters
    ----------
    scale : float
        A scalar multiplier `Tensor`. 0.0 disables the regularizer.

    Returns
    ---------
    A function with signature `mn_i(weights, name=None)` that apply Lo regularization.

    Raises
    ---------
    ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.


dictionary = {}<EOL>for idx, c in enumerate(string):<EOL><INDENT>dictionary.update({c: idx})<EOL><DEDENT>return dictionary<EOL>


return {x: <NUM_LIT:1> for x in dp_dict}<EOL>

Input a dictionary, return a dictionary that all items are set to one.

    Used for disable dropout, dropconnect layer and so on.

    Parameters
    ----------
    dp_dict : dictionary
        The dictionary contains key and number, e.g. keeping probabilities.

    Examples
    --------
    >>> dp_dict = dict_to_one( network.all_drop )
    >>> dp_dict = dict_to_one( network.all_drop )
    >>> feed_dict.update(dp_dict)


text = "<STR_LIT>" + str(port) + "<STR_LIT>"<EOL>text2 = "<STR_LIT>"<EOL>if not tl.files.exists_or_mkdir(log_dir, verbose=False):<EOL><INDENT>tl.logging.info("<STR_LIT>" % log_dir)<EOL><DEDENT>if _platform == "<STR_LIT>" or _platform == "<STR_LIT>":<EOL><INDENT>raise NotImplementedError()<EOL><DEDENT>elif _platform == "<STR_LIT>":<EOL><INDENT>tl.logging.info('<STR_LIT>' % text)<EOL>subprocess.Popen(<EOL>sys.prefix + "<STR_LIT>" + log_dir + "<STR_LIT>" + str(port), shell=True<EOL>)  <EOL><DEDENT>elif _platform == "<STR_LIT:win32>":<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>tl.logging.info(_platform + text2)<EOL><DEDENT>

Open Tensorboard.

    Parameters
    ----------
    log_dir : str
        Directory where your tensorboard logs are saved
    port : int
        TensorBoard port you want to open, 6006 is tensorboard default


return self.type == '<STR_LIT>'<EOL>

Returns true if this server is a parameter server


while not self._sess.should_stop():<EOL><INDENT>self.train_on_batch()  <EOL>if self.global_step % validate_step_size == <NUM_LIT:0>:<EOL><INDENT>log_str = '<STR_LIT>' % self.global_step<EOL>for n, m in self.validation_metrics:<EOL><INDENT>log_str += '<STR_LIT>' % (n.name, m)<EOL><DEDENT>logging.info(log_str)<EOL><DEDENT><DEDENT>

A helper function that shows how to train and validate a model at the same time.

        Parameters
        ----------
        validate_step_size : int
            Validate the training network every N steps.


@deprecated(date="<STR_LIT>", instructions="<STR_LIT>")<EOL>def create_distributed_session(<EOL>task_spec=None, checkpoint_dir=None, scaffold=None, hooks=None, chief_only_hooks=None, save_checkpoint_secs=<NUM_LIT>,<EOL>save_summaries_steps=object(), save_summaries_secs=object(), config=None, stop_grace_period_secs=<NUM_LIT>,<EOL>log_step_count_steps=<NUM_LIT:100><EOL>):

target = task_spec.target() if task_spec is not None else None<EOL>is_chief = task_spec.is_master() if task_spec is not None else True<EOL>return tf.train.MonitoredTrainingSession(<EOL>master=target, is_chief=is_chief, checkpoint_dir=checkpoint_dir, scaffold=scaffold,<EOL>save_checkpoint_secs=save_checkpoint_secs, save_summaries_steps=save_summaries_steps,<EOL>save_summaries_secs=save_summaries_secs, log_step_count_steps=log_step_count_steps,<EOL>stop_grace_period_secs=stop_grace_period_secs, config=config, hooks=hooks, chief_only_hooks=chief_only_hooks<EOL>)<EOL>

Creates a distributed session.

    It calls `MonitoredTrainingSession` to create a :class:`MonitoredSession` for distributed training.

    Parameters
    ----------
    task_spec : :class:`TaskSpecDef`.
        The task spec definition from create_task_spec_def()
    checkpoint_dir : str.
        Optional path to a directory where to restore variables.
    scaffold : ``Scaffold``
        A `Scaffold` used for gathering or building supportive ops.
        If not specified, a default one is created. It's used to finalize the graph.
    hooks : list of ``SessionRunHook`` objects.
        Optional
    chief_only_hooks : list of ``SessionRunHook`` objects.
        Activate these hooks if `is_chief==True`, ignore otherwise.
    save_checkpoint_secs : int
        The frequency, in seconds, that a checkpoint is saved
        using a default checkpoint saver. If `save_checkpoint_secs` is set to
        `None`, then the default checkpoint saver isn't used.
    save_summaries_steps : int
        The frequency, in number of global steps, that the
        summaries are written to disk using a default summary saver. If both
        `save_summaries_steps` and `save_summaries_secs` are set to `None`, then
        the default summary saver isn't used. Default 100.
    save_summaries_secs : int
        The frequency, in secs, that the summaries are written
        to disk using a default summary saver.  If both `save_summaries_steps` and
        `save_summaries_secs` are set to `None`, then the default summary saver
        isn't used. Default not enabled.
    config : ``tf.ConfigProto``
        an instance of `tf.ConfigProto` proto used to configure the session.
        It's the `config` argument of constructor of `tf.Session`.
    stop_grace_period_secs : int
        Number of seconds given to threads to stop after
        `close()` has been called.
    log_step_count_steps : int
        The frequency, in number of global steps, that the
        global step/sec is logged.

    Examples
    --------
    A simple example for distributed training where all the workers use the same dataset:

    >>> task_spec = TaskSpec()
    >>> with tf.device(task_spec.device_fn()):
    >>>      tensors = create_graph()
    >>> with tl.DistributedSession(task_spec=task_spec,
    ...                            checkpoint_dir='/tmp/ckpt') as session:
    >>>      while not session.should_stop():
    >>>           session.run(tensors)

    An example where the dataset is shared among the workers
    (see https://www.tensorflow.org/programmers_guide/datasets):

    >>> task_spec = TaskSpec()
    >>> # dataset is a :class:`tf.data.Dataset` with the raw data
    >>> dataset = create_dataset()
    >>> if task_spec is not None:
    >>>     dataset = dataset.shard(task_spec.num_workers, task_spec.shard_index)
    >>> # shuffle or apply a map function to the new sharded dataset, for example:
    >>> dataset = dataset.shuffle(buffer_size=10000)
    >>> dataset = dataset.batch(batch_size)
    >>> dataset = dataset.repeat(num_epochs)
    >>> # create the iterator for the dataset and the input tensor
    >>> iterator = dataset.make_one_shot_iterator()
    >>> next_element = iterator.get_next()
    >>> with tf.device(task_spec.device_fn()):
    >>>      # next_element is the input for the graph
    >>>      tensors = create_graph(next_element)
    >>> with tl.DistributedSession(task_spec=task_spec,
    ...                            checkpoint_dir='/tmp/ckpt') as session:
    >>>      while not session.should_stop():
    >>>           session.run(tensors)

    References
    ----------
    - `MonitoredTrainingSession <https://www.tensorflow.org/api_docs/python/tf/train/MonitoredTrainingSession>`__


return [os.path.join(base_dir, os.path.normpath(rp)) for rp in rel_paths]<EOL>

Expand a list of relative paths to a give base directory.

    Parameters
    ----------
    base_dir : str
        The target base directory

    rel_paths : list (or list-like)
        Collection of relative path strings

    Returns
    -------
    expanded_paths : list
        `rel_paths` rooted at `base_dir`

    Examples
    --------
    >>> jams.util.expand_filepaths('/data', ['audio', 'beat', 'seglab'])
    ['/data/audio', '/data/beat', '/data/seglab']


def find_with_extension(in_dir, ext, depth=<NUM_LIT:3>, sort=True):

assert depth >= <NUM_LIT:1><EOL>ext = ext.strip(os.extsep)<EOL>match = list()<EOL>for n in range(<NUM_LIT:1>, depth+<NUM_LIT:1>):<EOL><INDENT>wildcard = os.path.sep.join(["<STR_LIT:*>"]*n)<EOL>search_path = os.path.join(in_dir, os.extsep.join([wildcard, ext]))<EOL>match += glob.glob(search_path)<EOL><DEDENT>if sort:<EOL><INDENT>match.sort()<EOL><DEDENT>return match<EOL>

Naive depth-search into a directory for files with a given extension.

    Parameters
    ----------
    in_dir : str
        Path to search.
    ext : str
        File extension to match.
    depth : int
        Depth of directories to search.
    sort : bool
        Sort the list alphabetically

    Returns
    -------
    matched : list
        Collection of matching file paths.

    Examples
    --------
    >>> jams.util.find_with_extension('Audio', 'wav')
    ['Audio/LizNelson_Rainfall/LizNelson_Rainfall_MIX.wav',
     'Audio/LizNelson_Rainfall/LizNelson_Rainfall_RAW/LizNelson_Rainfall_RAW_01_01.wav',
     'Audio/LizNelson_Rainfall/LizNelson_Rainfall_RAW/LizNelson_Rainfall_RAW_02_01.wav',
     ...
     'Audio/Phoenix_ScotchMorris/Phoenix_ScotchMorris_STEMS/Phoenix_ScotchMorris_STEM_02.wav',
     'Audio/Phoenix_ScotchMorris/Phoenix_ScotchMorris_STEMS/Phoenix_ScotchMorris_STEM_03.wav',
    'Audio/Phoenix_ScotchMorris/Phoenix_ScotchMorris_STEMS/Phoenix_ScotchMorris_STEM_04.wav']


return os.path.splitext(os.path.basename(filepath))[<NUM_LIT:0>]<EOL>

Return the extension-less basename of a file path.

    Parameters
    ----------
    filepath : str
        Path to a file

    Returns
    -------
    base : str
        The name of the file, with directory and extension removed

    Examples
    --------
    >>> jams.util.filebase('my_song.mp3')
    'my_song'


if not os.path.exists(dpath):<EOL><INDENT>os.makedirs(dpath, mode=mode)<EOL><DEDENT>

Safely make a full directory path if it doesn't exist.

    Parameters
    ----------
    dpath : str
        Path of directory/directories to create

    mode : int [default=0777]
        Permissions for the new directories

    See also
    --------
    os.makedirs


if ns_key not in __NAMESPACE__:<EOL><INDENT>raise NamespaceError('<STR_LIT>'.format(ns_key))<EOL><DEDENT>sch = copy.deepcopy(JAMS_SCHEMA['<STR_LIT>']['<STR_LIT>'])<EOL>for key in ['<STR_LIT:value>', '<STR_LIT>']:<EOL><INDENT>try:<EOL><INDENT>sch['<STR_LIT>'][key] = __NAMESPACE__[ns_key][key]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return sch<EOL>

Construct a validation schema for a given namespace.

    Parameters
    ----------
    ns_key : str
        Namespace key identifier (eg, 'beat' or 'segment_tut')

    Returns
    -------
    schema : dict
        JSON schema of `namespace`


if ns_key not in __NAMESPACE__:<EOL><INDENT>raise NamespaceError('<STR_LIT>'.format(ns_key))<EOL><DEDENT>return __NAMESPACE__[ns_key]['<STR_LIT>']<EOL>

Determine whether a namespace has dense formatting.

    Parameters
    ----------
    ns_key : str
        Namespace key identifier

    Returns
    -------
    dense : bool
        True if `ns_key` has a dense packing
        False otherwise.


if '<STR_LIT:type>' in typespec:<EOL><INDENT>return __TYPE_MAP__.get(typespec['<STR_LIT:type>'], np.object_)<EOL><DEDENT>elif '<STR_LIT>' in typespec:<EOL><INDENT>return np.object_<EOL><DEDENT>elif '<STR_LIT>' in typespec:<EOL><INDENT>types = [__get_dtype(v) for v in typespec['<STR_LIT>']]<EOL>if all([t == types[<NUM_LIT:0>] for t in types]):<EOL><INDENT>return types[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>return np.object_<EOL>

Get the dtype associated with a jsonschema type definition

    Parameters
    ----------
    typespec : dict
        The schema definition

    Returns
    -------
    dtype : numpy.dtype
        The associated dtype


obs_sch = namespace(ns_key)<EOL>obs_sch['<STR_LIT:title>'] = '<STR_LIT>'<EOL>sch = copy.deepcopy(JAMS_SCHEMA['<STR_LIT>']['<STR_LIT>'])<EOL>sch['<STR_LIT>'] = obs_sch<EOL>return sch<EOL>

Construct a validation schema for arrays of a given namespace.

    Parameters
    ----------
    ns_key : str
        Namespace key identifier

    Returns
    -------
    schema : dict
        JSON schema of `namespace` observation arrays


def display_multi(annotations, fig_kw=None, meta=True, **kwargs):

if fig_kw is None:<EOL><INDENT>fig_kw = dict()<EOL><DEDENT>fig_kw.setdefault('<STR_LIT>', True)<EOL>fig_kw.setdefault('<STR_LIT>', True)<EOL>display_annotations = []<EOL>for ann in annotations:<EOL><INDENT>for namespace in VIZ_MAPPING:<EOL><INDENT>if can_convert(ann, namespace):<EOL><INDENT>display_annotations.append(ann)<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not len(display_annotations):<EOL><INDENT>raise ParameterError('<STR_LIT>')<EOL><DEDENT>fig, axs = plt.subplots(nrows=len(display_annotations), ncols=<NUM_LIT:1>, **fig_kw)<EOL>if len(display_annotations) == <NUM_LIT:1>:<EOL><INDENT>axs = [axs]<EOL><DEDENT>for ann, ax in zip(display_annotations, axs):<EOL><INDENT>kwargs['<STR_LIT>'] = ax<EOL>display(ann, meta=meta, **kwargs)<EOL><DEDENT>return fig, axs<EOL>

Display multiple annotations with shared axes

    Parameters
    ----------
    annotations : jams.AnnotationArray
        A collection of annotations to display

    fig_kw : dict
        Keyword arguments to `plt.figure`

    meta : bool
        If `True`, display annotation metadata for each annotation

    kwargs
        Additional keyword arguments to the `mir_eval.display` routines

    Returns
    -------
    fig
        The created figure
    axs
        List of subplot axes corresponding to each displayed annotation


times, midi = annotation.to_interval_values()<EOL>return mir_eval.display.piano_roll(times, midi=midi, **kwargs)<EOL>


times, values = annotation.to_interval_values()<EOL>if any(values):<EOL><INDENT>labels = values<EOL><DEDENT>else:<EOL><INDENT>labels = None<EOL><DEDENT>return mir_eval.display.events(times, labels=labels, **kwargs)<EOL>


times, labels = annotation.to_interval_values()<EOL>return mir_eval.display.labeled_intervals(times, labels, **kwargs)<EOL>


times, values = annotation.to_interval_values()<EOL>labels = [_['<STR_LIT>'] for _ in values]<EOL>return mir_eval.display.events(times, labels=labels, **kwargs)<EOL>