英文:
ValueError: Found Shape in Keras Does Not Match Input Shape
问题
以下是翻译好的部分:
我正在使用YouTube上的教程。我对它进行了一些修改,尽管代码基本相同,但我收到了一个错误消息,说找到的形状与输入形状不匹配
```python
ValueError: Layer "sequential"的输入0与该层不兼容:预期形状=(None, 621, 17),找到形状=(None, 17)
导致此错误的代码如下
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
datasetFull = pd.read_csv('College_Data.csv')
dataset = datasetFull.drop("Name", axis=1)
x = dataset.drop("Private", axis=1)
y = dataset["Private"]
y.replace('Yes', 1)
y.replace('No', 0)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
print(x_train.shape)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(256, input_shape=x_train.shape, activation='sigmoid'))
model.add(tf.keras.layers.Dense(256, activation='sigmoid'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
tf.reshape(x_train, [621, 17])
print(x_train.shape)
model.fit(x_train, y_train, epochs=1000)
不确定是否相关,但我还收到了看起来并没有真正停止进程的错误消息。
此TensorFlow二进制文件已经优化以在性能关键操作中使用可用的CPU指令。
要启用以下指令:SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA,在其他操作中重新构建TensorFlow并使用适当的编译器标志。
正如您所看到的,我尝试在将其用作输入形状之前以及将其提交给"fit"之前打印输入数据的形状。
我还尝试在将输入数据提交给Keras之前重新整形输入数据,使用了tf.reshape(x_train, [621, 17])
无论我做了什么,都会出现相同的错误:
ValueError: Layer "sequential"的输入0与该层不兼容:预期形状=(None, 621, 17),找到形状=(None, 17)
英文:
I am using a tutorial on youtube. I took my own spin on it and although the code is essentially identical, I am getting an error message saying that the found shape does not match input shape
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 621, 17), found shape=(None, 17)
The code leading to this looks like this
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
datasetFull = pd.read_csv('College_Data.csv')
dataset = datasetFull.drop("Name", axis=1)
x = dataset.drop("Private", axis=1)
y = dataset["Private"]
y.replace('Yes', 1)
y.replace('No', 0)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
print(x_train.shape)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(256, input_shape=x_train.shape, activation='sigmoid'))
model.add(tf.keras.layers.Dense(256, activation='sigmoid'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
tf.reshape(x_train, [621, 17])
print(x_train.shape)
model.fit(x_train, y_train, epochs=1000)
Not sure if this is relevant, but I also get error messages like this that do not seem to actually stop the process.
This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
As you can see, I tried to print
the shape of the input data before I take it as the input shape and before I submit it to fit
I also tried reshaping the input data right before submitting it to Keras with tf.reshape(x_train, [621, 17])
No matter what I did/do the same error always comes up:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 621, 17), found shape=(None, 17)
答案1
得分: 0
以下是翻译好的部分:
修复方法:
model.add(tf.keras.layers.Dense(256, input_shape=(x_train.shape[1], ), activation='sigmoid'))
输入形状必须是可迭代的,并表示独立变量的数量,或者简单来说(在您的情况下),x_train 中的列数。
改进:
不要使用以下代码:
y = dataset["Private"]
y.replace('Yes', 1)
y.replace('No', 0)
使用 LabelEncoder,其附加优势是简单、高效且易于将字符串转换为数字,反之亦然。而且,我不确定您是否真的通过 y.replace('Yes', 1)
将字符串转换为数字。您没有将更新后的值设置回 y。根据我的理解,应该是 y = y.replace('Yes', 1)
。对于替换 "no" 也是同样的情况。
MLP 中的输入和输出形状:
您可以参考 此链接 了解多层感知器模型的输入和输出形状。
Tensorflow 的警告:
其他错误消息不相关,只是一些无害的警告;您可以参考 此回答,其中对其进行了很好地解释。
英文:
A quick fix would be -
model.add(tf.keras.layers.Dense(256, input_shape=(x_train.shape[1], ), activation='sigmoid'))
The input shape must be an iterable and represent the number of independent variables, or in simple terms (IN YOUR CASE), the number of columns in x_train
Improvements
Rather than using -
y = dataset["Private"]
y.replace('Yes', 1)
y.replace('No', 0)
use LabelEncoder, the added advantage would be simple, efficient and easy to convert from strings to numbers and vice versa. Also, I am not sure if you are actually converting the strings into numbers just by using y.replace('Yes', 1)
. You are not setting the updated values back into y. According to me, it should have been y = y.replace('Yes', 1)
. Same for replacing "no"
Input and Output Shapes in MLP
You can refer this to know more about input and output shapes of multilayer perceptrons.
Warnings by Tensorflow
Those other error messages are not relevant but some harmless warnings; you can refer to this answer which explains it beautifully.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论