【问题标题】:Tensorflow probability: InvalidArgumentError: required broadcastable shapes张量流概率:InvalidArgumentError:所需的可广播形状
【发布时间】:2021-09-04 06:09:59
【问题描述】:

我的数据有 channels_first 格式。当我使用 tensorflow 概率层时,出现以下错误:

这是一个示例,其中输入形状为 [1,28,28],可重现代码:Gist(请确保您在 GPU 上运行代码。)

InvalidArgumentError:  required broadcastable shapes
     [[node gradient_tape/lambda/model_3_mixture_same_family_4_MixtureSameFamily_MixtureSameFamily/log_prob/model_3_mixture_same_family_4_MixtureSameFamily_independent_normal_4_IndependentNormal_Independentmodel_3_mixture_same_family_4_MixtureSameFamily_independent_normal_4_IndependentNormal_Normal/log_prob/model_3_mixture_same_family_4_MixtureSameFamily_independent_normal_4_IndependentNormal_Normal/log_prob/truediv/RealDiv_1 (defined at <ipython-input-22-243a182981d9>:9) ]] [Op:__inference_train_function_7663]

Errors may have originated from an input operation.
Input Source operations connected to node gradient_tape/lambda/model_3_mixture_same_family_4_MixtureSameFamily_MixtureSameFamily/log_prob/model_3_mixture_same_family_4_MixtureSameFamily_independent_normal_4_IndependentNormal_Independentmodel_3_mixture_same_family_4_MixtureSameFamily_independent_normal_4_IndependentNormal_Normal/log_prob/model_3_mixture_same_family_4_MixtureSameFamily_independent_normal_4_IndependentNormal_Normal/log_prob/truediv/RealDiv_1:
 model_3/mixture_same_family_4/MixtureSameFamily/independent_normal_4/IndependentNormal/Softplus (defined at /usr/local/lib/python3.7/dist-packages/tensorflow_probability/python/layers/distribution_layer.py:988)

Function call stack:
train_function

我不确定如何更改源代码以使其适用于通道优先输入形状。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我在尝试重现您的错误时遇到此错误:InvalidArgumentError: Conv2DCustomBackpropInputOp only supports NHWC. [[node gradient_tape/model_7/sequential_17/conv2d_53/Conv2D/Conv2DBackpropInput (defined at &lt;ipython-input-10-cf6a820743f5&gt;:98) ]] [Op:__inference_train_function_30892]
  • @AlexandreMahdhaoui 请确保您可以访问 GPU。 Conv2DCustomBackpropInputOp 仅在设备类型 CPU 上支持 NHWC。
  • 我在运行 Gist 时收到了 TypeError: x and y must have the same dtype, got tf.bool != tf.float32。我正在使用 GPU,是否有特定的 TF 版本用于复制?如果你分享一个重现问题的 colab 链接会更容易。
  • @Frightera 我更新了要点。这是 Colab 的链接:colab.research.google.com/drive/…。没有特定的 TF 版本。我的是TF2.6。
  • @DushiFdz 我明天会检查这个,如果它与源代码有关,我会看看它。

标签: python tensorflow tensorflow-probability


【解决方案1】:

您的preprocess 函数返回image, image 而不是image, sample['label']。如果你改变它,它应该可以工作!

我认为你也可以在你的损失中放弃 K.cast。

更新:实际上,当我运行这个时,我让 nan 不知所措。可能还有其他问题。但至少它克服了形状错误! ?‍♂️

【讨论】:

  • 谢谢克里斯。我正在使用的数据具有形状 (4,128,128,128),倒数第二层是 (3,128,128,128)。如果我这样做,params_size = tfpl.MixtureSameFamily.params_size(num_components, component_params_size=tfpl.IndependentNormal.params_size(event_shape)) output_l = Conv3D(params_size, (1,1,1), padding="same",data_format="channels_first")(output_layer) output=tfpl.MixtureSameFamily(num_components, tfpl.IndependentNormal(event_shape))(output_l) 我会收到错误消息:重塑的输入是一个具有 18432000 个值的张量,但请求的形状需要 442368 的倍数
  • 嗯,你能分享另一个要点或(如果可能的话)一个复制这个的 colab 吗?很高兴看到并尝试提供帮助。
猜你喜欢
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 2016-03-25
  • 2018-12-13
  • 2022-08-04
  • 1970-01-01
相关资源
最近更新 更多