Created
November 6, 2017 20:52
-
-
Save tomthetrainer/a9f6b5fdb2a6ed9b851cd3d2035869be to your computer and use it in GitHub Desktop.
This file contains 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
package ai.skymind.training.solutions; | |
import org.apache.log4j.BasicConfigurator; | |
import org.datavec.api.records.reader.RecordReader; | |
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; | |
import org.datavec.api.split.FileSplit; | |
import org.datavec.api.util.ClassPathResource; | |
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; | |
import org.deeplearning4j.eval.Evaluation; | |
import org.deeplearning4j.nn.api.OptimizationAlgorithm; | |
import org.deeplearning4j.nn.conf.ComputationGraphConfiguration; | |
import org.deeplearning4j.nn.conf.MultiLayerConfiguration; | |
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; | |
import org.deeplearning4j.nn.conf.Updater; | |
import org.deeplearning4j.nn.conf.layers.DenseLayer; | |
import org.deeplearning4j.nn.conf.layers.OutputLayer; | |
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; | |
import org.deeplearning4j.nn.weights.WeightInit; | |
import org.deeplearning4j.optimize.listeners.ScoreIterationListener; | |
import org.deeplearning4j.util.ModelSerializer; | |
import org.nd4j.linalg.activations.Activation; | |
import org.nd4j.linalg.api.ndarray.INDArray; | |
import org.nd4j.linalg.dataset.DataSet; | |
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; | |
import org.nd4j.linalg.lossfunctions.LossFunctions; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.File; | |
/** | |
* Created by tomhanlon on 2/23/17. | |
*/ | |
public class MultiLayertoCompGraph { | |
private static Logger log = LoggerFactory.getLogger(MultiLayertoCompGraph.class); | |
public static void main(String[] args) throws Exception { | |
BasicConfigurator.configure(); | |
int numLinesToSkip = 0; | |
String delimiter = ","; | |
int batchSize = 600; | |
int seed = 123; | |
int labelIndex = 8; //5 values in each row of the iris.txt CSV: 4 input features followed by an integer label (class) index. Labels are the 5th value (index 4) in each row | |
int numClasses = 30; | |
int numOutputs = 30; | |
double learningRate = 0.005; | |
int numInputs = 8; | |
int numHiddenNodes = 40; | |
int nEpochs = 50; | |
int iterations = 100; | |
File traindata = new ClassPathResource("abalone/abalone_train.csv").getFile(); | |
File testdata = new ClassPathResource("abalone/abalone_test.csv").getFile(); | |
//final String filenameTrain = new org.nd4j.linalg.io.ClassPathResource("/classification/saturn_data_train.csv").getFile().getPath(); | |
//final String filenameTest = new org.nd4j.linalg.io.ClassPathResource("/classification/saturn_data_eval.csv").getFile().getPath(); | |
//Load the training data: | |
RecordReader rrtrain = new CSVRecordReader(); | |
rrtrain.initialize(new FileSplit(traindata)); | |
DataSetIterator trainIter = new RecordReaderDataSetIterator(rrtrain,batchSize,labelIndex,numClasses); | |
//Load the test/evaluation data: | |
RecordReader rrTest = new CSVRecordReader(); | |
rrTest.initialize(new FileSplit(testdata)); | |
DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest,batchSize,labelIndex,numClasses); | |
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() | |
.seed(seed) | |
.iterations(iterations) | |
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) | |
.learningRate(learningRate) | |
.updater(Updater.NESTEROVS).momentum(0.9) | |
.regularization(true).l2(1e-4) | |
.weightInit(WeightInit.XAVIER) | |
.activation(Activation.TANH) | |
.list() | |
.layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes) | |
.build()) | |
.layer(1, new DenseLayer.Builder().nIn(numHiddenNodes).nOut(numHiddenNodes) | |
.build()) | |
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) | |
.activation(Activation.SOFTMAX) | |
.nIn(numHiddenNodes).nOut(numOutputs).build()) | |
.pretrain(false).backprop(true).build(); | |
MultiLayerNetwork model = new MultiLayerNetwork(conf); | |
System.out.println(conf.toJson()); | |
System.out.println("#########################"); | |
ComputationGraphConfiguration graphConfig = new ComputationGraphConfiguration().fromJson(model.conf().toJson()); | |
System.out.println(graphConfig.toJson()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment