Пример #1
0
    def generate_critic(self, state_shape, action_size, optimizer, LEARNING_RATE):
        assert(len(state_shape)==3),"shape mismatch"
        nr_day, nr_feature, nr_seller = state_shape
        assert(action_size == nr_seller)
        inp = Input(shape=(nr_day, nr_feature, nr_seller))
        print("inp shape=",inp.shape)
        action =  Input(shape=(nr_seller,))

        #Similarly, first background part, ASSUME seller data ordered
        dsf_inp = Permute((1,3,2))(inp)
        reshape_inp = Reshape((nr_day,nr_seller * nr_feature))(dsf_inp)
        background_feat = Bidirectional(GRU(self.bh))(reshape_inp) # (batch, bh)
        repeated_background_feat = RepeatVector(nr_seller)(background_feat) #(batch, nr_seller, bh)
        #individual part
        sdf_inp = Permute((3,1,2))(inp)
        ind_model = self.generate_individual_model(nr_day, nr_feature)
        individual_feat = TimeDistributed(ind_model)(sdf_inp) #(batch, nr_seller, ih)

        eval_model  = self.generate_eval_model(2*(self.ih+self.bh)+1)
        concat_feat = Concatenate(axis=-1)([repeated_background_feat, individual_feat, Reshape((-1,1))(action)]) #(batch, nr_seller, ih+bh)
        print("concat_feat shape = ",concat_feat.get_shape())
        values = TimeDistributed(eval_model)(concat_feat) #(batch, nr_seller ,1)
        print("values shape = ",values.get_shape())
        flat_values = Reshape((-1,))(values)
        #then reduce to one value by addition
        value = ReduceSum(axis = 1)(flat_values) #(batch,)
        #value  = Dense(1, activation="linear")(flat_values)
        print("value shape = ",value.get_shape())
        model = Model(inputs=[inp,action], outputs=[value])
        opt = optimizer(LEARNING_RATE)
        model.compile(loss="mse", optimizer=opt)
        return model, action, inp
Пример #2
0
 def build_model(self):
     inputs = Input(shape=(3, self.sequence_size, self.img_size,
                           self.img_size))
     x = TimeDistributed(
         Convolution2D(16, 3, 3, activation=Relu,
                       border_mode="same"))(inputs)
     x = TimeDistributed(
         Convolution2D(32, 3, 3, activation=Relu, border_mode="same"))(x)
     x = TimeDistributed(MaxPooling2D((2, 2)))(x)
     x = TimeDistributed(
         Convolution2D(64, 3, 3, activation=Relu, border_mode="same"))(x)
     x = TimeDistributed(
         Convolution2D(32, 3, 3, activation=Relu, border_mode="same"))(x)
     x = TimeDistributed(MaxPooling2D((2, 2)))(x)
     x = TimeDistributed(
         Convolution2D(8, 3, 3, activation=Relu, border_mode="same"))(x)
     x = TimeDistributed(Flatten())(x)
     x = LSTM(256)(x)
     x = Reshape([1, 16, 16])(x)
     x = Convolution2D(32, 3, 3, activation=Relu, border_mode='same')(x)
     x = UpSampling2D((2, 2))(x)
     x = Convolution2D(64, 3, 3, activation=Relu, border_mode='same')(x)
     x = UpSampling2D((2, 2))(x)
     x = Convolution2D(32, 3, 3, activation=Relu, border_mode='same')(x)
     x = UpSampling2D((2, 2))(x)
     x = Convolution2D(1, 3, 3, activation=Relu, border_mode='same')(x)
     x = UpSampling2D((2, 2))(x)
     print(x.get_shape())
     model = Model(input=inputs, output=x)
     model.compile(optimizer='rmsprop',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])
     return model
Пример #3
0
def build_sleepnet_lstm_model(num_features,
                              num_classes,
                              num_channels,
                              timestep=3,
                              fs=20):
    input = Input(shape=(timestep, num_features, num_channels))

    # two conv-nets in parallel for feature learning,
    # one with fine resolution another with coarse resolution
    # network to learn fine features

    # fine
    convFine = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=int(fs / 2),
               strides=int(fs / 6),
               padding='same',
               activation='relu',
               name='fConv1'))(input)
    convFine = TimeDistributed(BatchNormalization(name='fNorm1'))(convFine)
    convFine = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=8,
               padding='same',
               activation='relu',
               name='fConv2'))(convFine)
    convFine = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=8,
               padding='same',
               activation='relu',
               name='fConv3'))(convFine)
    convFine = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=8,
               padding='same',
               activation='relu',
               name='fConv4'))(convFine)
    convFine = TimeDistributed(BatchNormalization(name='fNorm2'))(convFine)
    fineShape = convFine.get_shape()
    convFine = TimeDistributed(Flatten(name='fFlat1'))(convFine)

    # coarse
    convCoarse = TimeDistributed(
        Conv1D(filters=32,
               kernel_size=fs * 2,
               strides=int(fs / 2),
               padding='same',
               activation='relu',
               name='cConv1'))(input)
    convCoarse = TimeDistributed(BatchNormalization(name='cNorm1'))(convCoarse)
    convCoarse = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=6,
               padding='same',
               activation='relu',
               name='cConv2'))(convCoarse)
    convCoarse = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=6,
               padding='same',
               activation='relu',
               name='cConv3'))(convCoarse)
    convCoarse = TimeDistributed(
        Conv1D(filters=64,
               kernel_size=6,
               padding='same',
               activation='relu',
               name='cConv4'))(convCoarse)
    convCoarse = TimeDistributed(BatchNormalization(name='cNorm2'))(convCoarse)
    coarseShape = convCoarse.get_shape()
    convCoarse = TimeDistributed(Flatten(name='cFlat1'))(convCoarse)

    x = concatenate([convFine, convCoarse], name='merge')
    x = TimeDistributed(Flatten())(x)
    # x = Flatten()(x)
    x = LSTM(32, activation='relu', name='bLstm2')(x)

    output = Dense(num_classes, activation='softmax', name='outLayer')(x)

    # adam = optimizer.Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-8, clipnorm=1.)
    model = Model(input=input, output=output, name='sleep_lstm_1D')
    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer='adam',
                  metrics=[metrics.sparse_categorical_accuracy])

    return model
Пример #4
0
x = Embedding(lx, embed_size, weights=[embedding_matrix])(inp)
x = Conv1D(filters=32, kernel_size=3, padding='same', activation='relu')(x)
x = MaxPooling1D(pool_size=2)(x)
x = LSTM(300, return_sequences=True)(x)
#x = AttentionDecoder(300,maxlen)(x)
#x = GlobalMaxPool1D()(x)
#x = Dense(300, activation="relu")(x)
#x = Dense(100, activation="relu")(x)
#x = Dropout(0.1)(x)
#ylayer=numpy.asarray(ylayer)
print(x.get_shape())
xa = TimeDistributed(Dense(3, activation="tanh"))(x)
xa = Flatten()(xa)
xa = Activation('softmax')(xa)
xa = RepeatVector(300)(xa)
print(xa.get_shape())
xa = Permute([2, 1])(xa)
print(xa.get_shape())
ot = Multiply()([x, xa])
ot = Lambda(lambda xin: K.sum(xin, axis=-1))(ot)

ol = TimeDistributed(Dense(3, activation="sigmoid"))(ot)
model = Model(inputs=inp, outputs=ol)
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

X_t[X_t == lx] = lx - 1
X_te[X_te == lx] = lx - 1

model.fit(X_t, ytrain, batch_size=200, epochs=1, validation_split=0.1)
Пример #5
0
def block_warp(block_name,input_layer,filters,kernal_size=3, dilation_rate=1,depthfilter=4,stride=1):
    def conv_block(input_layer,filters,k=3):
        y = Conv3D(filters=filters, kernel_size=k, padding='same')(input_layer)
        y = BatchNormalization()(y)
        y = Activation('relu')(y)
        return y

    if block_name == 'conv':
        y = conv_block(input_layer,filters)
        y = conv_block(y,filters)

    elif block_name == 'dialtion':
        y = Conv3D(filters=filters, kernel_size=kernal_size, padding='same', dilation_rate=dilation_rate)(input_layer)
        y = BatchNormalization()(y)
        y = Activation('relu')(y)

    elif block_name == 'deconv':
        y = Conv3DTranspose(filters=filters,kernel_size=3,strides=2,padding='same')(input_layer)
        y = BatchNormalization()(y)
        y = Activation('relu')(y)

    elif block_name == 'time_conv':
        y = TimeDistributed(Conv2D(filters=filters,kernel_size=3,padding='same'))(input_layer)
        y = TimeDistributed(BatchNormalization())(y)
        y = TimeDistributed(Activation('relu'))(y)

    elif block_name == 'time_deconv':
        y = TimeDistributed(Conv2DTranspose(filters=filters,kernel_size=3,padding='same',strides=2))(input_layer)
        y = TimeDistributed(BatchNormalization())(y)
        y = TimeDistributed(Activation('relu'))(y)

    elif block_name == 'inception':
        filters = filters//4

        c1 = conv_block(input_layer,filters,1)

        c3 = conv_block(input_layer,filters,1)
        c3 = conv_block(c3,filters,3)

        c5 = MaxPool3D(pool_size=3,padding='same',strides=1)(input_layer)
        c5 = conv_block(c5,filters,3)


        c7 = conv_block(input_layer,filters,1)
        c7 = conv_block(c7, filters, 3)
        c7 = conv_block(c7, filters, 3)

        y = concatenate([c1,c3,c5,c7])
        # c_all = BatchNormalization()(c_all)
        # y = Activation('relu')(c_all)
    elif block_name == 'sep':
        input_layer = Permute((4,1,2,3))(input_layer)
        sz = input_layer.get_shape()
        shape = tuple(int(sz[i]) for i in range(1, 5))
        input_layer = Reshape(shape+(1,))(input_layer)
        conv1 = TimeDistributed(Conv3D(filters=depthfilter,kernel_size=kernal_size,padding='same',strides=stride))(input_layer)
        conv1 = Permute((2,3,4,1,5))(conv1)
        sz = conv1.get_shape()
        shape = tuple(int(sz[i]) for i in range(1, 4))
        conv1 = Reshape(shape+(-1,))(conv1)
        conv1 = Conv3D(filters=filters,kernel_size=1,padding='same')(conv1)
        conv1 = BatchNormalization()(conv1)
        y = Activation('relu')(conv1)

    else:
        raise ValueError("layer error")
    return y
# WORD-LEVEL
sentence_input = Input(shape=(maxlen,), dtype='int32')
embedded_sequences = embedding_layer(sentence_input)

print('embedded_sequences SHAPE')
print(embedded_sequences.get_shape())

# Bidirectional GRU
l_gru = MultiplicativeLSTM(gru_output_size, return_sequences=True)(embedded_sequences)
l_dense = TimeDistributed(Dense(units=gru_output_size))(l_gru) 

print('l_gru SHAPE')
print(l_gru.get_shape())
print('l_dense SHAPE')
print(l_dense.get_shape())


# Word-Level Attention Layer
l_att = AttLayer()(l_dense)

print('l_att SHAPE')
print(l_att.get_shape())

sentEncoder = Model(sentence_input, l_att)
sentEncoder.compile(
    optimizer=Adam(0.0001),
    loss='mse',
    metrics={},
)