def resnet(args): neighbor_size, len_close, len_period, len_trend, nb_residual_unit, external_dim = \ args['neighbor_size'], args['len_close'], args['len_period'], \ args['len_trend'], args['nb_residual_unit'], args['external_dim'] external = True is_BN = True is_fuse = True slide_length = neighbor_size * 2 + 1 c_conf = (len_close, 2, slide_length, slide_length) p_conf = (len_period, 2, slide_length, slide_length) t_conf = (len_trend, 2, slide_length, slide_length) main_input = [] main_output = [] for conf in [c_conf, p_conf, t_conf]: if conf is not None: len_seq, nb_flow, height, width = conf input = Input(shape=(nb_flow * len_seq, height, width)) main_input.append(input) # conv1 # output = (image - filter + 2 * padding) / stride + 1 conv1 = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(input) #res_ouput res_output = rest_unit(is_BN, nb_residual_unit)(conv1) # conv2 activation = Activation('relu')(res_output) conv2 = Conv2D(2, (3, 3), strides=(1, 1), padding='same')(activation) main_output.append(conv2) # hamadard if is_fuse: new_output = [] for output in main_output: new_output.append(ilayer()(output)) main_output = merge.Add()(new_output) else: main_output = merge.Add()(main_output) if external: external_input = Input(shape=(external_dim, )) main_input.append(external_input) embedding = Dense(units=10)(external_input) embedding = Activation('relu')(embedding) h1 = Dense(units=2 * slide_length * slide_length)(embedding) activation = Activation('relu')(h1) external_output = Reshape((2, slide_length, slide_length))(activation) main_output = merge.Add()([main_output, external_output]) main_output = Activation('tanh')(main_output) model = Model(inputs=main_input, outputs=main_output) return model
def f(input_): # tile residuals to match output dim of skip_out for merge (capsule implementation) # note: tile only required in first wavenet block: after that, skip_out_dim channels # are already provided # Todo: clean up this horrible hack: using reshape instead of tile to work around "AttributeError: 'Tensor' object has no attribute '_keras_history'" using keras.backend.tile functionality if int(input_.shape[2]) < skip_out_dim: # residual = tile(input_, (1,1,skip_out_dim) ) thing1 = Reshape(target_shape=[int(input_.shape[1])])(input_) thing2 = RepeatVector(skip_out_dim)(thing1) residual = Reshape( target_shape=[int(thing2.shape[2]), int(thing2.shape[1])])(thing2) else: residual = input_ tanh_out = Conv1D(filters=n_atrous_filters, kernel_size=atrous_filter_size, dilation_rate=atrous_rate, padding='causal', activation='tanh')(input_) sigmoid_out = Conv1D(filters=n_atrous_filters, kernel_size=atrous_filter_size, dilation_rate=atrous_rate, padding='causal', activation='sigmoid')(input_) merged = merge.Multiply()([tanh_out, sigmoid_out]) skip_out = Conv1D(skip_out_dim, 1, activation='relu', border_mode='same')(merged) out = merge.Add()([skip_out, residual]) return out, skip_out
def test_mixing_preprocessing_and_regular_layers(self): x0 = Input(shape=(10, 10, 3)) x1 = Input(shape=(10, 10, 3)) x2 = Input(shape=(10, 10, 3)) y0 = merge.Add()([x0, x1]) y1 = image_preprocessing.CenterCrop(8, 8)(x2) y1 = convolutional.ZeroPadding2D(padding=1)(y1) z = merge.Add()([y0, y1]) z = normalization.Normalization()(z) z = convolutional.Conv2D(4, 3)(z) stage = preprocessing_stage.FunctionalPreprocessingStage([x0, x1, x2], z) data = [ np.ones((12, 10, 10, 3), dtype='float32'), np.ones((12, 10, 10, 3), dtype='float32'), np.ones((12, 10, 10, 3), dtype='float32') ] stage.adapt(data) _ = stage(data) stage.compile('rmsprop', 'mse') with self.assertRaisesRegex(ValueError, 'Preprocessing stage'): stage.fit(data, np.ones((12, 8, 8, 4))) ds_x0 = tf.data.Dataset.from_tensor_slices(np.ones((12, 10, 10, 3))) ds_x1 = tf.data.Dataset.from_tensor_slices(np.ones((12, 10, 10, 3))) ds_x2 = tf.data.Dataset.from_tensor_slices(np.ones((12, 10, 10, 3))) ds_x = tf.data.Dataset.zip((ds_x0, ds_x1, ds_x2)) ds_y = tf.data.Dataset.from_tensor_slices(np.ones((12, 8, 8, 4))) dataset = tf.data.Dataset.zip((ds_x, ds_y)).batch(4) with self.assertRaisesRegex(ValueError, 'Preprocessing stage'): stage.fit(dataset) _ = stage.evaluate(data, np.ones((12, 8, 8, 4))) _ = stage.predict(data)
def geo_lprnn_text_model(user_dim, len, place_dim=GRID_COUNT * GRID_COUNT, time_dim=config.time_dim, pl_d=config.pl_d, time_k=config.time_k, hidden_neurons=config.hidden_neurons, learning_rate=config.learning_rate): # RNN model construction pl_input = Input(shape=(len - 1, ), dtype='int32', name='pl_input') time_input = Input(shape=(len - 1, ), dtype='int32', name='time_input') user_input = Input(shape=(len - 1, ), dtype='int32', name='user_input') text_input = Input(shape=(len - 1, pl_d), dtype='float32', name='text_input') pl_embedding = Embedding(input_dim=place_dim + 1, output_dim=pl_d, name='pl_embedding', mask_zero=True)(pl_input) time_embedding = Embedding(input_dim=time_dim + 1, output_dim=time_k, name='time_embedding', mask_zero=True)(time_input) user_embedding = Embedding(input_dim=user_dim + 1, output_dim=place_dim + 1, name='user_embedding', mask_zero=True)(user_input) # text_embedding = Dense(pl_d)(text_input) attrs_latent = merge.Concatenate( [pl_embedding, time_embedding, text_input] ) # attrs_latent = merge([pl_embedding,time_embedding, text_input],mode='concat') # time_dist = TimeDistributed(Dense(50)) lstm_out = LSTM(hidden_neurons, return_sequences=True, name='lstm_layer')(attrs_latent) dense = Dense(place_dim + 1, name='dense')(lstm_out) out_vec = merge.Add( [dense, user_embedding]) # out_vec = merge([dense,user_embedding],mode='sum') pred = Activation('softmax')(out_vec) model = Model([pl_input, time_input, user_input, text_input], pred) # model.load_weights('./model/User_RNN_Seg_Epoch_0.3_rmsprop_300.h5') # Optimization sgd = optimizers.SGD(lr=learning_rate) rmsprop = optimizers.RMSprop(lr=learning_rate) model.compile(optimizer=rmsprop, loss='categorical_crossentropy') model.summary() return model
def astgcn(args): num_of_vertices, num_of_features, num_of_weeks, num_of_days, num_of_hours, cheb_polynomials, num_for_prediction, K =\ args['num_of_vertices'], args['num_of_features'], args['num_of_weeks'], args['num_of_days'], \ args['num_of_hours'], args['cheb_polynomials'], args['num_for_prediction'], args['K'] # params for week, day, hour, three submodules # double all the number of [week, day, hour, prediction number], for inflow and outflow params1 = [ {"K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": num_of_weeks * 2, "cheb_polynomials": cheb_polynomials}, {"K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": 1 * 2, "cheb_polynomials": cheb_polynomials} ] params2 = [ {"K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": num_of_days * 2, "cheb_polynomials": cheb_polynomials}, {"K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": 1 * 2, "cheb_polynomials": cheb_polynomials} ] params3 = [ {"K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": num_of_hours * 2, "cheb_polynomials": cheb_polynomials}, {"K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": 1 * 2, "cheb_polynomials": cheb_polynomials} ] all_params = [params1, params2, params3] hour_input = Input(shape=(num_of_vertices, num_of_features, num_of_hours * 2), dtype='float32', name='hour_input') day_input = Input(shape=(num_of_vertices, num_of_features, num_of_days * 2), dtype='float32', name='day_input') week_input = Input(shape=(num_of_vertices, num_of_features, num_of_weeks * 2), dtype='float32', name='week_input') main_input = [week_input, day_input, hour_input] submodules = [] for params, name in zip(all_params, ['week', 'day', 'hour']): submodules.append(ASTGCN_submodule(num_for_prediction * 2, params, name='{}_component'.format(name))) submodule_outputs = [] for idx in range(len(main_input)): print('input {} shape: {}'.format(idx, main_input[idx].shape)) submodule_outputs.append(submodules[idx](main_input[idx])) main_output = merge.Add()(submodule_outputs) if len(submodule_outputs) > 1 else submodule_outputs[0] model = Model(inputs=main_input, outputs=main_output, name='astgcn') return model
def make_residual_lstm_layers(input, rnn_width, rnn_depth): """ The intermediate LSTM layers return sequences, while the last returns a single element. The input is also a sequence. In order to match the shape of input and output of the LSTM to sum them we can do it only for all layers but the last. """ x = input for i in range(rnn_depth): return_sequences = i < rnn_depth - 1 x_rnn = LSTM(rnn_width, return_sequences=return_sequences)(input) x_rnn = BatchNormalization()(x_rnn) if return_sequences: # residual block x = merge.Add()([x, x_rnn]) else: # last layer does not return sequences and cannot be residual x = x_rnn return x
def create_critic_network(self, state_size, action_dim): print("Now we build the model") S = Input(shape=(84, 84, 4)) A = Input(shape=(3, )) x = Conv2D(16, kernel_size=8, strides=(3, 3), activation='relu')(S) x = Conv2D(32, kernel_size=4, strides=(2, 2), activation='relu')(x) x = Flatten()(x) y = Dense(1000, activation="relu")(A) h2 = Dense(1000, activation="relu")(x) h3 = merge.Add()([h2, y]) # h3 = Reshape((256, 1))(h3_prime) h4_prime = Dense(500, activation="relu")(h3) h4 = Dense(100, activation="relu")(h4_prime) V = Dense(1, activation='linear')(h4) model = Model(input=[S, A], output=V) adam = Adam(lr=self.LEARNING_RATE) model.compile(loss='mse', optimizer=adam) return model, A, S
def model_train(img_size, batch_size, epochs, optimizer, learning_rate, train_list, validation_list, style=2): print('Style {}.'.format(style)) if style == 1: input_img = Input(shape=img_size) #model = Sequential() model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', input_shape=img_size)(input_img) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(1, (3, 3), padding='same', kernel_initializer='he_normal')(model) res_img = model output_img = merge.Add()([res_img, input_img]) model = Model(input_img, output_img) #model.load_weights('vdsr_model_edges.h5') adam = Adam(lr=0.000005) #sgd = SGD(lr=1e-3, momentum=0.9, decay=1e-4, nesterov=False) sgd = SGD(lr=0.01, momentum=0.9, decay=0.001, nesterov=False) #model.compile(sgd, loss='mse', metrics=[PSNR, "accuracy"]) model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR, "accuracy"]) model.summary() else: input_img = Input(shape=img_size) model = Conv2D(64, (3, 3), padding='valid', kernel_initializer='he_normal')(input_img) model_0 = Activation('relu')(model) total_conv = 22 # should be even number total_conv -= 2 # subtract first and last residual_block_num = 5 # should be even number for _ in range(residual_block_num): # residual block model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model_0) model = Activation('relu')(model) for _ in range(int(total_conv / residual_block_num) - 1): model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model_0 = add([model, model_0]) model = Conv2D(1, (3, 3), padding='valid', kernel_initializer='he_normal')(model) res_img = model input_img1 = crop(1, 2, -2)(input_img) input_img1 = crop(2, 2, -2)(input_img1) print(input_img.shape) print(input_img1.shape) output_img = merge.Add()([res_img, input_img1]) # output_img = res_img model = Model(input_img, output_img) # model.load_weights('./vdsr_model_edges.h5') # adam = Adam(lr=learning_rate) adam = Adadelta() # sgd = SGD(lr=1e-7, momentum=0.9, decay=1e-2, nesterov=False) sgd = SGD(lr=learning_rate, momentum=0.9, decay=1e-4, nesterov=False, clipnorm=1) if optimizer == 0: model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR]) else: model.compile(sgd, loss='mse', metrics=[ssim, ssim_metric, PSNR]) model.summary() mycallback = MyCallback(model) timestamp = time.strftime("%m%d-%H%M", time.localtime(time.time())) csv_logger = callbacks.CSVLogger( 'data/callbacks/training_{}.log'.format(timestamp)) filepath = "./checkpoints/weights-improvement-{epoch:03d}-{PSNR:.2f}.hdf5" checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max') callbacks_list = [mycallback, checkpoint, csv_logger] # print('Loading training data.') # x = load_images(DATA_PATH) # print('Loading data label.') # y = load_images(LABEL_PATH) # print('Loading validation data.') # val = load_images(VAL_PATH) # print('Loading validation label.') # val_label = load_images(VAL_LABEL_PATH) # print(x.shape) # print(y.shape) # print(val.shape) # print(val_label.shape) with open('./model/vdsr_architecture.json', 'w') as f: f.write(model.to_json()) # datagen = ImageDataGenerator(rotation_range=45, # zoom_range=0.15, # horizontal_flip=True, # vertical_flip=True) # history = model.fit_generator(datagen.flow(x, y, batch_size=batch_size), # steps_per_epoch=len(x) // batch_size, # validation_data=(val, val_label), # validation_steps=len(val) // batch_size, # epochs=epochs, # callbacks=callbacks_list, # verbose=1, # shuffle=True, # workers=256, # use_multiprocessing=True) history = model.fit_generator( image_gen(train_list, batch_size=batch_size), steps_per_epoch=384400 * (len(train_list)) // batch_size, # steps_per_epoch=4612800//batch_size, validation_data=image_gen(validation_list, batch_size=batch_size), validation_steps=384400 * (len(validation_list)) // batch_size, epochs=epochs, workers=1024, callbacks=callbacks_list, verbose=1) print("Done training!!!") print("Saving the final model ...") model.save('vdsr_model.h5') # creates a HDF5 file del model # deletes the existing model # plt.plot(history.history['accuracy']) # plt.plot(history.history['val_accuracy']) # plt.title('Model accuracy') # plt.ylabel('Accuracy') # plt.xlabel('Epoch') # plt.legend(['Train', 'validation'], loc='upper left') # # plt.show() # plt.savefig('accuracy.png') # Plot training & validation loss values plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'validation'], loc='upper left') # plt.show() plt.savefig('loss.png') plt.plot(history.history['PSNR']) plt.plot(history.history['val_PSNR']) plt.title('Model PSNR') plt.ylabel('PSNR') plt.xlabel('Epoch') plt.legend(['Train', 'validation'], loc='upper left') # plt.show() plt.savefig('PSNR.png')
model_0 = add([model, model_0]) model = Conv2DTranspose(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(1, (3, 3), padding='valid', kernel_initializer='he_normal')(model) res_img = model from vdsr_deconv import crop input_img1 = crop(1, 2, -2)(input_img) input_img1 = crop(2, 2, -2)(input_img1) output_img = merge.Add()([res_img, input_img1]) model = Model(input_img, output_img) print(type(model)) adam = Adadelta() from vdsr_deconv import * model.load_weights(w_path, by_name=True) model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR]) vdsr = model vdsr.summary() print(type(vdsr)) li = os.listdir(img_path)
def model_train(img_size, batch_size, epochs, optimizer, learning_rate, train_list, validation_list, style=2): print('Style {}.'.format(style)) if style == 1: input_img = Input(shape=img_size) #model = Sequential() model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', input_shape=img_size)(input_img) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model) model = Activation('relu')(model) model = Conv2D(1, (3, 3), padding='same', kernel_initializer='he_normal')(model) res_img = model output_img = merge.Add()([res_img, input_img]) model = Model(input_img, output_img) #model.load_weights('vdsr_model_edges.h5') adam = Adam(lr=0.000005) sgd = SGD(lr=0.01, momentum=0.9, decay=0.001, nesterov=False) model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR, "accuracy"]) model.summary() else: input_img = Input(shape=img_size) model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(input_img) model = BatchNormalization()(model) model_0 = Activation('relu')(model) total_conv = 22 # should be even number total_conv -= 2 # subtract first and last residual_block_num = 5 # should be even number for _ in range(residual_block_num): # residual block model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(model_0) model = BatchNormalization()(model) model = Activation('relu')(model) print(_) for _ in range(int(total_conv/residual_block_num)-1): model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal', use_bias=False)(model) model = BatchNormalization()(model) model = Activation('relu')(model) model_0 = add([model, model_0]) print(_) model = Conv2DTranspose(64, (5, 5), padding='valid', kernel_initializer='he_normal', use_bias=False)(model) model = BatchNormalization()(model) model = LeakyReLU()(model) model = Conv2D(1, (5, 5), padding='valid', kernel_initializer='he_normal')(model) res_img = model #input_img1 = crop(1,22,-22)(input_img) #input_img1 = crop(2,22,-22)(input_img1) print(input_img.shape) output_img = merge.Add()([res_img, input_img]) # output_img = res_img model = Model(input_img, output_img) # model.load_weights('./vdsr_model_edges.h5') # adam = Adam(lr=learning_rate) adam = Adadelta() sgd = SGD(lr=learning_rate, momentum=0.9, decay=1e-4, nesterov=False, clipnorm=1) if optimizer == 0: model.compile(adam, loss='mse', metrics=[ssim, ssim_metric, PSNR]) else: model.compile(sgd, loss='mse', metrics=[ssim, ssim_metric, PSNR]) model.summary() mycallback = MyCallback(model) timestamp = time.strftime("%m%d-%H%M", time.localtime(time.time())) csv_logger = callbacks.CSVLogger('data/callbacks/deconv/training_{}.log'.format(timestamp)) filepath="./checkpoints/deconv/weights-improvement-{epoch:03d}-{PSNR:.2f}-{ssim:.2f}.hdf5" checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max') callbacks_list = [mycallback, checkpoint, csv_logger] with open('./model/deconv/vdsr_architecture.json', 'w') as f: f.write(model.to_json()) history = model.fit_generator(image_gen(train_list, batch_size=batch_size), steps_per_epoch=(409600//8)*len(train_list) // batch_size, validation_data=image_gen(validation_list,batch_size=batch_size), validation_steps=(409600//8)*len(validation_list) // batch_size, epochs=epochs, workers=1024, callbacks=callbacks_list, verbose=1) print("Done training!!!") print("Saving the final model ...") model.save('vdsr_model.h5') # creates a HDF5 file del model # deletes the existing model # Plot training & validation loss values plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'validation'], loc='upper left') # plt.show() plt.savefig('loss.png') plt.plot(history.history['PSNR']) plt.plot(history.history['val_PSNR']) plt.title('Model PSNR') plt.ylabel('PSNR') plt.xlabel('Epoch') plt.legend(['Train', 'validation'], loc='upper left') # plt.show() plt.savefig('PSNR.png')
def tcn(args): len_global, len_local, neighbor_size, is_BN, nb_res_unit, num_cnn, num_tcn, nb_stacks, dataset, width, height = \ args['len_global'], args['len_local'], args['neighbor_size'], args['is_BN'], \ args['nb_res_unit'], args['num_cnn'], args['num_tcn'], args['nb_stacks'], \ args['dataset'], args['width'], args['height'] main_input = [] neighbor_slide_len = neighbor_size * 2 + 1 local_feature_list = [] # ============================== # deal with the global local external data if dataset == 'bj_taxi': g_vacation = Input(shape=(len_global, ), dtype='int32', name='g_vacation') g_hour = Input(shape=(len_global, ), dtype='int32', name='g_hour') g_dayOfWeek = Input(shape=(len_global, ), dtype='int32', name='g_dayOfWeek') g_weather = Input(shape=(len_global, ), dtype='int32', name='g_weather') g_continuous_external = Input(shape=(len_global, 2), dtype='float32', name='g_continuous_external') main_input.append(g_vacation) main_input.append(g_hour) main_input.append(g_dayOfWeek) main_input.append(g_weather) main_input.append(g_continuous_external) embed_g_holiday = Embedding(output_dim=2, input_dim=2, input_length=len_global)(g_vacation) embed_g_hour = Embedding(output_dim=2, input_dim=25, input_length=len_global)(g_hour) embed_g_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_global)(g_dayOfWeek) embed_g_weather = Embedding(output_dim=2, input_dim=17, input_length=len_global)(g_weather) g_external = merge.Concatenate(axis=-1)([ embed_g_holiday, embed_g_hour, embed_g_dayOfWeek, embed_g_weather, g_continuous_external ]) else: g_hour = Input(shape=(len_global, ), dtype='int32', name='g_hour') g_dayOfWeek = Input(shape=(len_global, ), dtype='int32', name='g_dayOfWeek') main_input.append(g_hour) main_input.append(g_dayOfWeek) embed_g_hour = Embedding(output_dim=2, input_dim=25, input_length=len_global)(g_hour) embed_g_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_global)(g_dayOfWeek) g_external = merge.Concatenate(axis=-1)( [embed_g_hour, embed_g_dayOfWeek]) g_out = Flatten()(g_external) g_out = Dense(units=50)(g_out) g_out = Dropout(rate=0.1)(g_out) g_out = Activation('relu')(g_out) g_out = Dense(units=2 * width * height)(g_out) g_out = Activation('relu')(g_out) feature_external_g = Reshape((2, width, height))(g_out) # ============================== # deal with the local external data if dataset == 'bj_taxi': t_vacation = Input(shape=(len_local, ), dtype='int32', name='t_vacation') t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour') t_dayOfWeek = Input(shape=(len_local, ), dtype='int32', name='t_dayOfWeek') t_weather = Input(shape=(len_local, ), dtype='int32', name='t_weather') t_continuous_external = Input(shape=(len_local, 2), dtype='float32', name='t_continuous_external') main_input.append(t_vacation) main_input.append(t_hour) main_input.append(t_dayOfWeek) main_input.append(t_weather) main_input.append(t_continuous_external) embed_t_holiday = Embedding(output_dim=2, input_dim=2, input_length=len_local)(t_vacation) embed_t_hour = Embedding(output_dim=2, input_dim=25, input_length=len_local)(t_hour) embed_t_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_local)(t_dayOfWeek) embed_t_weather = Embedding(output_dim=2, input_dim=17, input_length=len_local)(t_weather) t_external = merge.Concatenate(axis=-1)([ embed_t_holiday, embed_t_hour, embed_t_dayOfWeek, embed_t_weather, t_continuous_external ]) else: t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour') t_dayOfWeek = Input(shape=(len_local, ), dtype='int32', name='t_dayOfWeek') main_input.append(t_hour) main_input.append(t_dayOfWeek) embed_t_hour = Embedding(output_dim=2, input_dim=25, input_length=len_local)(t_hour) embed_t_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_local)(t_dayOfWeek) t_external = merge.Concatenate(axis=-1)( [embed_t_hour, embed_t_dayOfWeek]) t_out = Flatten()(t_external) t_out = Dense(units=50)(t_out) t_out = Dropout(rate=0.1)(t_out) t_out = Activation('relu')(t_out) t_out = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(t_out) t_out = Activation('relu')(t_out) feature_external_t = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(t_out) # ============================== # deal with the current local flow data current_local_flow = \ Input(shape=(2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='current_local_flow') main_input.append(current_local_flow) cnn_out = cnn_unit(is_BN, num_cnn)(current_local_flow) activation = Activation('relu')(cnn_out) feature_current_local_flow = Conv2D(2, (3, 3), strides=(1, 1), padding='same')(activation) local_feature_list.append(feature_current_local_flow) # ============================== # deal with the local stacked flow data stack_local_flow = \ Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow') main_input.append(stack_local_flow) tcn_out = Reshape( (len_local, 2 * neighbor_slide_len * neighbor_slide_len))(stack_local_flow) for i in range(num_tcn): if i == num_tcn - 1 or (i == 0 and num_tcn == 1): tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=False)(tcn_out) else: tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=True)(tcn_out) o = Dense(units=(2 * neighbor_slide_len * neighbor_slide_len))(tcn_out) feature_stacked_local_flow = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(o) local_feature_list.append(feature_stacked_local_flow) # ============================== # fuse local stacked flow feature and current local flow feature, then fuse local external feature local_output = [] for feature in local_feature_list: local_output.append(ilayer()(feature)) feature_local_flow = merge.Add()(local_output) # todo consider the fuse method feature_local = merge.Add()([feature_external_t, feature_local_flow]) # ============================== # deal with the global flow data, then fuse global external feature global_flow = \ Input(shape=(len_global * 2, width, height), dtype='float32', name='global_flow') main_input.append(global_flow) # todo consider the model here conv1 = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(global_flow) activation = Activation('relu')(conv1) res_out = rest_unit(is_BN, nb_res_unit)(activation) activation = Activation('relu')(res_out) feature_global_flow = Conv2D(2, (3, 3), strides=(1, 1), padding='same')(activation) # todo consider the fuse method feature_global = merge.Add()([feature_external_g, feature_global_flow]) # reshape the global feaure feature_global = Flatten()(feature_global) feature_global = Dense(units=200)(feature_global) feature_global = Dropout(rate=0.1)(feature_global) feature_global = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(feature_global) feature_global = Activation('relu')(feature_global) feature_global = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(feature_global) # ============================== # fuse local and global feature to predict # index_cut = Input(shape=(2,), dtype='int32', name='index_cut') # main_input.append(index_cut) # row, column = index_cut[0].eval(), index_cut[1].eval(session=K.get_session()) # fuse the global and local feature output = [] for feature in [feature_global, feature_local]: output.append(ilayer()(feature)) main_output = merge.Add()(output) main_output = Activation('tanh')(main_output) model = Model(inputs=main_input, outputs=main_output, name='GLST-Net') return model
def tcn_nog_rdw_att(args): len_recent, len_daily, len_week, neighbor_size, num_tcn, nb_stacks, is_BN, num_cnn, dataset = \ args['len_recent'], args['len_daily'], args['len_week'], args['neighbor_size'], \ args['num_tcn'], args['nb_stacks'], args['BN'], args['num_cnn'], args['dataset'] main_input = [] neighbor_slide_len = neighbor_size * 2 + 1 local_feature_list = [] # ============================== # deal with the local external data if dataset == 'bj_taxi': # current current_vacation = Input(shape=(1, ), dtype='int32', name='current_vacation') current_hour = Input(shape=(1, ), dtype='int32', name='current_hour') current_dayOfWeek = Input(shape=(1, ), dtype='int32', name='current_dayOfWeek') current_weather = Input(shape=(1, ), dtype='int32', name='current_weather') current_continuous_external = Input(shape=(1, 2), dtype='float32', name='current_continuous_external') main_input.append(current_vacation) main_input.append(current_hour) main_input.append(current_dayOfWeek) main_input.append(current_weather) main_input.append(current_continuous_external) embed_current_vacation = Embedding(output_dim=2, input_dim=2, input_length=1)(current_vacation) embed_current_hour = Embedding(output_dim=2, input_dim=25, input_length=1)(current_hour) embed_current_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=1)(current_dayOfWeek) embed_current_weather = Embedding(output_dim=2, input_dim=17, input_length=1)(current_weather) current_external = merge.Concatenate(axis=-1)([ embed_current_vacation, embed_current_hour, embed_current_dayOfWeek, embed_current_weather, current_continuous_external ]) # recent external recent_vacation = Input(shape=(len_recent, ), dtype='int32', name='recent_vacation') recent_hour = Input(shape=(len_recent, ), dtype='int32', name='recent_hour') recent_dayOfWeek = Input(shape=(len_recent, ), dtype='int32', name='recent_dayOfWeek') recent_weather = Input(shape=(len_recent, ), dtype='int32', name='recent_weather') recent_continuous_external = Input(shape=(len_recent, 2), dtype='float32', name='recent_continuous_external') main_input.append(recent_vacation) main_input.append(recent_hour) main_input.append(recent_dayOfWeek) main_input.append(recent_weather) main_input.append(recent_continuous_external) embed_recent_vacation = Embedding( output_dim=2, input_dim=2, input_length=len_recent)(recent_vacation) embed_recent_hour = Embedding(output_dim=2, input_dim=25, input_length=len_recent)(recent_hour) embed_recent_dayOfWeek = Embedding( output_dim=2, input_dim=7, input_length=len_recent)(recent_dayOfWeek) embed_recent_weather = Embedding( output_dim=2, input_dim=17, input_length=len_recent)(recent_weather) recent_external = merge.Concatenate(axis=-1)([ embed_recent_vacation, embed_recent_hour, embed_recent_dayOfWeek, embed_recent_weather, recent_continuous_external ]) # daily external daily_vacation = Input(shape=(len_daily, ), dtype='int32', name='daily_vacation') daily_hour = Input(shape=(len_daily, ), dtype='int32', name='daily_hour') daily_dayOfWeek = Input(shape=(len_daily, ), dtype='int32', name='daily_dayOfWeek') daily_weather = Input(shape=(len_daily, ), dtype='int32', name='daily_weather') daily_continuous_external = Input(shape=(len_daily, 2), dtype='float32', name='daily_continuous_external') main_input.append(daily_vacation) main_input.append(daily_hour) main_input.append(daily_dayOfWeek) main_input.append(daily_weather) main_input.append(daily_continuous_external) embed_daily_vacation = Embedding( output_dim=2, input_dim=2, input_length=len_daily)(daily_vacation) embed_daily_hour = Embedding(output_dim=2, input_dim=25, input_length=len_daily)(daily_hour) embed_daily_dayOfWeek = Embedding( output_dim=2, input_dim=7, input_length=len_daily)(daily_dayOfWeek) embed_daily_weather = Embedding(output_dim=2, input_dim=17, input_length=len_daily)(daily_weather) daily_external = merge.Concatenate(axis=-1)([ embed_daily_vacation, embed_daily_hour, embed_daily_dayOfWeek, embed_daily_weather, daily_continuous_external ]) # weekly external week_vacation = Input(shape=(len_week, ), dtype='int32', name='week_vacation') week_hour = Input(shape=(len_week, ), dtype='int32', name='week_hour') week_dayOfWeek = Input(shape=(len_week, ), dtype='int32', name='week_dayOfWeek') week_weather = Input(shape=(len_week, ), dtype='int32', name='week_weather') week_continuous_external = Input(shape=(len_week, 2), dtype='float32', name='week_continuous_external') main_input.append(week_vacation) main_input.append(week_hour) main_input.append(week_dayOfWeek) main_input.append(week_weather) main_input.append(week_continuous_external) embed_week_vacation = Embedding(output_dim=2, input_dim=2, input_length=len_week)(week_vacation) embed_week_hour = Embedding(output_dim=2, input_dim=25, input_length=len_week)(week_hour) embed_week_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_week)(week_dayOfWeek) embed_week_weather = Embedding(output_dim=2, input_dim=17, input_length=len_week)(week_weather) week_external = merge.Concatenate(axis=-1)([ embed_week_vacation, embed_week_hour, embed_week_dayOfWeek, embed_week_weather, week_continuous_external ]) else: # current current_hour = Input(shape=(1, ), dtype='int32', name='current_hour') current_dayOfWeek = Input(shape=(1, ), dtype='int32', name='current_dayOfWeek') main_input.append(current_hour) main_input.append(current_dayOfWeek) embed_current_hour = Embedding(output_dim=2, input_dim=25, input_length=1)(current_hour) embed_current_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=1)(current_dayOfWeek) current_external = merge.Concatenate(axis=-1)( [embed_current_hour, embed_current_dayOfWeek]) # recent external recent_hour = Input(shape=(len_recent, ), dtype='int32', name='recent_hour') recent_dayOfWeek = Input(shape=(len_recent, ), dtype='int32', name='recent_dayOfWeek') main_input.append(recent_hour) main_input.append(recent_dayOfWeek) embed_recent_hour = Embedding(output_dim=2, input_dim=25, input_length=len_recent)(recent_hour) embed_recent_dayOfWeek = Embedding( output_dim=2, input_dim=7, input_length=len_recent)(recent_dayOfWeek) recent_external = merge.Concatenate(axis=-1)( [embed_recent_hour, embed_recent_dayOfWeek]) # daily external daily_hour = Input(shape=(len_daily, ), dtype='int32', name='daily_hour') daily_dayOfWeek = Input(shape=(len_daily, ), dtype='int32', name='daily_dayOfWeek') main_input.append(daily_hour) main_input.append(daily_dayOfWeek) embed_daily_hour = Embedding(output_dim=2, input_dim=25, input_length=len_daily)(daily_hour) embed_daily_dayOfWeek = Embedding( output_dim=2, input_dim=7, input_length=len_daily)(daily_dayOfWeek) daily_external = merge.Concatenate(axis=-1)( [embed_daily_hour, embed_daily_dayOfWeek]) # weekly external week_hour = Input(shape=(len_week, ), dtype='int32', name='week_hour') week_dayOfWeek = Input(shape=(len_week, ), dtype='int32', name='week_dayOfWeek') main_input.append(week_hour) main_input.append(week_dayOfWeek) embed_week_hour = Embedding(output_dim=2, input_dim=25, input_length=len_week)(week_hour) embed_week_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_week)(week_dayOfWeek) week_external = merge.Concatenate(axis=-1)( [embed_week_hour, embed_week_dayOfWeek]) current_out = Flatten()(current_external) current_out = Dense(units=20)(current_out) current_out = Dropout(rate=0.1)(current_out) current_out = Activation('relu')(current_out) current_out = Dense(units=10)(current_out) feature_external_current = Activation('relu')(current_out) recent_out = Dense(units=20)(recent_external) recent_out = Dropout(rate=0.1)(recent_out) recent_out = Activation('relu')(recent_out) recent_out = Dense(units=10)(recent_out) feature_external_recent = Activation('relu')(recent_out) daily_out = Dense(units=20)(daily_external) daily_out = Dropout(rate=0.1)(daily_out) daily_out = Activation('relu')(daily_out) daily_out = Dense(units=10)(daily_out) feature_external_daily = Activation('relu')(daily_out) week_out = Dense(units=20)(week_external) week_out = Dropout(rate=0.1)(week_out) week_out = Activation('relu')(week_out) week_out = Dense(units=10)(week_out) feature_external_week = Activation('relu')(week_out) # ============================== # deal with the current local flow data current_local_flow = \ Input(shape=(2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='current_local_flow') main_input.append(current_local_flow) current_local_flow = cnn_unit(is_BN, num_cnn)(current_local_flow) current_local_flow = Activation('relu')(current_local_flow) current_local_flow = Conv2D(2, (3, 3), strides=(1, 1), padding='same')(current_local_flow) current_local_flow = Flatten()(current_local_flow) current_local_flow = merge.Concatenate(axis=-1)( [feature_external_current, current_local_flow]) current_local_flow = Dense(units=64, activation='relu')(current_local_flow) local_feature_list.append(current_local_flow) # ============================== # deal with the recent stacked flow data recent_local_flow = \ Input(shape=(len_recent * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='recent_local_flow') main_input.append(recent_local_flow) recent_local_flow = Reshape( (len_recent, 2 * neighbor_slide_len * neighbor_slide_len))(recent_local_flow) recent_local_flow = merge.Concatenate(axis=-1)( [feature_external_recent, recent_local_flow]) recent_local_flow = Dense(units=64, activation='relu')(recent_local_flow) # deal with the daily stacked flow data daily_local_flow = \ Input(shape=(len_daily * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='daily_local_flow') main_input.append(daily_local_flow) daily_local_flow = Reshape( (len_daily, 2 * neighbor_slide_len * neighbor_slide_len))(daily_local_flow) daily_local_flow = merge.Concatenate(axis=-1)( [feature_external_daily, daily_local_flow]) daily_local_flow = Dense(units=64, activation='relu')(daily_local_flow) # deal with the week stacked flow data week_local_flow = \ Input(shape=(len_week * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='week_local_flow') main_input.append(week_local_flow) week_local_flow = Reshape( (len_week, 2 * neighbor_slide_len * neighbor_slide_len))(week_local_flow) week_local_flow = merge.Concatenate(axis=-1)( [feature_external_week, week_local_flow]) week_local_flow = Dense(units=64, activation='relu')(week_local_flow) for i in range(num_tcn): recent_local_flow = TCN(nb_stacks=nb_stacks, return_sequences=True)(recent_local_flow) daily_local_flow = TCN(nb_stacks=nb_stacks, return_sequences=True)(daily_local_flow) week_local_flow = TCN(nb_stacks=nb_stacks, return_sequences=True)(daily_local_flow) attention_hidden_recent = Lambda(lambda x: x[:, :])(recent_local_flow) attention_hidden_daily = Lambda(lambda x: x[:, :])(daily_local_flow) attention_hidden_week = Lambda(lambda x: x[:, :])(week_local_flow) att_recent = Attention()([attention_hidden_recent, current_local_flow]) att_daily = Attention()([attention_hidden_daily, current_local_flow]) att_week = Attention()([attention_hidden_week, current_local_flow]) # fuse attention output = [] for att in [att_recent, att_daily, att_week]: output.append(ilayer()(att)) att_final = merge.Add()(output) att_current = merge.Concatenate(axis=-1)([att_final, current_local_flow]) att_current = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(att_current) att_current = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(att_current) main_output = Activation('tanh')(att_current) model = Model(inputs=main_input, outputs=main_output, name='tcn_nog_rdw_att') return model
def tcn_no_global(args): len_local, neighbor_size, num_tcn, nb_stacks, is_BN, num_cnn, dataset = \ args['len_local'], args['neighbor_size'], args['num_tcn'], args['nb_stacks'], \ args['is_BN'], args['num_cnn'], args['dataset'] main_input = [] neighbor_slide_len = neighbor_size * 2 + 1 local_feature_list = [] # ============================== # deal with the local external data if dataset == 'bj_taxi': t_vacation = Input(shape=(len_local, ), dtype='int32', name='t_vacation') t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour') t_dayOfWeek = Input(shape=(len_local, ), dtype='int32', name='t_dayOfWeek') t_weather = Input(shape=(len_local, ), dtype='int32', name='t_weather') t_continuous_external = Input(shape=(len_local, 2), dtype='float32', name='t_continuous_external') main_input.append(t_vacation) main_input.append(t_hour) main_input.append(t_dayOfWeek) main_input.append(t_weather) main_input.append(t_continuous_external) embed_t_holiday = Embedding(output_dim=2, input_dim=2, input_length=len_local)(t_vacation) embed_t_hour = Embedding(output_dim=2, input_dim=25, input_length=len_local)(t_hour) embed_t_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_local)(t_dayOfWeek) embed_t_weather = Embedding(output_dim=2, input_dim=17, input_length=len_local)(t_weather) t_external = merge.Concatenate(axis=-1)([ embed_t_holiday, embed_t_hour, embed_t_dayOfWeek, embed_t_weather, t_continuous_external ]) else: t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour') t_dayOfWeek = Input(shape=(len_local, ), dtype='int32', name='t_dayOfWeek') main_input.append(t_hour) main_input.append(t_dayOfWeek) embed_t_hour = Embedding(output_dim=2, input_dim=25, input_length=len_local)(t_hour) embed_t_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_local)(t_dayOfWeek) t_external = merge.Concatenate(axis=-1)( [embed_t_hour, embed_t_dayOfWeek]) t_out = Flatten()(t_external) t_out = Dense(units=50)(t_out) t_out = Dropout(rate=0.1)(t_out) t_out = Activation('relu')(t_out) t_out = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(t_out) t_out = Activation('relu')(t_out) feature_external_t = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(t_out) # ============================== # deal with the current local flow data current_local_flow = \ Input(shape=(2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='current_local_flow') main_input.append(current_local_flow) cnn_out = cnn_unit(is_BN, num_cnn)(current_local_flow) activation = Activation('relu')(cnn_out) feature_current_local_flow = Conv2D(2, (3, 3), strides=(1, 1), padding='same')(activation) local_feature_list.append(feature_current_local_flow) # ============================== # deal with the local stacked flow data stack_local_flow = \ Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow') main_input.append(stack_local_flow) tcn_out = Reshape( (len_local, 2 * neighbor_slide_len * neighbor_slide_len))(stack_local_flow) for i in range(num_tcn): if i == num_tcn - 1 or (i == 0 and num_tcn == 1): tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=False)(tcn_out) else: tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=True)(tcn_out) o = Dense(units=(2 * neighbor_slide_len * neighbor_slide_len))(tcn_out) feature_stacked_local_flow = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(o) local_feature_list.append(feature_stacked_local_flow) # ============================== # fuse local stacked flow feature and current local flow feature, then fuse local external feature local_output = [] for feature in local_feature_list: local_output.append(ilayer()(feature)) feature_local_flow = merge.Add()(local_output) feature_local = merge.Add()([feature_external_t, feature_local_flow]) main_output = Activation('tanh')(feature_local) model = Model(inputs=main_input, outputs=main_output, name='tcn_nog') return model
def simple_tcn(args): len_local, neighbor_size, num_tcn, nb_stacks, dataset = \ args['len_local'], args['neighbor_size'], args['num_tcn'], args['nb_stacks'], args['dataset'] main_input = [] neighbor_slide_len = neighbor_size * 2 + 1 # ============================== # deal with the local external data if dataset == 'bj_taxi': t_vacation = Input(shape=(len_local, ), dtype='int32', name='t_vacation') t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour') t_dayOfWeek = Input(shape=(len_local, ), dtype='int32', name='t_dayOfWeek') t_weather = Input(shape=(len_local, ), dtype='int32', name='t_weather') t_continuous_external = Input(shape=(len_local, 2), dtype='float32', name='t_continuous_external') main_input.append(t_vacation) main_input.append(t_hour) main_input.append(t_dayOfWeek) main_input.append(t_weather) main_input.append(t_continuous_external) embed_t_holiday = Embedding(output_dim=2, input_dim=2, input_length=len_local)(t_vacation) embed_t_hour = Embedding(output_dim=2, input_dim=25, input_length=len_local)(t_hour) embed_t_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_local)(t_dayOfWeek) embed_t_weather = Embedding(output_dim=2, input_dim=17, input_length=len_local)(t_weather) t_external = merge.Concatenate(axis=-1)([ embed_t_holiday, embed_t_hour, embed_t_dayOfWeek, embed_t_weather, t_continuous_external ]) else: t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour') t_dayOfWeek = Input(shape=(len_local, ), dtype='int32', name='t_dayOfWeek') main_input.append(t_hour) main_input.append(t_dayOfWeek) embed_t_hour = Embedding(output_dim=2, input_dim=25, input_length=len_local)(t_hour) embed_t_dayOfWeek = Embedding(output_dim=2, input_dim=7, input_length=len_local)(t_dayOfWeek) t_external = merge.Concatenate(axis=-1)( [embed_t_hour, embed_t_dayOfWeek]) t_out = Flatten()(t_external) t_out = Dense(units=50)(t_out) t_out = Dropout(rate=0.1)(t_out) t_out = Activation('relu')(t_out) t_out = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(t_out) t_out = Activation('relu')(t_out) feature_external_t = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(t_out) # ============================== # deal with the local stacked flow data stack_local_flow = \ Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow') main_input.append(stack_local_flow) tcn_out = Reshape( (len_local, 2 * neighbor_slide_len * neighbor_slide_len))(stack_local_flow) for i in range(num_tcn): if i == num_tcn - 1 or (i == 0 and num_tcn == 1): tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=False)(tcn_out) else: tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=True)(tcn_out) o = Dense(units=(2 * neighbor_slide_len * neighbor_slide_len))(tcn_out) feature_stacked_local_flow = Reshape( (2, neighbor_slide_len, neighbor_slide_len))(o) feature_local = merge.Add()( [feature_external_t, feature_stacked_local_flow]) main_output = Activation('tanh')(feature_local) model = Model(inputs=main_input, outputs=main_output, name='simple-tcn') return model