Несоответствие в тензорном потоке 2.0 Модель прогнозирования и вызова методов. Вызов метода завершается ошибкой с InvalidArgumentError - PullRequest
1 голос
/ 02 марта 2020

Почему модель предсказывает работу, но модель (данные) не работает в tf 2.0 для следующего кода?

from tensorflow.keras import layers,Input
import tensorflow as tf
input_layer = Input(1)
d1 = layers.Dense(64, activation='relu')(input_layer)
d2 = layers.Dense(3, activation='relu')(d1)
model = tf.keras.Model(inputs=[input_layer], outputs=d2)
data = [[1.0]]
print(model.predict(data)) # Works
print(model(data)) # fails with  tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [] [Op:MatMul]

1 Ответ

1 голос
/ 24 марта 2020

Модель TensorFlow принимает тензоры только при вызове во время Стремительное выполнение , как указано в их репозитории GitHub. Это одна строк, которые были подняты при выполнении model(data).

# Eager execution on data tensors.
        with backend.name_scope(self._name_scope()):
          self._maybe_build(inputs)
          cast_inputs = self._maybe_cast_inputs(inputs)
          with base_layer_utils.autocast_context_manager(
              self._compute_dtype):
            outputs = self.call(cast_inputs, *args, **kwargs)  # <<< ERROR HERE
          self._handle_activity_regularization(inputs, outputs)
          self._set_mask_metadata(inputs, outputs, input_masks)

Я преобразовал переменную данных , которую вы использовали для прогнозирования, в Переменная Tensor .

См. Модифицированный код ниже:

from tensorflow.keras import layers, Input
import tensorflow as tf

input_layer = Input(1)
d1 = layers.Dense(64, activation='relu')(input_layer)
d2 = layers.Dense(3, activation='relu')(d1)
model = tf.keras.Model(inputs=[input_layer], outputs=d2)
data = [[1.0]]
print(model.predict(data)) # [[0.02674201 0.         0.        ]]
print(model(tf.Variable(data))) # tf.Tensor([[0.02674201 0.         0.        ]], shape=(1, 3), dtype=float32)

Вы можете просмотреть исходный код в TensorFlow Github .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...