com.simiacryptus.mindseye.layers.cudnn.BandReducerLayer Maven / Gradle / Ivy
/*
* 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.cudnn;
import com.google.gson.JsonObject;
import com.simiacryptus.mindseye.lang.*;
import com.simiacryptus.mindseye.lang.cudnn.CudaSettings;
import com.simiacryptus.mindseye.lang.cudnn.CudaSystem;
import com.simiacryptus.mindseye.lang.cudnn.MultiPrecision;
import com.simiacryptus.mindseye.lang.cudnn.Precision;
import com.simiacryptus.mindseye.layers.cudnn.PoolingLayer.PoolingMode;
import com.simiacryptus.ref.lang.RefUtil;
import com.simiacryptus.ref.wrappers.RefArrays;
import com.simiacryptus.ref.wrappers.RefList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
@SuppressWarnings("serial")
public class BandReducerLayer extends LayerBase implements MultiPrecision {
private PoolingLayer.PoolingMode mode = PoolingLayer.PoolingMode.Max;
private Precision precision = CudaSettings.INSTANCE().getDefaultPrecision();
private double alpha = 1.0;
public BandReducerLayer() {
super();
}
protected BandReducerLayer(@Nonnull final JsonObject json) {
super(json);
mode = RefUtil.get(RefArrays.stream(PoolingLayer.PoolingMode.values()).filter(i -> i.id == json.get("mode").getAsInt())
.findFirst());
precision = Precision.valueOf(json.get("precision").getAsString());
alpha = json.get("alpha").getAsDouble();
}
public double getAlpha() {
return alpha;
}
public void setAlpha(double alpha) {
this.alpha = alpha;
}
@Nonnull
public Layer getCompatibilityLayer() {
throw new RuntimeException("Not Implemented");
}
public PoolingMode getMode() {
return mode;
}
public void setMode(PoolingMode mode) {
this.mode = mode;
}
@Override
public Precision getPrecision() {
return precision;
}
@Override
public void setPrecision(final Precision precision) {
this.precision = precision;
}
@Nonnull
@SuppressWarnings("unused")
public static BandReducerLayer fromJson(@Nonnull final JsonObject json, Map rs) {
return new BandReducerLayer(json);
}
@Nullable
@Override
public Result eval(@Nullable final Result... inObj) {
if (!CudaSystem.isEnabled()) {
Layer compatibilityLayer = getCompatibilityLayer();
Result result = compatibilityLayer.eval(inObj);
compatibilityLayer.freeRef();
return result;
}
assert inObj != null;
final TensorList batch = Result.getData(inObj[0].addRef());
@Nonnull final int[] inputSize = batch.getDimensions();
batch.freeRef();
PoolingLayer poolingLayer = new PoolingLayer();
poolingLayer.setMode(mode);
poolingLayer.setPrecision(precision);
poolingLayer.setWindowX(inputSize[0]);
poolingLayer.setWindowY(inputSize[1]);
poolingLayer.setStrideX(inputSize[0]);
poolingLayer.setStrideY(inputSize[1]);
poolingLayer.setPaddingX(0);
poolingLayer.setPaddingY(0);
poolingLayer.setAlpha(alpha);
Result result = poolingLayer.eval(inObj);
poolingLayer.freeRef();
return result;
}
@Nonnull
@Override
public JsonObject getJson(Map resources, DataSerializer dataSerializer) {
@Nonnull final JsonObject json = super.getJsonStub();
json.addProperty("alpha", alpha);
json.addProperty("mode", mode.id);
json.addProperty("precision", precision.name());
return json;
}
@Nonnull
@Override
public RefList state() {
return RefArrays.asList();
}
public @SuppressWarnings("unused")
void _free() {
super._free();
}
@Nonnull
public @Override
@SuppressWarnings("unused")
BandReducerLayer addRef() {
return (BandReducerLayer) super.addRef();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy