使用keras进行多类别分类 [英] Multi-class classification using keras

查看:1009
本文介绍了使用keras进行多类别分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个神经网络,以便对使用k均值预先计算的类别进行分类.

I am developing a neural network in order to classify with classes pre-calculated with k-means.

数据集如下:

50,12500,2,1,5
50,8500,2,1,15
50,6000,2,1,9
50,8500,2,1,15

结果行是最后一行. 这是我尝试使用 Keras 的Python代码:

Where resulting row is the last row. Here is the code on Python with Keras I am trying to get working:

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline

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

# load dataset
dataset = numpy.genfromtxt ('../r-calculations/k-means/output16.csv', delimiter=",")
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
print(Y[0])
Y = np_utils.to_categorical(Y)

model = Sequential()
model.add(Dense(5, activation='tanh', input_dim=4))
#model.add(Dropout(0.25))
model.add(Dense(10, activation='tanh'))
#model.add(Dropout(0.25))
model.add(Dense(10, activation='relu'))
#model.add(Dropout(0.25))
model.add(Dense(17, activation='softmax'))

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.fit(X,Y, epochs=10, batch_size=10)
#print( model.predict(numpy.array([2,36,2,5,2384,1,2,4,3,1,1,4,33,3,1,1,2,1,1,1]).reshape((1,20))) )
#print( model.predict(numpy.array(X[0]).reshape((1,4))) )
#print( model.predict(numpy.array(X[1]).reshape((1,4))) )
#print( model.predict(numpy.array(X[2]).reshape((1,4))) )
result = model.predict(numpy.array(X[0]).reshape((1,4)))
for res in result[0]:
    print res

如果我做对了,现在我将获得每个类的概率作为输出.在其上调用"to_categorical"后,如何找回标签?

If I get it right, now I am getting a probability for each class as an output. How can I retrieve labels back after I have called "to_categorical" on it?

是否有一种获取班级编号的方法,而不是每个班级的概率?

Is there a way to get a class number, instead of probability for each class?

目前看来,它似乎工作不正常,损失很大,约为2,准确性约为0.29,我无法使其收敛.我在做什么错了?

For now it does not seem to be working right, big loss ~2, accuracy ~0.29 and I cannot make it to converge. What am I doing wrong?

更新3月19日 到目前为止,我已经解决了我的问题,我改变了很多次模型,终于找到了有效的配置.

UPDATE Mar 19 So far I have solved my problem, I changed my model a lot of times and finally found working configuration.

推荐答案

如果您想使用该类而不是概率,可以在预测时调用numpy argmax.

If you want the class instead of the probability you could call numpy argmax at your predictions.

或者使用方便的调用Forecast_classes而不是预测

Or use the convenient call predict_classes instead of predict

result = model.predict_classes(numpy.array(X[0]).reshape((1,4)))

对于您的结果,您可以尝试运行一些额外的时期,但是很难说出什么地方出了问题.可能是您的训练数据质量,错误的初始化,没有足够的数据,错误的模型(我只会使用relu激活).

As for your result, you could try running a few extra epochs, but it is hard to say what is wrong. Could be your training data quality, bad initialization, not having enough data, bad model (i'd use only relu activations).

这篇关于使用keras进行多类别分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆