当我将堆叠自动编码器应用为特征选择时出现此错误。
<块引用>ValueError: logits 和标签必须具有相同的形状 ((None, 20) vs (None, 1))
这是堆叠自动编码器的代码::
import tensorflow
from tensorflow.keras import layers
from tensorflow.keras import activations
from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Model
from sklearn.preprocessing import MinMaxScaler
def StackedAutoencoder(x_train, x_test, y_train, y_test):
scalar = MinMaxScaler()
x_train = scalar.fit_transform(x_train)
x_test = scalar.fit_transform(x_test)
input_size = x_train.shape[1]
hidden_size = 9
code_size = 3
input_data = Input(shape=(input_size,))
hidden_1 = Dense(hidden_size, activation='relu')(input_data)
code = Dense(code_size, activation='relu')(hidden_1)
hidden_2 = Dense(hidden_size, activation='relu')(code)
# decode
output_data = Dense(input_size, activation='sigmoid')(hidden_2)
autoencoder = Model(input_data, output_data)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, y_train, epochs=30, verbose=0,
batch_size=10, validation_data=(x_test, y_test))
reconstructions = autoencoder.predict(x_test)
return reconstructions
这是我在 main 中调用算法的代码:
import SAE as FS # SAE the file of stacked-autoencoder algorithm
print('CurrentFeatureSelection = ', 'Stacked_Autoencoder')
X_train_fs, X_test_fs = FS.StackedAutoencoder(X_train, X_test, y_train, y_test)