本文整理汇总了Python中keras.layers.core.Dense方法的典型用法代码示例。如果您正苦于以下问题:Python core.Dense方法的具体用法?Python core.Dense怎么用?Python core.Dense使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers.core
的用法示例。
在下文中一共展示了core.Dense方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: deep_mlp
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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 Dense [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: setUp
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [as 别名]
def setUp(self):
iris = load_iris()
theano.config.floatX = 'float32'
X = iris.data.astype(theano.config.floatX)
y = iris.target.astype(np.int32)
y_ohe = np_utils.to_categorical(y)
model = Sequential()
model.add(Dense(input_dim=X.shape[1], output_dim=5, activation='tanh'))
model.add(Dense(input_dim=5, output_dim=y_ohe.shape[1], activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='sgd')
model.fit(X, y_ohe, nb_epoch=10, batch_size=1, verbose=3, validation_data=None)
params = {'copyright': 'Václav Čadek', 'model_name': 'Iris Model'}
self.model = model
self.pmml = keras2pmml(self.model, **params)
self.num_inputs = self.model.input_shape[1]
self.num_outputs = self.model.output_shape[1]
self.num_connection_layers = len(self.model.layers)
self.features = ['x{}'.format(i) for i in range(self.num_inputs)]
self.class_values = ['y{}'.format(i) for i in range(self.num_outputs)]
示例4: get_residual_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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
示例5: model_create
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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
示例6: test_1o_1i
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [as 别名]
def test_1o_1i(self):
print('test a 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', input='input1')
graph.add_node(Dense(16, 4), name='dense3', input='dense1')
graph.add_output(name='output1', inputs=['dense2', 'dense3'], 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_test})
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)
示例7: test_1o_1i_2
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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)
示例8: test_1o_2i
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [as 别名]
def test_1o_2i(self):
print('test a non-sequential graph with 2 inputs and 1 output')
graph = Graph()
graph.add_input(name='input1', ndim=2)
graph.add_input(name='input2', ndim=2)
graph.add_node(Dense(32, 16), name='dense1', input='input1')
graph.add_node(Dense(32, 4), name='dense2', input='input2')
graph.add_node(Dense(16, 4), name='dense3', input='dense1')
graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum')
graph.compile('rmsprop', {'output1': 'mse'})
history = graph.fit({'input1': X_train, 'input2': X2_train, 'output1': y_train}, nb_epoch=10)
out = graph.predict({'input1': X_test, 'input2': X2_test})
assert(type(out == dict))
assert(len(out) == 1)
loss = graph.test_on_batch({'input1': X_test, 'input2': X2_test, 'output1': y_test})
loss = graph.train_on_batch({'input1': X_test, 'input2': X2_test, 'output1': y_test})
loss = graph.evaluate({'input1': X_test, 'input2': X2_test, 'output1': y_test})
print(loss)
assert(loss < 3.0)
graph.get_config(verbose=1)
示例9: test_recursive
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [as 别名]
def test_recursive(self):
print('test layer-like API')
graph = containers.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', input='input1')
graph.add_node(Dense(16, 4), name='dense3', input='dense1')
graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum')
seq = Sequential()
seq.add(Dense(32, 32, name='first_seq_dense'))
seq.add(graph)
seq.add(Dense(4, 4, name='last_seq_dense'))
seq.compile('rmsprop', 'mse')
history = seq.fit(X_train, y_train, batch_size=10, nb_epoch=10)
loss = seq.evaluate(X_test, y_test)
print(loss)
assert(loss < 2.5)
loss = seq.evaluate(X_test, y_test, show_accuracy=True)
pred = seq.predict(X_test)
seq.get_config(verbose=1)
示例10: test_vector_clf
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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)
示例11: test_vector_reg
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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)
示例12: test_img_clf
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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)
示例13: value_distribution_network
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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
示例14: build_3dcnn_model
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [as 别名]
def build_3dcnn_model(self, fusion_type, Fusion):
if len(Fusion[0]) == 1:
input_shape = (32, 32, len(Fusion))
model_in,model = self.cnn_2D(input_shape)
else:
input_shape = (32, 32, 5, len(Fusion))
model_in,model = self.cnn_3D(input_shape)
model = Dropout(0.5)(model)
model = Dense(32, activation='relu', name = 'fc2')(model)
model = Dense(self.config.classes, activation='softmax', name = 'fc3')(model)
model = Model(input=model_in,output=model)
# 统计参数
# model.summary()
plot_model(model,to_file='experiments/img/' + str(Fusion) + fusion_type + r'_model.png',show_shapes=True)
print(' Saving model Architecture')
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
# model.compile(optimizer=adam, loss=self.mycrossentropy, metrics=['accuracy']) #有改善,但不稳定
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
return model
示例15: cnn_2D
# 需要导入模块: from keras.layers import core [as 别名]
# 或者: from keras.layers.core import Dense [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