TensorflowReshapeがTypeErrorで失敗する:Pythonの `bool`として` tf.Tensor`を使用することは許可されていません

ジャスミン

埋め込みを表すテンソルがあり、形状を変更したいのですが、次のエラーで失敗します。

TypeError:tf.TensorPythonとしてaを使用するboolことは許可されていません。if t is not None:代わりにif t:使用してテンソルが定義されているかどうかをテストし、tf.condなどのTensorFlow opsを使用して、テンソルの値を条件とするサブグラフを実行します。

形を変えるために使用するコード:

embedding_feature = \
    Reshape((tf.shape(embedding_feature)[0],
             10, 20, tf.shape(embedding_feature)[2])) \
        (embedding_feature)

およびembedding_feature:

Tensor("tags_embedding/GatherV2:0", shape=(?, 200, 60), dtype=float32)

tf.shape()ここで他の質問で説明されているように、テンソルの動的形状をキャプチャするために使用しています。

https://github.com/tensorflow/tensorflow/issues/7253

複数の「なし」次元でテンソルを再形成する方法は?

完全なトレースバック:

Traceback (most recent call last):
  File "/usr/lib/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/eleni/Desktop/recommender/recommender/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5285, in get_controller
    yield g
  File "/home/eleni/Desktop/recommender/recommender/venv/lib/python3.6/site-packages/keras/engine/topology.py", line 622, in __call__
    output_shape = self.compute_output_shape(input_shape)
  File "/home/eleni/Desktop/recommender/recommender/venv/lib/python3.6/site-packages/keras/layers/core.py", line 390, in compute_output_shape
    input_shape[1:], self.target_shape)
  File "/home/eleni/Desktop/recommender/recommender/venv/lib/python3.6/site-packages/keras/layers/core.py", line 364, in _fix_unknown_dimension
    if dim < 0:
  File "/home/eleni/Desktop/recommender/recommender/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 665, in __bool__
    raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

どうすればこれを修正できますか?

jdehesa

Reshape層は、入力として(また、層に与えられた寸法は、バッチ寸法を含めるべきでない)整数の組を必要とします。事前にわからない形状に変形したい場合は、Lambdaレイヤーを使用します

from keras.layers import Lambda
import keras.backend as K

# ...
embedding_feature = Lambda(
     lambda x: K.reshape(x, [K.shape(x)[0], 10, 20, K.shape(x)[2]]))(embedding_feature)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ