Python tensorflow 2.0模型预测和调用方法中的不一致性。调用方法失败,返回InvalidArgumentError
为什么在TF2.0中,对于以下代码,模型预测工作,但模型(数据)失败Python tensorflow 2.0模型预测和调用方法中的不一致性。调用方法失败,返回InvalidArgumentError,python,tensorflow,tensorflow2.0,tf.keras,Python,Tensorflow,Tensorflow2.0,Tf.keras,为什么在TF2.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(input
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]
TensorFlow模型只接受在其GitHub存储库中所示的急切执行期间调用的张量。 这是执行
model(data)
时引发的行之一
您可以在TensorFlow中查看源代码
# 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)
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)