Keras 多层感知机 多类别的 softmax 分类模型代码

来源:互联网 发布:淘宝客服售前流程表 编辑:程序博客网 时间:2024/06/05 16:31

Multilayer Perceptron (MLP) for multi-class softmax classification:

from keras.models import Sequentialfrom keras.layers import Dense, Dropout, Activationfrom keras.optimizers import SGD# 生成随机数据import numpy as npx_train = np.random.random((1000, 20))y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)x_test = np.random.random((100, 20))y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)model = Sequential()# Dense(64) is a fully-connected layer with 64 hidden units.# in the first layer, you must specify the expected input data shape:# here, 20-dimensional vectors.model.add(Dense(64, activation='relu', input_dim=20))model.add(Dropout(0.5))model.add(Dense(64, activation='relu'))model.add(Dropout(0.5))model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)model.compile(loss='categorical_crossentropy',              optimizer=sgd,              metrics=['accuracy'])model.fit(x_train, y_train,          epochs=20,          batch_size=128)score = model.evaluate(x_test, y_test, batch_size=128)

https://keras.io/getting-started/sequential-model-guide/

更多教程:http://www.tensorflownews.com/

阅读全文
0 0
原创粉丝点击