text:
#!/usr/bin/python
#coding = utf-8
import numpy as np
import pandas as pd
from RiskQuantLib.Tool.strTool import getLastTradingDate

class setBase():

    def setCode(self,codeString):
        self.code = codeString

    def setName(self,nameString):
        self.name = nameString

    def setIndex(self,indexString):
        self.index = indexString

    def setSubSecurityType(self,subSecurityType):
        self.subSecurityType = subSecurityType

    def setIssuer(self,issuerString):
        self.issuer = issuerString

    def setIssuerObject(self,issuerObject):
        self.issuerObject = issuerObject

    def setIssuerIndustrySection(self,issuerIndustrySectionString):
        self.issuerIndustrySection = issuerIndustrySectionString

    def setIssuerLocation(self,issuerLocationString):
        self.issuerLocation = issuerLocationString

    def setIssuerCode(self,issuerCodeString):
        self.issuerCode = issuerCodeString

    def setHoldingAmount(self,holdingAmountNum,holdingDateTimeStamp = pd.Timestamp.now()):
        from RiskQuantLib.Property.Amount.amount import amount
        if not hasattr(self,'__holdingAmount'):
            self.__holdingAmount = amount(holdingAmountNum)
            self.holdingAmount = self.__holdingAmount.value
        else:
            self.__holdingAmount.setValue(holdingAmountNum)
            self.holdingAmount = self.__holdingAmount.value
        self.__holdingAmount.setEffectiveDate(holdingDateTimeStamp)

    def setLastDayHoldingAmount(self,lastDayHoldingAmountNum,lastDayTimeStamp = getLastTradingDate(pd.Timestamp.now())):
        from RiskQuantLib.Property.Amount.amount import amount
        if not hasattr(self,'__lastDayHoldingAmount'):
            self.__lastDayHoldingAmount = amount(lastDayHoldingAmountNum)
            self.lastDayHoldingAmount = self.__lastDayHoldingAmount.value
        else:
            self.__lastDayHoldingAmount.setValue(lastDayHoldingAmountNum)
            self.lastDayHoldingAmount = self.__lastDayHoldingAmount.value
        self.__lastDayHoldingAmount.setEffectiveDate(lastDayTimeStamp)

    def setIssueDate(self,issueDateTimeStamp):
        self.issueDate = issueDateTimeStamp

    def setLatestBuyinDate(self,latestBuyinDate):
        self.latestBuyinDate = latestBuyinDate

    def setTotalNetMarketValue(self, totalNetMarketValueNum, dateTimeStamp=pd.Timestamp.now()):
        from RiskQuantLib.Property.MarketValue.marketValue import marketValue
        if not hasattr(self, '__totalNetMarketValue'):
            self.__totalNetMarketValue = marketValue(totalNetMarketValueNum)
            self.totalNetMarketValue = self.__totalNetMarketValue.value
        else:
            self.__totalNetMarketValue.setValue(totalNetMarketValueNum)
            self.totalNetMarketValue = self.__totalNetMarketValue.value
        self.__totalNetMarketValue.setEffectiveDate(dateTimeStamp)

    def setHoldingNetMarketValue(self, holdingNetMarketValueNum, dateTimeStamp=pd.Timestamp.now()):
        from RiskQuantLib.Property.MarketValue.marketValue import marketValue
        if not hasattr(self, '__holdingNetMarketValue'):
            self.__holdingNetMarketValue = marketValue(holdingNetMarketValueNum)
            self.holdingNetMarketValue = self.__holdingNetMarketValue.value
        else:
            self.__holdingNetMarketValue.setValue(holdingNetMarketValueNum)
            self.holdingNetMarketValue = self.__holdingNetMarketValue.value
        self.__holdingNetMarketValue.setEffectiveDate(dateTimeStamp)

    def setDaysOfCashingOut(self,daysOfCashingOutNum):
        self.daysOfCashingOut = daysOfCashingOutNum

    def setLatestTradingDate(self,latestTradingDateTimeStamp):
        self.latestTradingDate = latestTradingDateTimeStamp

    def setLastDayBuyInMarketValue(self, lastDayBuyInMarketValueNum, lastDayTimeStamp = getLastTradingDate(pd.Timestamp.now())):
        from RiskQuantLib.Property.MarketValue.marketValue import marketValue
        if not hasattr(self, '__lastDayBuyInMarketValue'):
            self.__lastDayBuyInMarketValue = marketValue(lastDayBuyInMarketValueNum)
            self.lastDayBuyInMarketValue = self.__lastDayBuyInMarketValue.value
        else:
            self.__lastDayBuyInMarketValue.setValue(lastDayBuyInMarketValueNum)
            self.lastDayBuyInMarketValue = self.__lastDayBuyInMarketValue.value
        self.__lastDayBuyInMarketValue.setEffectiveDate(lastDayTimeStamp)

    def setHistoricalCost(self,historicalCostNum):
        from RiskQuantLib.Property.Cost.cost import cost
        if not hasattr(self, '__historicalCost'):
            self.__historicalCost = cost(historicalCostNum)
            self.historicalCost = self.__historicalCost.value
        else:
            self.__historicalCost.setValue(historicalCostNum)
            self.historicalCost = self.__historicalCost.value

    def setProfitAndLoss(self, profitAndLossNum):
        from RiskQuantLib.Property.ProfitAndLoss.profitAndLoss import profitAndLoss
        if not hasattr(self, '__profitAndLoss'):
            self.__profitAndLoss = profitAndLoss(profitAndLossNum)
            self.profitAndLoss = self.__profitAndLoss.value
        else:
            self.__profitAndLoss.setValue(profitAndLossNum)
            self.profitAndLoss = self.__profitAndLoss.value

    # build module, contents below will be automatically built and replaced, self-defined functions shouldn't be written here
    #-<Begin>
    #-<End>

max_stars_repo_name: SyuyaMurakami/RiskQuantLib-Doc

max_issues_repo_name: SyuyaMurakami/RiskQuantLib-Doc

max_forks_repo_name: SyuyaMurakami/RiskQuantLib-Doc


text:
# coding=utf-8
# Copyright 2022 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Train and test a robust model with the implicit bias of an optimizer."""

import copy

from absl import app
from absl import flags
from absl import logging
import cvxpy as cp
import jax
from jax import numpy as jnp
from jax.flatten_util import ravel_pytree
from ml_collections.config_flags import config_flags
import numpy as np
import scipy.linalg

import robust_optim.adversarial as adversarial
import robust_optim.data as data_loader
import robust_optim.model as model
from robust_optim.norm import norm_f
from robust_optim.norm import norm_type_dual
import robust_optim.optim as optim
import robust_optim.summary as summary_tools

FLAGS = flags.FLAGS

config_flags.DEFINE_config_file('config', None, 'Config file name.')

def evaluate_risks(data, predict_f, loss_f, model_param):
  """Returns the risk of a model for various loss functions.

  Args:
    data: An array of data samples for approximating the risk.
    predict_f: Function that predicts labels given input.
    loss_f: Function that outputs model's specific loss function.
    model_param: Model parameters.

  Returns:
    Dictionary of risks for following loss functions:
        (model's loss, 0/1, adversarial risk wrt a single norm-ball).
  """
  inputs, labels = data
  pred = predict_f(model_param, inputs)
  loss = loss_f(model_param, inputs, labels)
  zero_one_risk = (1 - (pred == labels)).mean()
  return {
      'loss': loss,
      'zero_one': zero_one_risk,
  }

def evaluate_adversarial_risk(data, predict_f, loss_adv_f, dloss_adv_dx,
                              model_param, normalize_f, config, rng_key):
  """Evaluating adversarial risk by looping over epsilon.

  Args:
    data: An array of data samples for approximating the risk.
    predict_f: Function that predicts labels given input.
    loss_adv_f: The loss function. This loss has to be specific to the model to
      tackle gradient masking.
    dloss_adv_dx: The gradient function of the adversarial loss w.r.t. the
      input. Ideally, we will have multiple loss functions even on different
      layers of network. This loss has to be specific to the model to tackle
      gradient masking.
    model_param: Model parameters.
    normalize_f: A function to normalize the weights of the model.
    config: Dictionary of hyperparameters.
    rng_key: JAX random number generator key.

  Returns:
    Dictionary adversarial risk wrt a range of norm-balls.
  """
  _, labels = data
  # If config.adv.eps_from_cvxpy, eps is reset after min-norm solution is found
  eps_iter, eps_tot = config.adv.eps_iter, config.adv.eps_tot
  config_new = copy.deepcopy(config.adv)
  adv_risk = []
  adv_eps = []
  for i in jnp.arange(0, 1.05, 0.05):
    config_new.eps_iter = float(eps_iter * i)
    config_new.eps_tot = float(eps_tot * i)
    x_adv_multi = adversarial.find_adversarial_samples_multi_attack(
        data, loss_adv_f, dloss_adv_dx,
        model_param, normalize_f, config_new, rng_key)
    correct_label = jnp.zeros(1)
    for x_adv in x_adv_multi:
      pred_adv = predict_f(model_param, x_adv)
      correct_label += (pred_adv == labels) / len(x_adv_multi)
    adv_risk += [float((1 - correct_label).mean())]
    adv_eps += [config_new.eps_tot]
  return {'adv/%s' % config.adv.norm_type: (adv_eps, adv_risk)}

def train(model_param, train_test_data, predict_f, loss_f, loss_adv_f,
          linearize_f, normalize_f, loss_and_prox_op, summary, config, rng_key):
  """Train a model and log risks."""
  dloss_dw = jax.grad(loss_f, argnums=0)
  dloss_adv_dx = jax.grad(loss_adv_f, argnums=1)

  train_data = train_test_data[0]
  xtrain, ytrain = train_data

  # Precompute min-norm solutions
  if config.enable_cvxpy:
    min_norm_w = {}
    for norm_type in config.available_norm_types:
      min_norm_w[norm_type] = compute_min_norm_solution(xtrain, ytrain,
                                                        norm_type)
    if config.adv.eps_from_cvxpy:
      dual_norm = norm_type_dual(config.adv.norm_type)
      wcomp = min_norm_w[dual_norm]
      wnorm = norm_f(wcomp, dual_norm)
      margin = 1. / wnorm
      config.adv.eps_tot = config.adv.eps_iter = float(2 * margin)

  if config['optim']['name'] == 'cvxpy':
    norm_type = config['optim']['norm']
    cvxpy_sol = compute_min_norm_solution(xtrain, ytrain, norm_type)
    model_param = jnp.array(cvxpy_sol)

  # Train loop
  optim_step, optim_options = optim.get_optimizer_step(config['optim'])
  niters = optim_options['niters']
  for step in range(1, niters):
    # Take one optimization step
    if config['optim']['name'] != 'cvxpy':
      if config['optim']['adv_train']['enable']:
        # Adversarial training
        rng_key, rng_subkey = jax.random.split(rng_key)
        x_adv = adversarial.find_adversarial_samples(train_data, loss_adv_f,
                                                     dloss_adv_dx,
                                                     model_param, normalize_f,
                                                     config.optim.adv_train,
                                                     rng_key)
        train_data_new = x_adv, ytrain
      else:
        # Standard training
        train_data_new = train_data
      if config['optim']['name'] == 'fista':
        model_param, optim_options = optim_step(train_data_new,
                                                loss_and_prox_op,
                                                model_param,
                                                optim_options)
      else:
        model_param, optim_options = optim_step(train_data_new,
                                                loss_f,
                                                model_param, optim_options)

    # Log risks and other statistics
    if (step + 1) % config.log_interval == 0:
      # Evaluate risk on train/test sets
      for do_train in [True, False]:
        data = train_test_data[0] if do_train else train_test_data[1]
        prefix = 'risk/train' if do_train else 'risk/test'
        risk = evaluate_risks(data, predict_f, loss_f, model_param)
        for rname, rvalue in risk.items():
          summary.scalar('%s/%s' % (prefix, rname), rvalue, step=step)
        rng_key, rng_subkey = jax.random.split(rng_key)
        risk = evaluate_adversarial_risk(data, predict_f, loss_adv_f,
                                         dloss_adv_dx,
                                         model_param, normalize_f, config,
                                         rng_subkey)
        for rname, rvalue in risk.items():
          summary.array('%s/%s' % (prefix, rname), rvalue, step=step)

      grad = dloss_dw(model_param, xtrain, ytrain)
      grad_ravel, _ = ravel_pytree(grad)
      model_param_ravel, _ = ravel_pytree(model_param)
      for norm_type in config.available_norm_types:
        # Log the norm of the gradient w.r.t. various norms
        if not norm_type.startswith('dft'):
          summary.scalar(
              'grad/norm/' + norm_type,
              norm_f(grad_ravel, norm_type),
              step=step)

        # Log weight norm
        if not norm_type.startswith('dft'):
          wnorm = norm_f(model_param_ravel, norm_type)
          summary.scalar('weight/norm/' + norm_type, wnorm, step=step)

        # Log margin for the equivalent linearized single layer model
        linear_param = linearize_f(model_param)
        min_loss = jnp.min(ytrain * (linear_param.T @ xtrain))
        wcomp = linear_param / min_loss
        wnorm = norm_f(wcomp, norm_type)
        margin = jnp.sign(min_loss) * 1 / wnorm
        summary.scalar('margin/' + norm_type, margin, step=step)
        summary.scalar('weight/linear/norm/' + norm_type, wnorm, step=step)

        # Cosine similarity between the current params and min-norm solution
        if config.enable_cvxpy:

          def cos_sim(a, b):
            return jnp.dot(a, b) / (jnp.linalg.norm(a) * jnp.linalg.norm(b))

          min_norm_w_ravel, _ = ravel_pytree(min_norm_w[norm_type])
          cs = cos_sim(linear_param.flatten(), min_norm_w_ravel)
          summary.scalar('csim_to_wmin/' + norm_type, cs, step=step)

      if 'step_size' in optim_options:
        summary.scalar('optim/step_size', optim_options['step_size'], step=step)

      logging.info('Epoch: [%d/%d]\t%s', step + 1, niters,
                   summary.last_scalars_to_str(config.log_keys))
      logging.flush()

  summary.flush()

def compute_min_norm_solution(x, y, norm_type):
  """Compute the min-norm solution using a convex-program solver."""
  w = cp.Variable((x.shape[0], 1))
  if norm_type == 'linf':
    # compute minimal L_infinity solution
    constraints = [cp.multiply(y, (w.T @ x)) >= 1]
    prob = cp.Problem(cp.Minimize(cp.norm_inf(w)), constraints)
  elif norm_type == 'l2':
    # compute minimal L_2 solution
    constraints = [cp.multiply(y, (w.T @ x)) >= 1]
    prob = cp.Problem(cp.Minimize(cp.norm2(w)), constraints)
  elif norm_type == 'l1':
    # compute minimal L_1 solution
    constraints = [cp.multiply(y, (w.T @ x)) >= 1]
    prob = cp.Problem(cp.Minimize(cp.norm1(w)), constraints)
  elif norm_type[0] == 'l':
    # compute minimal Lp solution
    p = float(norm_type[1:])
    constraints = [cp.multiply(y, (w.T @ x)) >= 1]
    prob = cp.Problem(cp.Minimize(cp.pnorm(w, p)), constraints)
  elif norm_type == 'dft1':
    w = cp.Variable((x.shape[0], 1), complex=True)
    # compute minimal Fourier L1 norm (||F(w)||_1) solution
    dft = scipy.linalg.dft(x.shape[0]) / np.sqrt(x.shape[0])
    constraints = [cp.multiply(y, (cp.real(w).T @ x)) >= 1]
    prob = cp.Problem(cp.Minimize(cp.norm1(dft @ w)), constraints)
  prob.solve(verbose=True)
  logging.info('Min %s-norm solution found (norm=%.4f)', norm_type,
               float(norm_f(w.value, norm_type)))
  return cp.real(w).value

def main_with_config(config):
  logging.info(str(config.log_dir))
  summary = summary_tools.SummaryWriter(config.log_dir,
                                        config.available_norm_types)
  logging.info(str(config))
  summary.object('config', config)

  rng_key = jax.random.PRNGKey(config.seed)
  rng_subkey = jax.random.split(rng_key, 3)

  model_ret = model.get_model_functions(rng_subkey[0], config.dim,
                                        **config.model)
  (model_param, predict_f, loss_f, loss_adv_f, linearize_f, normalize_f,
   loss_and_prox_op) = model_ret
  train_test_generator = data_loader.get_train_test_generator(config.dataset)
  train_test_data = train_test_generator(config, rng_subkey[1])

  train(model_param, train_test_data, predict_f, loss_f, loss_adv_f,
        linearize_f, normalize_f, loss_and_prox_op, summary, config,
        rng_subkey[2])

def main(_):
  config = FLAGS.config

  main_with_config(config)

if __name__ == '__main__':
  app.run(main)

ext: py

reason: import numpy,import scipy,import cvxpy,import jax,from jax


text:
from sentience.training.inputtype.Input import Input
import numpy as np

class CategoricalInput(Input):

    def __init__(self, name:str, categoryEnumeration:list):
        super().__init__(name)
        self.categoryEnumeration = categoryEnumeration

    def addValueToInputList(self, value, input:list):
        for category in self.categoryEnumeration:
            if category == value:
                input.append(np.float32(1))
            else:
                input.append(np.float32(0))

reason: import numpy


text:
import random
import numpy as np
from torch.utils.data.dataset import Dataset as BaseDataset
import scipy.ndimage

from .crops import ImageCropper

class Dataset(BaseDataset):
    def __init__(self, h5dataset, image_indexes, config, stage='train', transform=None):
        self.cropper = ImageCropper(config.img_rows,
                                    config.img_cols,
                                    config.target_rows,
                                    config.target_cols,
                                    config.train_pad if stage=='train' else config.test_pad,
                                    config.use_crop,
                                    config.use_resize)
        self.dataset = h5dataset
        self.image_indexes = image_indexes if isinstance(image_indexes, list) else image_indexes.tolist()
        self.transform = transform
        self.stage = stage
        self.keys = {'image', 'image_name'}
        self.config = config

    def __getitem__(self, item):
        raise NotImplementedError

    def image_to_float(self, image):
        return np.moveaxis(image / (255. if image.dtype == np.uint8 else 1), -1, 0).astype(np.float32)

class MaskedDataset(Dataset):
    def __init__(self, h5dataset, image_indexes, config, stage, transform=None):
        super(MaskedDataset, self).__init__(h5dataset, image_indexes, config, stage, transform)
        self.keys = {'image', 'mask', 'image_name'}

    def expand_mask(self, mask):
        return np.expand_dims(mask / 255., axis=0).astype(np.float32)

    def distance_transform(self, mask):
        emask = -scipy.ndimage.distance_transform_edt(~mask) + scipy.ndimage.distance_transform_edt(mask)
        emask_img = np.sign(emask) * np.log1p(np.abs(emask))
        return np.expand_dims(emask_img, axis=0).astype(np.float32)

class TrainDataset(MaskedDataset):
    def __init__(self, h5dataset, image_indexes, config, stage='train', transform=None):
        super(TrainDataset, self).__init__(h5dataset, image_indexes, config, stage, transform)

    def __getitem__(self, idx):
        """
        idx seems to be unused
        """
        im_idx = self.image_indexes[idx % len(self.image_indexes)]
        for ix in range(50):
            sx, sy = self.cropper.randomCropCoords()
            name = self.dataset['names'][im_idx]
            if 'alphas' in self.dataset:
                alpha = self.cropper.getImage(self.dataset, 'alphas', im_idx, sx, sy)
                if np.mean(alpha) < 3:
                    continue
            mask = self.cropper.getImage(self.dataset, 'masks', im_idx, sx, sy)
            if 3 < np.mean(mask) < 252:  # sample borders
                break
            if random.random() < .7:  # sample background twice less then target
                break
        im = self.cropper.getImage(self.dataset, 'images', im_idx, sx, sy)
        if self.transform is not None:
            im, mask = self.transform(im, mask)
        return {'image': self.image_to_float(im), 'mask': self.expand_mask(mask), 'image_name': name}

    def __len__(self):
        return len(self.image_indexes) * self.config.epoch_size # epoch size is len images

class SequentialDataset(Dataset):
    def __init__(self, h5dataset, image_indexes, config, stage='train', transform=None):
        super(SequentialDataset, self).__init__(h5dataset, image_indexes, config, stage, transform)
        self.good_tiles = []
        self.init_good_tiles()
        self.keys = {'image', 'image_name', 'sy', 'sx'}

    def init_good_tiles(self):
        self.good_tiles = []
        positions = self.cropper.positions
        for im_idx in self.image_indexes:
            if 'alphas' in self.dataset:
                alpha_generator = self.cropper.sequentialCrops(self.dataset['alphas'][im_idx])
                for idx, alpha in enumerate(alpha_generator):
                    if np.mean(alpha) > 5:
                        self.good_tiles.append((im_idx, *positions[idx]))
            else:
                for pos in positions:
                    self.good_tiles.append((im_idx, *pos))

    def __getitem__(self, idx):
        if idx >= self.__len__():
            return None
        im_idx, sx, sy = self.good_tiles[idx]
        im = self.cropper.getImage(self.dataset, 'images', im_idx, sx, sy)
        if self.transform is not None:
            im = self.transform(im)
        name = self.dataset['names'][im_idx]
        return {'image': self.image_to_float(im), 'startx': sx, 'starty': sy, 'image_name': name}

    def __len__(self):
        return len(self.good_tiles)

class ValDataset(SequentialDataset, MaskedDataset):
    def __init__(self, h5dataset, image_indexes, config, stage='train', transform=None):
        super(ValDataset, self).__init__(h5dataset, image_indexes, config, stage, transform)
        self.keys = {'image', 'mask', 'image_name', 'sx', 'sy'}

    def __getitem__(self, idx):
        res = SequentialDataset.__getitem__(self, idx)
        if res is None:
            return res
        im_idx, sx, sy = self.good_tiles[idx]
        mask = self.cropper.getImage(self.dataset, 'masks', im_idx, sx, sy)
        res['mask'] = self.expand_mask(mask)
        return res

max_forks_repo_name: jayden-chua/image-mask

reason: import numpy,import scipy


text:
# -*- coding: utf-8 -*-
"""
Created on 2017-5-9

@author: cheng.li
"""

import numpy as np

from alphamind.utilities import group_mapping
from alphamind.utilities import simple_abssum
from alphamind.utilities import transform

def long_short_builder(er: np.ndarray,
                       leverage: float = 1.,
                       groups: np.ndarray = None,
                       masks: np.ndarray = None) -> np.ndarray:
    er = er.copy()

    if masks is not None:
        er[masks] = 0.
        er[~masks] = er[~masks] - er[~masks].mean()

    if er.ndim == 1:
        er = er.reshape((-1, 1))

    if groups is None:
        return er / simple_abssum(er, axis=0) * leverage
    else:
        groups = group_mapping(groups)
        return transform(groups, er, 'scale', scale=leverage)

max_stars_repo_name: rongliang-tech/alpha-mind


text:
# coding=utf-8
# Copyright 2021 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Run training with Tandem-RL: one active and one passive agent."""

import sys

from absl import logging
from dopamine.discrete_domains import atari_lib
from dopamine.discrete_domains import gym_lib
from dopamine.discrete_domains import iteration_statistics
from dopamine.discrete_domains import run_experiment
import gin
import numpy as np
import tensorflow as tf
from tandem_dqn import minatar_env
from tandem_dqn import tandem_dqn_agent

@gin.configurable
def create_tandem_agents_and_checkpoints(sess, environment, agent_name='dqn',
                                         summary_writer=None, debug_mode=False):
  """Creates a tandem agent.

  Args:
    sess: TF session, unused since we are in JAX.
    environment: A gym environment (e.g. Atari 2600).
    agent_name: str, name of the agent to create.
    summary_writer: A Tensorflow summary writer to pass to the agent
      for in-agent training statistics in Tensorboard.
    debug_mode: bool, unused.

  Returns:
    An active and passive agent.
  """
  assert agent_name is not None
  del sess
  del debug_mode
  if agent_name == 'dqn':
    return tandem_dqn_agent.TandemDQNAgent(
        num_actions=environment.action_space.n, summary_writer=summary_writer)
  else:
    raise ValueError('Unknown agent: {}'.format(agent_name))

@gin.configurable
class TandemRunner(run_experiment.Runner):
  """Runner class to run Tandem RL experiments."""

  def __init__(self,
               base_dir,
               create_agent_fn,
               suite='atari'):
    if suite == 'atari':
      create_environment_fn = atari_lib.create_atari_environment
    elif suite == 'classic':
      create_environment_fn = gym_lib.create_gym_environment
    elif suite == 'minatar':
      create_environment_fn = minatar_env.create_minatar_env
    else:
      raise ValueError(f'Unknown suite: {suite}')
    super().__init__(base_dir, create_agent_fn,
                     create_environment_fn=create_environment_fn)

  def _initialize_episode(self, agent_type='active'):
    """Initialization for a new episode.

    Args:
      agent_type: str, the type of agent to run.

    Returns:
      action: int, the initial action chosen by the agent.
    """
    initial_observation = self._environment.reset()
    return self._agent.begin_episode(agent_type, initial_observation)

  def _run_one_episode(self, agent_type='active'):
    """Executes a full trajectory of the agent interacting with the environment.

    Args:
      agent_type: str, the type of agent to run.

    Returns:
      The number of steps taken and the total reward.
    """
    step_number = 0
    total_reward = 0.

    action = self._initialize_episode(agent_type)
    is_terminal = False

    # Keep interacting until we reach a terminal state.
    while True:
      observation, reward, is_terminal = self._run_one_step(action)

      total_reward += reward
      step_number += 1

      # Perform reward clipping.
      reward = np.clip(reward, -1, 1)

      if (self._environment.game_over or
          step_number == self._max_steps_per_episode):
        # Stop the run loop once we reach the true end of episode.
        break
      elif is_terminal:
        # If we lose a life but the episode is not over, signal an artificial
        # end of episode to the agent.
        if agent_type == 'active':
          self._agent.end_episode(reward)
        action = self._agent.begin_episode(agent_type, observation)
      else:
        action = self._agent.step(agent_type, reward, observation)

    self._end_episode(reward)

    return step_number, total_reward

  def _run_one_phase(self, min_steps, statistics, run_mode_str,
                     agent_type='active'):
    """Runs the agent/environment loop until a desired number of steps.

    We follow the Machado et al., 2017 convention of running full episodes,
    and terminating once we've run a minimum number of steps.

    Args:
      min_steps: int, minimum number of steps to generate in this phase.
      statistics: `IterationStatistics` object which records the experimental
        results.
      run_mode_str: str, describes the run mode for this agent.
      agent_type: str, the type of agent to run.

    Returns:
      Tuple containing the number of steps taken in this phase (int), the sum of
        returns (float), and the number of episodes performed (int).
    """
    step_count = 0
    num_episodes = 0
    sum_returns = 0.

    while step_count < min_steps:
      episode_length, episode_return = self._run_one_episode(agent_type)
      statistics.append({
          '{}_{}_episode_lengths'.format(run_mode_str,
                                         agent_type): episode_length,
          '{}_{}_episode_returns'.format(run_mode_str,
                                         agent_type): episode_return
      })
      step_count += episode_length
      sum_returns += episode_return
      num_episodes += 1
      # We use sys.stdout.write instead of logging so as to flush frequently
      # without generating a line break.
      sys.stdout.write('Steps executed: {} '.format(step_count) +
                       'Episode length: {} '.format(episode_length) +
                       'Return: {}\r'.format(episode_return))
      sys.stdout.flush()
    return step_count, sum_returns, num_episodes

  def _run_eval_phase(self, statistics, agent_type='active'):
    """Run evaluation phase on both passive and active learners.

    Args:
      statistics: `IterationStatistics` object which records the experimental
        results. Note - This object is modified by this method.
      agent_type: str, the type of agent to run.

    Returns:
      num_episodes: int, The number of episodes run in this phase.
      average_reward: float, The average reward generated in this phase.
    """
    # Perform the evaluation phase -- no learning.
    self._agent.eval_mode = True
    _, sum_returns, num_episodes = self._run_one_phase(
        self._evaluation_steps, statistics, 'eval', agent_type)
    average_return = sum_returns / num_episodes if num_episodes > 0 else 0.0
    logging.info('Average undiscounted return per eval episode (%s): %.2f',
                 agent_type, average_return)
    statistics.append({f'{agent_type}_eval_average_return': average_return})
    return num_episodes, average_return

  def _run_one_iteration(self, iteration):
    """Runs one iteration of agent/environment interaction.

    An iteration involves running several episodes until a certain number of
    steps are obtained. The interleaving of train/eval phases implemented here
    are to match the implementation of (Mnih et al., 2015).

    Args:
      iteration: int, current iteration number, used as a global_step for saving
        Tensorboard summaries.

    Returns:
      A dict containing summary statistics for this iteration.
    """
    statistics = iteration_statistics.IterationStatistics()
    logging.info('Starting iteration %d', iteration)
    num_episodes_train, average_reward_train, average_steps_per_second = (
        self._run_train_phase(statistics))
    active_num_episodes_eval, active_average_reward_eval = self._run_eval_phase(
        statistics, 'active')
    passive_num_episodes_eval, passive_average_reward_eval = (
        self._run_eval_phase(statistics, 'passive'))

    self._save_tensorboard_summaries(iteration, num_episodes_train,
                                     average_reward_train,
                                     active_num_episodes_eval,
                                     active_average_reward_eval,
                                     passive_num_episodes_eval,
                                     passive_average_reward_eval,
                                     average_steps_per_second)
    return statistics.data_lists

  def _save_tensorboard_summaries(self, iteration,
                                  num_episodes_train,
                                  average_reward_train,
                                  active_num_episodes_eval,
                                  active_average_reward_eval,
                                  passive_num_episodes_eval,
                                  passive_average_reward_eval,
                                  average_steps_per_second):
    """Save statistics as tensorboard summaries.

    Args:
      iteration: int, The current iteration number.
      num_episodes_train: int, number of training episodes run.
      average_reward_train: float, The average training reward.
      active_num_episodes_eval: int, number of active evaluation episodes run.
      active_average_reward_eval: float, The average active evaluation reward.
      passive_num_episodes_eval: int, number of passive evaluation episodes run.
      passive_average_reward_eval: float, The average passive evaluation reward.
      average_steps_per_second: float, The average number of steps per second.
    """
    summary = tf.compat.v1.Summary(value=[
        tf.compat.v1.Summary.Value(
            tag='Train/NumEpisodes', simple_value=num_episodes_train),
        tf.compat.v1.Summary.Value(
            tag='Train/AverageReturns', simple_value=average_reward_train),
        tf.compat.v1.Summary.Value(
            tag='Train/AverageStepsPerSecond',
            simple_value=average_steps_per_second),
        tf.compat.v1.Summary.Value(
            tag='Eval/ActiveNumEpisodes',
            simple_value=active_num_episodes_eval),
        tf.compat.v1.Summary.Value(
            tag='Eval/ActiveAverageReturns',
            simple_value=active_average_reward_eval),
        tf.compat.v1.Summary.Value(
            tag='Eval/PassiveNumEpisodes',
            simple_value=passive_num_episodes_eval),
        tf.compat.v1.Summary.Value(
            tag='Eval/PassiveAverageReturns',
            simple_value=passive_average_reward_eval)
    ])
    self._summary_writer.add_summary(summary, iteration)
    self._summary_writer.flush()

max_stars_repo_name: DionysisChristopoulos/google-research

max_issues_repo_name: DionysisChristopoulos/google-research

max_forks_repo_name: DionysisChristopoulos/google-research


text:
import time
import argparse
import os
import numpy as np
import glob
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.externals import joblib
from keras.models import Model

import config
import util
import matplotlib.pyplot as plt

def parse_args():
    """
    Parse input arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('--path', dest='path', help='Path to image', default=None, type=str)
    parser.add_argument('--accuracy', action='store_true', help='To print accuracy score')
    parser.add_argument('--plot_confusion_matrix', action='store_true')
    parser.add_argument('--execution_time', action='store_true')
    parser.add_argument('--store_activations', action='store_true')
    parser.add_argument('--novelty_detection', action='store_true')
    parser.add_argument('--model', type=str, required=True, help='Base model architecture',
                        choices=[config.MODEL_RESNET50, config.MODEL_RESNET152, config.MODEL_INCEPTION_V3,
                                 config.MODEL_VGG16])
    parser.add_argument('--data_dir', help='Path to data train directory')
    parser.add_argument('--batch_size', default=100, type=int, help='How many files to predict on at once')
    args = parser.parse_args()
    return args

def get_files(path):
    if os.path.isdir(path):
        files = glob.glob(path + '*.jpg')
    elif path.find('*') > 0:
        files = glob.glob(path)
    else:
        files = [path]

    if not len(files):
        print('No images found by the given path')
        exit(1)

    return files

def decode_predictions_custom(preds, top=3):
    CLASS_CUSTOM = ["3147", "3148", "3182", "3184", "3191", "3193", "3201", "3202", "3203", "3205", "3206", "3207",
                    "3208", "3209", "3211", "3212", "3214", "3215", "3216", "3217", "3218", "3220", "3226", "3227",
                    "3228", "3229", "3230", "3238", "3240", "3249", "3253", "3281", "3283", "3284", "3285", "3286",
                    "3287", "3288", "3290", "3291", "3292", "3293", "3298", "3299", "3300"]

    results = []
    for pred in preds:
        # print("pred:", pred)
        # print(" pred.argsort():", pred.argsort())
        #print(len(pred.argsort()))
        top_indices = pred.argsort()[-top:][::-1]
        #print("top_indices:", top_indices)
        # for i in top_indices:
        #     print("i:", i)

        #     #print("CLASS_CUSTOM1:", CLASS_CUSTOM[i])
        #
        #
        #     print((CLASS_CUSTOM[i]+":"+str(pred[i]*100)))
            #print(tuple(CLASS_CUSTOM[i])+pred[i]*100)
        #result = [tuple(CLASS_CUSTOM[i]) + (pred[i]*100,)for i in top_indices]
        result = [(CLASS_CUSTOM[i] + ":" + str("%.2f" % (pred[i] * 100))) for i in top_indices]

        results.append(result)
        # print("results:", results)
        #print(results[0])
    return results

def get_inputs_and_trues(files):
    inputs = []
    y_true = []

    for i in files:
        #print(i)
        x = model_module.load_img(i)
        try:
            image_class = i.split(os.sep)[-2]
            #print(i.split(os.sep))
            #print("image_class:", image_class)
            # print("wwwwwwwwwwwwwwwwwwwwwwwww1")
            keras_class = int(classes_in_keras_format[image_class])
            print("keras_class:", keras_class)
            # print(keras_class)
            y_true.append(keras_class)
        except Exception:
            # print("os.path.split(i):", (os.path.split(i)[1]))
            y_true.append(os.path.split(i)[1])

        inputs.append(x)
        # print(inputs)
        # print(len(inputs))
        # print("y_true:", y_true)
        # print(len(y_true))
        #
        # print("qwerrt")
    return y_true, inputs

def predict(path):
    files = get_files(path)
    #print("files:", files)
    n_files = len(files)
    print('Found {} files'.format(n_files))

    y_trues = []
    predictions = np.zeros(shape=(n_files,))
    nb_batch = int(np.ceil(n_files / float(args.batch_size)))
    #print("nb_batch:", nb_batch)
    for n in range(0, nb_batch):
        print('Batch {}'.format(n))
        n_from = n * args.batch_size
        n_to = min(args.batch_size * (n + 1), n_files)
        # print(n_from)
        # print(n_to)
        y_true, inputs = get_inputs_and_trues(files[n_from:n_to])
        y_trues += y_true
        # print("1111111111111111")
        #print(len(y_trues))
        # print("y_trues:", y_trues)
        #print(len(inputs))

        #print(inputs)

        # print("22222222222222222")
        # input = np.array(inputs)
        # print(input.shape)
        # print("3333333333333333333333333")

        if not args.store_activations:
            # Warm up the model
            # if n == 0:
            #     model.predict(np.array([inputs[0]]))
            #     print(model.predict(np.array([inputs[0]])))
            #     print(np.argmax(model.predict(np.array([inputs[0]])), axis=1))
            #     print("n=0")
            global out, out2

            out = model.predict(np.array(inputs))
            # print("out:", out)
            # out1 = np.array(out)
            # print("out.shape:", out1.shape)
            out2 = decode_predictions_custom(out, top=3)

            # print("top3:", out2)
            # print(len(out2))
            # out3 = np.array(out2)
            # print("out3.shape:", out3.shape)
            # print(np.argmax(out, axis=1))
            predictions[n_from:n_to] = np.argmax(out, axis=1)
            #print("predictions[n_from:n_to]:", predictions[n_from:n_to])

        if not args.store_activations:
            for i, p in enumerate(predictions):
                print("predictions:", predictions)
                #print("enumerate(predictions):", enumerate(predictions))
                print("i:", i)
                print("p:", p)
                print("class00:", classes_in_keras_format)
                print("keys:", list(classes_in_keras_format.keys()))
                print("key1:", list(classes_in_keras_format.keys())[1])
                print("values:", list(classes_in_keras_format.values()))
                print("class index(p):", list(classes_in_keras_format.values()).index(p))
                #print("out[i]", out[i])
                recognized_class = list(classes_in_keras_format.keys())[list(classes_in_keras_format.values()).index(p)]
                #print('recognized_class:', recognized_class)
                print(' picture:{}   top3:{}'.format(y_trues[i], out2[i]))
                print(' picture:{}   in {} file files be predicted as {} class  , score : {}'.format(y_trues[i], files[i].split(os.sep)[-2],
                                                                                recognized_class, ("%.2f" % (100 * np.max(
                                                                                out[i])))))

if __name__ == '__main__':
    args = parse_args()
    if args.data_dir:
        config.data_dir = args.data_dir
        config.set_paths()
    if args.model:
        config.model = args.model

    util.set_img_format()
    model_module = util.get_model_class_instance()
    model = model_module.load()
    classes_in_keras_format = util.get_classes_in_keras_format()
    predict(args.path)

max_stars_repo_path: predict (another copy).py

max_issues_repo_path: predict (another copy).py

max_forks_repo_path: predict (another copy).py


text:
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

import unittest

import numpy as np
import paddle
import maskrcnn_benchmark.modeling.backbone.fbnet_builder as fbnet_builder

if paddle.device.get_device()==  "gpu:0" :
    TEST_CUDA=True
else:
    TEST_CUDA = False

# TEST_CUDA = paddle.cuda.is_available()

# import torch
def _test_primitive(self, device, op_name, op_func, N, C_in, C_out, expand, stride):
    op = op_func(C_in, C_out, expand, stride).to(device)
    input = paddle.rand([N, C_in, 7, 7], dtype=paddle.float32).to(device)
    output = op(input)
    self.assertEqual(
        output.shape[:2], torch.Size([N, C_out]),
        'Primitive {} failed for shape {}.'.format(op_name, input.shape)
    )

class TestFBNetBuilder(unittest.TestCase):
    def test_identity(self):
        id_op = fbnet_builder.Identity(20, 20, 1)
        input = paddle.rand([10, 20, 7, 7], dtype=paddle.float32)
        output = id_op(input)
        np.testing.assert_array_equal(np.array(input), np.array(output))

        id_op = fbnet_builder.Identity(20, 40, 2)
        input = paddle.rand([10, 20, 7, 7], dtype=paddle.float32)
        output = id_op(input)
        np.testing.assert_array_equal(output.shape, [10, 40, 4, 4])

    def test_primitives(self):
        ''' Make sures the primitives runs '''
        for op_name, op_func in fbnet_builder.PRIMITIVES.items():
            print('Testing {}'.format(op_name))

            _test_primitive(
                self, "cpu",
                op_name, op_func,
                N=20, C_in=16, C_out=32, expand=4, stride=1
            )

    @unittest.skipIf(not TEST_CUDA, "no CUDA detected")
    def test_primitives_cuda(self):
        ''' Make sures the primitives runs on cuda '''
        for op_name, op_func in fbnet_builder.PRIMITIVES.items():
            print('Testing {}'.format(op_name))

            _test_primitive(
                self, "cuda",
                op_name, op_func,
                N=20, C_in=16, C_out=32, expand=4, stride=1
            )

    def test_primitives_empty_batch(self):
        ''' Make sures the primitives runs '''
        for op_name, op_func in fbnet_builder.PRIMITIVES.items():
            print('Testing {}'.format(op_name))

            # test empty batch size
            _test_primitive(
                self, "cpu",
                op_name, op_func,
                N=0, C_in=16, C_out=32, expand=4, stride=1
            )

    @unittest.skipIf(not TEST_CUDA, "no CUDA detected")
    def test_primitives_cuda_empty_batch(self):
        ''' Make sures the primitives runs '''
        for op_name, op_func in fbnet_builder.PRIMITIVES.items():
            print('Testing {}'.format(op_name))

            # test empty batch size
            _test_primitive(
                self, "cuda",
                op_name, op_func,
                N=0, C_in=16, C_out=32, expand=4, stride=1
            )

if __name__ == "__main__":
    unittest.main()


text:
"""
This script is used to split the storm events downloaded from the website.
"""
# import packages
import numpy as np
import pandas as pd
import os
import datetime
# define the repository path
from common_settings import obspath
import matplotlib.pyplot as plt
import seaborn as sns

# separate the long time series into one-year data sets
file_name = '126001A_daily.csv'
df = pd.read_csv(obspath + file_name, index_col='Datetime')
# conver the index of df into datetime
time_format = "%H:%M:%S %d/%m/%Y"
df.index = pd.to_datetime(df.index, format=time_format)
# df.set_index(['Time'], inplace=True)

for year in range(2006, 2020):
    print(year)
    time_str = [f'00:00:00 01/07/{year}', f'00:00:00 30/06/{year+1}']
    time_period = [pd.datetime.strptime(time_str[0], time_format),
        pd.datetime.strptime(time_str[1], time_format)]

    year1 = df.loc[time_period[0]:time_period[1], :] * 24 * 3600 / 1e6
    year1.rename(columns={year1.columns[0]: 'Flow (ML)'}, inplace=True)
    year1.to_csv(f'{obspath}{time_str[0][-4:]}_{time_str[1][-4:]}.csv')


text:
#   Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import unittest
import numpy as np
import sys

sys.path.append("..")
from op_test import OpTest
import paddle.fluid.core as core
import paddle.fluid as fluid
from paddle.nn.functional import interpolate
import paddle

from test_bilinear_interp_v2_op import bilinear_interp_np

paddle.enable_static()

class TestBilinearInterpOp(OpTest):

    def set_npu(self):
        self.__class__.use_npu = True
        self.place = paddle.NPUPlace(0)

    def setUp(self):
        self.set_npu()
        self.out_size = None
        self.actual_shape = None
        self.data_layout = 'NCHW'
        self.init_test_case()
        self.op_type = "bilinear_interp_v2"
        input_np = np.random.random(self.input_shape).astype(self.dtype)

        if self.data_layout == "NCHW":
            in_h = self.input_shape[2]
            in_w = self.input_shape[3]
        else:
            in_h = self.input_shape[1]
            in_w = self.input_shape[2]
        scale_h = 0
        scale_w = 0
        if self.scale:
            if isinstance(self.scale, float) or isinstance(self.scale, int):
                if self.scale > 0.:
                    scale_h = scale_w = float(self.scale)
            if isinstance(self.scale, list) and len(self.scale) == 1:
                scale_w = scale_h = self.scale[0]
            elif isinstance(self.scale, list) and len(self.scale) > 1:
                scale_w = self.scale[1]
                scale_h = self.scale[0]
            out_h = int(in_h * scale_h)
            out_w = int(in_w * scale_w)
        else:
            out_h = self.out_h
            out_w = self.out_w

        output_np = bilinear_interp_np(input_np, out_h, out_w, scale_w, scale_h,
                                       self.out_size, self.actual_shape,
                                       self.align_corners, self.align_mode,
                                       self.data_layout)

        self.inputs = {'X': input_np}
        if self.out_size is not None:
            self.inputs['OutSize'] = self.out_size
        if self.actual_shape is not None:
            self.inputs['OutSize'] = self.actual_shape

        self.attrs = {
            'out_h': self.out_h,
            'out_w': self.out_w,
            'interp_method': self.interp_method,
            'align_corners': self.align_corners,
            'align_mode': self.align_mode,
            'data_layout': self.data_layout
        }
        if self.scale:
            if isinstance(self.scale, float) or isinstance(self.scale, int):
                if self.scale > 0.:
                    self.scale = [self.scale]
            if isinstance(self.scale, list) and len(self.scale) == 1:
                self.scale = [self.scale[0], self.scale[0]]
            self.attrs['scale'] = self.scale
        self.outputs = {'Out': output_np}

    def test_check_output(self):
        self.check_output_with_place(self.place, atol=self.atol)

    def test_check_grad(self):
        self.__class__.exist_check_grad = True
        if self.dtype == 'float16':
            return
        self.max_relative_error = 0.005
        inputs_to_check = ['X']
        output_names = ['Out']
        no_grad_set = set()
        cpu_place = fluid.CPUPlace()
        cpu_grads = self._get_gradient(inputs_to_check, cpu_place, output_names,
                                       no_grad_set)
        npu_grads = self._get_gradient(inputs_to_check, self.place,
                                       output_names, no_grad_set)
        self._assert_is_close(cpu_grads, npu_grads, inputs_to_check,
                              self.max_relative_error,
                              "Gradient Check between places")

    def init_test_case(self):
        self.interp_method = 'bilinear'
        self.input_shape = [2, 3, 5, 7]
        self.out_h = 60
        self.out_w = 25
        self.scale = 1.5
        self.align_corners = False
        self.align_mode = 1
        self.dtype = 'float32'
        self.atol = 1e-5

class TestBilinearInterpCaseFP16(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCaseFP16, self).init_test_case()
        self.dtype = 'float16'
        self.atol = 1e-2

class TestBilinearInterpCase1(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase1, self).init_test_case()
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 1
        self.out_w = 1
        self.scale = 0.

class TestBilinearInterpCase2(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase2, self).init_test_case()
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
        self.scale = 0.

class TestBilinearInterpCase3(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase3, self).init_test_case()
        self.input_shape = [1, 1, 32, 64]
        self.out_h = 64
        self.out_w = 32
        self.scale = 0.

class TestBilinearInterpCase4(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase4, self).init_test_case()
        self.input_shape = [4, 1, 7, 8]
        self.out_h = 1
        self.out_w = 1
        self.scale = 0.
        self.out_size = np.array([2, 2]).astype("int32")

class TestBilinearInterpCase5(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase5, self).init_test_case()
        self.input_shape = [3, 3, 9, 6]
        self.out_h = 12
        self.out_w = 12
        self.scale = 0.
        self.out_size = np.array([11, 11]).astype("int32")

class TestBilinearInterpCase6(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase6, self).init_test_case()
        self.input_shape = [1, 1, 32, 64]
        self.out_h = 64
        self.out_w = 32
        self.scale = 0.
        self.out_size = np.array([65, 33]).astype("int32")

class TestBilinearInterpCase7(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpCase7, self).init_test_case()
        self.input_shape = [1, 1, 32, 64]
        self.out_h = 64
        self.out_w = 32
        self.scale = [2.0, 0.5]

class TestBilinearInterpSame(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpSame, self).init_test_case()
        self.input_shape = [2, 3, 32, 64]
        self.out_h = 32
        self.out_w = 64
        self.scale = 0.

class TestBilinearInterpActualShape(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpActualShape, self).init_test_case()
        self.input_shape = [3, 2, 32, 16]
        self.out_h = 64
        self.out_w = 32
        self.scale = 0.
        self.out_size = np.array([66, 40]).astype("int32")

class TestBilinearInterpDataLayout(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpDataLayout, self).init_test_case()
        self.input_shape = [2, 5, 5, 3]
        self.out_h = 2
        self.out_w = 2
        self.scale = 0.
        self.out_size = np.array([3, 3]).astype("int32")
        self.data_layout = "NHWC"

class TestBilinearInterpOtherMethod1(TestBilinearInterpOp):

    def set_align_mode(self):
        self.align_corners = False
        self.align_mode = 1

class TestBilinearInterpWithMethod2(TestBilinearInterpOp):

    def set_align_mode(self):
        self.align_corners = False
        self.align_mode = 0

class TestBilinearInterpWithMethod3(TestBilinearInterpOp):

    def set_align_mode(self):
        self.align_corners = True
        self.align_mode = 0

class TestBilinearInterpScale1(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpScale1, self).init_test_case()
        self.input_shape = [2, 3, 5, 7]
        self.out_h = 60
        self.out_w = 25
        self.scale = 2.

class TestBilinearInterpScale2(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpScale2, self).init_test_case()
        self.input_shape = [2, 3, 5, 7]
        self.out_h = 60
        self.out_w = 25
        self.scale = 1.

class TestBilinearInterpZero(TestBilinearInterpOp):

    def init_test_case(self):
        super(TestBilinearInterpZero, self).init_test_case()
        self.input_shape = [2, 3, 5, 7]
        self.out_h = 60
        self.out_w = 25
        self.scale = 0.2
        self.align_mode = 0

if __name__ == "__main__":
    unittest.main()


text:
import numpy as np
from datasketches import kll_ints_sketch, kll_floats_sketch, tgt_hll_type, hll_sketch

DEFAULT_SKETCH_SIZE = 2048
DEFAULT_HLL_K = 14
DEFAULT_HLL_TYPE = tgt_hll_type.HLL_8

def calculate_sketch_statistics(data):
    columns = list(data.columns)
    types = list(data.dtypes)

    stats_dict = {}
    for column, type in zip(columns, types):
        if type in [np.int32, np.int64, np.float64]:
            data_col = data[column].to_numpy()
            if data[column].dtype in [np.int32, np.int64]:
                kll = kll_ints_sketch(2048)
            elif data[column].dtype == np.float64:
                kll = kll_floats_sketch(2048)
            kll.update(data_col)
            stat_values = kll.get_quantiles([0.05,0.25,0.5,0.75,0.95])
            stat_names = ["0.05", "Q1", "Median", "Q3", "0.95"]

            hll = hll_sketch(DEFAULT_HLL_K, DEFAULT_HLL_TYPE)
            hll.update(data_col) #works with local fork (np.array extension)
            approx_distinct_count = hll.get_estimate()
            stat_values.append(round(approx_distinct_count))
            stat_names.append("Distinct Count")

            stat_pairs = [list(i) for i in zip(stat_names,stat_values)]
            stats_dict[column] = stat_pairs

    return stats_dict

def calculate_sketch_statistics_np(np_arr):
    columns = np_arr.keys()
    stats_dict = {}
    for column in columns:
        type = np_arr[column].dtype
        if type in [np.int32, np.int64, np.float64]:
            data_col = np_arr[column]
            if type in [np.int32, np.int64]:
                kll = kll_ints_sketch(2048)
            elif type == np.float64:
                kll = kll_floats_sketch(2048)
            kll.update(data_col)
            quantiles = kll.get_quantiles([0.05,0.25,0.5,0.75,0.95])
            quantile_names = ["0.05", "Q1", "Median", "Q3", "0.95"]
            stat_pairs = [list(i) for i in zip(quantile_names,quantiles)]
            stats_dict[column] = stat_pairs

    return stats_dict

max_stars_repo_name: tdoehmen/duckdq

max_issues_repo_name: tdoehmen/duckdq

max_forks_repo_name: tdoehmen/duckdq


text:
# pylint: disable=no-self-use,invalid-name
from typing import List

import numpy

from deep_qa.data.data_indexer import DataIndexer
from deep_qa.data.instances.multiple_choice_qa.question_answer_instance import IndexedQuestionAnswerInstance
from deep_qa.data.instances.multiple_choice_qa.question_answer_instance import QuestionAnswerInstance
from ....common.test_case import DeepQaTestCase

class TestQuestionAnswerInstance(DeepQaTestCase):
    def instance_to_line(self, question: str, answers: List[str], label: int, index=None):
        line = ''
        if index is not None:
            line += str(index) + '\t'
        line += question
        line += '\t'
        line += '###'.join(answers)
        line += '\t'
        line += str(label)
        return line

    def test_read_from_line_handles_three_column(self):
        question = "what is the answer"
        answers = ['a', 'b', 'c']
        label = 1
        line = self.instance_to_line(question, answers, label)
        instance = QuestionAnswerInstance.read_from_line(line)
        assert instance.question_text == question
        assert instance.answer_options == answers
        assert instance.label is label
        assert instance.index is None

    def test_read_from_line_handles_four_column(self):
        question = "what is the answer"
        answers = ['a', 'b', 'c']
        label = 1
        index = 23
        line = self.instance_to_line(question, answers, label, index)
        instance = QuestionAnswerInstance.read_from_line(line)
        assert instance.question_text == question
        assert instance.answer_options == answers
        assert instance.label is label
        assert instance.index is index

    def test_words_includes_question_and_answers(self):
        instance = QuestionAnswerInstance("a b c", ["d", "e f"], 1)
        assert instance.words() == {'words': ['a', 'b', 'c', 'd', 'e', 'f']}

    def test_to_indexed_instance_converts_correctly(self):
        instance = QuestionAnswerInstance("a A b", ["d", "e f D"], 1)
        data_indexer = DataIndexer()
        a_index = data_indexer.add_word_to_index("a")
        d_index = data_indexer.add_word_to_index("d")
        oov_index = data_indexer.get_word_index(data_indexer._oov_token)  # pylint: disable=protected-access
        indexed_instance = instance.to_indexed_instance(data_indexer)
        assert indexed_instance.question_indices == [a_index, a_index, oov_index]
        assert len(indexed_instance.option_indices) == 2
        assert indexed_instance.option_indices[0] == [d_index]
        assert indexed_instance.option_indices[1] == [oov_index, oov_index, d_index]

class TestIndexedQuestionAnswerInstance(DeepQaTestCase):
    def setUp(self):
        super(TestIndexedQuestionAnswerInstance, self).setUp()
        self.instance = IndexedQuestionAnswerInstance([1, 2, 3],
                                                      [[2, 3], [4], [5, 6]],
                                                      1)

    def test_get_padding_lengths_returns_three_correct_lengths(self):
        assert self.instance.get_padding_lengths() == {
                'num_sentence_words': 3,
                'answer_length': 2,
                'num_options': 3
                }

    def test_pad_calls_pad_on_all_options(self):
        self.instance.pad({'num_sentence_words': 2, 'answer_length': 2, 'num_options': 3})
        assert self.instance.question_indices == [2, 3]
        assert self.instance.option_indices[0] == [2, 3]
        assert self.instance.option_indices[1] == [0, 4]
        assert self.instance.option_indices[2] == [5, 6]

    def test_pad_adds_empty_options_when_necessary(self):
        self.instance.pad({'num_sentence_words': 1, 'answer_length': 1, 'num_options': 4})
        assert self.instance.question_indices == [3]
        assert self.instance.option_indices[0] == [3]
        assert self.instance.option_indices[1] == [4]
        assert self.instance.option_indices[2] == [6]
        assert self.instance.option_indices[3] == [0]
        assert len(self.instance.option_indices) == 4

    def test_pad_removes_options_when_necessary(self):
        self.instance.pad({'num_sentence_words': 1, 'answer_length': 1, 'num_options': 1})
        assert self.instance.question_indices == [3]
        assert self.instance.option_indices[0] == [3]
        assert len(self.instance.option_indices) == 1

    def test_as_training_data_produces_correct_numpy_arrays(self):
        self.instance.pad({'num_sentence_words': 3, 'answer_length': 2, 'num_options': 3})
        inputs, label = self.instance.as_training_data()
        assert numpy.all(label == numpy.asarray([0, 1, 0]))
        assert numpy.all(inputs[0] == numpy.asarray([1, 2, 3]))
        assert numpy.all(inputs[1] == numpy.asarray([[2, 3], [0, 4], [5, 6]]))


text:
# *****************************************************************************
#  Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#      * Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#      * Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#      * Neither the name of the NVIDIA CORPORATION nor the
#        names of its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
#  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# *****************************************************************************
import copy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

# "Gated Convolutional Neural Networks for Domain Adaptation"
#  https://arxiv.org/pdf/1905.06906.pdf
@torch.jit.script
def GTU(input_a, input_b, n_channels: int):
    """Gated Tanh Unit (GTU)"""
    in_act = input_a+input_b
    t_act = torch.tanh(in_act[:, :n_channels, :])
    s_act = torch.sigmoid(in_act[:, n_channels:, :])
    acts = t_act * s_act
    return acts

@torch.jit.script
def GTRU(input_a, input_b, n_channels: int):# saves significant VRAM and runs faster, unstable for first 150K~ iters. (test a difference layer initialization?)
    """Gated[?] Tanh ReLU Unit (GTRU)"""
    in_act = input_a+input_b
    t_act = torch.tanh(in_act[:, :n_channels, :])
    r_act = torch.nn.functional.relu(in_act[:, n_channels:, :], inplace=True)
    acts = t_act * r_act
    return acts

@torch.jit.script
def GLU(input_a, input_b, n_channels: int):
    """Gated Linear Unit (GLU)"""
    in_act = input_a+input_b
    l_act = in_act[:, :n_channels, :]
    s_act = torch.sigmoid(in_act[:, n_channels:, :])
    acts = l_act * s_act
    return acts

# Random units I wanted to try
@torch.jit.script
def TTU(input_a, input_b, n_channels: int):
    """Tanh Tanh Unit (TTU)"""
    in_act = input_a+input_b
    t_act = torch.tanh(in_act[:, :n_channels, :])
    t_act2 = torch.tanh(in_act[:, n_channels:, :])
    acts = t_act * t_act2
    return acts

@torch.jit.script
def STU(input_a, input_b, n_channels: int):
    """SeLU Tanh Unit (STU)"""
    in_act = input_a+input_b
    t_act = torch.tanh(in_act[:, :n_channels, :])
    s_act = torch.nn.functional.selu(in_act[:, n_channels:, :], inplace=True)
    acts = t_act * s_act
    return acts

@torch.jit.script
def GTSU(input_a, input_b, n_channels: int):
    """Gated TanhShrink Unit (GTSU)"""
    in_act = input_a+input_b
    t_act = torch.nn.functional.tanhshrink(in_act[:, :n_channels, :])
    s_act = torch.sigmoid(in_act[:, n_channels:, :])
    acts = t_act * s_act
    return acts

@torch.jit.script
def SPTU(input_a, input_b, n_channels: int):
    """Softplus Tanh Unit (SPTU)"""
    in_act = input_a+input_b
    t_act = torch.tanh(in_act[:, :n_channels, :])
    s_act = torch.nn.functional.softplus(in_act[:, n_channels:, :])
    acts = t_act * s_act
    return acts

@torch.jit.script
def GSIU(input_a, input_b, n_channels: int):
    """Gated Sinusoidal Unit (GSIU)"""
    in_act = input_a+input_b
    t_act = torch.sin(in_act[:, :n_channels, :])
    s_act = torch.sigmoid(in_act[:, n_channels:, :])
    acts = t_act * s_act
    return acts

@torch.jit.script
def GSIRU(input_a, input_b, n_channels: int):
    """Gated SIREN Unit (GSIRU)"""
    in_act = input_a+input_b
    in_act[:, :n_channels, :].detach().mul_(16) # modify tensor WITHOUT telling autograd.
    t_act = torch.sin(in_act[:, :n_channels, :])
    s_act = torch.sigmoid(in_act[:, n_channels:, :])
    acts = t_act * s_act
    return acts

@torch.jit.script
def GTSRU(input_a, input_b, n_channels: int):
    """Gated[?] TanhShrink ReLU Unit (GTSRU)"""
    in_act = input_a+input_b
    t_act = torch.nn.functional.tanhshrink(in_act[:, :n_channels, :])
    r_act = torch.nn.functional.relu(in_act[:, n_channels:, :], inplace=True)
    acts = t_act * r_act
    return acts

@torch.jit.script
def GSIRRU(input_a, input_b, n_channels: int): # best and fastest converging unit, uses a lot of VRAM.
    """Gated[?] SIREN ReLU Unit (GSIRRU)"""
    in_act = input_a+input_b
    in_act[:, :n_channels, :].detach().mul_(16) # modify tensor WITHOUT telling autograd.
    t_act = torch.sin(in_act[:, :n_channels, :])
    r_act = torch.nn.functional.relu(in_act[:, n_channels:, :], inplace=False)
    acts = t_act * r_act
    return acts

@torch.jit.script
def GSIRLRU(input_a, input_b, n_channels: int):
    """Gated[?] SIREN Leaky ReLU Unit (GSIRLRU)"""
    in_act = input_a+input_b
    in_act[:, :n_channels, :].detach().mul_(16) # modify tensor WITHOUT telling autograd.
    t_act = torch.sin(in_act[:, :n_channels, :])
    r_act = torch.nn.functional.leaky_relu(in_act[:, n_channels:, :], negative_slope=0.01, inplace=True)
    acts = t_act * r_act
    return acts

@torch.jit.script
def GSIRRLRU(input_a, input_b, n_channels: int):
    """Gated[?] SIREN Randomized Leaky ReLU Unit (GSIRRLRU)"""
    in_act = input_a+input_b
    in_act[:, :n_channels, :].detach().mul_(16) # modify tensor WITHOUT telling autograd.
    t_act = torch.sin(in_act[:, :n_channels, :])
    r_act = torch.nn.functional.rrelu(in_act[:, n_channels:, :], lower=0.01, upper=0.1, inplace=True)
    acts = t_act * r_act
    return acts

@torch.jit.script
def GTLRU(input_a, input_b, n_channels: int):
    """Gated[?] Tanh Leaky ReLU Unit (GTLRU)"""
    in_act = input_a+input_b
    t_act = torch.tanh(in_act[:, :n_channels, :])
    r_act = torch.nn.functional.leaky_relu(in_act[:, n_channels:, :], negative_slope=0.01, inplace=True)
    acts = t_act * r_act
    return acts

def get_gate_func(gated_unit_str):
    if gated_unit_str.upper() == 'GTU':
        return GTU
    elif gated_unit_str.upper() == 'GTRU':
        return GTRU
    elif gated_unit_str.upper() == 'GTLRU':
        return GTLRU
    elif gated_unit_str.upper() == 'GLU':
        return GLU
    elif gated_unit_str.upper() == 'TTU':
        return TTU
    elif gated_unit_str.upper() == 'STU':
        return STU
    elif gated_unit_str.upper() == 'GTSU':
        return GTSU
    elif gated_unit_str.upper() == 'SPTU':
        return SPTU
    elif gated_unit_str.upper() == 'GSIU':
        return GSIU
    elif gated_unit_str.upper() == 'GSIRU':
        return GSIRU
    elif gated_unit_str.upper() == 'GTSRU':
        return GTSRU
    elif gated_unit_str.upper() == 'GSIRRU':
        return GSIRRU
    elif gated_unit_str.upper() == 'GSIRLRU':
        return GSIRLRU
    elif gated_unit_str.upper() == 'GSIRRLRU':
        return GSIRRLRU
    else:
        raise Exception("gated_unit is invalid\nOptions are ('GTU','GTRU','GLU').")

class TransposedUpsampleNet(nn.Module):
    """
    Uses Transposed Convs to upsample a [B, C, T] Tensor.

    [B, in_channels, T] -> [B, out_channels, prod(scales)*T]
    """
    def __init__(self, in_channels, out_channels, hidden_channels, kernel_size=3, scales=[16, 16], use_last_layer_act_func=False, weightnorm=False, residual=False, residual_linear=False, rezero=False):
        super(TransposedUpsampleNet, self).__init__()
        self.residual = residual
        self.residual_linear = residual_linear
        self.res_weight = nn.Parameter(torch.rand(1)*0.02+0.01) if rezero else None
        self.scales = scales
        self.t_convs = nn.ModuleList()
        for i, scale in enumerate(scales):
            is_first_layer = bool(i == 0)
            is_last_layer = bool(i+1 == len(scales))
            in_dim = in_channels if is_first_layer else hidden_channels
            out_dim = out_channels if is_last_layer else hidden_channels
            k_size = kernel_size[i] if type(kernel_size) == list else kernel_size
            t_conv = nn.ConvTranspose1d(in_dim, out_dim, k_size, scale, padding=(k_size-scale)//2)
            if weightnorm:
                t_conv = torch.nn.utils.weight_norm(t_conv)
            self.t_convs.append(t_conv)
            if not is_last_layer or use_last_layer_act_func:
                self.t_convs.append( nn.LeakyReLU(negative_slope=0.4, inplace=True) )
        self.res_channels = min(in_channels, out_channels)

    def forward(self, x):# [B, C, T]
        if self.residual:
            scale = np.product(self.scales)
            x_interp = F.interpolate(x, scale_factor=scale, mode='linear' if self.residual_linear else 'nearest', align_corners=False)

        for layer in self.t_convs:
            if hasattr(layer, 'inplace'):
                layer.inplace = False if self.training else True
            x = layer(x)

        if self.residual:
            if self.res_weight:
                x *= self.res_weight
            x[:, :self.res_channels] += x_interp[:, :self.res_channels]
        return x

class WN(nn.Module):
    """
    This is the WaveNet like layer for the affine coupling.  The primary difference
    from WaveNet is the convolutions need not be causal.  There is also no dilation
    size reset.  The dilation only doubles on each layer
    """
    def __init__(self, n_in_channels, cond_in_channels, cond_layers, cond_hidden_channels, cond_kernel_size, cond_padding_mode, seperable_conv, merge_res_skip, upsample_mode, n_layers, n_channels, # audio_channels, mel_channels*n_group, n_layers, n_conv_channels
                 speaker_embed_dim, rezero, cond_activation_func='none', negative_slope=None, kernel_size=None, kernel_size_w=None, n_layers_dilations_w=None, n_layers_dilations_h=None, res_skip=True, cond_out_activation_func=True, upsample_first=None, gated_unit='GTU', upsample_factor=None,
                 transposed_conv_hidden_dim=256, transposed_conv_kernel_size=4, transposed_conv_scales=None): # bool: ReZero
        super(WN, self).__init__()
        cond_in_channels += speaker_embed_dim
        kernel_size = kernel_size_w or kernel_size
        assert(kernel_size % 2 == 1)
        assert(n_channels % 2 == 0)
        assert (res_skip or merge_res_skip) or n_layers == 1, "Cannot remove res_skip without using merge_res_skip"
        self.n_layers = n_layers
        self.n_channels = n_channels
        self.speaker_embed_dim = speaker_embed_dim
        self.merge_res_skip = merge_res_skip
        self.upsample_first = upsample_first
        self.upsample_mode = upsample_mode
        self.gated_unit = get_gate_func(gated_unit)

        self.in_layers = nn.ModuleList()
        self.res_skip_layers = nn.ModuleList()

        assert (not rezero), "WN ReZero is depreciated"

        start = nn.Conv1d(n_in_channels, n_channels, 1)
        start = nn.utils.weight_norm(start, name='weight')
        self.start = start

        # Initializing last layer to 0 makes the affine coupling layers
        # do nothing at first.  This helps with training stability
        end = nn.Conv1d(n_channels, 2*n_in_channels, 1)
        end.weight.data.zero_()
        end.bias.data.zero_()
        self.end = end

        if self.speaker_embed_dim:
            max_speakers = 512
            self.speaker_embed = nn.Embedding(max_speakers, self.speaker_embed_dim)

        self.interpolation_required = True
        t_in_dim = None
        t_out_dim = None
        if transposed_conv_scales is not None and len(transposed_conv_scales) > 0 and transposed_conv_hidden_dim and transposed_conv_kernel_size:
            t_in_dim = transposed_conv_hidden_dim if (self.upsample_first is False) else cond_in_channels
            t_out_dim = 2*n_channels*n_layers if (self.upsample_first is False) else cond_in_channels
            self.upsample_net = TransposedUpsampleNet(t_in_dim, t_out_dim, transposed_conv_hidden_dim, transposed_conv_kernel_size, transposed_conv_scales, use_last_layer_act_func=False)
            self.interpolation_required = bool( np.product(transposed_conv_scales) != upsample_factor )

        self.cond_out_activation_func = cond_out_activation_func
        if cond_layers:
            self.cond_layers = nn.ModuleList()
            cond_kernel_size = 2*cond_kernel_size - 1 # 1 -> 1, 2 -> 3, 3 -> 5
            cond_pad = int((cond_kernel_size - 1)/2)
            cond_output_channels = 2*n_channels*n_layers if (t_in_dim is None) else transposed_conv_hidden_dim
            # messy initialization for arbitrary number of layers, input dims and output dims
            cond_in_channels = t_out_dim if (t_out_dim is not None and self.upsample_first == 'before_wn_cond') else cond_in_channels
            dimensions = [cond_in_channels,]+[cond_hidden_channels]*(cond_layers-1)+[cond_output_channels,]
            in_dims = dimensions[:-1]
            out_dims = dimensions[1:]
            # 'zeros','replicate'
            for i in range(len(in_dims)):
                indim = in_dims[i]
                outim = out_dims[i]
                cond_layer = nn.Conv1d(indim, outim, cond_kernel_size, padding=cond_pad, padding_mode=cond_padding_mode)# (in_channels, out_channels, kernel_size)
                cond_layer = nn.utils.weight_norm(cond_layer, name='weight')
                self.cond_layers.append(cond_layer)

            cond_activation_func = cond_activation_func.lower()
            if cond_activation_func == 'none':
                pass
            elif cond_activation_func == 'lrelu':
                self.cond_activation_func = torch.nn.functional.relu
            elif cond_activation_func == 'relu':
                assert negative_slope, "negative_slope not defined in wn_config"
                self.cond_activation_func = torch.nn.LeakyReLU(negative_slope=negative_slope, inplace=False)
            elif cond_activation_func == 'tanh':
                self.cond_activation_func = torch.nn.functional.tanh
            elif cond_activation_func == 'sigmoid':
                self.cond_activation_func = torch.nn.functional.sigmoid
            else:
                raise NotImplementedError

        if type(n_layers_dilations_w) == int:
            n_layers_dilations_w = [n_layers_dilations_w,]*n_layers # constant dilation if using int
            print("WARNING: Using constant dilation factor for WN in_layer dilation width.")
        for i in range(n_layers):
            dilation = 2 ** i if n_layers_dilations_w is None else n_layers_dilations_w[i]
            padding = int((kernel_size*dilation - dilation)/2)
            if (not seperable_conv) or (kernel_size == 1):
                in_layer = nn.Conv1d(n_channels, 2*n_channels, kernel_size,
                                           dilation=dilation, padding=padding, padding_mode='zeros')
                in_layer = nn.utils.weight_norm(in_layer, name='weight')
            else:
                depthwise = nn.Conv1d(n_channels, n_channels, kernel_size,
                                    dilation=dilation, padding=padding, padding_mode='zeros', groups=n_channels)
                depthwise = nn.utils.weight_norm(depthwise, name='weight')
                pointwise = nn.Conv1d(n_channels, 2*n_channels, 1,
                                    dilation=dilation, padding=0)
                pointwise = nn.utils.weight_norm(pointwise, name='weight')
                in_layer = torch.nn.Sequential(depthwise, pointwise)
            self.in_layers.append(in_layer)

            # last one is not necessary
            if i < n_layers - 1 and not self.merge_res_skip:
                res_skip_channels = 2*n_channels
            else:
                res_skip_channels = n_channels

            if res_skip:
                res_skip_layer = nn.Conv1d(n_channels, res_skip_channels, 1)
                res_skip_layer = nn.utils.weight_norm(res_skip_layer, name='weight')
                self.res_skip_layers.append(res_skip_layer)

    def _upsample_mels(self, cond, audio_size, force_interpolate=False):
        if hasattr(self, 'upsample_net') and self.upsample_net is not None:
            cond = self.upsample_net(cond)
        if force_interpolate or (self.interpolation_required and not (cond.shape[2] == audio_size[2])):
            cond = F.interpolate(cond, size=audio_size[2], mode=self.upsample_mode, align_corners=True if self.upsample_mode == 'linear' else None)
        else:
            cond_len = cond.shape[2]
            audio_len = audio_size[2]
            pad_l = (cond_len-audio_len)//2
            pad_r = -(audio_len-cond_len)//2
            cond = cond[:, :, pad_l:-pad_r]
        return cond

    def forward(self, audio, spect, speaker_id=None):
        audio = self.start(audio)

        if self.speaker_embed_dim and speaker_id != None: # add speaker embeddings to spectrogram (channel dim)
            speaker_embeddings = self.speaker_embed(speaker_id)
            speaker_embeddings = speaker_embeddings.unsqueeze(-1).repeat(1, 1, spect.shape[2]) # shape like spect
            spect = torch.cat([spect, speaker_embeddings], dim=1) # and concat them

        if hasattr(self, 'cond_layers') and self.cond_layers:
            for i, layer in enumerate(self.cond_layers):
                spect = layer(spect)
                if hasattr(self, 'cond_activation_func') and (self.cond_out_activation_func or (i != len(self.cond_layers)-1)):
                    spect = self.cond_activation_func(spect)

        if self.upsample_first is False: # if spectrogram hasn't been upsampled in an earlier stage
            spect = self._upsample_mels(spect, audio.shape)

        for i in range(self.n_layers): # note, later layers learn lower frequency information
                                       # receptive field = 2**(n_layers-1)*kernel_size*n_group
                                       # If segment length < receptive field expect trouble learning lower frequencies as other layers try to compensate.
                                       # Since my audio is high-passed at 40Hz, (theoretically) you can expect 48000/(40*2) = 600 samples receptive field minimum required to learn.
            spect_offset = i*2*self.n_channels, (i+1)*2*self.n_channels
            spec = spect[:,spect_offset[0]:spect_offset[1],:]
            acts = self.gated_unit(
                self.in_layers[i](audio),
                spec,
                self.n_channels)

            res_skip_acts = self.res_skip_layers[i](acts) if ( hasattr(self, 'res_skip_layers') and len(self.res_skip_layers) ) else acts

            if i == 0:
                if (not self.merge_res_skip) and (i < self.n_layers - 1):
                    audio = audio + res_skip_acts[:,:self.n_channels,:]
                    output = res_skip_acts[:,self.n_channels:,:]
                else:
                    output = res_skip_acts
            else:
                if (not self.merge_res_skip) and (i < self.n_layers - 1):# if res_skip and not last layer
                    audio = audio + res_skip_acts[:,:self.n_channels,:]
                    output = output + res_skip_acts[:,self.n_channels:,:]
                else:
                    output = output + res_skip_acts

        return self.end(output).chunk(2, 1)

class WN_2d(nn.Module):
    """
    This is the WaveNet like layer for the affine coupling.  The primary difference
    from WaveNet is the convolutions are causal on the height dimension and non-causal on the width dim.  There is also no dilation
    size reset.  The dilation only doubles on each layer
    """
    def __init__(self, n_in_channels, cond_in_channels, cond_layers, cond_hidden_channels, cond_kernel_size, cond_padding_mode, seperable_conv, merge_res_skip, upsample_mode, n_layers, n_channels, # audio_channels, mel_channels*n_group, n_layers, n_conv_channels
                 kernel_size_w, kernel_size_h, speaker_embed_dim, rezero, cond_activation_func='none', negative_slope=None, n_layers_dilations_w=None, n_layers_dilations_h=1, res_skip=True, upsample_first=None, cond_out_activation_func=True, gated_unit='GTU', transposed_conv_hidden_dim=256, transposed_conv_kernel_size=4, transposed_conv_scales=None, upsample_factor=None):
        super(WN_2d, self).__init__()
        cond_in_channels += speaker_embed_dim
        assert(kernel_size_w % 2 == 1)
        assert(n_channels % 2 == 0)
        assert res_skip or merge_res_skip, "Cannot remove res_skip without using merge_res_skip"
        self.n_layers = n_layers
        self.n_channels = n_channels
        self.kernel_size_h = kernel_size_h
        self.speaker_embed_dim = speaker_embed_dim
        self.merge_res_skip = merge_res_skip
        self.upsample_first = upsample_first
        self.upsample_mode = upsample_mode
        self.gated_unit = get_gate_func(gated_unit)

        self.in_layers = nn.ModuleList()
        self.res_skip_layers = nn.ModuleList()

        assert (not rezero), "WN ReZero is depreciated"

        start = nn.Conv2d(1, n_channels, (1,1))
        start = nn.utils.weight_norm(start, name='weight')
        self.start = start

        # Initializing last layer to 0 makes the affine coupling layers
        # do nothing at first.  This helps with training stability
        end = nn.Conv2d(n_channels, 2, (1,1))
        end.weight.data.zero_()
        end.bias.data.zero_()
        self.end = end

        if self.speaker_embed_dim:
            max_speakers = 512
            self.speaker_embed = nn.Embedding(max_speakers, self.speaker_embed_dim)

        self.interpolation_required = True
        t_in_dim = None
        t_out_dim = None
        if transposed_conv_scales is not None and len(transposed_conv_scales) > 0 and transposed_conv_hidden_dim and transposed_conv_kernel_size:
            t_in_dim = transposed_conv_hidden_dim if (self.upsample_first is False) else cond_in_channels
            t_out_dim = 2*n_channels*n_layers if (self.upsample_first is False) else cond_in_channels
            self.upsample_net = TransposedUpsampleNet(t_in_dim, t_out_dim, transposed_conv_hidden_dim, transposed_conv_kernel_size, transposed_conv_scales, use_last_layer_act_func=False)
            self.interpolation_required = bool( np.product(transposed_conv_scales) != upsample_factor )

        self.cond_out_activation_func = cond_out_activation_func
        if cond_layers:
            self.cond_layers = nn.ModuleList()
            cond_kernel_size = 2*cond_kernel_size - 1 # 1 -> 1, 2 -> 3, 3 -> 5
            cond_pad = int((cond_kernel_size - 1)/2)
            cond_output_channels = 2*n_channels*n_layers if (t_in_dim is None or self.upsample_first == 'before_wn_cond') else transposed_conv_hidden_dim
            # messy initialization for arbitrary number of layers, input dims and output dims
            cond_in_channels = t_out_dim if (t_out_dim is not None and self.upsample_first == 'before_wn_cond') else cond_in_channels
            dimensions = [cond_in_channels,]+[cond_hidden_channels]*(cond_layers-1)+[cond_output_channels,]
            in_dims = dimensions[:-1]
            out_dims = dimensions[1:]
            # 'zeros','replicate'
            for i in range(len(in_dims)):
                indim = in_dims[i]
                outim = out_dims[i]
                cond_layer = nn.Conv1d(indim, outim, cond_kernel_size, padding=cond_pad, padding_mode=cond_padding_mode)# (in_channels, out_channels, kernel_size)
                cond_layer = nn.utils.weight_norm(cond_layer, name='weight')
                self.cond_layers.append(cond_layer)

            cond_activation_func = cond_activation_func.lower()
            if cond_activation_func == 'none':
                pass
            elif cond_activation_func == 'lrelu':
                self.cond_activation_func = torch.nn.functional.relu
            elif cond_activation_func == 'relu':
                assert negative_slope, "negative_slope not defined in wn_config"
                self.cond_activation_func = torch.nn.LeakyReLU(negative_slope=negative_slope, inplace=False)
            elif cond_activation_func == 'tanh':
                self.cond_activation_func = torch.nn.functional.tanh
            elif cond_activation_func == 'sigmoid':
                self.cond_activation_func = torch.nn.functional.sigmoid
            else:
                raise NotImplementedError

        if type(n_layers_dilations_w) == int:
            n_layers_dilations_w = [n_layers_dilations_w,]*n_layers # constant dilation if using int
            print("WARNING: Using constant dilation factor for WN in_layer dilation width.")
        if type(n_layers_dilations_h) == int:
            n_layers_dilations_h = [n_layers_dilations_h,]*n_layers # constant dilation if using int

        self.h_dilate = n_layers_dilations_h
        self.padding_h = []
        for i in range(n_layers):
            dilation_h = n_layers_dilations_h[i]
            dilation_w = 2 ** i if n_layers_dilations_w is None else n_layers_dilations_w[i]

            padding_w = ((kernel_size_w-1)*dilation_w)//2
            self.padding_h.append((kernel_size_h-1)*dilation_h) # causal padding https://theblog.github.io/post/convolution-in-autoregressive-neural-networks/
            if (not seperable_conv) or (kernel_size_w == 1 and kernel_size_h == 1):
                in_layer = nn.Conv2d(n_channels, 2*n_channels, (kernel_size_h,kernel_size_w),
                                           dilation=(dilation_h,dilation_w), padding=(0,padding_w), padding_mode='zeros')
                in_layer = nn.utils.weight_norm(in_layer, name='weight')
            else:
                depthwise = nn.Conv2d(n_channels, n_channels, (kernel_size_h,kernel_size_w),
                                    dilation=(dilation_h,dilation_w), padding=(0,padding_w), padding_mode='zeros', groups=n_channels)
                depthwise = nn.utils.weight_norm(depthwise, name='weight')
                pointwise = nn.Conv2d(n_channels, 2*n_channels, (1,1),
                                    dilation=(1,1), padding=(0,0))
                pointwise = nn.utils.weight_norm(pointwise, name='weight')
                in_layer = torch.nn.Sequential(depthwise, pointwise)
            self.in_layers.append(in_layer)

            # last one is not necessary
            if i < n_layers - 1 and not self.merge_res_skip:
                res_skip_channels = 2*n_channels
            else:
                res_skip_channels = n_channels

            if res_skip:
                res_skip_layer = nn.Conv2d(n_channels, res_skip_channels, (1,1))
                res_skip_layer = nn.utils.weight_norm(res_skip_layer, name='weight')
                self.res_skip_layers.append(res_skip_layer)

    def _upsample_mels(self, cond, audio_size, force_interpolate=False):
        if hasattr(self, 'upsample_net') and self.upsample_net is not None:
            cond = self.upsample_net(cond)
        if force_interpolate or (self.interpolation_required and not (cond.shape[2] == audio_size[3])):
            cond = F.interpolate(cond, size=audio_size[3], mode=self.upsample_mode, align_corners=True if self.upsample_mode == 'linear' else None)
        else:
            pad = (cond.shape[2] - audio_size[3])//2
            remainder = pad%2
            cond = cond[:, :, pad:-(pad+remainder)]
        return cond

    def forward(self, audio, spect, speaker_id=None, audio_queues=None, spect_queues=None):
        audio = audio.unsqueeze(1) #   [B, n_group//2, T//n_group] -> [B, 1, n_group//2, T//n_group]
        audio = self.start(audio) # [B, 1, n_group//2, T//n_group] -> [B, n_channels, n_group//2, T//n_group]
        if self.merge_res_skip:
            output = audio
        else:
            output = torch.zeros_like(audio)

        exec_cond_layer = (spect_queues is None) or ( any([x is None for x in spect_queues]) )
        if exec_cond_layer: # process spectrograms
            if self.speaker_embed_dim and speaker_id != None: # add speaker embeddings to spectrogram (channel dim)
                speaker_embeddings = self.speaker_embed(speaker_id)
                speaker_embeddings = speaker_embeddings.unsqueeze(-1).repeat(1, 1, spect.shape[2]) # shape like spect
                spect = torch.cat([spect, speaker_embeddings], dim=1) # and concat them

            if hasattr(self, 'cond_layers') and self.cond_layers:
                for i, layer in enumerate(self.cond_layers): # [B, cond_channels, T//hop_length] -> [B, n_channels*n_layers, T//hop_length]
                    spect = layer(spect)
                    if hasattr(self, 'cond_activation_func') and (self.cond_out_activation_func or (i != len(self.cond_layers)-1)):
                        spect = self.cond_activation_func(spect)

            if not self.upsample_first: # if spectrogram hasn't been upsampled in an earlier stage
                spect = self._upsample_mels(spect, audio.shape)# [B, n_channels*n_layers, T//hop_length] -> [B, n_channels*n_layers, T//n_group]

        if len(spect.shape) < 4:
            spect = spect.unsqueeze(2)# [B, n_channels*n_layers, T//n_group] -> [B, n_channels*n_layers, 1, T//n_group]
            #assert audio.size(3) == spect.size(3), f"audio size of {audio.size(3)} != spect size of {spect.size(3)}"

        for i in range(self.n_layers):
            if exec_cond_layer: # if cond layer has been ran.
                spect_offset = i*2*self.n_channels, (i+1)*2*self.n_channels
                spec = spect[:,spect_offset[0]:spect_offset[1]] # [B, 2*n_channels*n_layers, 1, T//n_group] -> [B, 2*n_channels, 1, T//n_group]
                if spect_queues is not None: # is spect_queues exists...
                    if spect_queues[i] is None: # but this index is empty...
                        spect_queues[i] = spec # save spec into this index.
            else:                       # else...
                spec = spect_queues[i] # load spec from this index.

            if audio_queues is None:# if training/validation...
                audio_cpad = F.pad(audio, (0,0,self.padding_h[i],0)) # apply causal height padding (left, right, top, bottom)
            else: # else, if conv-queue and inference/autoregressive sampling.
                if audio_queues[i] is None: # if first sample in autoregressive sequence, pad start with zeros
                    B, n_channels, n_group, T_group = audio.shape
                    audio_queues[i] = audio.new_zeros( size=[B, n_channels, self.padding_h[i], T_group] )

                # [B, n_channels, n_group, T//n_group]
                audio_queues[i] = audio_cpad = torch.cat((audio_queues[i], audio), dim=2)[:,:,-(self.padding_h[i]+1):] # pop old samples and append new sample to end of n_group dim

            acts = self.in_layers[i](audio_cpad) # [B, n_channels, n_group//2, T//n_group] -> [B, 2*n_channels, pad+n_group//2, T//n_group]
            acts = self.gated_unit(
                acts, # [B, 2*n_channels, n_group//2, T//n_group]
                spec, # [B, 2*n_channels, 1, T//n_group]
                self.n_channels)
            # acts.shape <- [B, n_channels, n_group//2, T//n_group]

            res_skip_acts = self.res_skip_layers[i](acts) if ( hasattr(self, 'res_skip_layers') and len(self.res_skip_layers) ) else acts
            # if merge_res_skip: [B, n_channels, n_group//2, T//n_group] -> [B, n_channels, n_group//2, T//n_group]
            # else: [B, n_channels, n_group//2, T//n_group] -> [B, 2*n_channels, n_group//2, T//n_group]

            if i == 0:
                if (not self.merge_res_skip) and (i < self.n_layers - 1):
                    audio += res_skip_acts[:,:self.n_channels,:]
                    output = res_skip_acts[:,self.n_channels:,:]
                else:
                    output = res_skip_acts
            else:
                if (not self.merge_res_skip) and (i < self.n_layers - 1):# if res_skip and not last layer
                    audio += res_skip_acts[:,:self.n_channels,:]
                    output += res_skip_acts[:,self.n_channels:,:]
                else:
                    output += res_skip_acts

        func_out = self.end(output).transpose(1,0) # [B, n_channels, n_group//2, T//n_group] -> [B, 2, n_group//2, T//n_group] -> [2, B, n_group//2, T//n_group]

        if audio_queues is not None:
            func_out = [func_out,]
            func_out.append(audio_queues)
        if spect_queues is not None:
            func_out.append(spect_queues)
        return func_out

max_stars_repo_name: AstraliteHeart/cookietts

max_issues_repo_name: AstraliteHeart/cookietts

max_forks_repo_name: AstraliteHeart/cookietts