分类 ANN 停留在 60%
Classification ANN stuck at 60%
我正在尝试在 10,000 的数据集上创建二元分类器。我尝试了多个激活器和优化器,但结果总是在 56.8% 和 58.9% 之间。考虑到几十次迭代中相当稳定的结果,我认为问题是:
- 我的数据集不可分类
- 我的模型坏了
这是数据集:training-set.csv
我可能还能获得 2000 条记录,但仅此而已。
我的问题是:我的模型构建方式中是否有什么东西阻止它学习到更高的程度?
请注意,我很高兴根据需要拥有尽可能多的层和节点,并且时间不是生成模型的因素。
dataframe = pandas.read_csv(r"training-set.csv", index_col=None)
dataset = dataframe.values
X = dataset[:,0:48].astype(float)
Y = dataset[:,48]
#count the input variables
col_count = X.shape[1]
#normalize X
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_scale = sc_X.fit_transform(X)
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_scale, Y, test_size = 0.2)
# define baseline model
activator = 'linear' #'relu' 'sigmoid' 'softmax' 'exponential' 'linear' 'tanh'
#opt = 'Adadelta' #adam SGD nadam RMSprop Adadelta
nodes = 1000
max_layers = 2
max_epochs = 100
max_batch = 32
loss_funct = 'binary_crossentropy' #for binary
last_act = 'sigmoid' # 'softmax' 'sigmoid' 'relu'
def baseline_model():
# create model
model = Sequential()
model.add(Dense(nodes, input_dim=col_count, activation=activator))
for x in range(0, max_layers):
model.add(Dropout(0.2))
model.add(Dense(nodes, input_dim=nodes, activation=activator))
#model.add(BatchNormalization())
model.add(Dense(1, activation=last_act)) #model.add(Dense(1, activation=last_act))
# Compile model
adam = Adam(lr=0.001)
model.compile(loss=loss_funct, optimizer=adam, metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=max_epochs, batch_size=max_batch)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
#confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
score = np.sum(cm.diagonal())/float(np.sum(cm))
两点:
绝对没有意义用线性激活堆叠致密层——它们只会产生一个线性单元;更改为 activator = 'relu'
(并且不要理会注释列表中的其他候选激活函数)。
不要不要默认使用dropout,尤其是当你的模型在学习上有困难时(就像这里);移除 dropout 层,并准备好将它们(其中一些)放回去,以防万一你看到过度拟合(你目前离那个点还很远,所以这不是什么值得担心的事情 现在).
我正在尝试在 10,000 的数据集上创建二元分类器。我尝试了多个激活器和优化器,但结果总是在 56.8% 和 58.9% 之间。考虑到几十次迭代中相当稳定的结果,我认为问题是:
- 我的数据集不可分类
- 我的模型坏了
这是数据集:training-set.csv
我可能还能获得 2000 条记录,但仅此而已。
我的问题是:我的模型构建方式中是否有什么东西阻止它学习到更高的程度?
请注意,我很高兴根据需要拥有尽可能多的层和节点,并且时间不是生成模型的因素。
dataframe = pandas.read_csv(r"training-set.csv", index_col=None)
dataset = dataframe.values
X = dataset[:,0:48].astype(float)
Y = dataset[:,48]
#count the input variables
col_count = X.shape[1]
#normalize X
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_scale = sc_X.fit_transform(X)
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_scale, Y, test_size = 0.2)
# define baseline model
activator = 'linear' #'relu' 'sigmoid' 'softmax' 'exponential' 'linear' 'tanh'
#opt = 'Adadelta' #adam SGD nadam RMSprop Adadelta
nodes = 1000
max_layers = 2
max_epochs = 100
max_batch = 32
loss_funct = 'binary_crossentropy' #for binary
last_act = 'sigmoid' # 'softmax' 'sigmoid' 'relu'
def baseline_model():
# create model
model = Sequential()
model.add(Dense(nodes, input_dim=col_count, activation=activator))
for x in range(0, max_layers):
model.add(Dropout(0.2))
model.add(Dense(nodes, input_dim=nodes, activation=activator))
#model.add(BatchNormalization())
model.add(Dense(1, activation=last_act)) #model.add(Dense(1, activation=last_act))
# Compile model
adam = Adam(lr=0.001)
model.compile(loss=loss_funct, optimizer=adam, metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=max_epochs, batch_size=max_batch)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
#confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
score = np.sum(cm.diagonal())/float(np.sum(cm))
两点:
绝对没有意义用线性激活堆叠致密层——它们只会产生一个线性单元;更改为
activator = 'relu'
(并且不要理会注释列表中的其他候选激活函数)。不要不要默认使用dropout,尤其是当你的模型在学习上有困难时(就像这里);移除 dropout 层,并准备好将它们(其中一些)放回去,以防万一你看到过度拟合(你目前离那个点还很远,所以这不是什么值得担心的事情 现在).