Пример #1
0
  public int shrink(float threshold) {
    int[] indexMap = new int[getFeatureSize()];
    int i, j, k, l, count = 1; // bias
    float max, min;

    for (i = 1; i < getFeatureSize(); i++) {
      k = i * getLabelSize();
      max = weight_vector.get(k);
      min = weight_vector.get(k);

      for (j = 1; j < getLabelSize(); j++) {
        max = Math.max(max, weight_vector.get(k + j));
        min = Math.min(min, weight_vector.get(k + j));
      }

      if (Math.abs(max - min) >= threshold) indexMap[i] = count++;
    }

    WeightVector newWeights =
        new WeightVectorDynamic(
            weight_vector.getLabelSize(), count, weight_vector.getActivationFunction());
    ObjectIterator<Entry<String, Integer>> it;
    int oldIndex, newIndex;
    Entry<String, Integer> e;

    // bias weights
    for (j = 0; j < getLabelSize(); j++) newWeights.set(j, weight_vector.get(j));

    for (Object2IntMap<String> map : feature_map.getIndexMaps()) {
      it = map.entrySet().iterator();

      while (it.hasNext()) {
        e = it.next();
        oldIndex = e.getValue();
        newIndex = indexMap[oldIndex];

        if (newIndex > 0) {
          e.setValue(newIndex);
          k = oldIndex * getLabelSize();
          l = newIndex * getLabelSize();

          for (j = 0; j < getLabelSize(); j++) newWeights.set(l + j, weight_vector.get(k + j));
        } else it.remove();
      }
    }

    weight_vector = newWeights;
    feature_map.setSize(count);
    return count;
  }