So I’m new to machine learning and I’ve tried to begin training a model but have been getting the org.deeplearning4j.exception.DL4JInvalidInputException
error for a day or two.
I’ve tried setting builder.setInputType(InputType.recurrent(inputSize)); // inputSize being 5
, as I have 5 features but I can’t figure it out, asked chatgpt and still couldnt figure it out. Looking around on forums and docs, still can’t figure it out. Please help me out, thanks in advance.
Here’s the code below (comically called robot):
public void processData() {
ObservableList<OHLCData> data = giveData();
System.out.println("Data size: " + data.size());
int timeStep = 50; // Minimum timestep for LSTM
List<double[][]> featureList = new ArrayList<>();
List<double[]> labelList = new ArrayList<>();
for (int i = 0; i < data.size() - timeStep; i++) {
double[][] features = new double[timeStep][5]; // 5 features per timestep
for (int j = 0; j < timeStep; j++) {
features[j] = extractFeatures(data.get(i + j));
}
featureList.add(features);
labelList.add(new double[]{data.get(i + timeStep).getClose()}); // Predicting the next close price
}
INDArray featureArray = Nd4j.create(featureList.toArray(new double[0][0][0]));
INDArray labelArray = Nd4j.create(labelList.toArray(new double[0][0]));
// Debugging: Print the shapes of the arrays before reshaping
System.out.println("Before Reshaping:");
System.out.println("Feature Array Shape: " + featureArray.shapeInfoToString());
System.out.println("Label Array Shape: " + labelArray.shapeInfoToString());
// Manually set the shape to ensure consistency
featureArray = featureArray.reshape('c', featureArray.size(0), timeStep, 5); // Explicitly reshape to [batch, timeSteps, features]
labelArray = labelArray.reshape('c', labelArray.size(0), 1, 1); // [batch, timeStep=1, output]
// Debugging: Check the shape after reshaping
System.out.println("After Reshaping:");
System.out.println("Feature Array Shape: " + featureArray.shapeInfoToString());
System.out.println("Label Array Shape: " + labelArray.shapeInfoToString());
DataSet dataset = new DataSet(featureArray, labelArray);
normalizeData(dataset);
trainModel(dataset);
}
private double[] extractFeatures(OHLCData data) {
return new double[]{
data.getOpen(),
data.getHigh(),
data.getLow(),
data.getClose(),
data.getVolume()
};
}
private void trainModel(DataSet dataset) {
int inputSize = 5; // Matching the 5 extracted features
int outputSize = 1; // Predicting the next close price
// Configure the network
NeuralNetConfiguration.ListBuilder builder = new NeuralNetConfiguration.Builder()
.seed(123)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.list()
.layer(0, new LSTM.Builder()
.nIn(inputSize)
.nOut(50)
.activation(Activation.TANH)
.build())
.layer(1, new RnnOutputLayer.Builder()
.nIn(50)
.nOut(outputSize)
.activation(Activation.IDENTITY)
.lossFunction(LossFunctions.LossFunction.MSE)
.build());
// Build and initialize the network
MultiLayerNetwork net = new MultiLayerNetwork(builder.build());
net.init();
net.setListeners(new ScoreIterationListener(100));
// Train the model
ListDataSetIterator<DataSet> trainIterator = new ListDataSetIterator<>(dataset.asList(), 32);
for (int epoch = 0; epoch < 10; epoch++) {
net.fit(trainIterator);
System.out.println("Epoch " + epoch + " complete");
}
System.out.println("Model training complete!");
this.model = net;
}
private void normalizeData(DataSet dataset) {
// Min-Max Scaler (scales values between 0 and 1)
DataNormalization normalizer = new NormalizerMinMaxScaler(0, 1);
// Fit the normalizer using dataset statistics (min/max values)
normalizer.fit(dataset);
// Transform dataset (apply normalization)
normalizer.transform(dataset);
System.out.println("Data normalization complete.");
}
Below are some logs.
(error) - org.deeplearning4j.exception.DL4JInvalidInputException: Received input with size(1) = 50 (input array shape = [32, 50, 5]); input.size(1) must match layer nIn size (nIn = 5)
logs regarding reshaping -
Before Reshaping: Feature Array Shape: Rank: 3, DataType: DOUBLE, Offset: 0, Order: c, Shape: [9950,50,5], Stride: [250,5,1] Label Array Shape: Rank: 2, DataType: DOUBLE, Offset: 0, Order: c, Shape: [9950,1], Stride: [1,1]
After Reshaping: Feature Array Shape: Rank: 3, DataType: DOUBLE, Offset: 0, Order: c, Shape: [9950,50,5], Stride: [250,5,1] Label Array Shape: Rank: 3, DataType: DOUBLE, Offset: 0, Order: c, Shape: [9950,1,1], Stride: [1,1,1]
Data size log (confirmation that it has required data) -
Data size: 10000
What I’m trying to accomplish is training a model to predict market data with 10000 datapoints, use a timestep of 50 so the model can use 50 points to predict the next one(51), with 5 features(Open, High, Low, Close) for now as I’ve removed computed indicators for now.
I’m not a fantastic coder but I don’t rely on chatgpt for everything, I mainly use it for planning and researching as its great for aggregating information with their sources. Like I said I tried looking into the docs, I’ve attempted to educate myself on ML along with DL4J, but I’m just stuck.