Question
Hello, I wanted to ask a couple of questions. I retrieve my data from the database. And I did not find in the examples the correct way to convert Java arrays to DataSetIterators. Below is an example of a code with toy data (my data is also an integer format, so the example is correct).
- How do I normalize my data? Below in the code there is a commented-out code, it converts only the input data, but the labels remain not converted.
- I added a listener to the model but it does not work, tell me what could be the problem.
- why can’t I evaluate test data without an iterator?
- How can I set the batch size if I do not use a DataSeIterator?
- Did I set the array shape for a recurrent neural network correctly? (split_sequence() method)
thanks.
public class Forecast {
public final static int BATCH_SIZE = 2;
public final static int RNG_SEED = 123;
public final static int TIME_STEPS = 3;
public final static int nEpoch = 10;
public static void main(String[] args) throws IOException, InterruptedException {
int[] train = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int[] test = {70, 80, 90};
DataSet dataSet = split_sequence(train, TIME_STEPS);
DataSetIterator train_iterator = new ListDataSetIterator(Collections.singletonList(dataSet), BATCH_SIZE);
// NormalizerMinMaxScaler normalizer = new NormalizerMinMaxScaler(0, 1);
// normalizer.fit(dataSet);
// normalizer.transform(dataSet);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(RNG_SEED)
.weightInit(WeightInit.XAVIER)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new Adam())
.l2(1e-4)
.list()
.layer(0,new LSTM.Builder()
.nIn(TIME_STEPS)
.nOut(10)
.activation(Activation.SOFTSIGN)
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue)
.gradientNormalizationThreshold(10)
.build())
.layer(1, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MSE)
.activation(Activation.IDENTITY)
.nIn(10)
.nOut(1)
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue)
.gradientNormalizationThreshold(10)
.build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
System.out.println("Train model...");
long startTime = System.currentTimeMillis();
model.setListeners(new ScoreIterationListener(1)); // doesn't work
for (int i=0; i < nEpoch; i++)
model.fit(train_iterator);
long endTime = System.currentTimeMillis();
System.out.println("=============run time===================== " + (endTime - startTime));
var eval = model.evaluateRegression(); // how i can evaluate model without iterator?
System.out.println(eval.stats());
}
//split a given univariate sequence into multiple samples where each sample has a specified number of time steps and the output is a single time step
public static DataSet split_sequence(int[] sequence, int n_steps) {
int arr_size = sequence.length - n_steps;
var input = new double[arr_size][n_steps];
var label = new double[arr_size];
for (int i = 0; i < sequence.length; i++){
int seq_end = i + n_steps;
if (seq_end > sequence.length-1) break;
input[i] = copyFromIntArray(Arrays.copyOfRange(sequence, i, seq_end)); // copyFromIntArray method converting int[] -> double[]
label[i] = sequence[seq_end];
}
INDArray x = Nd4j.create(input).reshape(new int[]{arr_size, n_steps, 1});
INDArray y = Nd4j.create(label).reshape(new int[]{arr_size, 1, 1});
return new DataSet(x,y);
}
Version Information
- DL4J 1.0.0-beta6
- Mac OS