python - 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,))
解决方案
尝试:
predictions = model.predict(features)
推荐阅读
- php - PHP num_rows 输出给出了意想不到的结果
- android - 在 Fire TV Stick 上侧载应用时失败 [INSTALL_FAILED_DEXOPT] 错误
- c# - 无法通过 GoDaddy SMTP 发送电子邮件
- spring - 如何更新 Spring 数据中的选择列?
- elasticsearch - 分析文章,查找指定语言的城市
- ios - 将 PDFDocument 保存为 CoreData 作为 NSData(如何将其转换为 NSData?)
- visual-studio-code - 是否可以将 vscode 终端模拟器的工作目录与编辑器的工作目录同步?
- javascript - Jquery用换行符填充文本区域,如果没有数据则什么都没有
- c# - 带有复选框的 Wpf TreeView
- java - Android扩展按钮检测onClick