本文整理汇总了Python中keras.layers.core.Flatten方法的典型用法代码示例。如果您正苦于以下问题:Python core.Flatten方法的具体用法?Python core.Flatten怎么用?Python core.Flatten使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers.core
的用法示例。
在下文中一共展示了core.Flatten方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_residual_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def get_residual_model(is_mnist=True, img_channels=1, img_rows=28, img_cols=28):
model = keras.models.Sequential()
first_layer_channel = 128
if is_mnist: # size to be changed to 32,32
model.add(ZeroPadding2D((2,2), input_shape=(img_channels, img_rows, img_cols))) # resize (28,28)-->(32,32)
# the first conv
model.add(Convolution2D(first_layer_channel, 3, 3, border_mode='same'))
else:
model.add(Convolution2D(first_layer_channel, 3, 3, border_mode='same', input_shape=(img_channels, img_rows, img_cols)))
model.add(Activation('relu'))
# [residual-based Conv layers]
residual_blocks = design_for_residual_blocks(num_channel_input=first_layer_channel)
model.add(residual_blocks)
model.add(BatchNormalization(axis=1))
model.add(Activation('relu'))
# [Classifier]
model.add(Flatten())
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
# [END]
return model
示例2: model_create
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def model_create(input_shape, num_classes):
logging.debug('input_shape {}'.format(input_shape))
model = Sequential()
model.add(Conv2D(32, (3, 3), border_mode='same', input_shape=input_shape))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# use binary_crossentropy if has just 2 prediction yes or no
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
return model
示例3: test_img_clf
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def test_img_clf(self):
print('image classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(3, 32, 32),
classification=True, nb_class=2)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Convolution2D(32, 3, 32, 32))
model.add(Activation('sigmoid'))
model.add(Flatten())
model.add(Dense(32, y_test.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2)
self.assertTrue(history.history['val_acc'][-1] > 0.9)
示例4: value_distribution_network
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def value_distribution_network(input_shape, num_atoms, action_size, learning_rate):
"""Model Value Distribution
With States as inputs and output Probability Distributions for all Actions
"""
state_input = Input(shape=(input_shape))
cnn_feature = Convolution2D(32, 8, 8, subsample=(4,4), activation='relu')(state_input)
cnn_feature = Convolution2D(64, 4, 4, subsample=(2,2), activation='relu')(cnn_feature)
cnn_feature = Convolution2D(64, 3, 3, activation='relu')(cnn_feature)
cnn_feature = Flatten()(cnn_feature)
cnn_feature = Dense(512, activation='relu')(cnn_feature)
distribution_list = []
for i in range(action_size):
distribution_list.append(Dense(num_atoms, activation='softmax')(cnn_feature))
model = Model(input=state_input, output=distribution_list)
adam = Adam(lr=learning_rate)
model.compile(loss='categorical_crossentropy',optimizer=adam)
return model
示例5: cnn_2D
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def cnn_2D(self, input_shape, modual=''):
#建立Sequential模型
model_in = Input(input_shape)
model = Conv2D(
filters = 6,
kernel_size = (3, 3),
input_shape = input_shape,
activation='relu',
kernel_initializer='he_normal',
name = modual+'conv1'
)(model_in)# now 30x30x6
model = MaxPooling2D(pool_size=(2,2))(model)# now 15x15x6
model = Conv2D(
filters = 8,
kernel_size = (4, 4),
activation='relu',
kernel_initializer='he_normal',
name = modual+'conv2'
)(model)# now 12x12x8
model = MaxPooling2D(pool_size=(2,2))(model)# now 6x6x8
model = Flatten()(model)
model = Dropout(0.5)(model)
model_out = Dense(100, activation='relu', name = modual+'fc1')(model)
return model_in, model_out
示例6: cnn_3D
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def cnn_3D(self, input_shape, modual=''):
#建立Sequential模型
model_in = Input(input_shape)
model = Convolution3D(
filters = 6,
kernel_size = (3, 3, 3),
input_shape = input_shape,
activation='relu',
kernel_initializer='he_normal',
name = modual+'conv1'
)(model_in)# now 30x30x3x6
model = MaxPooling3D(pool_size=(2,2,1))(model)# now 15x15x3x6
model = Convolution3D(
filters = 8,
kernel_size = (4, 4, 3),
activation='relu',
kernel_initializer='he_normal',
name = modual+'conv2'
)(model)# now 12x12x1x8
model = MaxPooling3D(pool_size=(2,2,1))(model)# now 6x6x1x8
model = Flatten()(model)
model = Dropout(0.5)(model)
model_out = Dense(100, activation='relu', name = modual+'fc1')(model)
return model_in, model_out
示例7: get_shallow_convnet
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def get_shallow_convnet(window_size=4096, channels=2, output_size=84):
inputs = Input(shape=(window_size, channels))
conv = ComplexConv1D(
32, 512, strides=16,
activation='relu')(inputs)
pool = AveragePooling1D(pool_size=4, strides=2)(conv)
pool = Permute([2, 1])(pool)
flattened = Flatten()(pool)
dense = ComplexDense(2048, activation='relu')(flattened)
predictions = ComplexDense(
output_size,
activation='sigmoid',
bias_initializer=Constant(value=-5))(dense)
predictions = GetReal(predictions)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer=Adam(lr=1e-4),
loss='binary_crossentropy',
metrics=['accuracy'])
return model
示例8: discriminator
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def discriminator(img_dim,alpha=0.2):
model = Sequential()
model.add(
Conv2D(64, kernel_size=5,strides=2,
padding='same',
input_shape=img_dim)
)
model.add(LeakyReLU(alpha))
model.add(Conv2D(128,kernel_size=5,strides=2,padding='same'))
model.add(BatchNormalization())
model.add(LeakyReLU(alpha))
model.add(Conv2D(256,kernel_size=5,strides=2,padding='same'))
model.add(BatchNormalization())
model.add(LeakyReLU(alpha))
model.add(Flatten())
model.add(Dense(1))
model.add(Activation('sigmoid'))
return model
# Define a combination of Generator and Discriminator
示例9: build_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def build_model(self):
model = Sequential()
model.add(Convolution2D(
16, 8, 8, input_shape=(self.num_frames,) + self.frame_dim,
subsample=(4, 4), activation="relu", init="he_uniform"
))
model.add(Convolution2D(
16, 4, 4, subsample=(2, 2), activation="relu", init="he_uniform"
))
model.add(Convolution2D(
32, 3, 3, subsample=(1, 1), activation="relu", init="he_uniform"
))
model.add(Flatten())
model.add(Dense(
512, activation="relu", init="he_uniform"
))
model.add(Dense(
self.num_actions, activation="linear", init="he_uniform"
))
model.compile(loss=self.q_loss, optimizer=self.optimizer)
self.model = model
示例10: drqn
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def drqn(input_shape, action_size, learning_rate):
model = Sequential()
model.add(TimeDistributed(Convolution2D(32, 8, 8, subsample=(4,4), activation='relu'), input_shape=(input_shape)))
model.add(TimeDistributed(Convolution2D(64, 4, 4, subsample=(2,2), activation='relu')))
model.add(TimeDistributed(Convolution2D(64, 3, 3, activation='relu')))
model.add(TimeDistributed(Flatten()))
# Use all traces for training
#model.add(LSTM(512, return_sequences=True, activation='tanh'))
#model.add(TimeDistributed(Dense(output_dim=action_size, activation='linear')))
# Use last trace for training
model.add(LSTM(512, activation='tanh'))
model.add(Dense(output_dim=action_size, activation='linear'))
adam = Adam(lr=learning_rate)
model.compile(loss='mse',optimizer=adam)
return model
示例11: a2c_lstm
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def a2c_lstm(input_shape, action_size, value_size, learning_rate):
"""Actor and Critic Network share convolution layers with LSTM
"""
state_input = Input(shape=(input_shape)) # 4x64x64x3
x = TimeDistributed(Convolution2D(32, 8, 8, subsample=(4,4), activation='relu'))(state_input)
x = TimeDistributed(Convolution2D(64, 4, 4, subsample=(2,2), activation='relu'))(x)
x = TimeDistributed(Convolution2D(64, 3, 3, activation='relu'))(x)
x = TimeDistributed(Flatten())(x)
x = LSTM(512, activation='tanh')(x)
# Actor Stream
actor = Dense(action_size, activation='softmax')(x)
# Critic Stream
critic = Dense(value_size, activation='linear')(x)
model = Model(input=state_input, output=[actor, critic])
adam = Adam(lr=learning_rate, clipnorm=1.0)
model.compile(loss=['categorical_crossentropy', 'mse'], optimizer=adam, loss_weights=[1., 1.])
return model
示例12: CapsuleNet
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def CapsuleNet(n_capsule = 10, n_routings = 5, capsule_dim = 16,
n_recurrent=100, dropout_rate=0.2, l2_penalty=0.0001):
K.clear_session()
inputs = Input(shape=(170,))
x = Embedding(21099, 300, trainable=True)(inputs)
x = SpatialDropout1D(dropout_rate)(x)
x = Bidirectional(
CuDNNGRU(n_recurrent, return_sequences=True,
kernel_regularizer=l2(l2_penalty),
recurrent_regularizer=l2(l2_penalty)))(x)
x = PReLU()(x)
x = Capsule(
num_capsule=n_capsule, dim_capsule=capsule_dim,
routings=n_routings, share_weights=True)(x)
x = Flatten(name = 'concatenate')(x)
x = Dropout(dropout_rate)(x)
# fc = Dense(128, activation='sigmoid')(x)
outputs = Dense(6, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])
return model
示例13: CapsuleNet_v2
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def CapsuleNet_v2(n_capsule = 10, n_routings = 5, capsule_dim = 16,
n_recurrent=100, dropout_rate=0.2, l2_penalty=0.0001):
K.clear_session()
inputs = Input(shape=(200,))
x = Embedding(20000, 300, trainable=True)(inputs)
x = SpatialDropout1D(dropout_rate)(x)
x = Bidirectional(
CuDNNGRU(n_recurrent, return_sequences=True,
kernel_regularizer=l2(l2_penalty),
recurrent_regularizer=l2(l2_penalty)))(x)
x = PReLU()(x)
x = Capsule(
num_capsule=n_capsule, dim_capsule=capsule_dim,
routings=n_routings, share_weights=True)(x)
x = Flatten(name = 'concatenate')(x)
x = Dropout(dropout_rate)(x)
# fc = Dense(128, activation='sigmoid')(x)
outputs = Dense(6, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])
return model
示例14: cnn
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def cnn(trn_set, tst_set):
trn_x, trn_y = trn_set
trn_y = np.squeeze(trn_y, axis=2)
tst_x, tst_y = tst_set
tst_y = np.squeeze(tst_y, axis=2)
model = Sequential()
model.add(Convolution2D(2, 5, 5, activation='sigmoid', input_shape=(1, 28, 28)))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.1))
return model, trn_x, trn_y, tst_x, tst_y
################################################################################
示例15: build
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Flatten [as 别名]
def build(width, height, depth, classes):
# Initialize the model along with the input shape to be 'channels_last'
model = Sequential()
input_shape = (height, width, depth)
# Update the image shape if 'channels_first' is being used
if K.image_data_format() == 'channels_first':
input_shape = (depth, height, width)
# Define the first (and only) CONV => RELU layer
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape))
model.add(Activation('relu'))
# Add a softmax classifier
model.add(Flatten())
model.add(Dense(classes))
model.add(Activation('softmax'))
# Return the network architecture
return model