Created
August 4, 2020 10:50
-
-
Save ssadedin/22f5bd93e959df1231fb45bd54dfe919 to your computer and use it in GitHub Desktop.
Problem with DL4J 1D Convolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import org.deeplearning4j.datasets.iterator.impl.ListDataSetIterator; | |
import org.deeplearning4j.nn.api.OptimizationAlgorithm; | |
import org.deeplearning4j.nn.conf.inputs.*; | |
import org.deeplearning4j.nn.conf.*; | |
import org.deeplearning4j.nn.conf.layers.*; | |
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; | |
import org.deeplearning4j.nn.weights.WeightInit; | |
import org.deeplearning4j.optimize.listeners.ScoreIterationListener; | |
import org.nd4j.linalg.activations.Activation; | |
import org.nd4j.linalg.api.ndarray.INDArray; | |
import org.nd4j.linalg.cpu.nativecpu.NDArray; | |
import org.nd4j.linalg.dataset.DataSet; | |
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; | |
import org.nd4j.linalg.dataset.api.iterator.DataSetIteratorFactory; | |
import org.nd4j.linalg.factory.NDArrayFactory; | |
import org.nd4j.linalg.factory.Nd4j; | |
import org.nd4j.linalg.lossfunctions.LossFunctions; | |
import org.nd4j.linalg.learning.config.*; | |
import gngs.*; | |
import graxxia.*; | |
public class TestConv1D { | |
public static void main(String[] args) { | |
double [][] features = new double [][] { | |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 0, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 0, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 0, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 0, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 1, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 0, 1, 1, 1, 1, 1, 1 }, | |
}; | |
double [][] labels = new double [][] { | |
{1, 0}, | |
{0, 1}, | |
{1, 0}, | |
{0, 1}, | |
{1, 0}, | |
{0, 1}, | |
{1, 0}, | |
{0, 1}, | |
{1, 0}, | |
{0, 1}, | |
}; | |
int data_len = features[0].length; | |
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() | |
.seed(0) | |
.l2(0.0005) | |
.weightInit(WeightInit.XAVIER) | |
.updater(new Adam(1e-3)) | |
.list() | |
.layer(new Convolution1DLayer.Builder(5) | |
//nIn and nOut specify depth. nIn here is the nChannels and nOut is the number of filters to be applied | |
.nIn(1) | |
// .rnnDataFormat(RNNFormat.NCW) | |
.stride(1) | |
.nOut(10) | |
.activation(Activation.IDENTITY) | |
.build()) | |
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) | |
.nOut(2) | |
.activation(Activation.SOFTMAX) | |
.build()) | |
.setInputType(InputType.convolutionalFlat(data_len,1,1 /*, CNN2DFormat.NHWC*/)) //See note below | |
.build(); | |
DataSet dataset = new DataSet(); | |
dataset.setFeatures(Nd4j.create(features)); | |
dataset.setLabels(Nd4j.create(labels)); | |
MultiLayerNetwork model = new MultiLayerNetwork (conf); | |
model.init(); | |
model.setListeners(new ScoreIterationListener(10)); | |
List<DataSet> datasets = new ArrayList<DataSet>(); | |
datasets.add(dataset); | |
DataSetIterator trainIter = new ListDataSetIterator(datasets); | |
for(int n=0; n<50; n++) { | |
model.fit(trainIter); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment