示例#1
0
def get_transformer_model(sample_length, step_ahead=3):
    # TODO make it as class

    def transformer_regression(units, x):
        query = Dense(8)(x)
        value = Dense(8)(x)
        key = Dense(8)(x)
        query, value, key = [
            tf.expand_dims(x, axis=1) for x in [query, value, key]
        ]
        x = Attention()([query, value, key])
        x = LayerNormalization()(x)
        x = GlobalAveragePooling1D(data_format='channels_last')(x)
        x = Dense(units)(x)
        return x

    in_seq = Input(shape=(sample_length, 3))
    timeof = doubleDense(sample_length, in_seq[:, :, 2])
    timeof2 = doubleDense(sample_length, in_seq[:, :, 2])
    river = transformer_regression(step_ahead,
                                   subtract([in_seq[:, :, 0], timeof]))
    rain = transformer_regression(step_ahead,
                                  subtract([in_seq[:, :, 1], timeof2]))
    out = add([rain, river])
    return Model(inputs=in_seq, outputs=out)
示例#2
0
def create_model(img_width=IMAGE_WIDTH, img_height=IMAGE_HEIGHT, resnet_weights="imagenet"):

    input_1 = Input(shape=(img_height, img_width, 3,))
    input_2 = Input(shape=(img_height, img_width, 3,))
    input_3 = Input(shape=(img_height, img_width, 3,))


    res_net = ResNet50(include_top=False,
                       pooling='avg',
                       weights=resnet_weights,
                       input_shape=(img_height, img_width, 3))
    for layer in res_net.layers:
        layer.trainable = False

    tower_1 = Model(inputs=res_net.input, outputs=res_net.layers[-1].output, name='resnet50_1')(input_1)
    tower_2 = Model(inputs=res_net.input, outputs=res_net.layers[-1].output, name='resnet50_2')(input_2)
    tower_3 = Model(inputs=res_net.input, outputs=res_net.layers[-1].output, name='resnet50_3')(input_3)
    #tower_1.trainable = False
    #tower_2.trainable = False
    #tower_3.trainable = False

    #tower_1 = ResNet50(include_top=False,
    #                   pooling='avg',
    #                   weights=resnet_weights) \
    #          (input_1)
    #tower_1 = MaxPooling2D((1, 9), strides=(1, 1), padding='same')(tower_1)

    #tower_2 = ResNet50(include_top=False,
    #                   pooling='avg',
    #                   weights=resnet_weights) \
    #          (input_2)
    ##tower_2 = MaxPooling2D((1, 9), strides=(1, 1), padding='same')(tower_2)
    #tower_2.trainable = False

    #tower_3 = ResNet50(include_top=False,
    #                   pooling='avg',
    #                   weights=resnet_weights) \
    #          (input_3)
    ##tower_3 = MaxPooling2D((1, 6), strides=(1, 1), padding='same')(tower_3)
    #tower_3.trainable = False

    difference_1 = subtract([tower_2, tower_1])
    difference_2 = subtract([tower_3, tower_1])

    #merged = concatenate([tower_1, tower_2, tower_3], axis=1)
    merged = concatenate([difference_1, difference_2], axis=1)
    merged = Flatten()(merged)
    merged = Dropout(0.2)(merged)

    out = Dense(20, activation='relu')(merged)
    #out1 = Dropout(0.2)(out1)
    #out2 = Dense(20, activation='relu')(out2)
    out = Dense(2, activation='softmax')(out)

    #model = Model(input_shape, out)
    model = Model(inputs=[input_1, input_2, input_3], outputs=[out])

    return model
示例#3
0
def createModel(inputSize):
    inputs = keras.Input(shape=(inputSize,
                                NeuralAI.NbOperators, NeuralAI.OpeStateSize + 3),
                         name="img")  # shape (H, W, C)
    norm = tf.keras.layers.LayerNormalization()(inputs)
    x = tf.keras.layers.Conv2D(128, kernel_size=3, padding='same', kernel_initializer="random_normal")(norm)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.Activation('relu')(x)
    block_1_output = tf.keras.layers.MaxPool2D(pool_size=3, padding='same')(x)

    block_2_output = ResnetBlock(128, 1, first_block=True)(block_1_output)

    # block_3_output = ResnetBlock(256, 2)(block_2_output)

    dense_input = layers.GlobalAveragePooling2D()(block_2_output)  # block_3_output
    dense_1 = layers.Dense(256, activation="relu")(dense_input)
    x = layers.Dropout(0.5)(dense_1)
    dense_2 = layers.Dense(NeuralAI.MaxDepth + 1)(x)
    main_output = layers.Activation("softmax", name="main_output")(dense_2)

    # reverse network
    x = layers.Dense(256, activation="relu")(dense_2)
    output_0 = layers.subtract([x, dense_1], name="output_0")

    x = layers.Dropout(0.5)(x)
    x = layers.Dense(128, activation="relu")(x)
    output_1 = layers.subtract([x, dense_input], name="output_1")

    x = tf.keras.layers.Reshape((1, 1, 128))(x)
    x = tf.keras.layers.UpSampling2D((16, 5))(x)
    """x = DeResnetBlock(64, 2)(x)
    output_2 = layers.subtract([x, block_2_output], name="output_2")"""

    # x = tf.keras.layers.MaxPool2D((2, 2), strides=2)(x)
    x = DeResnetBlock(128, 1)(x)
    output_2 = layers.subtract([x, block_1_output], name="output_2")

    x = tf.keras.layers.UpSampling2D((3, 3))(x)
    x = tf.keras.layers.MaxPool2D(3, strides=1)(x)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.Conv2DTranspose(NeuralAI.OpeStateSize + 3, kernel_size=(2, 3),
                                        kernel_initializer="random_normal")(x)
    output_3 = layers.subtract([x, norm], name="output_3")

    """model = keras.Model(inputs, outputs, name="deepai")
    opt = keras.optimizers.Adam(learning_rate=5 * 10 ** (-5))
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])"""

    trainModel = keras.Model(inputs, [main_output, output_0, output_1, output_2, output_3], name="deepai_train")
    """opt = keras.optimizers.Adam(learning_rate=1 * 10 ** (-5))
    trainModel.compile(loss=['categorical_crossentropy', 'mse'], loss_weights=[10, 10], optimizer=opt,
                       metrics=['accuracy'])"""
    # trainModel.summary()
    # model = extractTestModel(trainModel)
    return trainModel
示例#4
0
    def construct_q_network(self):
        input_layer = tfk.Input(shape = (self.observation_size * self.num_frames,), name="input_obs")
        lay1 = tfkl.Dense(self.observation_size * 2, name="fc_1")(input_layer)
        lay1 = tfka.relu(lay1, alpha=0.01) #leaky_relu

        lay2 = tfkl.Dense(self.observation_size, name="fc_2")(lay1)
        lay2 = tfka.relu(lay2, alpha=0.01) #leaky_relu

        lay3 = tfkl.Dense(self.action_size * 3, name="fc_3")(lay2)
        lay3 = tfka.relu(lay3, alpha=0.01) #leaky_relu

        advantage = tfkl.Dense(self.action_size * 2, name="fc_adv")(lay3)
        advantage = tfkl.Dense(self.action_size, name="adv")(advantage)

        value = tfkl.Dense(self.action_size * 2, name="fc_val")(lay3)
        value = tfkl.Dense(1, name="val")(value)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True, name="adv_mean")
        advantage = tfkl.subtract([advantage, advantage_mean], name="adv_subtract")
        Q = tf.math.add(value, advantage, name="Qout")

        self.model = tfk.Model(inputs=[input_layer], outputs=[Q],
                               name=self.__class__.__name__)

        # Backwards pass
        self.schedule = tfko.schedules.InverseTimeDecay(self.lr, self.lr_decay_steps, self.lr_decay_rate)
        self.optimizer = tfko.Adam(learning_rate=self.schedule)
示例#5
0
        def ls(yt, yp):
            f = dot([self.y2, self.x2], axes=-1, normalize=True)
            fg = dot([self.g2, self.x2], axes=-1, normalize=True)

            r = maximum(0.0, 0.3 + subtract([fg, f]))
            r = sum(r, axis=-1)
            return mean(r) # batch
示例#6
0
    def construct_q_network(self):
        """
        It uses the architecture defined in the `nn_archi` attributes.

        """
        self._model = Sequential()
        input_layer = Input(shape=(self._nn_archi.observation_size, ),
                            name="observation")

        lay = input_layer
        for lay_num, (size, act) in enumerate(
                zip(self._nn_archi.sizes, self._nn_archi.activs)):
            lay = Dense(size, name="layer_{}".format(lay_num))(
                lay)  # put at self.action_size
            lay = Activation(act)(lay)

        fc1 = Dense(self._action_size)(lay)
        advantage = Dense(self._action_size, name="advantage")(fc1)

        fc2 = Dense(self._action_size)(lay)
        value = Dense(1, name="value")(fc2)

        meaner = Lambda(lambda x: K.mean(x, axis=1))
        mn_ = meaner(advantage)
        tmp = subtract([advantage, mn_])
        policy = add([tmp, value], name="policy")

        self._model = Model(inputs=[input_layer], outputs=[policy])
        self._schedule_model, self._optimizer_model = self.make_optimiser()
        self._model.compile(loss='mse', optimizer=self._optimizer_model)

        self._target_model = Model(inputs=[input_layer], outputs=[policy])
示例#7
0
    def construct_q_network(self):
        input_layer = tfk.Input(shape=(self.observation_size *
                                       self.num_frames, ))
        lay1 = tfkl.Dense(self.observation_size * self.num_frames)(input_layer)

        lay2 = tfkl.Dense(512)(lay1)
        lay2 = tfka.relu(lay2, alpha=0.01)  #leaky_relu

        lay3 = tfkl.Dense(256)(lay2)
        lay3 = tfka.relu(lay3, alpha=0.01)  #leaky_relu

        lay4 = tfkl.Dense(128)(lay3)
        lay4 = tfka.relu(lay4, alpha=0.01)  #leaky_relu

        advantage = tfkl.Dense(64)(lay4)
        advantage = tfka.relu(advantage, alpha=0.01)  #leaky_relu
        advantage = tfkl.Dense(self.action_size)(advantage)

        value = tfkl.Dense(64)(lay4)
        value = tfka.relu(value, alpha=0.01)  #leaky_relu
        value = tfkl.Dense(1)(value)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True)
        advantage = tfkl.subtract([advantage, advantage_mean])
        Q = tf.math.add(value, advantage)

        self.model = tfk.Model(inputs=[input_layer], outputs=[Q])
        self.model.compile(loss='mse', optimizer=tfko.Adam(lr=self.lr))
示例#8
0
    def DownProj(h_in, num_filters, kernel_size=12):


        l0 = Conv2D(num_filters, kernel_size=kernel_size, strides=(scale_ratio,scale_ratio), padding='same',
                    kernel_initializer=test_initializer,
                    kernel_regularizer=l2(reg_scale))(h_in)
        #l0 = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(l0)
        l0 = LeakyReLU(alpha=0.01)(l0)

        h0 = Conv2DTranspose(num_filters, kernel_size=kernel_size, strides=(scale_ratio, scale_ratio), padding='same',
                             kernel_initializer=test_initializer,
                             kernel_regularizer=l2(reg_scale))(l0)
        #h0 = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(h0)
        h0 = LeakyReLU(alpha=0.01)(h0)

        e0 = subtract([h0, h_in])

        l1 = Conv2D(num_filters, kernel_size=kernel_size, strides=(scale_ratio,scale_ratio), padding='same',
                    kernel_initializer=test_initializer,
                    kernel_regularizer=l2(reg_scale))(e0)
        #l1 = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(l1)
        l1 = LeakyReLU(alpha=0.01)(l1)

        out = add([l1, l0])

        return out
示例#9
0
    def DenseDownProj(x_in, num_filters, kernel_size=12):
        h_in = Conv2D(num_filters, kernel_size=1, strides=1, padding='same',
                      kernel_initializer=test_initializer,
                      kernel_regularizer=l2(reg_scale))(x_in)
        h_in = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(h_in)

        l0 = Conv2D(num_filters, kernel_size=kernel_size, strides=(8,8), padding='same',
                    kernel_initializer=test_initializer,
                    kernel_regularizer=l2(reg_scale))(h_in)
        l0 = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(l0)

        h0 = Conv2DTranspose(num_filters, kernel_size=kernel_size, strides=(8, 8), padding='same',
                             kernel_initializer=test_initializer,
                             kernel_regularizer=l2(reg_scale))(l0)
        h0 = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(h0)

        e0 = subtract([h0, h_in])

        l1 = Conv2D(num_filters, kernel_size=kernel_size, strides=(8,8), padding='same',
                    kernel_initializer=test_initializer,
                    kernel_regularizer=l2(reg_scale))(e0)
        l1 = PReLU(alpha_initializer='zero', shared_axes=[1, 2])(l1)

        out = add([l1, l0])

        return out
示例#10
0
    def construct_q_network(self):
        # Uses the network architecture found in DeepMind paper
        # The inputs and outputs size have changed, as well as replacing the convolution by dense layers.
        self.model = Sequential()

        input_layer = Input(shape=(self.observation_size *
                                   self.training_param.NUM_FRAMES, ))
        lay1 = Dense(self.observation_size *
                     self.training_param.NUM_FRAMES)(input_layer)
        lay1 = Activation('relu')(lay1)

        lay2 = Dense(self.observation_size)(lay1)
        lay2 = Activation('relu')(lay2)

        lay3 = Dense(2 * self.action_size)(lay2)
        lay3 = Activation('relu')(lay3)

        fc1 = Dense(self.action_size)(lay3)
        advantage = Dense(self.action_size)(fc1)
        fc2 = Dense(self.action_size)(lay3)
        value = Dense(1)(fc2)

        meaner = Lambda(lambda x: K.mean(x, axis=1))
        mn_ = meaner(advantage)
        tmp = subtract([advantage, mn_])
        policy = add([tmp, value])

        self.model = Model(inputs=[input_layer], outputs=[policy])
        self.model.compile(loss='mse', optimizer=Adam(lr=self.lr_))

        self.target_model = Model(inputs=[input_layer], outputs=[policy])
        self.target_model.compile(loss='mse', optimizer=Adam(lr=self.lr_))
        print("Successfully constructed networks.")
    def build_optical_synthesis_generator(img_size=256, noise_dim=4):
        """
        build the generator model that use the conventional reflection synthetic model.
        the generator with the optical synthesis prior will only accept a noise-map from the encoder and convert it to
        an (1) alpha blending mask for fusing the transmission layer T and reflection layer R. (2) convolution kernel
        that blurs the reflection layer
        :param img_size: image size for reflection image R, transmission layer T
        :param noise_dim: noise_dim to concat with the input image (T, R)
        :return: tf.keras.Model object. The generator model accepts three 4-D tensors: (1) T. (2) R. (3) noise layer.
        The generator model will output two tensors:
        (1) [alpha_blending_mask] with (256, 256, 3) for mixing two layers.
        (2) [conv-kernel] used for blurring the reflection layer.
        """
        in_layer = tf.keras.layers.Input(shape=(img_size, img_size, 3 + 3 + noise_dim))

        # noise_in = tf.keras.layers.Input(shape=(img_size, img_size, noise_dim))
        # T_in = tf.keras.layers.Input(shape=(img_size, img_size, 3))
        # R_in = tf.keras.layers.Input(shape=(img_size, img_size, 3))
        # split the input tensor
        T_in, R_in, noise_in = tf.split(in_layer, [3, 3, noise_dim], axis=3)
        ds1 = Component.get_conv_block(noise_dim, 32, norm=False)(noise_in)
        ds2 = Component.get_conv_block(32, 64)(ds1)
        ds3 = Component.get_conv_block(64, 128)(ds2)  # d3: (32, 32)
        ds4 = Component.get_conv_block(128, 256)(ds3)
        ds5 = Component.get_conv_block(256, 256)(ds4)
        ds6 = Component.get_conv_block(256, 256)(ds5)

        us1 = Component.get_deconv_block(256, 256)(ds6)
        us2 = Component.get_deconv_block(512, 256)(tf.concat([us1, ds5], axis=3))
        us3 = Component.get_deconv_block(512, 128)(tf.concat([us2, ds4], axis=3))
        us4 = Component.get_deconv_block(256, 64)(tf.concat([us3, ds3], axis=3))  # us4: (64, 64, 64)
        us5 = Component.get_deconv_block(128, 32)(tf.concat([us4, ds2], axis=3))  # us5: (128, 128, 32)

        # let us handle the conv kernel first
        # us5 ---conv--- (32, 32, 16) ---reshape---> (32, 32, 3, 3)
        # (1, 128, 128, 32) -> (1, 64, 64, 16)
        down1 = Component.get_conv_block(32, 16)(us5)

        # (1, 64, 64, 16) -> (1, 32, 32, 9)
        down2 = Component.get_conv_block(16, 9)(down1)

        kernel = tf.reshape(down2, [32, 32, 3, 3])

        # the alpha blending mask
        alpha_mask = Component.get_deconv_block(64, 3, norm=False, non_linear='leaky_relu')(
            tf.concat([us5, ds1], axis=3))
        alpha_mask_sub = layers.subtract([tf.ones_like(alpha_mask), alpha_mask])
        # alpha_mask_sub = Component.get_deconv_block(64, 3, norm=False, non_linear='leaky_relu')(
        #     tf.concat([us5, ds1], axis=3))
        # the blurring kernel
        blurred_R = tf.nn.conv2d(R_in, kernel, strides=[1, 1, 1, 1], padding='SAME')

        # transmission
        t_layer = layers.multiply([T_in, alpha_mask])
        r_layer = layers.multiply([blurred_R, alpha_mask_sub])

        out = layers.add([t_layer, r_layer])

        return tf.keras.Model(in_layer, out)
示例#12
0
def get_tcn_model(sample_length, step_ahead=3, inner_encoding=8):
    # TODO make it as class

    def TCN_layer(units, x):
        x = tf.expand_dims(x, axis=-1)
        x = TCN(inner_encoding, kernel_size=2, dilations=[1, 2, 4], use_skip_connections=True)(x)
        x = LayerNormalization()(x)
        x = Dense(units)(x)
        return x

    in_seq = Input(shape=(sample_length, 3))
    timeof = doubleDense(sample_length, in_seq[:, :, 2])
    timeof2 = doubleDense(sample_length, in_seq[:, :, 2])
    river = TCN_layer(step_ahead, subtract([in_seq[:, :, 0], timeof]))
    rain = TCN_layer(step_ahead, subtract([in_seq[:, :, 1], timeof2]))
    out = add([rain, river])
    return Model(inputs=in_seq, outputs=out)
示例#13
0
def DownBlock(x, num_filter, kernel_size=8, stride=4, padding=2, activation=LeakyReLU):
    down_conv1 = Conv2D(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding)
    down_conv2 = Conv2DTranspose(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding)
    down_conv3 = Conv2D(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding)
    l0 = activation()(down_conv1(x))
    h0 = activation()(down_conv2(l0))
    l1 = activation()(down_conv3(subtract([h0, x])))
    return add([l1, l0])
示例#14
0
def UpBlock(x, num_filter, kernel_size=8, stride=4, padding='same', activation=LeakyReLU):
    up_conv1 = Conv2DTranspose(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding)
    up_conv2 = Conv2D(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding)
    up_conv3 = Conv2DTranspose(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding)
    h0 = activation()(up_conv1(x))
    l0 = activation()(up_conv2(h0))
    h1 = activation()(up_conv3(subtract([l0, x])))
    return add([h1, h0])
示例#15
0
def get_lstm_model(sample_length, step_ahead=3, memory_units=8):
    # TODO make it as class

    def LSTM_layer_wrapper(units, x):
        x = tf.expand_dims(x, axis=-1)
        x = LSTM(units=memory_units)(x)
        x = LayerNormalization()(x)
        x = Dense(units)(x)
        return x

    in_seq = Input(shape=(sample_length, 3))
    timeof = doubleDense(sample_length, in_seq[:, :, 2])
    timeof2 = doubleDense(sample_length, in_seq[:, :, 2])
    river = LSTM_layer_wrapper(step_ahead, subtract([in_seq[:, :, 0], timeof]))
    rain = LSTM_layer_wrapper(step_ahead, subtract([in_seq[:, :, 1], timeof2]))
    out = add([river, rain])
    return Model(inputs=[in_seq], outputs=out)
示例#16
0
def get_feed_forward_model(sample_length=128, step_ahead=3, inner_dimension=8):
    # TODO make it as class

    def encoding(units, x):
        x = Dense(inner_dimension)(x)
        x = LayerNormalization()(x)
        x = Dense(units)(x)
        x = LayerNormalization()(x)
        return x

    in_seq = Input(shape=(sample_length, 3))
    timeof = doubleDense(sample_length, in_seq[:, :, 2])
    timeof2 = doubleDense(sample_length, in_seq[:, :, 2])
    river = encoding(step_ahead, subtract([in_seq[:, :, 0], timeof]))
    rain = encoding(step_ahead, subtract([in_seq[:, :, 1], timeof2]))
    out = add([rain, river])
    return Model(inputs=in_seq, outputs=out)
    def get_model(self):
        """
        用于构建完整模型,即build_feature + Dense
        :return:
            model
        """
        input_category = Input(shape=(self.category_count, ))
        input_query1 = Input(shape=(self.query_len, ))
        input_query2 = Input(shape=(self.query_len, ))

        # Layer1: 特征抽取层
        if self.shared:
            # 调用了1次Model,是双塔共享模式
            model = self.build_feature()

            query_1 = model(input_query1)
            query_2 = model(input_query2)

        else:
            # 调用了2次Model,是双塔非共享模型
            query_1 = self.build_feature()(input_query1)
            query_2 = self.build_feature()(input_query2)

        if self.add_feature:
            # |q1-q2| 两特征之差的绝对值
            sub = subtract([query_1, query_2])
            sub = tf.abs(sub)
            # q1*q2 两特征按元素相乘
            mul = multiply([query_1, query_2])
            # max(q1,q2)^2 两特征取最大元素的平方
            max_square = multiply(
                [maximum([query_1, query_2]),
                 maximum([query_1, query_2])])

            merge_layers = Concatenate()(
                [query_1, query_2, sub, mul, max_square, input_category])
        else:
            merge_layers = Concatenate()([query_1, query_2, input_category])

        # Layer2:全连接层
        fc = None
        for i in range(len(self.dense_units)):
            if i == 0:
                fc = Dense(self.dense_units[i],
                           activation="relu")(merge_layers)
            elif i == len(self.dense_units) - 1:
                fc = Dense(1, activation='sigmoid')(fc)
            else:
                fc = Dense(self.dense_units[i], activation="relu")(fc)

        model = Model(inputs=[input_category, input_query1, input_query2],
                      outputs=[fc])
        model.summary()

        return model
示例#18
0
    def construct_q_network(self):
        # Defines input tensors and scalars
        self.trace_length = tf.Variable(1, dtype=tf.int32)
        self.dropout_rate = tf.Variable(0.0, dtype=tf.float32, trainable=False)
        input_mem_state = tfk.Input(dtype=tf.float32,
                                    shape=(self.h_size),
                                    name='input_mem_state')
        input_carry_state = tfk.Input(dtype=tf.float32,
                                      shape=(self.h_size),
                                      name='input_carry_state')
        input_layer = tfk.Input(dtype=tf.float32,
                                shape=(None, self.observation_size),
                                name='input_obs')

        # Forward pass
        lay1 = tfkl.Dense(512)(input_layer)
        # Bayesian NN simulate
        lay1 = tfkl.Dropout(self.dropout_rate)(lay1)

        lay2 = tfkl.Dense(256)(lay1)
        lay2 = tfka.relu(lay2, alpha=0.01)  #leaky_relu

        lay3 = tfkl.Dense(128)(lay2)
        lay3 = tfka.relu(lay3, alpha=0.01)  #leaky_relu

        lay4 = tfkl.Dense(self.h_size)(lay3)

        # Recurring part
        lstm_layer = tfkl.LSTM(self.h_size, return_state=True)
        lstm_input = lay4
        lstm_state = [input_mem_state, input_carry_state]
        lay5, mem_s, carry_s = lstm_layer(lstm_input, initial_state=lstm_state)
        lstm_output = lay5

        # Advantage and Value streams
        advantage = tfkl.Dense(64)(lstm_output)
        advantage = tfka.relu(advantage, alpha=0.01)  #leaky_relu
        advantage = tfkl.Dense(self.action_size)(advantage)

        value = tfkl.Dense(64)(lstm_output)
        value = tfka.relu(value, alpha=0.01)  #leaky_relu
        value = tfkl.Dense(1)(value)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True)
        advantage = tfkl.subtract([advantage, advantage_mean])
        Q = tf.math.add(value, advantage)

        # Backwards pass
        self.model = tfk.Model(
            inputs=[input_mem_state, input_carry_state, input_layer],
            outputs=[Q, mem_s, carry_s])
        losses = ['mse', self.no_loss, self.no_loss]
        self.model.compile(loss=losses, optimizer=tfko.Adam(lr=self.lr))
        self.model.summary()
    def __init__(self, model_params):

        super(SiameseModel, self).__init__()
        max_len = model_params['max_len']
        embedding_matrix = model_params['embedding_matrix']
        num_unique_words = model_params['num_unique_words']
        embedding_dim = model_params['embedding_dim']
        self.sentence_1 = Input(shape=(None, ))
        self.sentence_2 = Input(shape=(None, ))
        self.sentence_1_embedding = Embedding(num_unique_words,embedding_dim,\
                                              embeddings_initializer=keras.initializers.Constant(embedding_matrix),\
                                              trainable=False)(self.sentence_1)
        self.sentence_2_embedding = Embedding(num_unique_words,embedding_dim,\
                                              embeddings_initializer=keras.initializers.Constant(embedding_matrix),\
                                              trainable=False)(self.sentence_2)
        self.len_sent_1 = Input(shape=(None, 1), dtype='int32')
        self.len_sent_2 = Input(shape=(None, 1), dtype='int32')
        #self.squared_diff = Multiply([tf_sum(self.difference),tf_sum(self.difference)])

        self.shared_lstm = LSTM(1000)
        self.question_1_encoding = self.shared_lstm(self.sentence_1_embedding)
        self.question_2_encoding = self.shared_lstm(self.sentence_2_embedding)
        self.Hadamard = Multiply()(
            [self.question_1_encoding, self.question_2_encoding])
        self.difference = subtract(
            [self.question_1_encoding, self.question_2_encoding])
        self.squared_euclidean_distance = tf_sum(
            Multiply()([self.difference, self.difference]), axis=1)
        self.squared_euclidean_distance = k.expand_dims(
            self.squared_euclidean_distance, axis=-1)
        self.features = concatenate([self.question_1_encoding, self.question_2_encoding,\
                                  self.Hadamard, self.squared_euclidean_distance],)
        self.h1 = Dense(800, activation='relu')(self.features)
        self.dropout1 = keras.layers.Dropout(0.7)(self.h1)
        self.h2 = Dense(800, activation='relu')(self.dropout1)
        self.dropout2 = keras.layers.Dropout(0.7)(self.h2)
        self.out = Dense(2, activation='softmax')(self.dropout2)
        initial_learning_rate = model_params['lr']
        decay_rate = model_params['lr_decay_rate']
        learning_rate = keras.optimizers.schedules.ExponentialDecay(
            initial_learning_rate,
            decay_steps=10000,
            decay_rate=decay_rate,
            staircase=True)
        optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
        self.model = Model(inputs=[
            self.sentence_1, self.sentence_2, self.len_sent_1, self.len_sent_2
        ],
                           outputs=[self.out])

        self.model.compile(loss="binary_crossentropy",
                           optimizer=optimizer,
                           metrics=['binary_accuracy', f1_m])
示例#20
0
 def create_stack(self,
                  input_tensor: Input,
                  stack_ix: int = 0,
                  stack_weights: str = 'none') -> Model:
     output_weights = self.create_output_weights(stack_weights)
     model = self.create_basic_block(input_tensor, 0, stack_ix)
     backcasts = [model.outputs[0]]
     forecasts = [model.outputs[1]]
     for i in range(1, self.block_length):
         input_tensor = subtract([model.input, model.outputs[0]])
         model = self.create_basic_block(input_tensor, i, stack_ix)
         backcasts.append(model.outputs[0])
         forecasts.append(model.outputs[1])
     return Model(input=input_tensor, output=add(forecasts))
示例#21
0
    def get_model(self):
        # 加载预训练模型
        bert = build_bert_model(
            config_path=self.config_path, checkpoint_path=self.checkpoint_path,
            with_pool=True, return_keras_model=False, model="albert")

        # query1 = bert.model()([q1_token_in, q1_seg_in])
        # query2 = bert.model()([q2_token_in, q2_seg_in])

        q1_x_in = Input(shape=(None, ), name='Input-Token-q1')
        q2_x_in = Input(shape=(None,), name='Input-Token-q2')
        q1_s_in = Input(shape=(None,), name='Input-Segment-q1')
        q2_s_in = Input(shape=(None,), name='Input-Segment-q2')



        input_layer = bert.model.input
        input_layer.extend(bert.model.input)

        query1 = Dropout(rate=0.1)(bert.model.output)
        query2 = Dropout(rate=0.1)(bert.model.output)

        # |q1-q2| 两特征之差的绝对值
        sub = tf.abs(subtract([query1, query2]))
        # q1*q2 两特征按元素相乘
        mul = multiply([query1, query2])
        # max(q1,q2)^2 两特征取最大元素的平方
        max_square = multiply([maximum([query1, query2]), maximum([query1, query2])])

        merge_layers = Concatenate()([query1, query2, sub, mul, max_square])

        merge_layers = Dropout(rate=0.1)(merge_layers)

        fc = None
        for i in range(len(self.dense_units)):
            if i == 0:
                fc = Dense(self.dense_units[i], activation="relu", kernel_initializer=bert.initializer)(merge_layers)
            elif i == len(self.dense_units) - 1:
                fc = Dense(units=2, activation='softmax', kernel_initializer=bert.initializer)(fc)
            else:
                fc = Dense(self.dense_units[i], activation="relu", kernel_initializer=bert.initializer)(fc)

        model = Model(input_layer, fc)
        model.summary()
        return model
示例#22
0
    def forward_streams(self, hidden, q_len, name):
        # Advantage stream
        advantage = tfkl.Dense(64, name=name+"_fcadv")(hidden)
        advantage = tf.nn.leaky_relu(advantage, alpha=0.01, name=name+"_leakyadv")
        advantage = tfkl.Dense(q_len, name=name+"_adv")(advantage)
        advantage_mean = tf.math.reduce_mean(advantage, axis=1,
                                             keepdims=True,
                                             name=name+"_adv_mean")
        advantage = tfkl.subtract([advantage, advantage_mean],
                                  name= name+"_adv_sub")

        # Value stream
        value = tfkl.Dense(64, name=name+"_fcval")(hidden)
        value = tf.nn.leaky_relu(value, alpha=0.01, name=name+"_leakyval")
        value = tfkl.Dense(1, name=name+"_val")(value)

        # Q values = val + adv
        slice_q = tf.math.add(value, advantage, name=name+"_sliceq")
        return slice_q
示例#23
0
    def construct_q_network(self):
        """
        First the :attr:`l2rpn_baselines.BaseDeepQ.nn_archi` parameters are used to create a neural network
        to 'encode' the data. Then the leaps occur.

        Afterward the model is split into value an advantage, and treated as usually in any D3QN.

        """
        # Uses the network architecture found in DeepMind paper
        # The inputs and outputs size have changed, as well as replacing the convolution by dense layers.
        self._model = Sequential()
        input_x = Input(shape=(self._nn_archi.x_dim, ), name="x")
        inputs_tau = [
            Input(shape=(el, ), name="tau_{}".format(nm_)) for el, nm_ in zip(
                self._nn_archi.tau_dims, self._nn_archi.list_attr_obs_tau)
        ]

        lay = input_x
        for (size, act) in zip(self._nn_archi.sizes, self._nn_archi.activs):
            lay = Dense(size)(lay)  # put at self.action_size
            lay = Activation(act)(lay)

        # TODO multiple taus
        l_tau = lay
        for el, nm_ in zip(inputs_tau, self._nn_archi.list_attr_obs_tau):
            l_tau = l_tau + LtauBis(name="leap_{}".format(nm_))([lay, el])

        advantage = Dense(self._action_size)(l_tau)
        value = Dense(1, name="value")(l_tau)

        meaner = Lambda(lambda x: K.mean(x, axis=1))
        mn_ = meaner(advantage)
        tmp = subtract([advantage, mn_])
        policy = add([tmp, value], name="policy")

        self._model = Model(inputs=[input_x, *inputs_tau], outputs=[policy])
        self._schedule_model, self._optimizer_model = self.make_optimiser()
        self._model.compile(loss='mse', optimizer=self._optimizer_model)

        self._target_model = Model(inputs=[input_x, *inputs_tau],
                                   outputs=[policy])
示例#24
0
    def construct_D3QN(self):
        input_layer = tfkl.Input(shape=(self.observation_size *
                                        self.num_frames, ))
        layer1 = tfkl.Dense(self.observation_size *
                            self.num_frames)(input_layer)
        layer1 = tfka.relu(layer1, alpha=0.01)  #Leaky ReLU

        layer2 = tfkl.Dense(self.observation_size * 2)(layer1)
        layer2 = tfka.relu(layer2, alpha=0.01)

        layer3 = tfkl.Dense(self.observation_size)(layer2)
        layer3 = tfka.relu(layer3, alpha=0.01)

        layer4 = tfkl.Dense(self.action_size * 3)(layer3)
        layer4 = tfka.relu(layer4, alpha=0.01)

        value_stream = tfkl.Dense(self.action_size * 2)(layer4)
        value_stream = tfka.relu(value_stream, alpha=0.01)

        V = tfkl.Dense(1)(value_stream)

        advantage_stream = tfkl.Dense(self.action_size * 2)(layer4)
        advantage_stream = tfka.relu(advantage_stream, alpha=0.01)

        advantage = tfkl.Dense(self.action_size)(advantage_stream)

        advantage_mean = tf.math.reduce_mean(advantage, axis=1, keepdims=True)
        A = tfkl.subtract([advantage, advantage_mean])
        Q = tf.math.add(V, A)

        self.model = tfk.Model(inputs=[input_layer], outputs=[Q])

        self.schedule = tfko.schedules.InverseTimeDecay(
            self.lr, self.lr_decay_steps, self.lr_decay_rate)
        self.optimizer = tfko.Adam(learning_rate=self.schedule)

        self.target_model = tfk.Model(inputs=[input_layer], outputs=[Q])
        self.target_model.set_weights(self.model.get_weights())
        return
示例#25
0
    def construct_q_network(self):
        # Uses the network architecture found in DeepMind paper
        # The inputs and outputs size have changed, as well as replacing the convolution by dense layers.
        self.model = Sequential()
        input_x = Input(shape=(self.observation_size -
                               (self.tau_dim_end - self.tau_dim_start), ),
                        name="x")
        input_tau = Input(shape=(self.tau_dim_end - self.tau_dim_start, ),
                          name="tau")

        lay1 = Dense(self.observation_size)(input_x)
        lay1 = Activation('relu')(lay1)

        lay2 = Dense(self.observation_size)(lay1)
        lay2 = Activation('relu')(lay2)

        lay3 = Dense(2 * self.action_size)(lay2)  # put at self.action_size
        lay3 = Activation('relu')(lay3)

        l_tau = Ltau()((lay3, input_tau))

        fc1 = Dense(self.action_size)(l_tau)
        advantage = Dense(self.action_size)(fc1)
        fc2 = Dense(self.action_size)(lay3)
        value = Dense(1)(fc2)

        meaner = Lambda(lambda x: K.mean(x, axis=1))
        mn_ = meaner(advantage)
        tmp = subtract([advantage, mn_])
        policy = add([tmp, value], name="policy")

        self.model = Model(inputs=[input_x, input_tau], outputs=[policy])
        self.schedule_model, self.optimizer_model = self.make_optimiser()
        self.model.compile(loss='mse', optimizer=self.optimizer_model)

        self.target_model = Model(inputs=[input_x, input_tau],
                                  outputs=[policy])
        print("Successfully constructed networks.")
示例#26
0
    def construct_q_network(self):
        # Defines input tensors and scalars
        self.trace_length = tf.Variable(1, dtype=tf.int32, name="trace_length")
        self.dropout_rate = tf.Variable(0.0,
                                        dtype=tf.float32,
                                        trainable=False,
                                        name="dropout_rate")
        input_mem_state = tfk.Input(dtype=tf.float32,
                                    shape=(self.h_size),
                                    name='input_mem_state')
        input_carry_state = tfk.Input(dtype=tf.float32,
                                      shape=(self.h_size),
                                      name='input_carry_state')
        input_layer = tfk.Input(dtype=tf.float32,
                                shape=(None, self.observation_size),
                                name='input_obs')

        # Get shapes from input_layer
        batch_size = tf.shape(input_layer)[0]
        trace_len = tf.shape(input_layer)[1]
        data_size = tf.shape(input_layer)[-1]

        # Reshape for dense processing
        input_format = tf.reshape(input_layer, (-1, input_layer.shape[-1]),
                                  name="dense_reshape")

        # Bayesian NN simulate
        lay1 = tfkl.Dropout(self.dropout_rate,
                            name="bnn_dropout")(input_format)
        # Forward pass
        lay1 = tfkl.Dense(512, name="fc_1")(lay1)
        lay1 = tf.nn.leaky_relu(lay1, alpha=0.01, name="leak_fc_1")
        lay2 = tfkl.Dense(256, name="fc_2")(lay1)
        lay2 = tf.nn.leaky_relu(lay2, alpha=0.01, name="leak_fc_2")
        lay3 = tfkl.Dense(128, name="fc_3")(lay2)
        lay3 = tf.nn.leaky_relu(lay3, alpha=0.01, name="leak_fc_3")
        lay4 = tfkl.Dense(self.h_size, name="fc_4")(lay3)

        # Reshape to (batch_size, trace_len, data_size) for rnn
        rnn_format = tf.reshape(lay4, (batch_size, trace_len, self.h_size),
                                name="rnn_reshape")
        # Recurring part
        lstm_layer = tfkl.LSTM(self.h_size, return_state=True, name="lstm")
        lstm_state = [input_mem_state, input_carry_state]
        lstm_output, mem_s, carry_s = lstm_layer(rnn_format,
                                                 initial_state=lstm_state)

        # Advantage and Value streams
        advantage = tfkl.Dense(64, name="fc_adv")(lstm_output)
        advantage = tf.nn.leaky_relu(advantage, alpha=0.01, name="leak_adv")
        advantage = tfkl.Dense(self.action_size, name="adv")(advantage)
        advantage_mean = tf.math.reduce_mean(advantage,
                                             axis=1,
                                             keepdims=True,
                                             name="adv_mean")
        advantage = tfkl.subtract([advantage, advantage_mean], name="adv_sub")

        value = tfkl.Dense(64, name="fc_val")(lstm_output)
        value = tf.nn.leaky_relu(value, alpha=0.01, name="leak_val")
        value = tfkl.Dense(1, name="val")(value)

        Q = tf.math.add(value, advantage, name="Qout")

        # Backwards pass
        model_inputs = [input_mem_state, input_carry_state, input_layer]
        model_outputs = [Q, mem_s, carry_s]
        self.model = tfk.Model(inputs=model_inputs,
                               outputs=model_outputs,
                               name=self.__class__.__name__)
        losses = [self._mse_loss, self._no_loss, self._no_loss]
        self.optimizer = tfko.Adam(lr=self.lr, clipnorm=1.0)
        self.model.compile(loss=losses, optimizer=self.optimizer)
示例#27
0
embedding = Embedding(corpus_length, vector_dim, input_length=1)
reshape = Reshape((vector_dim, 1))

input_main = Input((1, ))
input_search_words = Input((1, ))

main_embedding = reshape(embedding(input_main))
search_words_embedding = reshape(embedding(input_search_words))
similarity = Reshape((1, ))(dot([main_embedding, search_words_embedding],
                                axes=1,
                                normalize=True))
output = Dense(1, activation='sigmoid')(similarity)
# output2 = dot([main_embedding, search_words_embedding], axes=0)

subtract_layer = subtract([main_embedding, search_words_embedding])
eucledian_output = multiply([subtract_layer, subtract_layer])

# eucledian_output = dot([main_embedding, search_words_embedding], axes=1, normalize = True)

model = Model(inputs=[input_main, input_search_words], outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam')

Euclid_Model = Model(inputs=[input_main, input_search_words],
                     outputs=eucledian_output)
Embeddings = Model(inputs=[input_main], outputs=main_embedding)

#The creation of the model has been refernced from the following link - https://adventuresinmachinelearning.com/word2vec-keras-tutorial/


def sum_values(pred):
示例#28
0
def createModel():
    #5.1 Set input shape which is 1 channel of 42x42 2D image
    inputShape = (20, 16, 1)
    inputs = Input(shape=inputShape)
    inputs2 = Conv2D(16, (2, 2),
                     padding="same",
                     strides=(1, 1),
                     kernel_initializer='he_normal',
                     activation='relu')(inputs)
    inputs2 = Conv2D(32, (2, 2),
                     padding="same",
                     strides=(1, 1),
                     kernel_initializer='he_normal',
                     activation='relu')(inputs2)

    #5.2 Auto encoder
    #This is to process the input so the neccessary feature that can be used for image reconstruction can be gathered.
    #5.2.1 First hidden layer. 64 neurons, downsize the image to (21,21). Conv2D is chosen over maxPooling as it allow the filter kernel to be trained.
    '''
    x           = Conv2D(64, (8,1), padding="same",strides=(1,2),kernel_initializer='he_normal', activation='relu')(inputs)
    encoded     = Conv2D(40, (4,1), padding="same",strides=(1,2),kernel_initializer='he_normal', activation='relu')(x)
    x           = UpSampling2D(size=(1, 2))(encoded)
    x           = Conv2D(40, (4,1), padding="same",kernel_initializer='he_normal', activation='relu')(x)
    x           = UpSampling2D(size=(1, 2))(x)
    x           = Conv2D(64, (8,1), padding="same",kernel_initializer='he_normal', activation='relu')(x)
    x           = Dense(1, name="horizontal")(x)
    '''
    '''    
    #horizontal
    x = AveragePooling2D(pool_size=(1, 8), strides=(1,8))(inputs)
    x = Lambda(lambda x: x - 0.375)(x)
    x = Activation("relu")(x)
    x = Lambda(lambda x: x *1024)(x)
    x = Activation("tanh")(x)
    x = UpSampling2D(size=(1, 8))(x)

    #vertical
    x2 = AveragePooling2D(pool_size=(5, 1), strides=(5,1))(inputs)
    x2 = Lambda(lambda x2: x2 - 0.33)(x2)
    x2 = Activation("relu")(x2)
    x2 = Lambda(lambda x2: x2 *1024)(x2)
    x2 = Activation("tanh")(x2)
    x2 = UpSampling2D(size=(5, 1))(x2)

    #heat trap
    x3 = AveragePooling2D(pool_size=(4, 4), strides=(4,4))(inputs)
    x3 = Lambda(lambda x3: x3 - 0.25)(x3)
    x3 = Activation("relu")(x3)
    x3 = Lambda(lambda x3: x3 *1024)(x3)
    x3 = Activation("tanh")(x3)
    x3 = UpSampling2D(size=(4, 4))(x3)
    '''

    #heat trap
    #x3 = AveragePooling2D(pool_size=(4, 4), strides=(4,4))(inputs2)
    x3 = inputs2
    x3 = Conv2D(32, (4, 4),
                padding="same",
                strides=(4, 4),
                kernel_initializer='he_normal',
                activation='relu')(x3)
    x3_2 = Lambda(lambda x3: x3 - 0.2)(x3)
    x3_2 = Activation("relu")(x3_2)
    x3_2 = Lambda(lambda x3_2: x3_2 + 0.2)(x3_2)
    x3_2 = Lambda(lambda x3_2: x3_2 * 8)(x3_2)
    x3 = multiply([x3, x3_2])

    #x3 = Activation("tanh")(x3)
    x3 = UpSampling2D(size=(4, 4))(x3)
    #x3 = multiply([x3,inputs])
    x3 = Lambda(lambda x3: x3 * 0.5)(x3)

    #x3a = AveragePooling2D(pool_size=(2, 2), strides=(2,2))(inputs2)
    x3a = inputs2
    x3a = Conv2D(32, (2, 2),
                 padding="same",
                 strides=(2, 2),
                 kernel_initializer='he_normal',
                 activation='relu')(x3a)

    x3a_2 = Lambda(lambda x3a: x3a - 0.2)(x3a)
    x3a_2 = Activation("relu")(x3a_2)
    x3a_2 = Lambda(lambda x3a_2: x3a_2 + 0.2)(x3a_2)
    x3a_2 = Lambda(lambda x3a_2: x3a_2 * 8)(x3a_2)
    x3a = multiply([x3a, x3a_2])

    #x3a = Activation("tanh")(x3a)
    x3a = UpSampling2D(size=(2, 2))(x3a)
    #x3a = multiply([x3a,inputs])
    x3a = Lambda(lambda x3a: x3a * 0.5)(x3a)

    x3 = add([x3, x3a], name="cluster")

    #vertical
    #x2 = subtract([inputs,x3])
    x2 = inputs2
    #x2 = AveragePooling2D(pool_size=(5, 1), strides=(5,1))(x2)
    x2 = Conv2D(32, (5, 1),
                padding="same",
                strides=(5, 1),
                kernel_initializer='he_normal',
                activation='relu')(x2)

    x2_2 = Lambda(lambda x2: x2 - 0.2)(x2)
    x2_2 = Activation("relu")(x2_2)
    x2_2 = Lambda(lambda x2_2: x2_2 + 0.2)(x2_2)
    x2_2 = Lambda(lambda x2_2: x2_2 * 8)(x2_2)
    x2 = multiply([x2, x2_2])

    #x2 = Activation("tanh")(x2)
    x2 = UpSampling2D(size=(5, 1), name='vertical')(x2)
    #x2= multiply([x2,inputs], name='vertical')
    #x2 = add([x2,x2a])

    #horizontal
    #x = subtract([inputs,x3])
    #x = subtract([x,x2])
    x = inputs2
    #x = AveragePooling2D(pool_size=(1, 8), strides=(1,8))(x)
    x = Conv2D(32, (1, 8),
               padding="same",
               strides=(1, 8),
               kernel_initializer='he_normal',
               activation='relu')(x)
    #x = Lambda(lambda x: x *8)(x)
    x_2 = Lambda(lambda x: x - 0.2)(x)
    x_2 = Activation("relu")(x_2)
    x_2 = Lambda(lambda x_2: x_2 + 0.2)(x_2)
    x_2 = Lambda(lambda x_2: x_2 * 8)(x_2)
    x = multiply([x, x_2])
    #x = Activation("relu")(x)

    x = UpSampling2D(size=(1, 8), name="horizontal")(x)
    #x = multiply([x,inputs])
    #x = add([x,xa])

    y = subtract([inputs2, x])
    y = subtract([y, x2])
    y = subtract([y, x3])

    #y = Lambda(lambda y: y * 8)(y)
    #y = Lambda(lambda y: y - 0.125)(y)
    y = Activation("relu", name="pepper")(y)
    #y = Lambda(lambda y: y *1024)(y)
    #y = Activation("tanh")(y)
    #y = multiply([y,inputs])

    #=======================================================
    '''
    x           = Conv2D(32, (1,8), padding="same",strides=(1,8),kernel_initializer='he_normal', activation='relu')(x)
    x           = Lambda(lambda x: x - 0.1)(x)
    x           = Activation("relu")(x)
    x           = Lambda(lambda x: x * 1.5)(x)
    #x           = Lambda(lambda x: x * 8)(x)
    x_code      = Conv2D(16, (2,1), padding="same",strides=(1,1),kernel_initializer='he_normal', activation='relu')(x)
    x           = UpSampling2D(size=(1,8), name="horizontal" )(x_code)
    #x2           = Dense(1)(x2)
    
    x2           = Conv2D(32, (5,1), padding="same",strides=(5,1),kernel_initializer='he_normal', activation='relu')(x2)
    x2           = Lambda(lambda x2: x2 - 0.1)(x2)
    x2           = Activation("relu")(x2)
    x2           = Lambda(lambda x2: x2 * 1.5)(x2)
    #x2           = Lambda(lambda x2: x2 * 8)(x2)
    x2_code      = Conv2D(16, (2,1), padding="same",strides=(1,1),kernel_initializer='he_normal', activation='relu')(x2)
    x2           = UpSampling2D(size=(5, 1), name="vertical" )(x2_code)
    #x2           = Dense(1)(x2)

    x3      = Conv2D(32, (4,4), padding="same",strides=(4,4),kernel_initializer='he_normal', activation='relu')(x3)
    x3      = Lambda(lambda x3: x3 - 0.1)(x3)
    x3      = Activation("relu")(x3)
    x3_code      = Lambda(lambda x3: x3 * 1.5)(x3)
    #x3_code = Lambda(lambda x3: x3 * 16)(x3)
    #x3           = Dropout(0.3)(x3)
    x3           = UpSampling2D(size=(4, 4) )(x3_code)
    x3           = Dense(1)(x3)
    
    x3b      = Conv2D(32, (6,6), padding="same",strides=(4,4),kernel_initializer='he_normal', activation='relu')(x3)
    x3b      = Lambda(lambda x3b: x3b - 0.1)(x3b)
    x3b      = Activation("relu")(x3b)
    x3b_code      = Lambda(lambda x3b: x3b * 1.5)(x3b)
    #x3b_code = Lambda(lambda x3b: x3b * 16)(x3b)
    #x3b          = Dropout(0.3)(x3b)
    x3b          = UpSampling2D(size=(4, 4) )(x3b_code)
    x3b          = Dense(1)(x3b)

    x3c     = Conv2D(32, (2,2), padding="same",strides=(2,2),kernel_initializer='he_normal', activation='relu')(x3)
    x3c      = Lambda(lambda x3c: x3c - 0.1)(x3c)
    x3c      = Activation("relu")(x3c)
    x3c_code      = Lambda(lambda x3c: x3c * 1.5)(x3c)
    #x3c_code = Lambda(lambda x3c: x3c * 4)(x3c)
    #x3c          = Dropout(0.3)(x3c)
    x3c          = UpSampling2D(size=(2, 2) )(x3c_code)
    x3c          = Dense(1)(x3c)
    
    x3           = add([x3,x3b,x3c]) 
    x3           = Lambda(lambda x3: x3 /3, name="cluster")(x3)

    
    y           = Conv2D(32, (2,2), padding="same",strides=(1,1),kernel_initializer='he_normal', activation='relu')(y)
    y           = Conv2D(16, (2,2), padding="same",strides=(1,1),kernel_initializer='he_normal', activation='relu', name="pepper")(y)
    #y           = Dropout(0.3)(y)
    #y           = Conv2D(1, (1,1), padding="same",strides=(1,1),kernel_initializer='he_normal', activation='relu')(y)
    #y           = Dense(1)(y)
    
    #y = Lambda(lambda y: y - 0.1)(y)
    #y = Activation("relu")(y)
    #y = Lambda(lambda y: y *1024)(y)
    #y = Activation("tanh")(y)
    #y           = multiply([y,inputs])
    #y           = subtract([y,x], name="pepper")
    '''

    #outputs = add([x,x2,x3,y])
    #outputs = Lambda(lambda outputs: outputs /4)(outputs)

    outputs = concatenate([x, x2, x3])
    outputs = Conv2D(16, (2, 2),
                     padding="same",
                     strides=(1, 1),
                     kernel_initializer='he_normal',
                     activation='relu')(outputs)
    outputs = Conv2D(8, (2, 2),
                     padding="same",
                     strides=(1, 1),
                     kernel_initializer='he_normal',
                     activation='relu')(outputs)
    #outputs = Conv2D(1, (2,2), padding="same",strides=(1,1),kernel_initializer='he_normal', activation='relu')(outputs)
    '''
    xx=Flatten()(x_code)
    xx2=Flatten()(x2_code)
    xx3a=Flatten()(x3_code)
    xx3b=Flatten()(x3b_code)
    xx3c=Flatten()(x3c_code)
    
    classifier=concatenate([xx,xx2,xx3a,xx3b,xx3c])
    classifier=Dense(64,kernel_initializer='he_normal', activation='relu')(classifier)
    classifier=Dense(8,kernel_initializer='he_normal', activation='softmax')(classifier)
    '''

    model = Model(inputs=inputs, outputs=outputs)
    model.compile(loss='mean_squared_error',
                  optimizer=optimizers.RMSprop(),
                  metrics=['accuracy'])
    model2 = Model(inputs=inputs, outputs=outputs)
    model2.compile(loss='mean_squared_error',
                   optimizer=optimizers.RMSprop(),
                   metrics=['accuracy'])
    #model2       = Model(inputs=inputs,outputs=classifier)
    #model2.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam() , metrics=['accuracy'])
    return model, model2
    max2_2 = MaxPooling2D((2, 2), strides=(2, 2))(batch2_2)
    conv2_3 = Convolution2D(256, (3, 3), activation='selu')(max2_2)
    batch2_3 = BatchNormalization()(conv2_3)
    max2_3 = MaxPooling2D((2, 2), strides=(2, 2))(batch2_3)
    conv2_4 = Convolution2D(256, (3, 3), activation='selu')(max2_3)
    batch2_4 = BatchNormalization()(conv2_4)
    fcl2 = Flatten()(batch2_4)
    dense2_1 = Dense(4096, activation='selu')(fcl2)
    dense2_2 = Dense(1024, activation='selu')(dense2_1)

    X2 = Input(shape=(HEIGHT, WIDTH, 3))
    # flatten and dense twice
    fcl3 = Flatten()(batch3_4)
    dense3_1 = Dense(4096, activation='selu')(fcl3)
    dense3_2 = Dense(1024, activation='selu')(dense3_1)
    dense_layer = backend.abs(subtract([dense_2, dense2_2]))
    #dense to 1 output
    out = Dense(1,
                activation='sigmoid',
                kernel_regularizer=regularizers.l1(1e-3))(dense_layer)
    #hyper
    siamese_net = Model(inputs=[X, X1], outputs=out)
    print(siamese_net.summary())

    siamese_net.compile(loss='binary_crossentropy',
                        optimizer=keras.optimizers.Adam(learning_rate=5e-6,
                                                        beta_1=0.9,
                                                        beta_2=0.999),
                        metrics=['accuracy'])
    siamese_net.fit([x_right, x_pos],
                    y1,
示例#30
0
 def subNode(self, inputNode1, inputNode2, name=""):
     if inputNode1.shape() != inputNode2.shape():
         self.logger("dimensionality error with " + str(inputNode1) +
                     " and " + str(inputNode2) + " in pytorch")
         return None
     return layers.subtract([inputNode1, inputNode2], name=name)