ValueError:检查输入时出错:预期lstm_13_input具有3个维,但数组的形状为(1,1)

时间:2019-12-05 21:15:45

标签: tensorflow lstm tensorflow-datasets

我从Tensorflow开始,在一些培训示例中遇到了几个问题:

    import os
    os.environ["CUDA_VISIBLE_DEVICES"]="-1"    
    import tensorflow as tf
    import numpy as np
    from tensorflow.keras import layers

    x_train = tf.Variable([[1]], dtype=tf.int32)
    y_train = tf.Variable([[1]], dtype=tf.int32)
    ds = tf.data.Dataset.from_tensor_slices((x_train,y_train))

    model = tf.keras.Sequential([
    layers.LSTM(2, input_shape=(2,1),activation='sigmoid', return_sequences=True)
    ])
    model.compile(optimizer='adam',
                 loss='mse',
                 metrics=['accuracy'])
    model.fit(ds, steps_per_epoch=1, epochs=10)

错误消息:

Epoch 1/10
1/1 [==============================] - 0s 23ms/step

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-815b2ec2170e> in <module>
      5              loss='mse',
      6              metrics=['accuracy'])
----> 7 model.fit(ds, steps_per_epoch=1, epochs=10)

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    726         max_queue_size=max_queue_size,
    727         workers=workers,
--> 728         use_multiprocessing=use_multiprocessing)
    729 
    730   def evaluate(self,

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
    322                 mode=ModeKeys.TRAIN,
    323                 training_context=training_context,
--> 324                 total_epochs=epochs)
    325             cbks.make_logs(model, epoch_logs, training_result, ModeKeys.TRAIN)
    326 

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in run_one_epoch(model, iterator, execution_function, dataset_size, batch_size, strategy, steps_per_epoch, num_samples, mode, training_context, total_epochs)
    121         step=step, mode=mode, size=current_batch_size) as batch_logs:
    122       try:
--> 123         batch_outs = execution_function(iterator)
    124       except (StopIteration, errors.OutOfRangeError):
    125         # TODO(kaftan): File bug about tf function and errors.OutOfRangeError?

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in execution_function(input_fn)
     84     # `numpy` translates Tensors to values in Eager mode.
     85     return nest.map_structure(_non_none_constant_value,
---> 86                               distributed_function(input_fn))
     87 
     88   return execution_function

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in __call__(self, *args, **kwds)
    455 
    456     tracing_count = self._get_tracing_count()
--> 457     result = self._call(*args, **kwds)
    458     if tracing_count == self._get_tracing_count():
    459       self._call_counter.called_without_tracing()

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _call(self, *args, **kwds)
    501       # This is the first call of __call__, so we have to initialize.
    502       initializer_map = object_identity.ObjectIdentityDictionary()
--> 503       self._initialize(args, kwds, add_initializers_to=initializer_map)
    504     finally:
    505       # At this point we know that the initialization is complete (or less

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    406     self._concrete_stateful_fn = (
    407         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 408             *args, **kwds))
    409 
    410     def invalid_creator_scope(*unused_args, **unused_kwds):

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   1846     if self.input_signature:
   1847       args, kwargs = None, None
-> 1848     graph_function, _, _ = self._maybe_define_function(args, kwargs)
   1849     return graph_function
   1850 

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2148         graph_function = self._function_cache.primary.get(cache_key, None)
   2149         if graph_function is None:
-> 2150           graph_function = self._create_graph_function(args, kwargs)
   2151           self._function_cache.primary[cache_key] = graph_function
   2152         return graph_function, args, kwargs

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2039             arg_names=arg_names,
   2040             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2041             capture_by_value=self._capture_by_value),
   2042         self._function_attributes,
   2043         # Tell the ConcreteFunction to clean up its graph once it goes out of

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/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)
    913                                           converted_func)
    914 
--> 915       func_outputs = python_func(*func_args, **func_kwargs)
    916 
    917       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    356         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    357         # the function a weak reference to itself to avoid a reference cycle.
--> 358         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    359     weak_wrapped_fn = weakref.ref(wrapped_fn)
    360 

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in distributed_function(input_iterator)
     71     strategy = distribution_strategy_context.get_strategy()
     72     outputs = strategy.experimental_run_v2(
---> 73         per_replica_function, args=(model, x, y, sample_weights))
     74     # Out of PerReplica outputs reduce or pick values to return.
     75     all_outputs = dist_utils.unwrap_output_dict(

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/distribute/distribute_lib.py in experimental_run_v2(self, fn, args, kwargs)
    758       fn = autograph.tf_convert(fn, ag_ctx.control_status_ctx(),
    759                                 convert_by_default=False)
--> 760       return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    761 
    762   def reduce(self, reduce_op, value, axis):

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/distribute/distribute_lib.py in call_for_each_replica(self, fn, args, kwargs)
   1785       kwargs = {}
   1786     with self._container_strategy().scope():
-> 1787       return self._call_for_each_replica(fn, args, kwargs)
   1788 
   1789   def _call_for_each_replica(self, fn, args, kwargs):

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/distribute/distribute_lib.py in _call_for_each_replica(self, fn, args, kwargs)
   2130         self._container_strategy(),
   2131         replica_id_in_sync_group=constant_op.constant(0, dtypes.int32)):
-> 2132       return fn(*args, **kwargs)
   2133 
   2134   def _reduce_to(self, reduce_op, value, destinations):

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    290   def wrapper(*args, **kwargs):
    291     with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.DISABLED):
--> 292       return func(*args, **kwargs)
    293 
    294   if inspect.isfunction(func) or inspect.ismethod(func):

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in train_on_batch(model, x, y, sample_weight, class_weight, reset_metrics)
    251   x, y, sample_weights = model._standardize_user_data(
    252       x, y, sample_weight=sample_weight, class_weight=class_weight,
--> 253       extract_tensors_from_dataset=True)
    254   batch_size = array_ops.shape(nest.flatten(x, expand_composites=True)[0])[0]
    255   # If `model._distribution_strategy` is True, then we are in a replica context

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2470           feed_input_shapes,
   2471           check_batch_axis=False,  # Don't enforce the batch size.
-> 2472           exception_prefix='input')
   2473 
   2474     # Get typespecs for the input data and sanitize it if necessary.

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    563                            ': expected ' + names[i] + ' to have ' +
    564                            str(len(shape)) + ' dimensions, but got array '
--> 565                            'with shape ' + str(data_shape))
    566         if not check_batch_axis:
    567           data_shape = data_shape[1:]

ValueError: Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (1, 1)

我真的没有得到错误。 LSTM单元格的input_shape=(time_steps, dimensions)可以,不是吗?就像我说的,这只是一个尝试,它是尽可能简单的示例:)

我已经尝试使用参数,主要是input_shape。我还尝试使用使用yield [1],[1]的生成器,但这也不起作用...

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我找到了几个答案。 Keras中的LSTM期望3维输入。通过改变 x_train = tf.Variable([[1]], dtype=tf.int32)x_train = tf.Variable([[[1]]], dtype=tf.int32)即将运行,但会引发以下异常

WARNING:tensorflow:Falling back from v2 loop because of error: Failed to find data adapter that can handle input: <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>, <class 'NoneType'>
Train on 1 samples
Epoch 1/10

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-b03e676177f1> in <module>
     5              loss='mse',
     6              metrics=['accuracy'])
----> 7 model.fit(y_train,y_train, steps_per_epoch=1, epochs=10)

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   726         max_queue_size=max_queue_size,
   727         workers=workers,
--> 728         use_multiprocessing=use_multiprocessing)
   729 
   730   def evaluate(self,

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
   672         validation_steps=validation_steps,
   673         validation_freq=validation_freq,
--> 674         steps_name='steps_per_epoch')
   675 
   676   def evaluate(self,

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
   297           else:
   298             actual_inputs = ins()
--> 299           batch_outs = f(actual_inputs)
   300         except errors.OutOfRangeError:
   301           if is_dataset:

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in __call__(self, inputs)
  3732               'You must feed a value for placeholder %s' % (tensor,))
  3733       if not isinstance(value, ops.Tensor):
-> 3734         value = ops.convert_to_tensor(value, dtype=tensor.dtype)
  3735       if value.dtype != tensor.dtype:
  3736         # Temporary workaround due to `convert_to_tensor` not casting floats.

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype, dtype_hint)
  1182   preferred_dtype = deprecation.deprecated_argument_lookup(
  1183       "dtype_hint", dtype_hint, "preferred_dtype", preferred_dtype)
-> 1184   return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  1185 
  1186 

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
  1240       name=name,
  1241       preferred_dtype=dtype_hint,
-> 1242       as_ref=False)
  1243 
  1244 

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors)
  1294 
  1295     if ret is None:
-> 1296       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  1297 
  1298     if ret is NotImplemented:

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in _dense_var_to_tensor(var, dtype, name, as_ref)
  1787 
  1788 def _dense_var_to_tensor(var, dtype=None, name=None, as_ref=False):
-> 1789   return var._dense_var_to_tensor(dtype=dtype, name=name, as_ref=as_ref)  # pylint: disable=protected-access
  1790 
  1791 

/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in _dense_var_to_tensor(***failed resolving arguments***)
  1208       raise ValueError(
  1209           "Incompatible type conversion requested to type {!r} for variable "
-> 1210           "of type {!r}".format(dtype.name, self.dtype.name))
  1211     if as_ref:
  1212       return self.read_value().op.inputs[0]

ValueError: Incompatible type conversion requested to type 'float32' for variable of type 'int32'

dtype=tf.int32更改为dtype=tf.float32即可。

为什么不能将整数值提供给LSTM?

此外,还会发出警告:

WARNING:tensorflow:Falling back from v2 loop because of error: Failed to find data adapter that can handle input: <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>, <class 'NoneType'>

这可以通过将tf.Variable更改为tf.constant或使用numpy.ndarray而不是tf类型来解决。我以为那些在内部由numpy数组表示?!

注意:我不再将数据集传递给fit函数,而是将x_train变量作为输入和输出数据单独输入。使用数据集仍然会提供错误消息。这可以通过批量处理数据集来解决,例如ds.batch(1)并将epochs=10更改为epochs=1,因为tensorflow期望训练数据集的大小与steps_per_epoch*epochs相匹配。

最好的问候, 卡斯滕