【发布时间】:2018-01-14 15:41:04
【问题描述】:
我正在尝试使用自动编码器和 Keras 检测欺诈行为。我将以下代码编写为Notebook:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.preprocessing import StandardScaler
from keras.layers import Input, Dense
from keras.models import Model
import matplotlib.pyplot as plt
data = pd.read_csv('../input/creditcard.csv')
data['normAmount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
data = data.drop(['Time','Amount'],axis=1)
data = data[data.Class != 1]
X = data.loc[:, data.columns != 'Class']
encodingDim = 7
inputShape = X.shape[1]
inputData = Input(shape=(inputShape,))
X = X.as_matrix()
encoded = Dense(encodingDim, activation='relu')(inputData)
decoded = Dense(inputShape, activation='sigmoid')(encoded)
autoencoder = Model(inputData, decoded)
encoder = Model(inputData, encoded)
encodedInput = Input(shape=(encodingDim,))
decoderLayer = autoencoder.layers[-1]
decoder = Model(encodedInput, decoderLayer(encodedInput))
autoencoder.summary()
autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = autoencoder.fit(X, X,
epochs=10,
batch_size=256,
validation_split=0.33)
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
我可能遗漏了一些东西,我的准确率停留在 0 并且我的测试损失低于我的火车损失。
任何见解都会得到满足
【问题讨论】:
-
自动编码器通常做回归,在回归问题上使用准确度没有意义。
标签: python tensorflow deep-learning keras autoencoder