Note:
        Implementation of http://www.aclweb.org/anthology/E17-1104
        We didn't use k-max pooling but GlobalMaxPool1D at the end and didn't explore it in the
        intermediate layers.
480775
def vdcnn(embedding_size, maxlen, max_features,
          filter_nr, kernel_size, repeat_block, dense_size, repeat_dense, output_size, output_activation,
          max_pooling, mean_pooling, weighted_average_attention, concat_mode,
          dropout_embedding, conv_dropout, dense_dropout, dropout_mode,
          conv_kernel_reg_l2, conv_bias_reg_l2,
          dense_kernel_reg_l2, dense_bias_reg_l2,
          use_prelu, use_batch_norm, batch_norm_first):
    

    input_text = Input(shape=(maxlen,))
    x = Embedding(input_dim=max_features, output_dim=embedding_size)(input_text)

    x = dropout_block(dropout_embedding, dropout_mode)(x)

    x = convolutional_block(filter_nr, kernel_size, use_batch_norm, use_prelu, conv_dropout, dropout_mode,
                            conv_kernel_reg_l2, conv_bias_reg_l2, batch_norm_first)(x)

    for i in range(repeat_block):
        if i + 1 != repeat_block:
            x = vdcnn_block(filter_nr, kernel_size, use_batch_norm, use_prelu, conv_dropout, dropout_mode,
                            conv_kernel_reg_l2, conv_bias_reg_l2, batch_norm_first, last_block=False)(x)
        else:
            x = vdcnn_block(filter_nr, kernel_size, use_batch_norm, use_prelu, conv_dropout, dropout_mode,
                            conv_kernel_reg_l2, conv_bias_reg_l2, batch_norm_first, last_block=True)(x)

    predictions = classification_block(dense_size=dense_size, repeat_dense=repeat_dense,
                                       output_size=output_size, output_activation=output_activation,
                                       max_pooling=max_pooling,
                                       mean_pooling=mean_pooling,
                                       weighted_average_attention=weighted_average_attention,
                                       concat_mode=concat_mode,
                                       dropout=dense_dropout,
                                       kernel_reg_l2=dense_kernel_reg_l2, bias_reg_l2=dense_bias_reg_l2,
                                       use_prelu=use_prelu, use_batch_norm=use_batch_norm,
                                       batch_norm_first=batch_norm_first)(x)
    model = Model(inputs=input_text, outputs=predictions)
    return model
297616
def Pool(pool='AnyPool', **kwargs):
    '''
    

    '''

    if pool == 'MPIPool':
        return MPIPool(**kwargs)
    elif pool == 'MultiPool':
        return MultiPool(**kwargs)
    elif pool == 'SerialPool':
        return SerialPool(**kwargs)
    elif pool == 'AnyPool':
        if MPIPool.enabled():
            return MPIPool(**kwargs)
        elif MultiPool.enabled():
            return MultiPool(**kwargs)
        else:
            return SerialPool(**kwargs)
    else:
        raise ValueError('Invalid pool ``%s``.' % pool)
1041939
private static int[] seed(SparseDataset data, int k) {
        int n = data.size();

        int[] y = new int[n];
        SparseArray centroid = data.get(Math.randomInt(n)).x;

        double[] D = new double[n];
        for (int i = 0; i < n; i++) {
            D[i] = Double.MAX_VALUE;
        }

        // pick the next center
        for (int i = 1; i < k; i++) {
            for (int j = 0; j < n; j++) {
                double dist = Math.JensenShannonDivergence(data.get(j).x, centroid);
                if (dist < D[j]) {
                    D[j] = dist;
                    y[j] = i - 1;
                }
            }

            double cutoff = Math.random() * Math.sum(D);
            double cost = 0.0;
            int index = 0;
            for (; index < n; index++) {
                cost += D[index];
                if (cost >= cutoff) {
                    break;
                }
            }

            centroid = data.get(index).x;
        }

        for (int j = 0; j < n; j++) {
            // compute the distance between this sample and the current center
            double dist = Math.JensenShannonDivergence(data.get(j).x, centroid);
            if (dist < D[j]) {
                D[j] = dist;
                y[j] = k - 1;
            }
        }

        return y;
    }
71426
public static int cudnnPoolingForward(
        cudnnHandle handle, 
        cudnnPoolingDescriptor poolingDesc, 
        Pointer alpha, 
        cudnnTensorDescriptor xDesc, 
        Pointer x, 
        Pointer beta, 
        cudnnTensorDescriptor yDesc, 
        Pointer y)
    {
        return checkResult(cudnnPoolingForwardNative(handle, poolingDesc, alpha, xDesc, x, beta, yDesc, y));
    }
493517
private void softmax() {
        double max = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < outputLayer.units; i++) {
            if (outputLayer.output[i] > max) {
                max = outputLayer.output[i];
            }
        }

        double sum = 0.0;
        for (int i = 0; i < outputLayer.units; i++) {
            double out = Math.exp(outputLayer.output[i] - max);
            outputLayer.output[i] = out;
            sum += out;
        }

        for (int i = 0; i < outputLayer.units; i++) {
            outputLayer.output[i] /= sum;
        }
    }
996062
private void init(int maxPoolSize)
   {
      this.maxPoolSize = maxPoolSize;

      this.createdCount = new AtomicInteger(0);
      this.destroyedCount = new AtomicInteger(0);
      this.maxCreationTime = new AtomicLong(Long.MIN_VALUE);
      this.maxGetTime = new AtomicLong(Long.MIN_VALUE);
      this.maxPoolTime = new AtomicLong(Long.MIN_VALUE);
      this.maxUsageTime = new AtomicLong(Long.MIN_VALUE);
      this.maxUsedCount = new AtomicInteger(Integer.MIN_VALUE);
      this.maxWaitCount = new AtomicInteger(0);
      this.maxWaitTime = new AtomicLong(Long.MIN_VALUE);
      this.timedOut = new AtomicInteger(0);
      this.totalBlockingTime = new AtomicLong(0);
      this.totalBlockingTimeInvocations = new AtomicLong(0);
      this.totalCreationTime = new AtomicLong(0);
      this.totalGetTime = new AtomicLong(0);
      this.totalGetTimeInvocations = new AtomicLong(0);
      this.totalPoolTime = new AtomicLong(0);
      this.totalPoolTimeInvocations = new AtomicLong(0);
      this.totalUsageTime = new AtomicLong(0);
      this.totalUsageTimeInvocations = new AtomicLong(0);
      this.inUseCount = new AtomicInteger(0);
      this.blockingFailureCount = new AtomicInteger(0);
      this.waitCount = new AtomicInteger(0);

      this.commitCount = new AtomicLong(0L);
      this.commitTotalTime = new AtomicLong(0L);
      this.commitMaxTime = new AtomicLong(0L);
      this.endCount = new AtomicLong(0L);
      this.endTotalTime = new AtomicLong(0L);
      this.endMaxTime = new AtomicLong(0L);
      this.forgetCount = new AtomicLong(0L);
      this.forgetTotalTime = new AtomicLong(0L);
      this.forgetMaxTime = new AtomicLong(0L);
      this.prepareCount = new AtomicLong(0L);
      this.prepareTotalTime = new AtomicLong(0L);
      this.prepareMaxTime = new AtomicLong(0L);
      this.recoverCount = new AtomicLong(0L);
      this.recoverTotalTime = new AtomicLong(0L);
      this.recoverMaxTime = new AtomicLong(0L);
      this.rollbackCount = new AtomicLong(0L);
      this.rollbackTotalTime = new AtomicLong(0L);
      this.rollbackMaxTime = new AtomicLong(0L);
      this.startCount = new AtomicLong(0L);
      this.startTotalTime = new AtomicLong(0L);
      this.startMaxTime = new AtomicLong(0L);

      SortedSet<String> n = new TreeSet<String>();
      Map<String, Class> t = new HashMap<String, Class>();

      n.add(ACTIVE_COUNT);
      t.put(ACTIVE_COUNT, int.class);

      n.add(AVAILABLE_COUNT);
      t.put(AVAILABLE_COUNT, int.class);

      n.add(AVERAGE_BLOCKING_TIME);
      t.put(AVERAGE_BLOCKING_TIME, long.class);

      n.add(AVERAGE_CREATION_TIME);
      t.put(AVERAGE_CREATION_TIME, long.class);

      n.add(AVERAGE_GET_TIME);
      t.put(AVERAGE_GET_TIME, long.class);

      n.add(AVERAGE_USAGE_TIME);
      t.put(AVERAGE_USAGE_TIME, long.class);

      n.add(AVERAGE_POOL_TIME);
      t.put(AVERAGE_POOL_TIME, long.class);

      n.add(BLOCKING_FAILURE_COUNT);
      t.put(BLOCKING_FAILURE_COUNT, int.class);

      n.add(CREATED_COUNT);
      t.put(CREATED_COUNT, int.class);

      n.add(DESTROYED_COUNT);
      t.put(DESTROYED_COUNT, int.class);

      n.add(IDLE_COUNT);
      t.put(IDLE_COUNT, int.class);

      n.add(IN_USE_COUNT);
      t.put(IN_USE_COUNT, int.class);

      n.add(MAX_CREATION_TIME);
      t.put(MAX_CREATION_TIME, long.class);

      n.add(MAX_GET_TIME);
      t.put(MAX_GET_TIME, long.class);

      n.add(MAX_POOL_TIME);
      t.put(MAX_POOL_TIME, long.class);

      n.add(MAX_USAGE_TIME);
      t.put(MAX_USAGE_TIME, long.class);

      n.add(MAX_USED_COUNT);
      t.put(MAX_USED_COUNT, int.class);

      n.add(MAX_WAIT_COUNT);
      t.put(MAX_WAIT_COUNT, int.class);

      n.add(MAX_WAIT_TIME);
      t.put(MAX_WAIT_TIME, long.class);

      n.add(TIMED_OUT);
      t.put(TIMED_OUT, int.class);

      n.add(TOTAL_BLOCKING_TIME);
      t.put(TOTAL_BLOCKING_TIME, long.class);

      n.add(TOTAL_CREATION_TIME);
      t.put(TOTAL_CREATION_TIME, long.class);

      n.add(TOTAL_GET_TIME);
      t.put(TOTAL_GET_TIME, long.class);

      n.add(TOTAL_POOL_TIME);
      t.put(TOTAL_POOL_TIME, long.class);

      n.add(TOTAL_USAGE_TIME);
      t.put(TOTAL_USAGE_TIME, long.class);

      n.add(WAIT_COUNT);
      t.put(WAIT_COUNT, int.class);

      n.add(XA_COMMIT_COUNT);
      t.put(XA_COMMIT_COUNT, long.class);
      n.add(XA_COMMIT_AVERAGE_TIME);
      t.put(XA_COMMIT_AVERAGE_TIME, long.class);
      n.add(XA_COMMIT_TOTAL_TIME);
      t.put(XA_COMMIT_TOTAL_TIME, long.class);
      n.add(XA_COMMIT_MAX_TIME);
      t.put(XA_COMMIT_MAX_TIME, long.class);

      n.add(XA_END_COUNT);
      t.put(XA_END_COUNT, long.class);
      n.add(XA_END_AVERAGE_TIME);
      t.put(XA_END_AVERAGE_TIME, long.class);
      n.add(XA_END_TOTAL_TIME);
      t.put(XA_END_TOTAL_TIME, long.class);
      n.add(XA_END_MAX_TIME);
      t.put(XA_END_MAX_TIME, long.class);

      n.add(XA_FORGET_COUNT);
      t.put(XA_FORGET_COUNT, long.class);
      n.add(XA_FORGET_AVERAGE_TIME);
      t.put(XA_FORGET_AVERAGE_TIME, long.class);
      n.add(XA_FORGET_TOTAL_TIME);
      t.put(XA_FORGET_TOTAL_TIME, long.class);
      n.add(XA_FORGET_MAX_TIME);
      t.put(XA_FORGET_MAX_TIME, long.class);

      n.add(XA_PREPARE_COUNT);
      t.put(XA_PREPARE_COUNT, long.class);
      n.add(XA_PREPARE_AVERAGE_TIME);
      t.put(XA_PREPARE_AVERAGE_TIME, long.class);
      n.add(XA_PREPARE_TOTAL_TIME);
      t.put(XA_PREPARE_TOTAL_TIME, long.class);
      n.add(XA_PREPARE_MAX_TIME);
      t.put(XA_PREPARE_MAX_TIME, long.class);

      n.add(XA_RECOVER_COUNT);
      t.put(XA_RECOVER_COUNT, long.class);
      n.add(XA_RECOVER_AVERAGE_TIME);
      t.put(XA_RECOVER_AVERAGE_TIME, long.class);
      n.add(XA_RECOVER_TOTAL_TIME);
      t.put(XA_RECOVER_TOTAL_TIME, long.class);
      n.add(XA_RECOVER_MAX_TIME);
      t.put(XA_RECOVER_MAX_TIME, long.class);

      n.add(XA_ROLLBACK_COUNT);
      t.put(XA_ROLLBACK_COUNT, long.class);
      n.add(XA_ROLLBACK_AVERAGE_TIME);
      t.put(XA_ROLLBACK_AVERAGE_TIME, long.class);
      n.add(XA_ROLLBACK_TOTAL_TIME);
      t.put(XA_ROLLBACK_TOTAL_TIME, long.class);
      n.add(XA_ROLLBACK_MAX_TIME);
      t.put(XA_ROLLBACK_MAX_TIME, long.class);

      n.add(XA_START_COUNT);
      t.put(XA_START_COUNT, long.class);
      n.add(XA_START_AVERAGE_TIME);
      t.put(XA_START_AVERAGE_TIME, long.class);
      n.add(XA_START_TOTAL_TIME);
      t.put(XA_START_TOTAL_TIME, long.class);
      n.add(XA_START_MAX_TIME);
      t.put(XA_START_MAX_TIME, long.class);

      this.names = Collections.unmodifiableSortedSet(n);
      this.types = Collections.unmodifiableMap(t);
      this.enabled = new AtomicBoolean(true);
      
      ResourceBundle defaultResourceBundle = 
         ResourceBundle.getBundle("poolstatistics", Locale.US,
                                  SecurityActions.getClassLoader(PoolStatisticsImpl.class));
      this.rbs = new HashMap<Locale, ResourceBundle>(1);
      this.rbs.put(Locale.US, defaultResourceBundle);

      clear();
   }
71776
public static KryoPool withBuffer(int poolSize,
      final KryoInstantiator ki,
      final int outBufferMin,
      final int outBufferMax) {
    return new KryoPool(poolSize) {
      protected SerDeState newInstance() {
        return new SerDeState(ki.newKryo(), new Input(), new Output(outBufferMin, outBufferMax));
      }
    };
  }
332510
private DoubleMinMax sampleMinMax(Relation<O> relation, DistanceQuery<O> distFunc) {
    int size = relation.size();
    Random rnd = new Random();
    // estimate minimum and maximum.
    int k = (int) Math.max(25, FastMath.pow(relation.size(), 0.2));
    TreeSet<DoubleDBIDPair> minhotset = new TreeSet<>();
    TreeSet<DoubleDBIDPair> maxhotset = new TreeSet<>(Collections.reverseOrder());

    int randomsize = (int) Math.max(25, FastMath.pow(relation.size(), 0.2));
    double rprob = ((double) randomsize) / size;
    ArrayModifiableDBIDs randomset = DBIDUtil.newArray(randomsize);

    DBIDIter iter = relation.iterDBIDs();
    if(!iter.valid()) {
      throw new EmptyDataException();
    }
    DBID firstid = DBIDUtil.deref(iter);
    iter.advance();
    minhotset.add(DBIDUtil.newPair(Double.MAX_VALUE, firstid));
    maxhotset.add(DBIDUtil.newPair(Double.MIN_VALUE, firstid));
    for(; iter.valid(); iter.advance()) {
      // generate candidates for min distance.
      ArrayList<DoubleDBIDPair> np = new ArrayList<>(k * 2 + randomsize * 2);
      for(DoubleDBIDPair pair : minhotset) {
        // skip the object itself
        if(DBIDUtil.equal(iter, pair)) {
          continue;
        }
        double d = distFunc.distance(iter, pair);
        np.add(DBIDUtil.newPair(d, iter));
        np.add(DBIDUtil.newPair(d, pair));
      }
      for(DBIDIter iter2 = randomset.iter(); iter2.valid(); iter2.advance()) {
        double d = distFunc.distance(iter, iter2);
        np.add(DBIDUtil.newPair(d, iter));
        np.add(DBIDUtil.newPair(d, iter2));
      }
      minhotset.addAll(np);
      shrinkHeap(minhotset, k);

      // generate candidates for max distance.
      ArrayList<DoubleDBIDPair> np2 = new ArrayList<>(k * 2 + randomsize * 2);
      for(DoubleDBIDPair pair : minhotset) {
        // skip the object itself
        if(DBIDUtil.equal(iter, pair)) {
          continue;
        }
        double d = distFunc.distance(iter, pair);
        np2.add(DBIDUtil.newPair(d, iter));
        np2.add(DBIDUtil.newPair(d, pair));
      }
      for(DBIDIter iter2 = randomset.iter(); iter2.valid(); iter2.advance()) {
        double d = distFunc.distance(iter, iter2);
        np.add(DBIDUtil.newPair(d, iter));
        np.add(DBIDUtil.newPair(d, iter2));
      }
      maxhotset.addAll(np2);
      shrinkHeap(maxhotset, k);

      // update random set
      if(randomset.size() < randomsize) {
        randomset.add(iter);
      }
      else if(rnd.nextDouble() < rprob) {
        randomset.set((int) Math.floor(rnd.nextDouble() * randomsize), iter);
      }
    }
    return new DoubleMinMax(minhotset.first().doubleValue(), maxhotset.first().doubleValue());
  }
1170113
public static VectorAccumulator mkMaxAccumulator() {
        return new VectorAccumulator() {
            private double result = Double.NEGATIVE_INFINITY;

            @Override
            public void update(int i, double value) {
                result = Math.max(result, value);
            }

            @Override
            public double accumulate() {
                double value = result;
                result = Double.NEGATIVE_INFINITY;
                return value;
            }
        };
    }
753969
public void calculateMaxFindSkip()
    {
        Map<DFAState<T>,Integer> indexOf = new NumMap<>();
        Map<DFAState<T>,Integer> skip = new NumMap<>();
        Deque<DFAState<T>> stack = new ArrayDeque<>();
        findSkip(root, indexOf, stack, new LinkedList());
        root.setAcceptStartLength(1);
    }
983667
@SuppressWarnings("unchecked")
    private void fixdownMax(int k) {
        int c = 2 * k;
        while (c <= size) {
            int m = maxChildOrGrandchild(k);
            if (m > c + 1) { // grandchild
                if (((Comparable<? super K>) array[m]).compareTo(array[k]) <= 0) {
                    break;
                }
                K tmp = array[k];
                array[k] = array[m];
                array[m] = tmp;
                if (((Comparable<? super K>) array[m]).compareTo(array[m / 2]) < 0) {
                    tmp = array[m];
                    array[m] = array[m / 2];
                    array[m / 2] = tmp;
                }
                // go down
                k = m;
                c = 2 * k;
            } else { // child
                if (((Comparable<? super K>) array[m]).compareTo(array[k]) > 0) {
                    K tmp = array[k];
                    array[k] = array[m];
                    array[m] = tmp;
                }
                break;
            }
        }
    }
115094
private static int[] allocateShardsToNodes(int numShards, int numNodes, List<Set<Integer>> weightPerServer) {
      int[] shardsPerServer = new int[numNodes];
      Iterator<Integer> cyclicNodeIterator = Stream.iterate(0, i -> (i + 1) % numNodes).iterator();
      while (numShards > 0) {
         int slot = cyclicNodeIterator.next();
         if (!weightPerServer.get(slot).isEmpty()) {
            shardsPerServer[slot]++;
            numShards--;
         }
      }
      return shardsPerServer;
   }
959668
protected boolean incrementPoolSize(int expectingMaxSize) {

        Integer maxSize = null;
        long currentOverflowPoolSize;
        try {
            lock.lockInterruptibly();
            int currentMaxSize = poolAdapter.getMaxPoolSize();
            boolean incrementMaxPoolSize = currentMaxSize < maxOverflowPoolSize;
            if (currentMaxSize > expectingMaxSize) {
                LOGGER.info("Pool size changed by other thread, expected {} and actual value {}", expectingMaxSize, currentMaxSize);
                return incrementMaxPoolSize;
            }
            if (!incrementMaxPoolSize) {
                return false;
            }
            currentOverflowPoolSize = overflowPoolSize.incrementAndGet();
            poolAdapter.setMaxPoolSize(++currentMaxSize);
            maxSize = currentMaxSize;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return false;
        } finally {
            lock.unlock();
        }
        LOGGER.info("Pool size changed from previous value {} to {}", expectingMaxSize, maxSize);
        maxPoolSizeHistogram.update(maxSize);
        overflowPoolSizeHistogram.update(currentOverflowPoolSize);
        return true;
    }
120518
def MaxPool(a, k, strides, padding, data_format):
    
    if data_format.decode("ascii") == "NCHW":
        a = np.rollaxis(a, 1, -1),

    patches = _pool_patches(a, k, strides, padding.decode("ascii"))
    pool = np.amax(patches, axis=tuple(range(-len(k), 0)))

    if data_format.decode("ascii") == "NCHW":
        pool = np.rollaxis(pool, -1, 1)

    return pool,
332022
public PooledExecutionServiceConfigurationBuilder pool(String alias, int minSize, int maxSize) {
    PooledExecutionServiceConfigurationBuilder other = new PooledExecutionServiceConfigurationBuilder(this);
    other.pools.add(new Pool(alias, minSize, maxSize));
    return other;
  }
1168996
def top_k_softmax(x, k):
  """"""
  x = tf.nn.softmax(x)
  top_x, _ = tf.nn.top_k(x, k=k+1)
  min_top = tf.reduce_min(top_x, axis=-1, keepdims=True)
  x = tf.nn.relu((x - min_top) + 1e-12)
  x /= tf.reduce_sum(x, axis=-1, keepdims=True)
  return x, tf.reduce_max(top_x, axis=-1)
906435
public SDVariable maxPooling3d(SDVariable input, Pooling3DConfig pooling3DConfig) {
        pooling3DConfig.setType(Pooling3D.Pooling3DType.MAX);
        return pooling3d(input, pooling3DConfig);
    }