Tensorflow ValueError:检查输入时出错:预期lstm_13_输入有3个维度,但得到了形状为(1,1)的数组
我从Tensorflow开始,在一些培训示例中遇到了几个问题:Tensorflow ValueError:检查输入时出错:预期lstm_13_输入有3个维度,但得到了形状为(1,1)的数组,tensorflow,lstm,tensorflow-datasets,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 = t
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)
1/10纪元
1/1[====================================]-0s 23毫秒/步
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
5损失='mse',
6指标=[‘准确度’])
---->7模型拟合(ds,每个历元的步长=1,历元=10)
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/keras/engine/training.py in fit(self、x、y、批大小、历元、冗余、回调、验证拆分、验证数据、洗牌、类权重、样本权重、初始历元、每个历元的步骤、验证步骤、验证频率、最大队列大小、工人、使用多处理、**kwargs)
726最大队列大小=最大队列大小,
727名工人=工人,
-->728使用多处理=使用多处理)
729
730 def评估(自我,
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/keras/engine/training\u v2.py适合(self、model、x、y、批处理大小、历元、冗余、回调、验证拆分、验证数据、洗牌、类权重、样本权重、初始历元、每历元步长、验证步长、验证步长、验证频率、**kwargs)
322模式=ModeKeys.TRAIN,
323培训上下文=培训上下文,
-->324个总记录(每个记录=个记录)
325 cbks.生成日志(模型、历元日志、训练结果、模式键.训练)
326
/运行单历元中的opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/keras/engine/training\u v2.py(模型、迭代器、执行函数、数据集大小、批处理大小、策略、每历元步骤、样本数、模式、训练上下文、总历元)
121步骤=步骤,模式=模式,大小=当前批次大小)作为批次日志:
请尝试:
-->123批处理输出=执行函数(迭代器)
124除外(StopIteration,errors.OutOfRangeError):
125#TODO(kaftan):关于tf函数和错误的文件错误。OutOfRangeError?
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/keras/engine/training\u v2\u utils.py in execution\u函数(输入\u fn)
84#`numpy`以渴望模式将张量转换为值。
85返回nest.map\u结构(\u非\u无\u常量\u值,
--->86分布函数(输入函数)
87
88返回执行函数
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in uuuu调用(self,*args,**kwds)
455
456跟踪\u计数=自。\u获取\u跟踪\u计数()
-->457结果=自调用(*args,**kwds)
458如果跟踪计数==self.\u获取跟踪计数():
459 self.\u调用\u计数器。调用\u而不跟踪()
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in_调用(self,*args,**kwds)
501#这是u call u的第一个调用,因此我们必须初始化。
502初始值设定项\u map=object\u identity.ObjectIdentityDictionary()
-->503自我初始化(参数、KWD、添加初始值设定项到=初始值设定项映射)
504最后:
505#此时我们知道初始化已完成(或更少
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in_initialize(self、args、kwds、add_initializers_to)
406自具体状态=(
407 self._stateful_fn._get_concrete_function_internal_garbage_collected(#pylint:disable=protected access
-->408*args,**科威特第纳尔)
409
410 def无效的创建者范围(*未使用的参数,**未使用的参数):
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/eager/function.py in\u get\u concrete\u function\u internal\u garbage\u collected(self,*args,**kwargs)
1846如果self.input\u签名:
1847 args,kwargs=None,None
->1848图形函数,u,u=self._可能定义函数(args,kwargs)
1849返回图函数
1850
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/eager/function.py in\u maybe\u define\u function(self、args、kwargs)
2148 graph\u function=self.\u function\u cache.primary.get(cache\u key,None)
2149如果图形_函数为无:
->2150图形函数=自身。创建图形函数(args、kwargs)
2151 self.\u function\u cache.primary[cache\u key]=图形\u函数
2152返回图_函数,args,kwargs
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow\u core/python/eager/function.py in\u create\u graph\u function(self、args、kwargs、override\u flat\u arg\u shapes)
2039 arg_name=arg_name,
2040覆盖平面形状=覆盖平面形状,
->2041按值捕获=自身。_按值捕获),
2042自我功能属性,
2043#告诉ConcreteFunction在退出时清理其图形
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py in func_graph_from_py func(名称、python_func、args、kwargs、签名、func_图、自动签名、自动签名选项、添加控制依赖项、自动签名、操作返回值、集合、按值捕获、覆盖平面形状)
913(转换函数)
914
-->915 func_outputs=python_func(*func_args,**func_kwargs)
916
917#不变量:`func_outputs`只包含张量、复合传感器、,
/opt/prog/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args,**kwds)
356#uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我们给予
357#函数对自身进行弱引用以避免引用
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'
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'>