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)