
[英]Data type added as string when creating variable …array([0., 0., 0., …, 0., 0., 0.], dtype=float32)
[英]TensorFlow : TypeError: Expected binary or unicode string, got array([[[0., 0., 0., …, 0., 0., 0.],
我想使用 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.
我将不胜感激任何建议。 谢谢你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.