我在Keras中使用自编码器。我希望打乱训练数据x_train
,使得自编码器能够重构来自同一类别的不同样本。这是可能的吗?
model_train = autoencoder.fit(x_train, x_train, batch_size=32, epochs=1000, shuffle=True, callbacks=[checkpoint, early_stopping], validation_data=(x_test, x_test))
我认为shuffle=True
是打乱x_train
并基于相同的配对计算损失,这并不是我想要的效果。
回答:
这是可能的,但Keras不会为你这样做,因为它会同时打乱数据和标签。假设你已经有了标签,我发现这个函数对你的目的非常有用:
import numpy as npdef create_pairs(data, labels): # 排除批次维度 pairs = np.empty(0, 2, *data.shape[1:]) for label in np.unique(labels): idxs = np.where(labels == label)[0] # 索引必须是偶数以便创建配对 idxs = idxs if len(idxs) % 2 == 0 else idxs[:-1] np.random.shuffle(idxs) samples = data[idxs].reshape((-1, 2, *data.shape[1:])) pairs = np.vstack((pairs, samples)) return pairs[:, 0], pairs[:, 1]
现在数据已被打乱并分成配对,你可以训练你的模型:
x_train, y_train = create_pairs(data, labels)history = model.fit( x_train, y_train, batch_size=32, epochs=1000, shuffle=True, callbacks=[checkpoint, early_stopping], validation_split=0.2)