Tensorflow 小型MLP的Keras层形状不相容性
我有一个简单的MLP内置Keras。我输入的形状是:Tensorflow 小型MLP的Keras层形状不相容性,tensorflow,machine-learning,keras,mlp,Tensorflow,Machine Learning,Keras,Mlp,我有一个简单的MLP内置Keras。我输入的形状是: X_train.shape - (6, 5) Y_train.shape - 6 创建模型 这给了我一个错误: ValueError: Error when checking input: expected dense_1_input to have shape (6,) but got array with shape (5,). 我有两个问题: 为什么会出现上述错误,如何解决 output=model.layers[-1]。o
X_train.shape - (6, 5)
Y_train.shape - 6
创建模型
这给了我一个错误:
ValueError: Error when checking input: expected dense_1_input to have shape (6,) but got array with shape (5,).
我有两个问题:
output=model.layers[-1]。output
是返回给定输入向量的softmax向量的方法吗?我在凯拉斯从来没有这样做过在输入层中,使用input_shape=(X_train.shape[1]),而最后一层的维度必须等于要预测的类数 返回softmax向量的方法是model.predict(X) 这里有一个完整的例子
n_sample = 5
n_class = 2
X = np.random.uniform(0,1, (n_sample,6))
y = np.random.randint(0,n_class, n_sample)
model = Sequential()
model.add(Dense(32, input_shape=(X.shape[1],), activation='relu'))
model.add(Dense(n_class, activation='softmax'))
# Compile and fit
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=10, batch_size=1, verbose=1)
# Get output vector from softmax
model.predict(X)
你好我的例子中的问题是n_sample=n_classes.pass n_sample在最后一个稠密层中(这是一个非常奇怪的问题,我不希望有好的结果)
n_sample = 5
n_class = 2
X = np.random.uniform(0,1, (n_sample,6))
y = np.random.randint(0,n_class, n_sample)
model = Sequential()
model.add(Dense(32, input_shape=(X.shape[1],), activation='relu'))
model.add(Dense(n_class, activation='softmax'))
# Compile and fit
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=10, batch_size=1, verbose=1)
# Get output vector from softmax
model.predict(X)