本文整理汇总了Python中keras.layers.core.Activation方法的典型用法代码示例。如果您正苦于以下问题:Python core.Activation方法的具体用法?Python core.Activation怎么用?Python core.Activation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers.core
的用法示例。
在下文中一共展示了core.Activation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: deep_mlp
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def deep_mlp(self):
"""
Deep Multilayer Perceptrop.
"""
if self._config.num_mlp_layers == 0:
self.add(Dropout(0.5))
else:
for j in xrange(self._config.num_mlp_layers):
self.add(Dense(self._config.mlp_hidden_dim))
if self._config.mlp_activation == 'elu':
self.add(ELU())
elif self._config.mlp_activation == 'leaky_relu':
self.add(LeakyReLU())
elif self._config.mlp_activation == 'prelu':
self.add(PReLU())
else:
self.add(Activation(self._config.mlp_activation))
self.add(Dropout(0.5))
示例2: create
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def create(self):
assert self._config.textual_embedding_dim == 0, \
'Embedding cannot be learnt but must be fixed'
language_forward = Sequential()
language_forward.add(self._config.recurrent_encoder(
self._config.hidden_state_dim, return_sequences=False,
input_shape=(self._config.max_input_time_steps, self._config.input_dim)))
self.language_forward = language_forward
language_backward = Sequential()
language_backward.add(self._config.recurrent_encoder(
self._config.hidden_state_dim, return_sequences=False,
go_backwards=True,
input_shape=(self._config.max_input_time_steps, self._config.input_dim)))
self.language_backward = language_backward
self.add(Merge([language_forward, language_backward]))
self.deep_mlp()
self.add(Dense(self._config.output_dim))
self.add(Activation('softmax'))
示例3: get_residual_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [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
示例4: model_create
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [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
示例5: test_1o_1i_2
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def test_1o_1i_2(self):
print('test a more complex non-sequential graph with 1 input and 1 output')
graph = Graph()
graph.add_input(name='input1', ndim=2)
graph.add_node(Dense(32, 16), name='dense1', input='input1')
graph.add_node(Dense(32, 4), name='dense2-0', input='input1')
graph.add_node(Activation('relu'), name='dense2', input='dense2-0')
graph.add_node(Dense(4, 16), name='dense3', input='dense2')
graph.add_node(Dense(16, 4), name='dense4', inputs=['dense1', 'dense3'], merge_mode='sum')
graph.add_output(name='output1', inputs=['dense2', 'dense4'], merge_mode='sum')
graph.compile('rmsprop', {'output1': 'mse'})
history = graph.fit({'input1': X_train, 'output1': y_train}, nb_epoch=10)
out = graph.predict({'input1': X_train})
assert(type(out == dict))
assert(len(out) == 1)
loss = graph.test_on_batch({'input1': X_test, 'output1': y_test})
loss = graph.train_on_batch({'input1': X_test, 'output1': y_test})
loss = graph.evaluate({'input1': X_test, 'output1': y_test})
print(loss)
assert(loss < 2.5)
graph.get_config(verbose=1)
示例6: test_vector_clf
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def test_vector_clf(self):
nb_hidden = 10
print('vector classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(10,),
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(Dense(X_train.shape[-1], nb_hidden))
model.add(Activation('relu'))
model.add(Dense(nb_hidden, y_train.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2)
print(history.history)
self.assertTrue(history.history['val_acc'][-1] > 0.9)
示例7: test_vector_reg
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def test_vector_reg(self):
nb_hidden = 10
print('vector regression data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(10,), output_shape=(2,),
classification=False)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
model = Sequential()
model.add(Dense(X_train.shape[-1], nb_hidden))
model.add(Activation('tanh'))
model.add(Dense(nb_hidden, y_train.shape[-1]))
model.compile(loss='hinge', optimizer='adagrad')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), verbose=2)
self.assertTrue(history.history['val_loss'][-1] < 0.9)
示例8: test_temporal_clf
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def test_temporal_clf(self):
print('temporal classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(5,10),
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(GRU(X_train.shape[-1], y_train.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
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)
示例9: test_img_clf
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [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)
示例10: conv_block
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def conv_block(input_tensor, filters, strides, d_rates):
x = Conv2D(filters[0], kernel_size=1, dilation_rate=d_rates[0])(input_tensor)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters[1], kernel_size=3, strides=strides, padding='same', dilation_rate=d_rates[1])(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters[2], kernel_size=1, dilation_rate=d_rates[2])(x)
x = BatchNormalization()(x)
shortcut = Conv2D(filters[2], kernel_size=1, strides=strides)(input_tensor)
shortcut = BatchNormalization()(shortcut)
x = add([x, shortcut])
x = Activation('relu')(x)
return x
示例11: identity_block
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def identity_block(input_tensor, filters, d_rates):
x = Conv2D(filters[0], kernel_size=1, dilation_rate=d_rates[0])(input_tensor)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters[1], kernel_size=3, padding='same', dilation_rate=d_rates[1])(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters[2], kernel_size=1, dilation_rate=d_rates[2])(x)
x = BatchNormalization()(x)
x = add([x, input_tensor])
x = Activation('relu')(x)
return x
示例12: __transition_block
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def __transition_block(ip, nb_filter, compression=1.0, weight_decay=1e-4):
''' Apply BatchNorm, Relu 1x1, Conv2D, optional compression, dropout and Maxpooling2D
Args:
ip: keras tensor
nb_filter: number of filters
compression: calculated as 1 - reduction. Reduces the number of feature maps
in the transition block.
dropout_rate: dropout rate
weight_decay: weight decay factor
Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool
'''
concat_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(ip)
x = Activation('relu')(x)
x = Conv2D(int(nb_filter * compression), (1, 1), kernel_initializer='he_normal', padding='same', use_bias=False,
kernel_regularizer=l2(weight_decay))(x)
x = AveragePooling2D((2, 2), strides=(2, 2))(x)
# global context block
x = global_context_block(x)
return x
示例13: build_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def build_model(layers):
"""
模型定义
"""
model = Sequential()
model.add(LSTM(units=layers[1], input_shape=(layers[1], layers[0]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(layers[2], return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(units=layers[3]))
model.add(Activation("tanh"))
start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
print("> Compilation Time : ", time.time() - start)
return model
示例14: build_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def build_model():
"""
定义模型
"""
model = Sequential()
model.add(LSTM(units=Conf.LAYERS[1], input_shape=(Conf.LAYERS[1], Conf.LAYERS[0]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(Conf.LAYERS[2], return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(units=Conf.LAYERS[3]))
# model.add(BatchNormalization(weights=None, epsilon=1e-06, momentum=0.9))
model.add(Activation("tanh"))
# act = PReLU(alpha_initializer='zeros', weights=None)
# act = LeakyReLU(alpha=0.3)
# model.add(act)
start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
print("> Compilation Time : ", time.time() - start)
return model
示例15: build_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Activation [as 别名]
def build_model(layers):
model = Sequential()
model.add(LSTM(
input_dim=layers[0],
output_dim=layers[1],
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
layers[2],
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[2]))
model.add(Activation("linear"))
start = time.time()
model.compile(loss="mse", optimizer="rmsprop", metrics=['accuracy'])
print("Compilation Time : ", time.time() - start)
return model