Commit 12e2c374 authored by Julian Dierkes's avatar Julian Dierkes

Added LeakyRelu and TransConv

parent 0bef9f58
......@@ -30,7 +30,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>cnn-arch</artifactId>
<version>0.3.3-SNAPSHOT</version>
<version>0.3.4-SNAPSHOT</version>
......@@ -393,4 +393,4 @@
</snapshotRepository>
</distributionManagement>
</project>
\ No newline at end of file
</project>
......@@ -189,6 +189,20 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
}
}
//output type function for transposed convolution
protected static List<ArchTypeSymbol> computeTransConvOutputShape(ArchTypeSymbol inputType, LayerSymbol method, int channels) {
String borderModeSetting = method.getStringValue(AllPredefinedLayers.PADDING_NAME).get();
if (borderModeSetting.equals(AllPredefinedLayers.PADDING_SAME)){
return computeTransConvOutputShapeWithSamePadding(inputType, method, channels);
}
else if (borderModeSetting.equals(AllPredefinedLayers.PADDING_VALID)){
return computeTransConvOutputShapeWithValidPadding(inputType, method, channels);
}
else{
throw new IllegalStateException("border_mode is " + borderModeSetting + ". This should never happen.");
}
}
//padding with border_mode=valid, no padding
private static List<ArchTypeSymbol> computeOutputShapeWithValidPadding(ArchTypeSymbol inputType, LayerSymbol method, int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
......@@ -217,6 +231,29 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
.build());
}
//padding with border_mode=valid, no padding
private static List<ArchTypeSymbol> computeTransConvOutputShapeWithValidPadding(ArchTypeSymbol inputType, LayerSymbol method, int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int kernelHeight = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(0);
int kernelWidth = method.getIntTupleValue(AllPredefinedLayers.KERNEL_NAME).get().get(1);
int inputHeight = inputType.getHeight();
int inputWidth = inputType.getWidth();
int outputWidth;
int outputHeight;
outputWidth = (inputWidth - 1) * strideWidth + kernelWidth;
outputHeight = (inputHeight - 1) * strideHeight + kernelHeight;
return Collections.singletonList(new ArchTypeSymbol.Builder()
.height(outputHeight)
.width(outputWidth)
.channels(channels)
.elementType("-oo", "oo")
.build());
}
//padding until no data gets discarded, same as valid with a stride of 1
private static List<ArchTypeSymbol> computeOutputShapeWithNoLossPadding(ArchTypeSymbol inputType, LayerSymbol method, int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
......@@ -255,6 +292,24 @@ abstract public class PredefinedLayerDeclaration extends LayerDeclarationSymbol
.build());
}
//padding with border_mode='same'
private static List<ArchTypeSymbol> computeTransConvOutputShapeWithSamePadding(ArchTypeSymbol inputType, LayerSymbol method, int channels){
int strideHeight = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(0);
int strideWidth = method.getIntTupleValue(AllPredefinedLayers.STRIDE_NAME).get().get(1);
int inputHeight = inputType.getHeight();
int inputWidth = inputType.getWidth();
int outputWidth = inputWidth * strideWidth;
int outputHeight = inputHeight * strideHeight;
return Collections.singletonList(new ArchTypeSymbol.Builder()
.height(outputHeight)
.width(outputWidth)
.channels(channels)
.elementType("-oo", "oo")
.build());
}
protected List<String> computeStartAndEndValue(List<ArchTypeSymbol> inputTypes, BinaryOperator<Rational> startValAccumulator, BinaryOperator<Rational> endValAccumulator){
Stream.Builder<Rational> startValues = Stream.builder();
Stream.Builder<Rational> endValues = Stream.builder();
......
......@@ -30,10 +30,12 @@ public class AllPredefinedLayers {
//predefined layer names
public static final String FULLY_CONNECTED_NAME = "FullyConnected";
public static final String CONVOLUTION_NAME = "Convolution";
public static final String TRANS_CONV_NAME = "TransConv";
public static final String SOFTMAX_NAME = "Softmax";
public static final String SIGMOID_NAME = "Sigmoid";
public static final String TANH_NAME = "Tanh";
public static final String RELU_NAME = "Relu";
public static final String LEAKY_RELU_NAME = "LeakyRelu";
public static final String DROPOUT_NAME = "Dropout";
public static final String POOLING_NAME = "Pooling";
public static final String GLOBAL_POOLING_NAME = "GlobalPooling";
......@@ -89,10 +91,12 @@ public class AllPredefinedLayers {
return Arrays.asList(
FullyConnected.create(),
Convolution.create(),
TransConv.create(),
Softmax.create(),
Sigmoid.create(),
Tanh.create(),
Relu.create(),
LeakyRelu.create(),
Dropout.create(),
Flatten.create(),
Pooling.create(),
......
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.monticar.cnnarch.predefined;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.monticore.lang.monticar.ranges._ast.ASTRange;
import de.monticore.lang.monticar.types2._ast.ASTElementType;
import de.se_rwth.commons.logging.Log;
import java.util.*;
public class LeakyRelu extends PredefinedLayerDeclaration {
private LeakyRelu() {
super(AllPredefinedLayers.LEAKY_RELU_NAME);
}
@Override
public List<ArchTypeSymbol> computeOutputTypes(List<ArchTypeSymbol> inputTypes, LayerSymbol layer, VariableSymbol.Member member) {
return Collections.singletonList(
new ArchTypeSymbol.Builder()
.channels(layer.getInputTypes().get(0).getChannels())
.height(layer.getInputTypes().get(0).getHeight())
.width(layer.getInputTypes().get(0).getWidth())
.elementType("0", "oo")
.build());
}
@Override
public void checkInput(List<ArchTypeSymbol> inputTypes, LayerSymbol layer, VariableSymbol.Member member) {
errorIfInputSizeIsNotOne(inputTypes, layer);
}
public static LeakyRelu create(){
LeakyRelu declaration = new LeakyRelu();
List<ParameterSymbol> parameters = new ArrayList<>(Arrays.asList(
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.ALPHA_NAME)
.constraints(Constraints.POSITIVE)
.defaultValue(0)
.build()));
declaration.setParameters(parameters);
return declaration;
}
}
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.monticar.cnnarch.predefined;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TransConv extends PredefinedLayerDeclaration {
private TransConv() {
super(AllPredefinedLayers.TRANS_CONV_NAME);
}
@Override
public List<ArchTypeSymbol> computeOutputTypes(List<ArchTypeSymbol> inputTypes, LayerSymbol layer, VariableSymbol.Member member) {
return computeTransConvOutputShape(layer.getInputTypes().get(0),
layer,
layer.getIntValue(AllPredefinedLayers.CHANNELS_NAME).get());
}
@Override
public void checkInput(List<ArchTypeSymbol> inputTypes, LayerSymbol layer, VariableSymbol.Member member) {
errorIfInputSizeIsNotOne(inputTypes, layer);
}
public static TransConv create(){
TransConv declaration = new TransConv();
List<ParameterSymbol> parameters = new ArrayList<>(Arrays.asList(
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.KERNEL_NAME)
.constraints(Constraints.INTEGER_TUPLE, Constraints.POSITIVE)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.CHANNELS_NAME)
.constraints(Constraints.INTEGER, Constraints.POSITIVE)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.STRIDE_NAME)
.constraints(Constraints.INTEGER_TUPLE, Constraints.POSITIVE)
.defaultValue(Arrays.asList(1, 1))
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.NOBIAS_NAME)
.constraints(Constraints.BOOLEAN)
.defaultValue(false)
.build(),
new ParameterSymbol.Builder()
.name(AllPredefinedLayers.PADDING_NAME)
.constraints(Constraints.PADDING_TYPE)
.defaultValue(AllPredefinedLayers.PADDING_SAME)
.build()));
declaration.setParameters(parameters);
return declaration;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment