4

在“Hands on machine learning with scikit-learn and tensorflow 2.0”一书的第 17 章中,他们使用 tf.data.Dataset 和 window() 方法将顺序数据集拆分为多个窗口:

n_steps = 100
window_length = n_steps + 1 # target = input shifted 1 character ahead
dataset = dataset.window(window_length, shift=1, drop_remainder=True)

要将顺序数据集切割成多个窗口,他们使用以下方法:

dataset = dataset.flat_map(lambda window: window.batch(window_length))

但是当我执行上面的那一行时,我收到以下错误:

AttributeError                            Traceback (most recent call last)

<ipython-input-18-5b215fb4cb71> in <module>()
----> 1 dataset = dataset.flat_map(lambda window: window.batch(window_length))

10 frames

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in flat_map(self, map_func)
   1650       Dataset: A `Dataset`.
   1651     """
-> 1652     return FlatMapDataset(self, map_func)
   1653 
   1654   def interleave(self,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in __init__(self, input_dataset, map_func)
   4069     self._input_dataset = input_dataset
   4070     self._map_func = StructuredFunctionWrapper(
-> 4071         map_func, self._transformation_name(), dataset=input_dataset)
   4072     if not isinstance(self._map_func.output_structure, DatasetSpec):
   4073       raise TypeError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in __init__(self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs)
   3219       with tracking.resource_tracker_scope(resource_tracker):
   3220         # TODO(b/141462134): Switch to using garbage collection.
-> 3221         self._function = wrapper_fn.get_concrete_function()
   3222 
   3223         if add_to_graph:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in get_concrete_function(self, *args, **kwargs)
   2530     """
   2531     graph_function = self._get_concrete_function_garbage_collected(
-> 2532         *args, **kwargs)
   2533     graph_function._garbage_collector.release()  # pylint: disable=protected-access
   2534     return graph_function

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_garbage_collected(self, *args, **kwargs)
   2494       args, kwargs = None, None
   2495     with self._lock:
-> 2496       graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
   2497       if self.input_signature:
   2498         args = self.input_signature

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2775 
   2776       self._function_cache.missed.add(call_context_key)
-> 2777       graph_function = self._create_graph_function(args, kwargs)
   2778       self._function_cache.primary[cache_key] = graph_function
   2779       return graph_function, args, kwargs

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2665             arg_names=arg_names,
   2666             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667             capture_by_value=self._capture_by_value),
   2668         self._function_attributes,
   2669         # Tell the ConcreteFunction to clean up its graph once it goes out of

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in wrapper_fn(*args)
   3212           attributes=defun_kwargs)
   3213       def wrapper_fn(*args):  # pylint: disable=missing-docstring
-> 3214         ret = _wrapper_helper(*args)
   3215         ret = structure.to_tensor_list(self._output_structure, ret)
   3216         return [ops.convert_to_tensor(t) for t in ret]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in _wrapper_helper(*args)
   3154         nested_args = (nested_args,)
   3155 
-> 3156       ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
   3157       # If `func` returns a list of tensors, `nest.flatten()` and
   3158       # `ops.convert_to_tensor()` would conspire to attempt to stack

/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    263       except Exception as e:  # pylint:disable=broad-except
    264         if hasattr(e, 'ag_error_metadata'):
--> 265           raise e.ag_error_metadata.to_exception(e)
    266         else:
    267           raise

AttributeError: in user code:

    <ipython-input-16-5b215fb4cb71>:1 None  *
        dataset = dataset.flat_map(lambda window: window.batch(window_length))

    AttributeError: '_NestedVariant' object has no attribute 'batch'

为什么会这样?请帮忙。你可以在这里找到对应的笔记本

4

0 回答 0