首页 > 解决方案 > Tensorflow 错误:In[0] 不是矩阵 [Op:MatMul]

问题描述

我正在尝试使用 TensorFlow 对一些数字数据进行分类。我正在编写教程,但从 csv 中导入了一些其他数据。跑线

predictions = model(features)

导致错误

InvalidArgumentError: In[0] is not a matrix [Op:MatMul]

当特征定义为

<tf.Tensor: id=8, shape=(1000,), dtype=float32, numpy=array([-0.15328342,...)

标签为

<tf.Tensor: id=9, shape=(), dtype=string, numpy=b'left'>

我的代码是

tf.enable_eager_execution()
...
X_train, X_test, y_train, y_test =
train_test_split(df.drop(['label'], axis=1), df["label"], test_size=0.25, random_state=42)

dataset = 
tf.data.Dataset.from_tensor_slices(((X_train.values,y_train.values)))
dataset.label_name=label_name
dataset.column_names=column_names
dataset.num_epochs=1

features, labels = next(iter(dataset))

model = tf.keras.Sequential([
tf.keras.layers.Dense(2000, activation=tf.nn.relu, input_shape=(1000,)),  
tf.keras.layers.Dense(2000, activation=tf.nn.relu),
tf.keras.layers.Dense(1000)
])

predictions = model(features)
predictions[:1000]

变化

predictions = model.predict(features)

Error when checking input: expected dense_24_input to have shape (1000,) but got array with shape (1,)

看起来定义我的张量有问题。

print("Features: {}".format(feature_names))
print("Label: {}".format(label_name))

返回

Features: Index(['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10',
   ...
   'v991', 'v992', 'v993', 'v994', 'v995', 'v996', 'v997', 'v998', 'v999',
   'v1000'],
  dtype='object', length=1000)
Label: label

但是只有一个没有名称的张量 (shape=(1000,))

标签: pythontensorflowkeras

解决方案


尝试:

predictions = model.predict(features)

推荐阅读