keras基于多层感知器的softmax多分类

Posted kerly-python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keras基于多层感知器的softmax多分类相关的知识,希望对你有一定的参考价值。

# 基于多层感知器的softmax多分类:
```
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import numpy as np

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_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)
```

# MLP的二分类:
```
import numpy as np
from keras.models import Sequential
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)
```
# 类似VGG的卷积神经网络:
```
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD

x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)

model = Sequential()

model.add(Conv2D(32, (3, 3), activation=‘relu‘, input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation=‘relu‘))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation=‘relu‘))
model.add(Conv2D(64, (3, 3), activation=‘relu‘))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(256, 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)

model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)
```
# 使用LSTM的序列分类
```
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import LSTM

model = Sequential()
model.add(Embedding(max_features, output_dim=256))
model.add(LSTM(128))
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, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)
```

# 使用1D卷积的序列分类
```
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D

model = Sequential()
model.add(Conv1D(64, 3, activation=‘relu‘, input_shape=(seq_length, 100)))
model.add(Conv1D(64, 3, activation=‘relu‘))
model.add(MaxPooling1D(3))
model.add(Conv1D(128, 3, activation=‘relu‘))
model.add(Conv1D(128, 3, activation=‘relu‘))
model.add(GlobalAveragePooling1D())
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, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)
```

以上是关于keras基于多层感知器的softmax多分类的主要内容,如果未能解决你的问题,请参考以下文章

(数据科学学习手札44)在Keras中训练多层感知机

theano-windows学习笔记十——多层感知机手写数字分类

多层感知机的从零开始实现( 从D2L 包中抽取函数)

多层感知机的从零开始实现( 从D2L 包中抽取函数)

小白学习keras教程一基于波士顿住房数据集训练简单的MLP回归模型

感知机+激活函数+多层感知机的实现