[英]Tensorflow.keras: Shape of input is (), EVEN THOUGH SHAPE IS (768, 8)
I hope I am doing this right (first post).我希望我做对了(第一篇文章)。
I tried to use tensorflow.keras to classify the data from here .我尝试使用 tensorflow.keras 对来自这里的数据进行分类。 I am aware the input shape, shape of input data and shape of targets are important when passing arguments into tf.keras.Sequential.fit()我知道输入形状、输入数据形状和目标形状在将 arguments 传递到 tf.keras.Sequential.fit() 时很重要
The message I get is:我得到的信息是:
ValueError: Error when checking input: expected dense_159_input to have 2 dimensions, but got array with shape () ValueError:检查输入时出错:预期的dense_159_input有2维,但得到了形状为()的数组
So here is what I have done:所以这就是我所做的:
def loadDataset(file_name):
data = pd.read_csv("data/" + file_name)
# print(data.head(10))
data = data.to_numpy()
random.seed(20)
X = data[:, 0:8]
y = data[:, -1]
X = np.asarray(X).reshape(X.shape[0], X.shape[1])
X = tf.keras.utils.normalize(X, axis=0)
y = np.asarray(y).reshape(y.shape[0], 1)
return X, y
title = "datasets_228_482_diabetes.csv"
X, y = loadDataset(title)
print(X.shape)
print(y.shape)
(768, 8) (768, 1) (768, 8) (768, 1)
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
model = Sequential()
model.add(Dense(4, activation = "relu", input_shape=(8,)))
model.add(Dense(4, activation = "relu"))
model.add(Dense(1, activation = "sigmoid"))
model.compile(optimizer = "Adam",
loss = "binary_crossentropy",
metrics=["accuracy"])
model.fit(X, y, batch_size = 16, epochs = 1, validation_data = 0.1)
I tried making the shapes X (768, 8, 1) and y (768, 1, 1) instead in case that was the issue but then the error says it expected 2 dimensions but got three.我尝试制作形状 X (768, 8, 1) 和 y (768, 1, 1) 以防万一这是问题,但随后错误说它需要 2 个维度但得到了 3 个。 Which makes total sense to me.这对我来说完全有意义。 I just don't understand the error above saying that the input data X has no shape when X is of shape (768, 8).我只是不明白上面的错误是说输入数据 X 在 X 的形状为 (768, 8) 时没有形状。
Any help would be greatly appreciated!任何帮助将不胜感激! Cheers干杯
I would say that the error comes from validation_data
which is supposed to be as X
, some data shaped (..., 8)
.我会说错误来自validation_data
,它应该是X
,一些数据形状(..., 8)
。 Since you are passing 0.1
the dense layer doesn't understand what you are giving him.因为你通过了0.1
,所以密集层不明白你给他的是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.