# Making an iterator over a List

**URL:** <https://community.konduit.ai/t/making-an-iterator-over-a-list/1434>\
**Category:** DL4J\
**Created:** [June 9, 2021, 4:15pm UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434 "2021-06-09T16:15:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MJL](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@MJL](https://community.konduit.ai/u/MJL)\
**Post date:** [June 9, 2021, 4:15pm UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434/1 "2021-06-09T16:15:56Z")

</div>

Hi everyone

I’m pretty new to dl4j and just getting started. I want to build a simple example where the input is a Map\<Double, Double\>, with the first Double being the feature/input (100 random number between 0 and 100), and the second Double being the target (1 if the input is \>50, 0 otherwise).

But I can’t seem to produce an Iterator over the Data. Here’s what I’ve tried:

```auto
    public static DataSetIterator fromEntriesToDataSet(Map<Double, Double> entries) {
        final Double[] boxedDoubles = entries.keySet().toArray(new Double[entries.size()]);
        final double[] unboxedDoubles = Stream.of(boxedDoubles).mapToDouble(Double::doubleValue).toArray();

        INDArray inputNDArray = Nd4j.create(unboxedDoubles);

        final Double[] bools = entries.values().toArray(new Double[entries.size()]);
        INDArray outPut = Nd4j.create(Arrays.asList(bools));

        DataSet dataSet = new DataSet(inputNDArray, outPut);
        List<DataSet> listDs = dataSet.asList(); //Cannot convert to list: feature set rank must be in range 2 to 5 inclusive. Got shape: [100]
        return new ListDataSetIterator<>(listDs, entries.size());
    }

```

As you can see from the comment, it fails to convert the DataSet to a List, saying it’s got the wrong rank. What am I doing wrong?  
Of course, if there’s an easier way to get an Iterator I’m all ears too. I just saw from examples that they usually put the data into INDArray s, and that these need arrays, so I figured I had to convert the data that way.

---

<div class="post-metadata">

**Author:** ![MJL](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@MJL](https://community.konduit.ai/u/MJL)\
**Post date:** [June 9, 2021, 9:44pm UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434/2 "2021-06-09T21:44:18Z")

</div>

Looks like I found the mistake:  
I made a new attempt, this time with 2 features. For whatever reason, this satisfied the program (why can’t we use 1-dimensional data?) Then I continued getting the error message, but this time it had a problem with the rank/dimension of the _labels,_ not the features. The solution was to add the shape as a parameter when creating the INDArray. Here’s the new method:

```auto
    public static DataSetIterator fromEntriesToDataSet(Map<Double[], Double> entries) {
        final Double[][] boxedDoubles = entries.keySet().toArray(new Double[entries.size()][numFeatures]);
        double[][] unboxedDoubles = Utilities.unbox2DArrayOfDoubles(boxedDoubles, numFeatures);

        INDArray inputNDArray = Nd4j.create(unboxedDoubles);

        final Double[] bools = entries.values().toArray(new Double[entries.size()]);
        INDArray outPut = Nd4j.create(Utilities.unbox1DArrayOfDoubles(bools), entries.size(), 1);

        DataSet dataSet = new DataSet(inputNDArray, outPut);
        List<DataSet> listDs = dataSet.asList();
        return new ListDataSetIterator<>(listDs, entries.size());
    }

```

---

<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:** [June 10, 2021, 9:09am UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434/3 "2021-06-10T09:09:49Z")

</div>

@MJL neural networks are about minibatches. Depending on the dataset, they are usually in the form of minibatch size by the number of columns.

If you do time series or images, they will have \> 2 dimensinos.  
What you usually do with this iterator is have it return 1 instance at a time wrapped as a dataset.

A “DataSet” in dl4j is basically an input example + a set of labels.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [June 11, 2021, 9:02am UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434/4 "2021-06-11T09:02:28Z")

</div>

> [@agibsonccc](#):
>
> A “DataSet” in dl4j is basically an input example + a set of labels.

I’d go so far as to say, that the `DataSet` type is misnamed, it should rather be called MiniBatch or something like that, as for most cases it only ever holds a subset of the actual data set.

Anyway, for your situation, i.e. where you can effectively put the entire dataset into a single mini batch, the easiest solution is to just not create a DataSetIterator, and instead create a single DataSet object.

The DataSet object holds two tensors (and optionally masks for those tensors, but those aren’t necessary here). In your particular case the shape of those tensors should look like this:

- Features: [numExamples, numFeatures], i.e. [100, 1] for the data in your first post, and [100, 2] for the data in your second post.
- Labels: [numExamples, numLabels], i.e. [100,1] in your case (one could also model your problem with two classes and then it would be [100,2] instead).

As ND4J supports tensors of different ranks and shapes, and 1 is an especially ambiguous size in this regard, you’ve got to provide a shape along with your double array when turning it into a tensor.

As you’ve got boxed values on the input anyway, I’d probably transform the data something like this. But note, that is only really something that you should do with toy problems. For real world problems, this approach is too slow:

```java
    DataSet mapToDataSet(Map<Double, Double> input){
        final INDArray features = Nd4j.create(input.size(), 1);
        final INDArray labels = Nd4j.create(input.size(), 1);
        int i = 0;
        for (final Map.Entry<Double, Double> entry : input.entrySet()) {
            features.put(i,0, entry.getKey());
            labels.put(i, 0, entry.getValue());          
            i++;
        }
        return new DataSet(features, labels);
    }

```

In the above example I’ve shown how you can do it for the case in your initial post, but I think you can trivially see how to apply the same approach to the two dimensional case you’ve got in your followup post.

---

<div class="post-metadata">

**Author:** ![MJL](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@MJL](https://community.konduit.ai/u/MJL)\
**Post date:** [June 11, 2021, 9:36am UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434/5 "2021-06-11T09:36:43Z")

</div>

Thanks! One question: Why would a problem with 2 classes have labels the shape of [100,2] ? Are they one-hot-encoded? Why not just have one dimension which contains a number based on which class it is?

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [June 11, 2021, 9:43am UTC](https://community.konduit.ai/t/making-an-iterator-over-a-list/1434/6 "2021-06-11T09:43:46Z")

</div>

> [@MJL](#):
>
> Are they one-hot-encoded?

They are one hot encoded because that allows you to use a multi-class cross entropy loss function. That way you get a nice probability distribution as your output.

> [@MJL](#):
>
> Why not just have one dimension which contains a number based on which class it is?

Assume you have 10 classes and you have one output that uses regression to calculate which class it should be. What does the output “5” tell you? That it is class 6? That it isn’t quite certain if it is class 1 or class 10? What does the output “-1” mean? What does the output “12” mean?

The only time that a single output works for classification is when you’ve got just two classes and you can constrain it to be between 0 and 1. In every other case it doesn’t make any sense.

Even though it does work, having 2 outputs in that case still makes sense, as it gives you twice as much gradient information to work with.
