# passing the dataset to the fit method of the MultiLayerNetwork class

**URL:** https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535
**Category:** DL4J
**Created:** [April 26, 2023, 7:01am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535 "2023-04-26T07:01:43Z")
**Posts on this page:** 13
**Page:** 2

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [April 27, 2023, 2:51am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/21 "2023-04-27T02:51:20Z")

</div>

@Arasaka sure no hurry on my part.

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [April 30, 2023, 1:56pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/22 "2023-04-30T13:56:40Z")

</div>

@agibsonccc hello, I’m back to work. I need to know if I’m doing everything right. This is the error that started to appear: Sequence lengths do not match for RnnOutputLayer input and labels:Arrays should be rank 3 with shape [minibatch, size, sequenceLength] - mismatch on dimension 2 (sequence length).  
As I understand it, that error indicates that the shape (shape) of the input and output data does not match the expected shape. In this case, RnnOutputLayer expects the dimension of the third axis of the input data (sequenceLength) to match the dimension of the third axis of the labels.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [May 1, 2023, 3:38am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/23 "2023-05-01T03:38:29Z")

</div>

@Arasaka do you have a minimal reproducer for me I can run from a main method? Thanks!

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [May 6, 2023, 2:26pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/24 "2023-05-06T14:26:24Z")

</div>

@agibsonccc

I’m on a short business trip, so I’ll have to wait a couple of days. I apologize for the wait.

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [August 22, 2023, 7:12am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/25 "2023-08-22T07:12:02Z")

</div>

@agibsonccc hello. Finally back and ready to continue if you don’t mind. I did it a little differently, and now the error is this: Exception in thread “main” java.lang.IllegalStateException: Cannot merge time series with different size for dimension 1 (first shape: [1, 6, 300], 1th shape: [1 , 5, 300]  
at org.nd4j.linalg.dataset.api.DataSetUtil.mergeTimeSeries(DataSetUtil.java:467)  
at org.nd4j.linalg.dataset.api.DataSetUtil.mergeFeatures(DataSetUtil.java:206)  
at org.nd4j.linalg.dataset.api.DataSetUtil.mergeFeatures(DataSetUtil.java:226)  
at org.nd4j.linalg.dataset.MultiDataSet.merge(MultiDataSet.java:488)  
I tried to align with paddings, another error began to appear. How should the fit method properly accept? Exactly what measurements should be the same?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 22, 2023, 8:35pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/26 "2023-08-22T20:35:37Z")

</div>

@Arasaka you should figure out whatever the max possible lengths are and use masking to address different lengths like that. See more here:

> **[Recurrent Neural Network](https://deeplearning4j.konduit.ai/v/en-1.0.0-beta7/models/recurrent)**
>
> Recurrent Neural Network (RNN) implementations in DL4J.

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [August 23, 2023, 7:25pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/27 "2023-08-23T19:25:43Z")

</div>

@agibsonccc thanks, i read the article. Equalized measurements with shape to [1, 6, 300]. Another exception was thrown: Exception in thread “main” java.lang.IllegalStateException: Invalid input: EmbeddingSequenceLayer expects either rank 2 input of shape [minibatch,seqLength] or rank 3 input of shape [minibatch,1,seqLength]. Got rank 3 input of shape [4, 6, 300]  
I tried reshape to get the desired shape of rank 3, that is, [1, 1, 6]. An exception appeared: Exception in thread “main” org.nd4j.linalg.exception.ND4JIllegalStateException: New shape length doesn’t match original length: [6] vs [1800]. Original shape: [1, 6, 300] New Shape: [1, 1, 6]  
I understand that all this is due to vectorSize, which is 300. I tried to make a mask, it showed [1, 6] in the logs, I added it to the List. This exception recurred: Exception in thread “main” java.lang.IllegalStateException: Invalid input: EmbeddingSequenceLayer expects either rank 2 input of shape [minibatch,seqLength] or rank 3 input of shape [minibatch,1,seqLength]. Got rank 3 input of shape [4, 6, 300]  
I tried to put it in the first place when placing INDArray in List (_**new MultiDataSet(inputMask, input, output, null)**_), the following exception appeared: Invalid size: cannot get size of dimension 2 for rank 2 NDArray (array shape: [1, 6])  
Maybe there is an opportunity to change the submission form in the EmbeddingSequenceLayer? Or am I doing something wrong with masks?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 25, 2023, 1:26pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/28 "2023-08-25T13:26:02Z")

</div>

> [@Arasaka](#):
>
> uenceLayer expects either rank 2 input of shape [minibatch,seqLength] or rank 3 input of shape [minibatch,1,seqLength]. Got rank 3 input of shape [4, 6, 300]

@Arasaka could you clarify your data pipeline and the like? I can’t tel what’s off yet. A reminder of your use case would be appreciated.

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [August 26, 2023, 3:37am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/29 "2023-08-26T03:37:45Z")

</div>

@agibsonccc ok, i’ll hide some things if you don’t mind

public static MultiDataSet convertDataToMultiDataSet(String data) throws IOException {  
List dataSetList = new ArrayList\<\>();

```
InputStream modelIn = new FileInputStream(*path to the model*);
SentenceModel sentenceModel = new SentenceModel(modelIn);
SentenceDetectorME sentenceDetectorME = new SentenceDetectorME(sentenceModel);

String[] sentences = sentenceDetectorME.sentDetect(data);

Tokenizer tokenizer = SimpleTokenizer.INSTANCE;

int maxLength = 0;
for (String sentence : sentences) {
    String[] tokens = tokenizer.tokenize(sentence);
    maxLength = Math.max(maxLength, tokens.length);
}

int vectorSize = 300;
int numClasses = 2;
INDArray padding = Nd4j.zeros(1, maxLength, vectorSize);

for (String sentence : sentences) {

    if (sentence.trim().isEmpty()) {
        continue;
    }

    INDArray inputMask = Nd4j.zeros(1, maxLength); // mask
    INDArray input = Nd4j.zeros(1, maxLength, vectorSize);
    INDArray output = Nd4j.zeros(1, maxLength, numClasses);

    String[] tokens = tokenizer.tokenize(sentence);
    int length = Math.min(tokens.length, maxLength);
    //int length = tokens.length;

    System.out.println("Processing sentence: " + sentence);
    System.out.println("Tokens: " + Arrays.toString(tokens));

    for (int i = 0; i < length; i++) {
        String token = tokens[i];
        System.out.println("Processing token: " + token);
        INDArray vector = getWordVector(token);
        if (vector != null) {
            input.put(new INDArrayIndex[]{NDArrayIndex.point(0), NDArrayIndex.point(i), NDArrayIndex.all()}, vector);
            inputMask.put(new INDArrayIndex[]{NDArrayIndex.point(0), NDArrayIndex.interval(0, length)}, Nd4j.ones(1, length )); // установить маску
        }
    }

    int length1 = (int) input.size(1);

    if (length1 < maxLength) {
        INDArray inputCopy = input.dup(); // copy
        inputCopy.put(new INDArrayIndex[]{NDArrayIndex.all(), NDArrayIndex.interval(length1, maxLength), NDArrayIndex.all()}, padding);
        dataSetList.add(new MultiDataSet(inputCopy, output));
    }
    else {
        dataSetList.add(new MultiDataSet(input, output));
    }

    System.out.println("Input shape: " + Arrays.toString(input.shape()));
    System.out.println("Output shape: " + Arrays.toString(output.shape()));
    System.out.println("InputMask shape: " + Arrays.toString(inputMask.shape()));

    dataSetList.add(new MultiDataSet(input, output, inputMask, null));
    //dataSetList.add(new MultiDataSet(input, output));

}

return MultiDataSet.merge(dataSetList);

```

}

I have three lines displayed in the console twice. For the first time:  
Input shape: [1, 6, 300]  
output shape: [1, 6, 2]  
InputMask shape: [1, 6]  
A second time:  
Input shape: [1, 6, 300]  
output shape: [1, 6, 2]  
InputMask shape: [1, 6]  
Thanks in advance for your reply.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [August 29, 2023, 11:17am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/30 "2023-08-29T11:17:57Z")

</div>

@Arasaka Sorry for the late reply.  
Could you tell me a bit more about your problem? I don’t need secret/proprietary information just what you’re trying to doo.  
It looks like you’re using opennlp. Are you feeding the output of that to dl4j?

It looks like you are trying to train custom word embeddings? Is that what the embedding sequence layer is for?

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [August 29, 2023, 12:52pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/31 "2023-08-29T12:52:29Z")

</div>

@agibsonccc let me elaborate on what I’m trying to do.

Yes, I’m using the OpenNLP library for sentence splitting and tokenization. Then the result of her work is transferred to dl4j for training the neural network.

No, learning my own word embeddings is not my goal.  
In my code, I use the already pre-trained word2vec model to get vector representations for each word in the sentence.

You are right that the EmbeddingSequenceLayer is just designed to work with sequences of vector representations of words. I use it to feed word embeddings to LSTM input.

Please ask any additional questions if any clarification is needed! It is important for me to understand what I am doing wrong.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 1, 2023, 7:37am UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/32 "2023-09-01T07:37:20Z")

</div>

@Arasaka you shouldn’t need the embedding layer then. Your embeddings are external. Just skip straight to using LSTMs. Look around at this:

> <https://github.com/deeplearning4j/deeplearning4j-examples/blob/051c59bd06b38ed39ca92f5940a6ca43b0f34c0f/dl4j-examples/src/main/java/org/deeplearning4j/examples/advanced/modelling/textclassification/customcorpusword2vec/TrainNews.java#L28>

---

<div class="post-metadata">

### Author: ![Arasaka](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arasaka/32/1190_2.png) [@Arasaka](https://community.konduit.ai/u/Arasaka)
#### Post date: [September 7, 2023, 2:09pm UTC](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535/33 "2023-09-07T14:09:11Z")

</div>

@agibsonccc I apologize for the long answer, at this moment I am considering your example. If there are any questions, I will write.

[Previous page](https://community.konduit.ai/t/passing-the-dataset-to-the-fit-method-of-the-multilayernetwork-class/2535.md?page=1)
