本文整理汇总了Python中keras.models.Sequential类的典型用法代码示例。如果您正苦于以下问题:Python Sequential类的具体用法?Python Sequential怎么用?Python Sequential使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Sequential类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: conv2d_work
def conv2d_work(input_dim):
seq = Sequential()
assert self.config['num_conv2d_layers'] > 0
for i in range(self.config['num_conv2d_layers']):
seq.add(Conv2D(filters=self.config['2d_kernel_counts'][i], kernel_size=self.config['2d_kernel_sizes'][i], padding='same', activation='relu'))
seq.add(MaxPooling2D(pool_size=(self.config['2d_mpool_sizes'][i][0], self.config['2d_mpool_sizes'][i][1])))
return seq
示例2: train_model
def train_model():
# (X_train, Y_train, X_test, Y_test) = prapare_train()
X_ = []
with open('../data/train_matrix.out') as train_file:
X_train = json.load(train_file)
for x in X_train:
a = len(x)
print a/2
x1 = x[:a/2]
x2 = x[a/2:]
x3 = []
x3.append(x1)
x3.append(x2)
X_.append(x3)
# X_test = pickle.load('../data/test_matrix.out')
Y_train = [1,0,0]*3
# Y_test = [1,0,0]*3
# print len(X_train) - len(Y_train)
# print len(X_test) - len(Y_test)
model = Sequential()
model = get_nn_model()
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# model.fit(X_train, Y_train,
# batch_size=batch_size,
# nb_epoch=nb_epoch,
# validation_data=(X_test, Y_test))
#2
model.fit(X_, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_split = 0.2)
print 'ok'
示例3: create
def create(self):
language_model = Sequential()
self.textual_embedding(language_model, mask_zero=True)
self.language_model = language_model
visual_model_factory = \
select_sequential_visual_model[self._config.trainable_perception_name](
self._config.visual_dim)
visual_model = visual_model_factory.create()
visual_dimensionality = visual_model_factory.get_dimensionality()
self.visual_embedding(visual_model, visual_dimensionality)
#visual_model = Sequential()
#self.visual_embedding(visual_model)
# the below should contain all zeros
zero_model = Sequential()
zero_model.add(RepeatVector(self._config.max_input_time_steps)-1)
visual_model.add(Merge[visual_model, zero_model], mode='concat')
self.visual_model = visual_model
if self._config.multimodal_merge_mode == 'dot':
self.add(Merge([language_model, visual_model], mode='dot', dot_axes=[(1,),(1,)]))
else:
self.add(Merge([language_model, visual_model], mode=self._config.multimodal_merge_mode))
self.add(self._config.recurrent_encoder(
self._config.hidden_state_dim,
return_sequences=False,
go_backwards=self._config.go_backwards))
self.deep_mlp()
self.add(Dense(self._config.output_dim))
self.add(Activation('softmax'))
示例4: __init__
def __init__(self, restore=None, session=None, Dropout=Dropout, num_labels=10):
self.num_channels = 1
self.image_size = 28
self.num_labels = num_labels
model = Sequential()
nb_filters = 64
layers = [Conv2D(nb_filters, (5, 5), strides=(2, 2), padding="same",
input_shape=(28, 28, 1)),
Activation('relu'),
Conv2D(nb_filters, (3, 3), strides=(2, 2), padding="valid"),
Activation('relu'),
Conv2D(nb_filters, (3, 3), strides=(1, 1), padding="valid"),
Activation('relu'),
Flatten(),
Dense(32),
Activation('relu'),
Dropout(.5),
Dense(num_labels)]
for layer in layers:
model.add(layer)
if restore != None:
model.load_weights(restore)
self.model = model
示例5: __init__
class QLearn:
def __init__(self, actions, epsilon, alpha, gamma):
# instead of a dictionary, we'll be using
# a neural network
# self.q = {}
self.epsilon = epsilon # exploration constant
self.alpha = alpha # discount constant
self.gamma = gamma # discount factor
self.actions = actions
# Build the neural network
self.network = Sequential()
self.network.add(Dense(50, init='lecun_uniform', input_shape=(4,)))
# self.network.add(Activation('sigmoid'))
#self.network.add(Dropout(0.2))
self.network.add(Dense(20, init='lecun_uniform'))
# self.network.add(Activation('sigmoid'))
# #self.network.add(Dropout(0.2))
self.network.add(Dense(2, init='lecun_uniform'))
# self.network.add(Activation('linear')) #linear output so we can have range of real-valued outputs
# rms = SGD(lr=0.0001, decay=1e-6, momentum=0.5) # explodes to non
rms = RMSprop()
# rms = Adagrad()
# rms = Adam()
self.network.compile(loss='mse', optimizer=rms)
# Get a summary of the network
self.network.summary()
示例6: test_dropout
def test_dropout(layer_class):
for unroll in [True, False]:
layer_test(layer_class,
kwargs={'units': units,
'dropout': 0.1,
'recurrent_dropout': 0.1,
'unroll': unroll},
input_shape=(num_samples, timesteps, embedding_dim))
# Test that dropout is applied during training
x = K.ones((num_samples, timesteps, embedding_dim))
layer = layer_class(units, dropout=0.5, recurrent_dropout=0.5,
input_shape=(timesteps, embedding_dim))
y = layer(x)
assert y._uses_learning_phase
y = layer(x, training=True)
assert not getattr(y, '_uses_learning_phase')
# Test that dropout is not applied during testing
x = np.random.random((num_samples, timesteps, embedding_dim))
layer = layer_class(units, dropout=0.5, recurrent_dropout=0.5,
unroll=unroll,
input_shape=(timesteps, embedding_dim))
model = Sequential([layer])
assert model.uses_learning_phase
y1 = model.predict(x)
y2 = model.predict(x)
assert_allclose(y1, y2)
示例7: Simple
def Simple(layers, func, ipt):
model = Sequential()
#model.add(BatchNormalization(input_shape = [ipt]))
model.add(Dense(layers[0], input_dim = ipt, activation = func[0]))
for i in range(1, len(layers)):
model.add(Dense(layers[i], activation = func[i]))
return model
示例8: encoders_m
def encoders_m(self, inputs):
input_encoder_m = Sequential()
input_encoder_m.add(Embedding(input_dim=self.vocab_size,
output_dim=64))
input_encoder_m.add(Dropout(0.3))
encode_m = input_encoder_m(inputs)
return encode_m
示例9: encoders_c
def encoders_c(self, inputs):
input_encoder_c = Sequential()
input_encoder_c.add(Embedding(input_dim=self.vocab_size,
output_dim=self.query_maxlen))
input_encoder_c.add(Dropout(0.3))
encoder_c = input_encoder_c(inputs)
return encoder_c
示例10: build_part1_RNN
def build_part1_RNN(window_size):
model = Sequential()
model.add(LSTM(5, input_shape=(window_size,1) ))
#model.add(Dropout(0.5))
model.add(Dense(1))
return model
示例11: build_part2_RNN
def build_part2_RNN(window_size, num_chars):
model = Sequential()
model.add(LSTM(200, input_shape=(window_size, num_chars)))
#model.add(Dropout(0.5))
model.add(Dense(num_chars, activation='softmax'))
return model
示例12: get_item_subgraph
def get_item_subgraph(input_shape, latent_dim):
# Could take item metadata here, do convolutional layers etc.
model = Sequential()
model.add(Dense(latent_dim, input_shape=input_shape))
return model
示例13: create_model
def create_model(kernel_regularizer=None, activity_regularizer=None):
model = Sequential()
model.add(Dense(num_classes,
kernel_regularizer=kernel_regularizer,
activity_regularizer=activity_regularizer,
input_shape=(data_dim,)))
return model
示例14: fork
def fork (model, n=2):
forks = []
for i in range(n):
f = Sequential()
f.add (model)
forks.append(f)
return forks
示例15: build_partial_cnn1
def build_partial_cnn1(img_rows, img_cols):
model = Sequential()
#model.add(Convolution2D(nb_filter=100, nb_row=5, nb_col=5,
model.add(Convolution2D(nb_filter=10, nb_row=2, nb_col=2,
init='glorot_uniform', activation='linear',
border_mode='valid',
input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Convolution2D(nb_filter=100, nb_row=5, nb_col=5,
'''model.add(Convolution2D(nb_filter=512, nb_row=5, nb_col=5,
init='glorot_uniform', activation='linear',
border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))'''
return model