Beispiel #1
0
 def _get_act_by_name(self, act):
     str_act = ['relu', 'tanh', 'sigmoid', 'linear', 'softmax', 'softplus', 'softsign', 'hard_sigmoid']
     if (act in str_act):
         return Activation(act)
     else:
         return {'prelu': PReLU(), 'elu': ELU(), 'srelu': SReLU(), 'lrelu': LeakyReLU(),
                 'psoftplus': ParametricSoftplus(), 'trelu': ThresholdedReLU()}[act]
Beispiel #2
0
def build_model(
    X_sparse,
    dim=128,
):
    inputs = []
    flatten_layers = []
    columns = range(X.shape[1])

    inputs_sparse = Input(shape=(X_sparse.shape[1], ),
                          dtype='float32',
                          name='input_%s' % 'sparse')
    flatten_layers.append(inputs_sparse)
    inputs.append(inputs_sparse)

    fc1 = Dense(hidden, )(inputs_sparse)
    fc1 = SReLU()(fc1)
    fc1 = BatchNormalization()(fc1)
    dp1 = Dropout(0.7)(fc1)

    outputs = Dense(1, activation='sigmoid', name='outputs')(dp1)

    model = Model(input=inputs, output=outputs)
    nadam = Nadam()
    sgd = SGD(lr=0.005, decay=1e-6, momentum=0.99, nesterov=True)
    model.compile(
        # optimizer=sgd,
        optimizer='adam',
        loss='binary_crossentropy')

    return model
Beispiel #3
0
def create_layer(name):
    if name == 'aabh':
        return AdaptativeAssymetricBiHyperbolic()
    elif name == 'abh':
        return AdaptativeBiHyperbolic()
    elif name == 'ah':
        return AdaptativeHyperbolic()
    elif name == 'ahrelu':
        return AdaptativeHyperbolicReLU()
    elif name == 'srelu':
        return SReLU()
    elif name == 'prelu':
        return PReLU()
    elif name == 'lrelu':
        return LeakyReLU()
    elif name == 'trelu':
        return ThresholdedReLU()
    elif name == 'elu':
        return ELU()
    elif name == 'pelu':
        return PELU()
    elif name == 'psoftplus':
        return ParametricSoftplus()
    elif name == 'sigmoid':
        return Activation('sigmoid')
    elif name == 'relu':
        return Activation('relu')
    elif name == 'tanh':
        return Activation('tanh')
    elif name == 'softplus':
        return Activation('softplus')
def model_20161223_fnn_v1(feature_size):
    # select features
    # fields = feature_size.keys()
    FFM_L2 = 0.0000001
    FFM_DIM = 5
    fields = [
        'ad_id_fact',
        'uuid',
        'leak',
        'weekday',
        'day',
        'hour',
        'geo_1',
        'geo_2',
        'geo_3',
        'geo_location',
        'platform',
        'advertiser_id',
        'campaign_id',
        'cat_1',
        'cat_2',
        'source_id',
        'topic_1',
        'topic_2',
        'topic_3',
        'topic_4',
        'topic_5',
        'topic_num',
    ]

    # get model
    print('Create model input')
    model_inputs = {}
    fnn_layers = []
    for field in fields:
        model_inputs[field] = Input(shape=(1,), dtype='int32', name='input_' + field)
        embed = Flatten()(Embedding(
            feature_size[field] + 1,
            FFM_DIM,
            input_length=1,
            name='embed_{}'.format(field),
            W_regularizer=l2_reg(FFM_L2),
        )(model_inputs[field]))
        fnn_layers.append(embed)

    concat_embed = merge(fnn_layers, mode='concat')
    dense = Dropout(0.2)(Dense(1024, activation='tanh')(concat_embed))
    dense = Dropout(0.2)(Dense(1024, activation='relu')(dense))
    dense = Dropout(0.2)(Dense(512, activation='relu')(dense))
    dense = SReLU()(dense)
    output = Dense(1, activation='sigmoid')(dense)
    # import ipdb; ipdb.set_trace()
    print('compile model')
    input_field = model_inputs.keys()
    model = Model(input=[model_inputs[field] for field in input_field], output=output)
    optimizer = Adadelta(lr=0.1, rho=0.9)
    model.compile(optimizer=optimizer, loss='binary_crossentropy')
    print(model.summary())
    return input_field, model
def feed_forward_2851(input_size, output_size):
    model = Sequential()
    model.add(Dense(128, input_dim=input_size))
    model.add(SReLU())
    model.add(Dropout(0.5))

    model.add(Dense(128))
    model.add(SReLU())
    model.add(Dropout(0.5))

    model.add(Dense(128))
    model.add(SReLU())
    model.add(Dropout(0.5))

    model.add(Dense(output_size))
    model.add(Activation('linear'))
    return model
def build_block(input, filters, r, c, border_mode='same'):
    x = input

    conv1 = BatchNormalization(axis=1)(x)
    conv1 = AtrousConvolution2D(filters / 2, 1, 1, border_mode=border_mode)(x)
    conv1 = SReLU()(conv1)

    conv1 = BatchNormalization(axis=1)(conv1)
    conv1 = AtrousConvolution2D(filters / 2, 3, 3, border_mode=border_mode)(x)
    conv1 = SReLU()(conv1)

    conv1 = BatchNormalization(axis=1)(conv1)
    conv1 = AtrousConvolution2D(filters, r, c, border_mode=border_mode)(conv1)
    conv1 = SReLU()(conv1)

    y = conv1

    y = merge([x, y], mode="sum")
    return y
Beispiel #7
0
def get_conv_activation(x, name):
    if name == 'relu':
        return Activation('relu')(x)
    elif name == 'elu':
        return ELU()(x)
    elif name == 'prelu':
        return PReLU()(x)
    elif name == 'srelu':
        return SReLU()(x)
    elif name == 'none':
        return x
Beispiel #8
0
def makecnn(in_shape, K):
    model = Sequential()
    model.add(
        Convolution2D(32, 3, 3, border_mode='same', input_shape=in_shape[1:]))
    model.add(SReLU())
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='tf'))
    model.add(GaussianNoise(1))
    model.add(GaussianDropout(0.4))
    model.add(Convolution2D(32, 3, 3, border_mode='same'))
    model.add(SReLU())
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='tf'))
    model.add(GaussianNoise(1))
    model.add(GaussianDropout(0.4))
    model.add(Flatten())
    model.add(Dense(64))
    model.add(SReLU())
    model.add(Dense(64))
    # model.add(SReLU())
    model.add(Dense(1))
    model.add(Activation('linear'))
    return model
Beispiel #9
0
 def get_activation(p_act):
     act = p_act.lower()
     if act == 'prelu':
         from keras.layers.advanced_activations import PReLU
         act = PReLU(**activation_props)
     elif act == 'srelu':
         from keras.layers.advanced_activations import SReLU
         act = SReLU(**activation_props)
     elif act == 'leakyrelu':
         from keras.layers.advanced_activations import LeakyReLU
         act = LeakyReLU(**activation_props)
     return act
Beispiel #10
0
def build_model():
    uinput = Input(shape=(1, ), dtype='int32')
    iinput = Input(shape=(1, ), dtype='int32')

    embed_u = Embedding(
        num_u,
        128,
        # dropout=0.2,
        input_length=1)(uinput)

    embed_i = Embedding(
        num_i,
        128,
        # dropout=0.2,
        input_length=1)(iinput)

    flatten_u = Flatten()(embed_u)
    flatten_i = Flatten()(embed_i)

    merge_ui = merge([flatten_u, flatten_i], mode="concat")

    fc1 = Dense(512)(merge_ui)
    fc1 = SReLU()(fc1)
    # fc1 = BatchNormalization(mode=2)(fc1)
    dp1 = Dropout(0.5)(fc1)

    fc2 = Dense(128)(dp1)
    fc2 = SReLU()(fc2)
    # fc2 = BatchNormalization(mode=2)(fc2)
    dp2 = Dropout(0.2)(fc2)

    output = Dense(1)(dp2)

    model = Model(input=[uinput, iinput], output=output)
    sgd = SGD(lr=0.5, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='mse')

    return model
Beispiel #11
0
def test_srelu():
    from keras.layers.advanced_activations import SReLU
    np.random.seed(1337)
    inp = np.array([-2, -1., -0.5, 0., 0.5, 1., 2.])
    out = np.array([-1.5, -1., -0.5, 0., 0.5, 1., 3.])
    input_size = len(inp)
    for train in [True, False]:
        layer = SReLU(input_shape=inp.flatten().shape)
        ones_proto = np.ones(input_size)
        layer.set_weights([
            ones_proto * -1., ones_proto * 0.5, ones_proto * 2.,
            ones_proto * 2.
        ])
        layer.input = K.variable(inp)
        outp = K.eval(layer.get_output(train))
        assert_allclose(out, outp)

        layer.get_config()
Beispiel #12
0
def test_srelu():
    from keras.layers.advanced_activations import SReLU
    np.random.seed(1337)
    inp = np.array([-2, -1., -0.5, 0., 0.5, 1., 2.])
    out = np.array([-1.5, -1., -0.5, 0., 0.5, 1., 3.])
    input_size = len(inp)
    for train in [True, False]:
        layer = SReLU(input_shape=inp.flatten().shape)
        ones_proto = np.ones(input_size)
        layer.set_weights([ones_proto * -1., ones_proto * 0.5,
                           ones_proto * 2., ones_proto * 2.])
        layer.input = K.variable(inp)
        outp = K.eval(layer.get_output(train))
        assert_allclose(out, outp)

        layer.get_config()
def init_model(num_input, num_hnodes, num_output):

    model = Sequential()
    model.add(
        Dense(num_hnodes,
              input_dim=num_input,
              init='he_uniform',
              W_regularizer=l2(0.01)))
    #model.add(Activation('sigmoid'))
    model.add(
        SReLU(t_left_init='zero',
              a_left_init='glorot_uniform',
              t_right_init='glorot_uniform',
              a_right_init='one'))
    model.add(Dense(num_output, init='he_uniform'))
    sgd = keras.optimizers.SGD(lr=0.1, momentum=0.1, decay=0.0, nesterov=True)
    nadam = keras.optimizers.Nadam(lr=0.1,
                                   beta_1=0.9,
                                   beta_2=0.999,
                                   epsilon=1e-08,
                                   schedule_decay=0.004)
    model.compile(loss='mean_absolute_error', optimizer=nadam)
    return model
Beispiel #14
0
def build_model(X, dim=128):

    inputs_p = Input(shape=(1, ), dtype='int32')

    embed_p = Embedding(num_q, dim, dropout=0.2, input_length=1)(inputs_p)

    inputs_d = Input(shape=(1, ), dtype='int32')

    embed_d = Embedding(num_e, dim, dropout=0.2, input_length=1)(inputs_d)

    flatten_p = Flatten()(embed_p)

    flatten_d = Flatten()(embed_d)

    flatten = merge([
        flatten_p,
        flatten_d,
    ], mode='concat')

    fc1 = Dense(512)(flatten)
    fc1 = SReLU()(fc1)
    dp1 = Dropout(0.7)(fc1)

    outputs = Dense(1, activation='sigmoid', name='outputs')(dp1)

    inputs = [
        inputs_p,
        inputs_d,
    ]

    model = Model(input=inputs, output=outputs)
    nadam = Nadam()
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=nadam, loss='binary_crossentropy')

    return model
def get_unet():
    inputs = Input((1, img_rows, img_cols))
    conv1 = AtrousConvolution2D(32, 3, 3, border_mode='same')(inputs)
    conv1 = SReLU()(conv1)
    # conv1 = build_block(inputs,32,3,3)
    conv1 = build_block(conv1, 32, 3, 3)

    # conv1 = AtrousConvolution2D(32, 3, 3, border_mode='same')(conv1)
    # conv1 = SReLU()(conv1)
    # conv1 = BatchNormalization(axis=1)(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = AtrousConvolution2D(64, 3, 3, border_mode='same')(pool1)
    conv2 = SReLU()(conv2)
    # conv2 = build_block(conv1,64,3,3)
    conv2 = build_block(conv2, 64, 3, 3)

    # conv2= AtrousConvolution2D(64, 3, 3, border_mode='same')(conv2)
    # conv2 = SReLU()(conv2)
    # conv2 = BatchNormalization(axis=1)(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = AtrousConvolution2D(128, 3, 3, border_mode='same')(pool2)
    conv3 = SReLU()(conv3)
    # conv3 = build_block(pool2,128,3,3)
    conv3 = build_block(conv3, 128, 3, 3)

    # conv3 = AtrousConvolution2D(128, 3, 3, border_mode='same')(conv3)
    # conv3 = SReLU()(conv3)
    # conv3 = BatchNormalization(axis=1)(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = AtrousConvolution2D(256, 3, 3, border_mode='same')(pool3)
    conv4 = SReLU()(conv4)
    # conv4 = build_block(pool3,256,3,3)
    conv4 = build_block(conv4, 256, 3, 3)

    # conv4 = AtrousConvolution2D(256, 3, 3, border_mode='same')(conv4)
    # conv4 = SReLU()(conv4)
    # conv4 = BatchNormalization(axis=1)(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = AtrousConvolution2D(512, 3, 3, border_mode='same')(pool4)
    conv5 = SReLU()(conv5)
    # conv5 = build_block(pool4,512,3,3)
    conv5 = build_block(conv5, 512, 3, 3)

    # conv5 = AtrousConvolution2D(512, 3, 3, border_mode='same')(conv5)
    # conv5 = SReLU()(conv5)
    # conv5 = BatchNormalization(axis=1)(conv5)

    up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4],
                mode='concat',
                concat_axis=1)
    conv6 = AtrousConvolution2D(256, 3, 3, border_mode='same')(up6)
    conv6 = SReLU()(conv6)
    # conv6 = build_block(up6,256,3,3)
    conv6 = build_block(conv6, 256, 3, 3)

    # conv6 = AtrousConvolution2D(256, 3, 3, border_mode='same')(conv6)
    # conv6 = SReLU()(conv6)
    # conv6 = BatchNormalization(axis=1)(conv6)

    up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3],
                mode='concat',
                concat_axis=1)
    conv7 = AtrousConvolution2D(128, 3, 3, border_mode='same')(up7)
    conv7 = SReLU()(conv7)
    # conv7 = build_block(up7,128,3,3)
    conv7 = build_block(conv7, 128, 3, 3)

    # conv7 = AtrousConvolution2D(128, 3, 3, border_mode='same')(conv7)
    # conv7 = SReLU()(conv7)
    # conv7 = BatchNormalization(axis=1)(conv7)

    up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2],
                mode='concat',
                concat_axis=1)
    conv8 = AtrousConvolution2D(64, 3, 3, border_mode='same')(up8)
    conv8 = SReLU()(conv8)
    # conv8 = build_block(up8,64,3,3)
    conv8 = build_block(conv8, 64, 3, 3)

    # conv8 = AtrousConvolution2D(64, 3, 3, border_mode='same')(conv8)
    # conv8 = SReLU()(conv8)
    # conv8 = BatchNormalization(axis=1)(conv8)

    up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1],
                mode='concat',
                concat_axis=1)
    conv9 = AtrousConvolution2D(32, 3, 3, border_mode='same')(up9)
    conv9 = SReLU()(conv9)
    # conv9 = build_block(up9,32,3,3)
    conv9 = build_block(conv9, 32, 3, 3)

    # conv9 = AtrousConvolution2D(32, 3, 3, border_mode='same')(conv9)
    # conv9 = SReLU()(conv9)
    # conv9 = BatchNormalization(axis=1)(conv9)
    '''
    output
    '''
    conv10 = AtrousConvolution2D(1, 1, 1, activation='sigmoid')(conv9)

    model = Model(input=inputs, output=conv10)
    # sgd = sgd()
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss=dice_coef_loss, metrics=[dice_coef])

    return model
Beispiel #16
0
selector = SelectPercentile(f_classif, percentile=23)  #根据最高得分用于特征选择
selector.fit(X_train, y_train)
X_train = selector.transform(X_train).toarray()
X_val = selector.transform(X_val).toarray()
train_sp = selector.transform(train_sp)
test_sp = selector.transform(test_sp)  #删掉原程序中toarray。原程序报错
print("# Num of Features: ", X_train.shape[1])  #4822个特征 ,类似 one.hot编码
group_lb = LabelBinarizer()
labels = group_lb.fit_transform(Y)  #把Y的每个元素转化若干位的二进制向量
y_train = group_lb.transform(y_train)
y_val = group_lb.transform(y_val)

inputs = Input(shape=(X_train.shape[1], ),
               dtype='float32')  #实例化一个Keras张量,行数对应特征个数
fc1 = Dense(512)(inputs)  #隐藏层维数512?
fc1 = SReLU()(fc1)  #隐藏层的激活函数,类似sigmod
dp1 = Dropout(0.5)(fc1)  #dropout防止过拟合

y_train = [y_train, y_train_gender, y_train_age
           ]  #得到list类型数据,行向量与数值、数值相对应.  y_train[0][0,:]得到第一个list的第一行
y_val = [y_val, y_val_gender, y_val_age]
outputs_gender = Dense(1, activation='sigmoid',
                       name='outputs_gender')(dp1)  #dense定义输出层维度为1
outputs_age = Dense(1, activation='linear', name='outputs_age')(dp1)
outputs = Dense(12, activation='softmax', name='outputs')(dp1)  #label总共有12种
inputs = [
    inputs,
]

outputs = [
    outputs,
X_val = selector.transform(X_val).toarray()

train_sp = selector.transform(train_sp)
test_sp = selector.transform(test_sp).toarray()

print("# Num of Features: ", X_train.shape[1])

group_lb = LabelBinarizer()
labels = group_lb.fit_transform(Y)
y_train = group_lb.transform(y_train)
y_val = group_lb.transform(y_val)

inputs = Input(shape=(X_train.shape[1], ), dtype='float32')

fc1 = Dense(512)(inputs)
fc1 = SReLU()(fc1)
dp1 = Dropout(0.5)(fc1)

y_train = [y_train, y_train_gender, y_train_age]
y_val = [y_val, y_val_gender, y_val_age]

# fc1_g = Dense(256)(dp1)
# fc1_g = SReLU()(fc1_g)
# dp1_g = Dropout(0.5)(fc1_g)
outputs_gender = Dense(1, activation='sigmoid', name='outputs_gender')(dp1)

# fc1_a = Dense(256)(dp1)
# fc1_a = SReLU()(fc1_a)
# dp1_a = Dropout(0.5)(fc1_a)
outputs_age = Dense(1, activation='linear', name='outputs_age')(dp1)
Beispiel #18
0
    # merge_ui =  merge([pool_1u,pool_1i],mode="concat")

    # flatten_1 = Flatten()(merge_ui)
    # def max_1d(X):
    #     return K.max(X, axis=1)

    # lambda_1u = Lambda(max_1d, output_shape=(128,))(conv_1u)
    # lambda_1i = Lambda(max_1d, output_shape=(128,))(conv_1i)
    flatten_u = Flatten()(embed_u)
    flatten_i = Flatten()(embed_i)

    merge_ui = merge([flatten_u, flatten_i], mode="concat")

    fc1 = Dense(512)(merge_ui)
    fc1 = SReLU()(fc1)
    dp1 = Dropout(0.5)(fc1)

    fc1_1 = Dense(256)(dp1)
    fc1_1 = SReLU()(fc1_1)
    dp1_1 = Dropout(0.5)(fc1_1)

    fc1_1 = Dense(512)(dp1_1)
    fc1_1 = SReLU()(fc1_1)
    dp1_1 = Dropout(0.5)(fc1_1)

    dp1 = merge([dp1, dp1_1], mode='sum')

    fc1_1 = Dense(256)(dp1)
    fc1_1 = SReLU()(fc1_1)
    dp1 = Dropout(0.5)(fc1_1)
Beispiel #19
0
    def fitModel(self, trainFilename, testFilename):
        trainDF = pd.read_csv(trainFilename)
        testDF = pd.read_csv(testFilename)
        headers = trainDF.columns.values.tolist()
        inputs = list(set(headers) - set(outputs))
        inputs = list(set(inputs) - set(fold))
        inputs.sort()

        for output in outputs:
            trainX = trainDF.loc[:, inputs].as_matrix()
            testX = testDF.loc[:, inputs].as_matrix()
            trainY = trainDF[output].as_matrix()
            if True:
                preProcModelInput = preprocessing.MinMaxScaler()
                preProcModelInput.fit_transform(trainX)
                trainX = preProcModelInput.transform(trainX)
                testX = preProcModelInput.transform(testX)
                print 'train size:'
                print trainX.shape
                print 'test size:'
                print testX.shape
            if PP_OUT_FLAG:
                preProcModelOutput = preprocessing.MinMaxScaler()
                preProcModelOutput.fit_transform(trainY.reshape(-1, 1))
                trainY = preProcModelOutput.transform(trainY.reshape(-1, 1))
                trainY = np.squeeze(np.asarray(trainY))
                print trainY
            if LOG_FLAG:
                trainY = transformer.transform(trainY.reshape(-1, 1))
                trainY = np.squeeze(np.asarray(trainY))
                print trainY
            for key in ['dl']:
                testDataFrameCpy = testDF.copy()
                testDataFrameCpy['output'] = output
                testDataFrameCpy['tag'] = self.inputTag
                testDataFrameCpy['method'] = key
                testDataFrameCpy['obsr'] = testDF[output].values.tolist()
                outputFilename = os.path.basename(trainFilename).replace('train', 'pred_%s_%s' % (output, key))
                outputFilename = '../results/%s' % (outputFilename)
                modelFilename = outputFilename.replace('pred', 'model')
                modelFilename = modelFilename.replace('csv', 'txt')
                if key == 'dl':
                    ndim = trainX.shape[1]
                    model = Sequential()
                    model.add(Dense(10, input_dim=ndim, init='normal'))
                    model.add(SReLU())
                    model.add(Dense(5, init='normal'))
                    model.add(SReLU())
                    model.add(Dense(1, init='normal'))
                    model.add(SReLU())
                    model.compile(loss='mae', optimizer='nadam')
                    model.fit(trainX, trainY, nb_epoch=1000)
                    yhat = model.predict(testX)
                else:
                    # other learning algorithms; removed for dl benchmarking
                    pass
                if LOG_FLAG:
                    yhat = transformer.inverse_transform(yhat.reshape(-1, 1))
                if PP_OUT_FLAG:
                    yhat = preProcModelOutput.inverse_transform(yhat.reshape(-1, 1))
                predY = np.squeeze(np.asarray(yhat))
                header = 'pred'
                testDataFrameCpy[header] = predY
                testDataFrameCpy.to_csv(outputFilename, index=False)
Beispiel #20
0
                                         char_to_index, index_to_char,
                                         end_index)
validation_generator = TitleBatchGenerator(validation_data, BATCH_SIZE,
                                           char_to_index, index_to_char,
                                           end_index)
samples_per_epoch = GetSamplesPerEpoch(training_titles, BATCH_SIZE)
num_chars = end_index + 1

model = Sequential()
model.add(LSTM(1024, return_sequences=True, input_dim=num_chars))
#model.add(TimeDistributed(SReLU()))
model.add(LSTM(1024, return_sequences=True))
#model.add(TimeDistributed(SReLU()))
model.add(LSTM(1024, return_sequences=False))
model.add(Dense(2048, init='he_normal'))
model.add(SReLU())
model.add(Dense(num_chars))
model.add(Activation('softmax'))

adam = Adam()
model.compile(loss='categorical_crossentropy', optimizer=adam)
checkpointer = ModelCheckpoint(filepath='latestweights.hdf5',
                               verbose=1,
                               save_best_only=False)
lowest_loss = 2  #We don't really care for models above this

while True:
    callback = model.fit_generator(training_generator,
                                   validation_data=validation_generator,
                                   samples_per_epoch=327680,
                                   nb_val_samples=131072,
Beispiel #21
0
def main():
    phone_brand_device_model = pd.read_csv(path+'phone_brand_device_model.csv',
                    dtype={'device_id': np.str})
    num_p = len(np.unique(phone_brand_device_model['phone_brand']))
    num_d = len(np.unique(phone_brand_device_model['device_model']))
    
    del phone_brand_device_model
    
    X_train = pd.read_csv(path+'X_train.csv',
                    dtype={'device_id': np.str})
    X_test = pd.read_csv(path+'X_test.csv',
                    dtype={'device_id': np.str})

    inputs = Input(shape=(maxlen,), dtype='int32')
    
    embed = Embedding(
                    max_index_label,
                    dim,
                    dropout=0.2,
                    input_length=maxlen
                    )(inputs)


    inputs_p = Input(shape=(1,), dtype='int32')
    
    embed_p = Embedding(
                    num_p,
                    dim,
                    dropout=0.2,
                    input_length=1
                    )(inputs_p)

    inputs_d = Input(shape=(1,), dtype='int32')
    
    embed_d = Embedding(
                    num_d,
                    dim,
                    dropout=0.2,
                    input_length=1
                    )(inputs_d)

    conv_1 = Convolution1D(nb_filter=64,
                        filter_length=3,
                        border_mode='same',
                        activation='relu',
                        subsample_length=1)(embed)
    pool_1 = MaxPooling1D(pool_length=maxlen/2)(conv_1)



    flatten= Flatten()(pool_1)

    flatten_p= Flatten()(embed_p)

    flatten_d= Flatten()(embed_d)

    flatten = merge([flatten,flatten_p,flatten_d],mode='concat')
    
    fc1 = Dense(128)(flatten)
    fc1 = SReLU()(fc1)
    dp1 = Dropout(0.5)(fc1)
    
    fc2 = Dense(64)(dp1)
    fc2 = SReLU()(fc2)
    dp2 = Dropout(0.5)(fc2)


    fc1_g = Dense(128)(flatten)
    fc1_g = SReLU()(fc1_g)
    dp1_g = Dropout(0.5)(fc1_g)
    outputs_gender = Dense(1,activation='sigmoid',name='outputs_gender')(dp1_g)

    fc1_a = Dense(128)(flatten)
    fc1_a = SReLU()(fc1_a)
    dp1_a = Dropout(0.5)(fc1_a)
    outputs_age = Dense(1,activation='linear',name='outputs_age')(dp1_a)

    outputs = Dense(12,activation='softmax',name='outputs')(dp2)
    
    inputs = [
                inputs,
                inputs_p,
                inputs_d,
            ]

    outputs = [
                outputs,
                outputs_gender,
                outputs_age,
            ]

    model = Model(input=inputs, output=outputs)
    nadam = Nadam(lr=1e-4)
    sgd = SGD(lr=0.005, decay=1e-6, momentum=0.9, nesterov=True)
    # model.compile(
    #             optimizer=sgd,
    #             loss = 'categorical_crossentropy',
    #             )
    
    model.compile(
                optimizer=nadam,
                loss={'outputs': 'categorical_crossentropy', 'outputs_gender': 'binary_crossentropy','outputs_age':'mse'},
                loss_weights={'outputs': 1., 'outputs_gender': 1,'outputs_age':1}
              )
    
    model_name = 'cnn_%s.hdf5'%dim
    model_checkpoint = ModelCheckpoint(path+model_name, monitor='val_outputs_loss', save_best_only=True)
    plot(model, to_file=path+'%s.png'%model_name.replace('.hdf5',''),show_shapes=True)
    
    nb_epoch = 20
    batch_size = 128
    load_model = False
    
    if load_model:
        print('Load Model')
        model.load_weights(path+model_name)
    
    events = pd.read_pickle(path+'events_transform.pkl')
    tr_gen = X_train_generatetor_infinite(dim=dim,maxlen=maxlen,batch_size=batch_size,name="X_train.csv",events=events)
    te_gen = X_train_generatetor_infinite(dim=dim,maxlen=maxlen,batch_size=batch_size,name="X_test.csv",events=events)
    
    model.fit_generator(
        tr_gen, 
        samples_per_epoch=X_train.shape[0], 
        nb_epoch=nb_epoch, 
        verbose=1, 
        callbacks=[model_checkpoint], 
        validation_data=te_gen, 
        nb_val_samples=X_test.shape[0], 
        class_weight={}, 
        max_q_size=10
        )
    
    X_train = pd.read_csv(path+'gender_age_train.csv')
    group_le = LabelEncoder()
    group_lb = LabelBinarizer()
    labels = group_le.fit_transform(X_train['group'].values)
    labels = group_lb.fit_transform(labels)


    device_id = pd.read_csv(path+'gender_age_test.csv')['device_id']

    test = get_test(dim=dim,maxlen=maxlen,name='gender_age_test.csv',events=events)
    y_preds = model.predict(test)[0]

    # Write results
    submission = pd.DataFrame(y_preds, columns=group_le.classes_)
    submission["device_id"] = device_id
    submission = submission.set_index("device_id")
    submission.to_csv('submission_%s.csv'%dim, index=True, index_label='device_id')