keras 例子

基于多层感知机的softmax分类

from keras.model import Sequential
from keras.layres import Dense,Dropout,Activation
from keras.optimizers import SGD
# create dummy data 
import numpy as np
x_train=np.random.random((1000,20))
y_train=keras.utils.to_categorical(np.random.randint(10,size=(10000,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.1,decay=1e-6,momentum=0.9,nesterov=T)
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.model 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))
mode.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation="sigmod))
model.compile(loss="binary_crossentory",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)

卷积神经网络

import numpy as np
from keras.model import Sequential
from keras.layes import Dense,Dropout,Activation,Flatten
from keras.layes import Conv2D,MaxPooling2D
from keras.optimizers import SGD
 
x_train=np.random.random((100,100,100,3))
y_train=keras.utilis.to_categorical(np.random.randint(10,size=(100,1)),num_classes=10)
x_test=np.random.random((20,100,100,3))
y_test=keras.utilis.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(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=T)
model.compile(loss='categorical_crossentropy',optimizer=sgd)

model.fit(x_train,y_train,batch_size=32,epochs=10)
score=model.evalution(x_test,y_test,batch_size=32)

使用LSTM的序列分类

from keras.model import Sequential
from keras.layers import Dense,Dropout
from keras.layes import Embedding
from keras.layes 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='sigmod'))
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])

model.fit(x_train,y_train,batch_size=16,epoch=10)
score=model.evaluate(x_test,y_test,batch_size  =16)

使用ID卷积的序列分类

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='sigmod'))

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)

    原文作者:Liam_ml
    原文地址: https://www.jianshu.com/p/875e2a5a3326
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞