Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables is deprecated

I'm trying to turn a Tensorflow Dataset into a NumPy array and I'm getting a deprecation warning:

FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.

What is the "new" way of doing this?

Reproducible example with numpy 1.18.1 and tensorflow 2.1.0:

import tensorflow as tf
import numpy as np

ds = tf.data.Dataset.enumerate(tf.data.Dataset.range(20, 40))

np.vstack(ds)

What I tried: I can only do it with one dimension at a time.

np.fromiter(ds.map(lambda x, y: x), float)
like image 406
Nicolas Gervais Avatar asked Nov 07 '22 06:11

Nicolas Gervais


1 Answers

With just numpy, and using a generator expression:

In [105]: np.stack((np.ones(3) for _ in range(3)))                                      
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py:3254: 
FutureWarning: arrays to stack must be passed as a "sequence" type 
such as list or tuple. Support for non-sequence iterables such as generators 
is deprecated as of NumPy 1.16 and will raise an error in the future.:
Out[105]: 
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

Using a list comprehension to create the arrays:

In [106]: np.stack([np.ones(3) for _ in range(3)])                                      
Out[106]: 
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

I don't use tf, so can only guess as to what:

tf.data.Dataset.enumerate(tf.data.Dataset.range(20, 40))

produces. But as I understand it tensorflow has a distinction between tensors that 'generator-like', potential executions (pipeline?), and 'eager' evaluation, in which the tensors, and tensor expressions, are actually evaluated, producing arrays (or similar object). np.stack tries to convert its inputs into arrays.

A code example for Tf.data.Dataset.enumerate:

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.enumerate(start=5)
for element in dataset.as_numpy_iterator():
  print(element)

enumerate returns an interator. You still have to iterate it, as in this for loop. OR list(dataset).

https://www.tensorflow.org/api_docs/python/tf/data/Dataset#enumerate

like image 144
hpaulj Avatar answered Nov 14 '22 22:11

hpaulj