繁体   English   中英

TensorFlow:TypeError:预期的二进制或Unicode字符串,得到了数组([[[0.,0.,0.,…,0.,0.,0.],

TensorFlow : TypeError: Expected binary or unicode string, got array([[[0., 0., 0., …, 0., 0., 0.],

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我想使用tf.data.Dataset.from_tensor_slices影响numpy数组到train_loader变量,但出现错误。 有人可以帮我解决吗

这是我使用的代码:在这里,我按30的比例分割数据以进行训练和验证:

x_train = np.concatenate((abnormal_scans[:10],normal_scans[:30]), axis=0)
y_train = np.concatenate((abnormal_labels[:10], normal_labels[:30]), axis=0)
x_val = np.concatenate((abnormal_scans[10:], normal_scans[20:]), axis=0)
y_val = np.concatenate((abnormal_labels[10:], normal_labels[20:]), axis=0)

在这里,我定义了数据加载器:

train_loader = tf.data.Dataset.from_tensor_slices((x_train, y_train))
validation_loader = tf.data.Dataset.from_tensor_slices((x_val, y_val))

我收到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-1941618d53dd> in <module>
      1 # Define data loaders.
----> 2 train_loader = tf.data.Dataset.from_tensor_slices((x_train, y_train))
      3 validation_loader = tf.data.Dataset.from_tensor_slices((x_val, y_val))
      4 
      5 batch_size = 2

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in from_tensor_slices(tensors)
   1683   @functools.wraps(DatasetV2.from_tensor_slices)
   1684   def from_tensor_slices(tensors):
-> 1685     return DatasetV1Adapter(DatasetV2.from_tensor_slices(tensors))
   1686 
   1687   @staticmethod

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in from_tensor_slices(tensors)
    362       Dataset: A `Dataset`.
    363     """
--> 364     return TensorSliceDataset(tensors)
    365 
    366   class _GeneratorState(object):

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in __init__(self, tensors)
   2219     """See `Dataset.from_tensor_slices()` for details."""
   2220     with ops.name_scope("tensors"):
-> 2221       tensors = structure_lib.normalize_tensors(tensors)
   2222 
   2223     batched_structure = structure_lib.Structure.from_value(tensors)

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\data\util\structure.py in normalize_tensors(tensors)
    304         prepared.append(t)
    305       else:
--> 306         prepared.append(ops.convert_to_tensor(t, name="component_%d" % i))
    307   return nest.pack_sequence_as(tensors, prepared)
    308 

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, preferred_dtype, dtype_hint)
   1085   preferred_dtype = deprecation.deprecated_argument_lookup(
   1086       "dtype_hint", dtype_hint, "preferred_dtype", preferred_dtype)
-> 1087   return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
   1088 
   1089 

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
   1143       name=name,
   1144       preferred_dtype=dtype_hint,
-> 1145       as_ref=False)
   1146 
   1147 

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_symbolic_tensors, accept_composite_tensors)
   1222 
   1223     if ret is None:
-> 1224       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1225 
   1226     if ret is NotImplemented:

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    303                                          as_ref=False):
    304   _ = as_ref
--> 305   return constant(v, dtype=dtype, name=name)
    306 
    307 

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
    244   """
    245   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 246                         allow_broadcast=True)
    247 
    248 

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    282       tensor_util.make_tensor_proto(
    283           value, dtype=dtype, shape=shape, verify_shape=verify_shape,
--> 284           allow_broadcast=allow_broadcast))
    285   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    286   const_tensor = g.create_op(

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    571     raise TypeError(
    572         "Element type not supported in TensorProto: %s" % numpy_dtype.name)
--> 573   append_fn(tensor_proto, proto_values)
    574 
    575   return tensor_proto

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\tensor_util.py in SlowAppendObjectArrayToTensorProto(tensor_proto, proto_values)
    152 
    153   def SlowAppendObjectArrayToTensorProto(tensor_proto, proto_values):
--> 154     tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
    155 
    156   def SlowAppendBoolArrayToTensorProto(tensor_proto, proto_values):

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\tensor_util.py in <listcomp>(.0)
    152 
    153   def SlowAppendObjectArrayToTensorProto(tensor_proto, proto_values):
--> 154     tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
    155 
    156   def SlowAppendBoolArrayToTensorProto(tensor_proto, proto_values):

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\util\compat.py in as_bytes(bytes_or_text, encoding)
     63   else:
     64     raise TypeError('Expected binary or unicode string, got %r' %
---> 65                     (bytes_or_text,))
     66 
     67 

TypeError: Expected binary or unicode string, got array([[[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]],

       ...,

       [[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]]], dtype=float32)

我试图通过执行此转换来修复它。 但它没有用:

def conv_t(arg):
  arg = tf.convert_to_tensor(arg, dtype=tf.float32)
  return arg

x_train = conv_t(x_train)

我收到此错误:

TypeError                                 Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-9-3e359ede7705> in <module>
      3   return arg
      4 
----> 5 x_train = conv_t(x_train)
      6 
      7 # Define data loaders.

<ipython-input-9-3e359ede7705> in conv_t(arg)
      1 def conv_t(arg):
----> 2   arg = tf.convert_to_tensor(arg, dtype=tf.float32)
      3   return arg
      4 
      5 x_train = conv_t(x_train

)〜_anaconda3 \\ envs \\ PythonCPU \\ lib \\ site-packages \\ tensorflow \\ python \\ framework \\ constant_op.py in _constant_impl(value,dtype,shape,name,verify_shape,allow_broadcast)282 tensor_util.make_tensor_proto(283 value,dtype = dtype ,shape = shape,verify_shape = verify_shape,-> 284 allow_broadcast = allow_broadcast))285 dtype_value = attr_value_pb2.AttrValue(type = tensor_value.tensor.dtype)286 const_tensor = g.create_op(

~\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    438   if isinstance(values, (np.ndarray, np.generic)):
    439     if dtype:
--> 440       nparray = values.astype(dtype.as_numpy_dtype)
    441     else:
    442       nparray = values

ValueError: setting an array element with a sequence.

我将不胜感激任何建议! 谢谢你。

问题暂未有回复.您可以查看右边的相关问题.
3 UML组成1. *和0. *

我在UML中有一个问题。 我是UML的初学者,我不知道我是否可以这样做: ...

2017-12-05 16:56:50 2 255   uml
8 java中0和0.有什么区别?

我实际上没有特定的代码,但是我和一个朋友使用的代码完全相同,只是他使用了0.而我只使用了0 。 我的编译了,但是直到他删除了点之后他才开始编译。 但是现在,我已经初始化了一个变量double x = 0. 。偶然地,忘了删除点,然后编译了它! 为什么? ...

9 1 1 1 [0.] 84.714:为什么要打印括号?

我已经编写了如下的python代码: 我不明白为什么str(data [i,j,k])的输出带有方括号:[0.] 这里,data [i,j,k]是坐标(i,j,k)的值。 我怎样才能打印出值(不带括号)? ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM