def build_model(layers):
model = Sequential()
model.add(GRU(input_dim=layers[0], output_dim=layers[1], activation='tanh', return_sequences=True))
model.add(Dropout(0.15)) # Dropout overfitting
# model.add(GRU(layers[2],activation='tanh', return_sequences=True))
# model.add(Dropout(0.2)) # Dropout overfitting
model.add(GRU(layers[2], activation='tanh', return_sequences=False))
model.add(Dropout(0.15)) # Dropout overfitting
model.add(Dense(output_dim=layers[3]))
model.add(Activation("linear"))
start = time.time()
# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
# model.compile(loss="mse", optimizer=sgd)
model.compile(loss="mse", optimizer="rmsprop") # Nadam rmsprop
print "Compilation Time : ", time.time() - start
return model
python类Activation()的实例源码
def generator_model(noise_dim=100, aux_dim=47, model_name="generator"):
# Merge noise and auxilary inputs
gen_input = Input(shape=(noise_dim,), name="noise_input")
aux_input = Input(shape=(aux_dim,), name="auxilary_input")
x = concatenate([gen_input, aux_input], axis=-1)
# Dense Layer 1
x = Dense(10 * 100)(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 10*100
# Reshape the tensors to support CNNs
x = Reshape((100, 10))(x) # shape is 100 x 10
# Conv Layer 1
x = Conv1D(filters=250, kernel_size=13, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 100 x 250
x = UpSampling1D(size=2)(x) # output shape is 200 x 250
# Conv Layer 2
x = Conv1D(filters=100, kernel_size=13, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 200 x 100
x = UpSampling1D(size=2)(x) # output shape is 400 x 100
# Conv Layer 3
x = Conv1D(filters=1, kernel_size=13, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('tanh')(x) # final output shape is 400 x 1
generator_model = Model(
outputs=[x], inputs=[gen_input, aux_input], name=model_name)
return generator_model
def discriminator_model():
model = Sequential()
model.add(Convolution2D(64,5,5,
border_mode='same',
input_shape=(1,28,28),
dim_ordering="th"))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="th"))
model.add(Convolution2D(128,5,5, border_mode='same', dim_ordering="th"))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="th"))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
return model
def discriminator_model():
""" return a (b, 1) logits"""
model = Sequential()
model.add(Convolution2D(64, 4, 4,border_mode='same',input_shape=(IN_CH*2, img_cols, img_rows)))
model.add(BatchNormalization(mode=2))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, 4, 4,border_mode='same'))
model.add(BatchNormalization(mode=2))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(512, 4, 4,border_mode='same'))
model.add(BatchNormalization(mode=2))
model.add(Activation('tanh'))
model.add(Convolution2D(1, 4, 4,border_mode='same'))
model.add(BatchNormalization(mode=2))
model.add(Activation('tanh'))
model.add(Activation('sigmoid'))
return model
def discriminator_model():
model = Sequential()
model.add(Convolution2D(
64, 5, 5,
border_mode='same',
input_shape=(1, 28, 28)))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, 5, 5))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
return model
def build(input_shape, classes):
model = Sequential()
# CONV => RELU => POOL
model.add(Conv2D(20, kernel_size=5, padding="same",
input_shape=input_shape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# CONV => RELU => POOL
model.add(Conv2D(50, kernel_size=5, padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Flatten => RELU layers
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
# a softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
return model
# network and training
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[3]))
model.add(Activation("linear"))
start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
print("> Compilation Time : ", time.time() - start)
return model
def init_model():
start_time = time.time()
print 'Compiling Model ... '
model = Sequential()
model.add(Dense(500, input_dim=784))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(300))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(10))
model.add(Activation('softmax'))
rms = RMSprop()
model.compile(loss='categorical_crossentropy', optimizer=rms,
metrics=['accuracy'])
print 'Model compiled in {0} seconds'.format(time.time() - start_time)
return model
def init_model():
"""
"""
start_time = time.time()
print 'Compiling model...'
model = Sequential()
model.add(Convolution2D(64, 3,3, border_mode='valid', input_shape=INPUT_SHAPE))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(.25))
model.add(Flatten())
model.add(Dense(10))
model.add(Activation('softmax'))
rms = RMSprop()
model.compile(loss='categorical_crossentropy', optimizer=rms,
metrics=['accuracy'])
print 'Model compiled in {0} seconds'.format(time.time() - start_time)
model.summary()
return model
def build(nc, w, h,
loss='categorical_crossentropy',
optimizer='adam',
**kwargs):
data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None?
inp = Input(shape=(h, w, 3))
enet = encoder.build(inp)
enet = decoder.build(enet, nc=nc)
name = 'enet_naive_upsampling'
enet = Reshape((data_shape, nc))(enet) # TODO: need to remove data_shape for multi-scale training
enet = Activation('softmax')(enet)
model = Model(inputs=inp, outputs=enet)
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy', 'mean_squared_error'])
return model, name
def build(nc, w, h,
loss='categorical_crossentropy',
# optimizer='adadelta'):
optimizer='adam',
metrics=None,
**kwargs):
data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None?
inp = Input(shape=(h, w, 3), name='image')
enet = encoder.build(inp)
enet = decoder.build(enet, nc=nc)
name = 'enet_unpooling'
# TODO: need to remove data_shape for multi-scale training
enet = Reshape((data_shape, nc))(enet)
enet = Activation('softmax', name='output')(enet)
model = Model(inputs=inp, outputs=enet)
if metrics is None:
metrics = ['accuracy']
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
return model, name
def __init__(self):
super().__init__()
self._learning = True
self._learning_rate = .1
self._discount = .1
self._epsilon = .9
# Create Model
model = Sequential()
model.add(Dense(2, init='lecun_uniform', input_shape=(2,)))
model.add(Activation('relu'))
model.add(Dense(10, init='lecun_uniform'))
model.add(Activation('relu'))
model.add(Dense(4, init='lecun_uniform'))
model.add(Activation('linear'))
rms = RMSprop()
model.compile(loss='mse', optimizer=rms)
self._model = model
def fire_module(x, squeeze=16, expand=64):
x = Convolution2D(squeeze, 1, 1, border_mode='valid')(x)
x = Activation('relu')(x)
left = Convolution2D(expand, 1, 1, border_mode='valid')(x)
left = Activation('relu')(left)
right= ZeroPadding2D(padding=(1, 1))(x)
right = Convolution2D(expand, 3, 3, border_mode='valid')(right)
right = Activation('relu')(right)
y = merge([left, right], mode='concat', concat_axis=1)
return y
# Original SqueezeNet from paper. Global Average Pool implemented manually with Average Pooling Layer
def fire_module(x, squeeze=16, expand=64):
x = Convolution2D(squeeze, 1, 1, border_mode='valid')(x)
x = Activation('relu')(x)
left = Convolution2D(expand, 1, 1, border_mode='valid')(x)
left = Activation('relu')(left)
right= ZeroPadding2D(padding=(1, 1))(x)
right = Convolution2D(expand, 3, 3, border_mode='valid')(right)
right = Activation('relu')(right)
x = merge([left, right], mode='concat', concat_axis=1)
return x
# Original SqueezeNet from paper. Global Average Pool implemented manually with Average Pooling Layer
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
co_lstm_predict_sequence.py 文件源码
项目:copper_price_forecast
作者: liyinwei
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
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
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
def __init__(self, sizes,
cell = RNNCell.LSTM,
dropout = 0.2,
activation = 'linear',
loss = 'mse',
optimizer = 'rmsprop'): #beta_1
self.model = Sequential()
self.model.add(cell(
input_dim = sizes[0],
output_dim = sizes[1],
return_sequences = True
))
for i in range(2, len(sizes) - 1):
self.model.add(cell(sizes[i], return_sequences = False))
self.model.add(Dropout(dropout))
self.model.add(Dense(output_dim = sizes[-1]))
self.model.add(Activation(activation))
self.model.compile(loss=loss, optimizer=optimizer)
def get_simple_model():
model = Sequential()
model.add(ZeroPadding2D(padding=(3, 3), input_shape=(nb_input_layers, NB_ROWS, NB_COLS)))
model.add(Convolution2D(96, 5, 5))
model.add(Activation('relu'))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(Convolution2D(192, 3, 3))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
print("Compiling model")
model.compile(loss='categorical_crossentropy', optimizer='adam')
print("Compiled model")
return model
###############################################################################
def first_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = Conv1D(k1,1,padding='same')(tensor_input)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,padding='same')(out)
pooling = MaxPooling1D(pooling_size,padding='same')(tensor_input)
# out = merge([out,pooling],mode='sum')
out = add([out,pooling])
return out
def repeated_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = BatchNormalization()(x)
out = Activation('relu')(out)
out = Conv1D(k1,kernel_size,padding='same')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,padding='same')(out)
pooling = MaxPooling1D(pooling_size,padding='same')(x)
out = add([out, pooling])
#out = merge([out,pooling])
return out
def first_2d_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = Conv2D(k1,1,padding='same',data_format='channels_last')(tensor_input)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv2D(k2,kernel_size,padding='same',data_format='channels_last')(out)
pooling = MaxPooling2D(pooling_size,padding='same',data_format='channels_last')(tensor_input)
# out = merge([out,pooling],mode='sum')
out = add([out,pooling])
return out
def repeated_2d_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = BatchNormalization()(x)
out = Activation('relu')(out)
out = Conv2D(k1,kernel_size,padding='same',data_format='channels_last')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv2D(k2,kernel_size,padding='same',data_format='channels_last')(out)
pooling = MaxPooling2D(pooling_size,padding='same',data_format='channels_last')(x)
out = add([out, pooling])
#out = merge([out,pooling])
return out
def first_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = Conv1D(k1,1,padding='same')(tensor_input)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,padding='same')(out)
pooling = MaxPooling1D(pooling_size,padding='same')(tensor_input)
# out = merge([out,pooling],mode='sum')
out = add([out,pooling])
return out
def repeated_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = BatchNormalization()(x)
out = Activation('relu')(out)
out = Conv1D(k1,kernel_size,padding='same')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,padding='same')(out)
pooling = MaxPooling1D(pooling_size,padding='same')(x)
out = add([out, pooling])
#out = merge([out,pooling])
return out
def first_2d_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = Conv2D(k1,1,padding='same',data_format='channels_last')(tensor_input)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv2D(k2,kernel_size,padding='same',data_format='channels_last')(out)
pooling = MaxPooling2D(pooling_size,padding='same',data_format='channels_last')(tensor_input)
# out = merge([out,pooling],mode='sum')
out = add([out,pooling])
return out
def first_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = Conv1D(k1,1,padding='same')(tensor_input)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,strides=2,padding='same')(out)
pooling = MaxPooling1D(pooling_size,strides=2,padding='same')(tensor_input)
# out = merge([out,pooling],mode='sum')
out = add([out,pooling])
return out
def repeated_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = BatchNormalization()(x)
out = Activation('relu')(out)
out = Conv1D(k1,kernel_size,padding='same')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,strides=2,padding='same')(out)
pooling = MaxPooling1D(pooling_size,strides=2,padding='same')(x)
out = add([out, pooling])
#out = merge([out,pooling])
return out
def repeated_2d_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = BatchNormalization()(x)
out = Activation('relu')(out)
out = Conv2D(k1,kernel_size,2,padding='same',data_format='channels_last')(out)
out = BatchNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv2D(k2,kernel_size,2,padding='same',data_format='channels_last')(out)
pooling = MaxPooling2D(pooling_size,padding='same',data_format='channels_last')(x)
out = add([out, pooling])
#out = merge([out,pooling])
return out
def model(X_train, X_test, Y_train, Y_test):
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([400, 512, 600])}}))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense(10))
model.add(Activation('softmax'))
rms = RMSprop()
model.compile(loss='categorical_crossentropy', optimizer=rms, metrics=['accuracy'])
nb_epoch = 10
batch_size = 128
model.fit(X_train, Y_train,
batch_size=batch_size, nb_epoch=nb_epoch,
verbose=2,
validation_data=(X_test, Y_test))
score, acc = model.evaluate(X_test, Y_test, verbose=0)
return {'loss': -acc, 'status': STATUS_OK, 'model': model}