All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.simiacryptus.mindseye.eval.BatchedTrainable Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
/*
 * Copyright (c) 2019 by Andrew Charneski.
 *
 * The author licenses this file to you 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.
 */

package com.simiacryptus.mindseye.eval;

import com.simiacryptus.lang.TimedResult;
import com.simiacryptus.lang.UncheckedSupplier;
import com.simiacryptus.mindseye.lang.Layer;
import com.simiacryptus.mindseye.lang.PointSample;
import com.simiacryptus.mindseye.lang.Tensor;
import com.simiacryptus.mindseye.opt.TrainingMonitor;
import com.simiacryptus.ref.lang.RefUtil;
import com.simiacryptus.ref.wrappers.*;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.Function;

public abstract class BatchedTrainable extends TrainableWrapper implements DataTrainable {

  protected final int batchSize;
  private boolean verbose = false;

  public BatchedTrainable(final DataTrainable inner, final int batchSize) {
    super(inner);
    this.batchSize = batchSize;
  }

  public BatchedTrainable(@Nullable final Layer network, final int batchSize) {
    this(new BasicTrainable(network), batchSize);
  }

  public int getBatchSize() {
    return batchSize;
  }

  public boolean isVerbose() {
    return verbose;
  }

  public void setVerbose(boolean verbose) {
    this.verbose = verbose;
  }

  @Override
  public PointSample measure(@Nullable final TrainingMonitor monitor) {
    @Nonnull final RefList tensors = RefArrays.asList(getData());
    int size = tensors.size();
    TimedResult timedResult = TimedResult
        .time(RefUtil.wrapInterface((UncheckedSupplier) () -> {
          DataTrainable inner = getInner();
          if (batchSize < size) {
            final int batches = (int) Math.ceil(size * 1.0 / batchSize);
            final int evenBatchSize = (int) Math.ceil(size * 1.0 / batches);
            @Nonnull final RefList> collection = RefLists.partition(tensors.addRef(), evenBatchSize);
            PointSample temp_36_0001 = RefUtil.get(collection.stream()
                .map(RefUtil.wrapInterface((Function, PointSample>) trainingData -> {
                  if (batchSize < trainingData.size()) {
                    trainingData.freeRef();
                    throw new RuntimeException();
                  }
                  assert inner != null;
                  inner.setData(trainingData.addRef());
                  trainingData.freeRef();
                  PointSample measure = super.measure(monitor);
                  inner.setData(new RefArrayList<>());
                  return measure;
                }, inner)).reduce((a, b) -> {
                  PointSample temp_36_0002 = a.add(b == null ? null : b.addRef());
                  if (null != b)
                    b.freeRef();
                  a.freeRef();
                  return temp_36_0002;
                }));
            collection.freeRef();
            return temp_36_0001;
          } else {
            assert inner != null;
            inner.setData(tensors.addRef());
            PointSample measure = super.measure(monitor);
            inner.setData(new RefArrayList<>());
            inner.freeRef();
            return measure;
          }
        }, tensors));
    PointSample result = timedResult.getResult();
    if (null != monitor && isVerbose()) {
      monitor.log(RefString.format("Evaluated %s items in %.4fs (%s/%s)", size, timedResult.timeNanos / 1e9,
          result.getMean(), result.delta.getMagnitude()));
    }
    timedResult.freeRef();
    return result;
  }

  public @SuppressWarnings("unused")
  void _free() {
    super._free();
  }

  @Nonnull
  public @Override
  @SuppressWarnings("unused")
  BatchedTrainable addRef() {
    return (BatchedTrainable) super.addRef();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy