使用Keras进行二进制分类:性能不佳

时间:2016-12-29 03:48:17

标签: python performance classification keras

我正在尝试使用Keras开发用于二进制分类目的的多层感知器,并且我对我获得的(差)性能感到惊讶(训练集的准确率为57%)。逻辑回归正确地对100%的样本进行分类。

我创建了一个带有2个输入的数据集:输入A是一个正弦函数。输入B =输入A加滞后。当输入A> =输入B时,输出= 1.否则,输出= 0.

SIN SIN-1   Direction
0   0   1
0.06279052  0   1
0.125333234 0.06279052  1
0.187381315 0.125333234 1
0.248689887 0.187381315 1
0.309016994 0.248689887 1
0.368124553 0.309016994 1
0.425779292 0.368124553 1
0.481753674 0.425779292 1
0.535826795 0.481753674 1
0.587785252 0.535826795 1
0.63742399  0.587785252 1

我遇到的问题类似于" Keras低精度分类任务"中描述的问题。答案指向数据集的方向,我不相信这里的问题。

见下面的代码。为了提高模型的准确性,我缺少什么?将神经元添加到图层或将输出图层的激活函数更改为softmax并没有产生任何更好的结果。

import numpy
import pandas
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load the dataset
dataframe = pandas.read_csv('SIN.csv', usecols=[0,1,2], engine='python')
dataset = dataframe.values

X = dataset[:,0:2].astype(float)
Y = dataset[:,2].astype(int)

# split into train and test sets
train_size = int(len(X) * 0.80)
test_size = len(X) - train_size
Xtrain, Xtest = X[0:train_size,:], X[train_size:len(X),:]
Ytrain, Ytest = Y[0:train_size], Y[train_size:len(Y)]


print(len(Xtrain), len(Xtest))

# create and fit Multilayer Perceptron model
model = Sequential()
model.add(Dense(2, input_dim=2, init='uniform', activation='relu'))
model.add(Dense(2, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(Xtrain, Ytrain, nb_epoch=20, batch_size=2, verbose=2)

# Estimate model performance
trainScore = model.evaluate(Xtrain, Ytrain, verbose=2)
print('Train Score: %.2f' % trainScore[1])
testScore = model.evaluate(Xtest, Ytest, verbose=2)
print('Test Score: %.2f' % testScore[1])

0 个答案:

没有答案