无法将包含每个单元格中numpy数组的pandas列转换为tensorflow张量

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.valuprint(y)y=tf.convert_to_tensor(y)

这个问题发生在开发一个keras模型时,其中标签是一个类似这样的数组,相关链接如下

这是我遇到的问题的实际链接

*这是我得到的错误*

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).

这是我在输出中得到的错误,上面有一个链接指向原始问题,但我已经找出了问题发生的确切位置,所以…在这里需要帮助


回答:

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注