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

com.simiacryptus.mindseye.layers.java.BiasLayer 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.layers.java;

import com.google.gson.JsonObject;
import com.simiacryptus.lang.ref.RecycleBin;
import com.simiacryptus.mindseye.lang.*;
import com.simiacryptus.util.FastRandom;
import com.simiacryptus.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.DoubleSupplier;
import java.util.function.IntToDoubleFunction;

/**
 * Adds a bias tensor to the input. Expects a single input of the same dimension as the bias tensor.
 */
@SuppressWarnings("serial")
public class BiasLayer extends LayerBase {

  @SuppressWarnings("unused")
  private static final Logger log = LoggerFactory.getLogger(BiasLayer.class);
  /**
   * The Bias.
   */
  @Nullable
  public final Tensor bias;

  /**
   * Instantiates a new Bias key.
   */
  protected BiasLayer() {
    super();
    bias = null;
  }

  /**
   * Instantiates a new Bias key.
   *
   * @param dims the dims
   */
  public BiasLayer(final int... dims) {
    bias = new Tensor(dims);
  }


  /**
   * Instantiates a new Bias key.
   *
   * @param json the json
   * @param rs
   */
  protected BiasLayer(@Nonnull final JsonObject json, Map rs) {
    super(json);
    bias = Tensor.fromJson(json.get("bias"), rs);
  }

  /**
   * From json bias key.
   *
   * @param json the json
   * @param rs   the rs
   * @return the bias key
   */
  public static BiasLayer fromJson(@Nonnull final JsonObject json, Map rs) {
    return new BiasLayer(json, rs);
  }

  @Override
  protected void _free() {
    bias.freeRef();
    super._free();
  }

  /**
   * Add double [ ].
   *
   * @param input the input
   * @return the double [ ]
   */
  public double[] add(@Nonnull final double[] input) {
    final double[] array = RecycleBin.DOUBLES.obtain(input.length);
    double[] bias = this.bias.getData();
    if (1 == bias.length) {
      for (int i = 0; i < array.length; i++) {
        array[i] = input[i] + bias[0];
      }
    } else {
      for (int i = 0; i < array.length; i++) {
        array[i] = input[i] + bias[i];
      }
    }
    return array;
  }

  /**
   * Add weights bias key.
   *
   * @param f the f
   * @return the bias key
   */
  @Nonnull
  public BiasLayer addWeights(@Nonnull final DoubleSupplier f) {
    double[] bias = this.bias.getData();
    Util.add(f, bias);
    return this;
  }

  @Nonnull
  @Override
  public Result eval(@Nonnull final Result... inObj) {
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    TensorList input;
    if (0 == inObj.length) {
      input = TensorArray.create();
    } else {
      input = inObj[0].getData();
    }
    return new Result(TensorArray.wrap(input.stream().parallel()
        .map(r -> {
          @Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
          r.freeRef();
          return tensor;
        }).toArray(i -> new Tensor[i])),
        (@Nonnull final DeltaSet buffer, @Nonnull final TensorList delta) -> {
          if (!isFrozen()) {
            final Delta deltaBuffer = buffer.get(BiasLayer.this.getId(), bias);
            if (1 == bias.length()) {
              delta.stream().parallel().forEach(d -> {
                @Nullable final double[] array = d.getData();
                deltaBuffer.addInPlace(1 == array.length ? array : new double[]{Arrays.stream(array).sum()});
                d.freeRef();
              });
            } else {
              delta.stream().parallel().forEach(d -> {
                deltaBuffer.addInPlace(d.getData());
                d.freeRef();
              });
            }
            deltaBuffer.freeRef();
          }
          if (0 < inObj.length && inObj[0].isAlive()) {
            delta.addRef();
            inObj[0].accumulate(buffer, delta);
          }
          delta.freeRef();
        }) {

      @Override
      protected void _free() {
        Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
      }


      @Override
      public boolean isAlive() {
        return 0 < inObj.length && inObj[0].isAlive() || !isFrozen();
      }
    };
  }

  @Nonnull
  @Override
  public JsonObject getJson(Map resources, DataSerializer dataSerializer) {
    @Nonnull final JsonObject json = super.getJsonStub();
    json.add("bias", bias.getJson(resources, dataSerializer));
    return json;
  }


  /**
   * Set nn key.
   *
   * @param ds the ds
   * @return the nn key
   */
  @Nonnull
  public Layer set(@Nonnull final double[] ds) {
    double[] bias = this.bias.getData();
    for (int i = 0; i < ds.length; i++) {
      bias[i] = ds[i];
    }
    return this;
  }

  /**
   * Sets weights.
   *
   * @param f the f
   * @return the weights
   */
  @Nonnull
  public BiasLayer setWeights(@Nonnull final IntToDoubleFunction f) {
    double[] bias = this.bias.getData();
    for (int i = 0; i < bias.length; i++) {
      bias[i] = f.applyAsDouble(i);
    }
    return this;
  }

  /**
   * Sets weights log.
   *
   * @param value the value
   * @return the weights log
   */
  @Nonnull
  public BiasLayer setWeightsLog(final double value) {
    double[] bias = this.bias.getData();
    for (int i = 0; i < bias.length; i++) {
      bias[i] = (FastRandom.INSTANCE.random() - 0.5) * Math.pow(10, value);
    }
    return this;
  }

  @Nonnull
  @Override
  public List state() {
    return Arrays.asList(bias.getData());
  }

  /**
   * Set bias key.
   *
   * @param tensor the tensor
   * @return the bias key
   */
  @Nonnull
  public BiasLayer set(@Nonnull Tensor tensor) {
    double[] bias = this.bias.getData();
    assert bias.length == tensor.length();
    for (int i = 0; i < bias.length; i++) {
      bias[i] = tensor.get(i);
    }
    return this;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy