profile
viewpoint

Ask questionsValueError: Data cardinality is ambiguous. No issue in version 2.0.0 but fails with 2.2.0/2.3.0

Functional API Multi input/output model works correctly in TF 2.0.0 but fails with TF 2.2.0 and 2.3.0

Describe the current behavior Created a Multi Input/Output model using the functional api. model.fit fails with the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-20532c7b88ab> in <module>()
     11 print('x',np.asarray(x).shape)
     12 print('y',y.shape)
---> 13 model.fit(x,y,epochs=1)

3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
    280             label, ", ".join(str(i.shape[0]) for i in nest.flatten(data)))
    281       msg += "Please provide data which shares the same first dimension."
--> 282       raise ValueError(msg)
    283     num_samples = num_samples.pop()
    284 

ValueError: Data cardinality is ambiguous:
  x sizes: 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224
  y sizes: 2
Please provide data which shares the same first dimension.

Colab Github Gist: https://colab.research.google.com/gist/sramakrishnan247/0897350c315280935b1617e325665c08/tf2-3-issue.ipynb

Describe the expected behavior model.fit should work successfully (similar to tensorflow 2.0.0)

x (10, 2, 224, 224, 20)
y (2, 3)
Train on 2 samples
14/2 [==================================================================================================================================================================================================================] - 34s 2s/sample - loss: 1.4392
<tensorflow.python.keras.callbacks.History at 0x7f444928b048>

Colab Github gist: https://colab.research.google.com/gist/sramakrishnan247/08becc3e024ad21a2b90fa2ebabcfe76/tf2-0-sample.ipynb

tensorflow/tensorflow

Answer questions omalleyt12

@sramakrishnan247 Thanks for the issue!

The issue you're running into is bc you're passing a list-of-lists for x. We dropped support for treating inner lists as Tensors bc it's ambiguous as to whether a list should be interpreted as a Tensor or as a Python list (as this example shows). Instead, you can pass a list of np.arrays like this:

x = []
for i in range(10):
  x.append([])
data = np.random.rand(2,10,224, 224, 20)
for data_vector in data:
  for index in range(10):
    x[index].append(data_vector[index]) 

for i in range(10):
  x[i] = np.asarray(x[i]) 
   
y = np.random.rand(2,3)
print('x',np.asarray(x).shape)
print('y',y.shape)
model.fit(x,y,epochs=1)
useful!
source:https://uonfu.com/
Github User Rank List