基于多层感知器(MLP)的多分类和二分类

基于多层感知器的二分类

序贯模型

'''
import numpy as np
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train, y_train,
epochs=20,
batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
print('score1',score)
'''

函数式模型

'''
input_1 = Input(shape=(20,))
x = Dense(64,activation='relu')(input_1)
x = Dropout(0.5)(x)
x = Dense(64,activation='relu')(x)
x = Dropout(0.5)(x)
output_1 = Dense(1,activation='softmax')(x)
model = Model(input_1,output_1)
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train, y_train,
epochs=20,
batch_size=128)
score1 = model.evaluate(x_test, y_test, batch_size=128)
'''

基于多层感知器的多分类

序贯式模型

'''
import numpy as np
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout
from keras.optimizers import SGD
import numpy as np
import keras
x_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()
model.add(Dense(64, activation='relu', input_shape=(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)
'''

函数式模型

'''
input_1 = Input(shape=(20,))
x = Dense(64,activation='relu')(input_1)
x = Dropout(0.5)(x)
x = Dense(64,activation='relu')(x)
x = Dropout(0.5)(x)
output_1 = Dense(10,activation='softmax')(x)
model = Model(input_1,output_1)
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)
score1 = model.evaluate(x_test, y_test, batch_size=128)
'''

你可能感兴趣的:(基于多层感知器(MLP)的多分类和二分类)