from keras.models import Sequential, Model
时间: 2024-01-03 20:03:35 浏览: 78
3.2.Keras实战.pptx
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Input
# create a sequential model
model = Sequential()
# add layers to the model
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
# create an input tensor
input_tensor = Input(shape=(28,28,1))
# create a model using functional API
x = Conv2D(32, kernel_size=(3,3), activation='relu')(input_tensor)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Conv2D(64, kernel_size=(3,3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
output_tensor = Dense(10, activation='softmax')(x)
model = Model(inputs=input_tensor, outputs=output_tensor)
阅读全文