Home > Software design >  Failed to convert a pandas column with numpy array in each cell to a tensorflow tensor
Failed to convert a pandas column with numpy array in each cell to a tensorflow tensor

Time:09-17

df=pd.DataFrame({
'image_path':'a',
'valu':[tf.zeros((75,),dtype=tf.dtypes.float32).numpy()]
 })
 df=df.iloc[1:,:]

 for i in range(10):
     ten=tf.zeros((75,), dtype=tf.dtypes.float32)
     ten=ten.numpy()
     df.loc[len(df.index)]=['a',ten]

df.head()

df.head()

 image_path      valu
 0       a       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...
 1       a       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...
 2       a       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...
 3       a       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...
 4       a       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

y=df.valu
print(y)
y=tf.convert_to_tensor(y)

this problem is happening in developing a keras model where label is a array just like this one link for that is given below

This is the link for the actual problem i was facing

*this is the error i got *

ValueError                                Traceback (most recent call last)
 <ipython-input-48-108b1fd37149> in <module>
       1 y=df.valu
       2 print(y)
 ----> 3 y=tf.convert_to_tensor(y)
       4 print(y.shape)
       5 print(y)

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
     199     """Call target, and fall back on dispatchers if there is a TypeError."""
     200     try:
 --> 201       return target(*args, **kwargs)
     202     except (TypeError, ValueError):
     203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor_v2_with_dispatch(value, dtype, dtype_hint, name)
    1403   """
    1404   return convert_to_tensor_v2(
 -> 1405       value, dtype=dtype, dtype_hint=dtype_hint, name=name)
    1406 
    1407 

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
    1413       name=name,
    1414       preferred_dtype=dtype_hint,
 -> 1415       as_ref=False)
    1416 
    1417 

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/profiler/trace.py in wrapped(*args, **kwargs)
     161         with Trace(trace_name, **trace_kwargs):
     162           return func(*args, **kwargs)
 --> 163       return func(*args, **kwargs)
     164 
     165     return wrapped

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
    1538 
    1539     if ret is None:
 -> 1540       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    1541 
    1542     if ret is NotImplemented:

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
     337                                          as_ref=False):
     338   _ = as_ref
 --> 339   return constant(v, dtype=dtype, name=name)
     340 
     341 

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
     263   """
     264   return _constant_impl(value, dtype, shape, name, verify_shape=False,
 --> 265                         allow_broadcast=True)
     266 
     267 

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
     274       with trace.Trace("tf.constant"):
     275         return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
 --> 276     return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
     277 
     278   g = ops.get_default_graph()

  /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
     299 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
     300   """Implementation of eager constant."""
 --> 301   t = convert_to_eager_tensor(value, ctx, dtype)
     302   if shape is None:
     303     return t

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
      96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
      97   ctx.ensure_initialized()
 ---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
      99 
      100 

 ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

this was the error i got in the output above there is a link which the original problem but i figured out where exactly the problem was occuring due to so ... need a help here

CodePudding user response:

x = [list(i) for i in y.values]
y=tf.convert_to_tensor(x)
print(y)
  • Related