我在colab和我的环境中尝试了VQ-VAE的keras示例。我遇到了同样的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-a6e2591462e2> in <module>()
9 # Feed the whole array and retrieving the pixel value probabilities for the next
10 # pixel.
---> 11 probs = sampler.predict(priors)
12 # Use the probabilities to pick pixel values and append the values to the priors.
13 priors[:, row, col] = probs[:, row, col]
9 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1749 for step in data_handler.steps():
1750 callbacks.on_predict_batch_begin(step)
-> 1751 tmp_batch_outputs = self.predict_function(iterator)
1752 if data_handler.should_sync:
1753 context.async_wait()
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
883
884 with OptionalXlaContext(self._jit_compile):
--> 885 result = self._call(*args, **kwds)
886
887 new_tracing_count = self.experimental_get_tracing_count()
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
931 # This is the first call of __call__, so we have to initialize.
932 initializers = []
--> 933 self._initialize(args, kwds, add_initializers_to=initializers)
934 finally:
935 # At this point we know that the initialization is complete (or less
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
758 self._concrete_stateful_fn = (
759 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 760 *args, **kwds))
761
762 def invalid_creator_scope(*unused_args, **unused_kwds):
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
3064 args, kwargs = None, None
3065 with self._lock:
-> 3066 graph_function, _ = self._maybe_define_function(args, kwargs)
3067 return graph_function
3068
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
3461
3462 self._function_cache.missed.add(call_context_key)
-> 3463 graph_function = self._create_graph_function(args, kwargs)
3464 self._function_cache.primary[cache_key] = graph_function
3465
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3306 arg_names=arg_names,
3307 override_flat_arg_shapes=override_flat_arg_shapes,
-> 3308 capture_by_value=self._capture_by_value),
3309 self._function_attributes,
3310 function_spec=self.function_spec,
/usr/local/lib/python3.7/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, acd_record_initial_resource_uses)
1005 _, original_func = tf_decorator.unwrap(python_func)
1006
-> 1007 func_outputs = python_func(*func_args, **func_kwargs)
1008
1009 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
666 # the function a weak reference to itself to avoid a reference cycle.
667 with OptionalXlaContext(compile_with_xla):
--> 668 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
669 return out
670
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
TypeError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1586 predict_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1576 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1569 run_step **
outputs = model.predict_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1537 predict_step
return self(x, training=False)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:415 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:550 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1044 __call__
self._set_save_spec(inputs, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py:530 _method_wrapper
result = method(self, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:3049 _set_save_spec
flat_specs = [tf_utils.get_tensor_spec(x) for x in flat_kwarg]
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:3049 <listcomp>
flat_specs = [tf_utils.get_tensor_spec(x) for x in flat_kwarg]
/usr/local/lib/python3.7/dist-packages/keras/utils/tf_utils.py:467 get_tensor_spec
spec = tf.TensorSpec(shape=t.shape, dtype=t.dtype, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py:51 __init__
self._shape = tensor_shape.TensorShape(shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:784 __init__
self._dims = [as_dimension(dims)]
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:729 as_dimension
return Dimension(value)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:209 __init__
.format(value, type(value))), None)
<string>:3 raise_from
TypeError: Dimension value must be integer or None or have an __index__ method, got value '<attribute 'shape' of 'numpy.generic' objects>' with type '<class 'getset_descriptor'>'
错误来自这部分代码:
# Create a mini sampler model.
inputs = layers.Input(shape=pixel_cnn.input_shape[1:])
x = pixel_cnn(inputs, training=False)
dist = tfp.distributions.Categorical(logits=x)
sampled = dist.sample()
sampler = keras.Model(inputs, sampled)
# Create an empty array of priors.
batch = 10
priors = np.zeros(shape=(batch,) + (pixel_cnn.input_shape)[1:])
batch, rows, cols = priors.shape
# Iterate over the priors because generation has to be done sequentially pixel by pixel.
for row in range(rows):
for col in range(cols):
# Feed the whole array and retrieving the pixel value probabilities for the next
# pixel.
probs = sampler.predict(priors)
# Use the probabilities to pick pixel values and append the values to the priors.
priors[:, row, col] = probs[:, row, col]
print(f"Prior shape: {priors.shape}")
据我所知,问题来自priors
数组尺寸与pixel_cnn输入不匹配。但输入是KerasTensor(type_spec=TensorSpec(shape=(None, 32, 32), dtype=tf.float32, name='input_2'), name='input_2', description="created by layer 'input_2'")
,priors
的形状是(10, 32, 32)
,所以没有错配…
我发现了这些类似的问题,但我仍然很难理解发生了什么:
TypeError:尺寸值必须是整数或None,或者有索引方法,得到TensorShape([None, 1])
TypeError:尺寸值必须是整数或None,或者有索引方法,获取值'TensorShape([None, 16])'
有人能解释一下为什么这个错误存在以及如何修复它吗?
是的,这是不记录包版本和jupyter环境的不幸副作用。tensorflow-probability包仍然处于1.0之前的阶段,这会导致下面的事情发生变化,除非您固定包的版本。
在本例中,我查找了Sayak Paul在写这篇文章时可能使用的版本,结果是0.13.0。使用这个特定版本的tensorflow-probability使错误消失。
我认为这个错误与急于执行一个没有分类数据的函数有关,但这只是一个猜测。将它包装在tf.py_function中并不能满足我的需求,降级到0.13.0就可以了。在0.13.0之后的版本在这个特定的例子中再也不起作用了。
编辑:我在https://keras.io/examples/generative/vq_vae/上更新了代码,以便与新版本的tensorflow-probability一起使用。不再需要将版本固定为0.13.0