-
Notifications
You must be signed in to change notification settings - Fork 11
/
ctr_fnn.py
77 lines (65 loc) · 2.65 KB
/
ctr_fnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import pandas as pd
from time import time
from sklearn import metrics
from sample_encoding_keras import *
from utility import Options, ctr_batch_generator, read_feat_index, eval_auc
import tensorflow as tf
opts = Options()
print('Loading data...')
#############################
# Settings for ipinyou
#############################
opts.sequence_length = 22
opts.total_training_sample = 127127
# change here for different camp
# 1458 2261 2997 3386 3476 2259 2821 3358 3427 all
BASE_PATH = './data/make-ipinyou-data/all/'
opts.field_indices_path = BASE_PATH + 'field_indices.txt'
#opts.train_path = BASE_PATH + 'train.yzx.10.txt'
opts.train_path = BASE_PATH + 'train.yzx.txt'
opts.test_path = BASE_PATH + 'test.yzx.txt'
opts.featindex = BASE_PATH + 'featindex.txt'
opts.model_name = 'ipinyou_concat'
opts.epochs_to_train = 5
### Paramerters for tuning
opts.batch_size = 64
opts.embedding_size = 16
opts.dropout = 0.1
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
K.set_session(sess)
id2cate, cate2id, opts.vocabulary_size = read_feat_index(opts)
#############################
# Model
#############################
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Embedding, Dropout, Activation, Flatten, Merge, merge
input_tensor = tf.placeholder(tf.int32, shape=(opts.batch_size,opts.sequence_length))
a = Input(batch_shape=(opts.batch_size,opts.sequence_length), tensor=input_tensor,name='input')
e = Embedding(opts.vocabulary_size,
opts.embedding_size,
input_length=opts.sequence_length, dropout=opts.dropout, name='embedding')(a)
x = Lambda(lambda t: tf.reshape(t, [-1, opts.sequence_length * opts.embedding_size]))(e)
x = Dense(128, activation='sigmoid')(x)
x = Dense(32, activation='sigmoid')(x)
x = Dense(1, activation='sigmoid', name='prob')(x)
model = Model(input=a, output=x)
model.compile(optimizer='nadam',
loss='binary_crossentropy',
metrics=['accuracy'])
print(model.summary())
opts.current_epoch = 1
target = [model.get_layer(name='prob').output]
get_prob = K.function([input_tensor], target)
#############################
# Model Training
#############################
for i in range(opts.epochs_to_train):
print("Current Global Epoch: ", opts.current_epoch)
training_batch_generator = ctr_batch_generator(opts)
history = model.fit_generator(training_batch_generator, opts.total_training_sample, 1)
if i % 1 == 0:
model.save('model_'+opts.model_name+'_' + str(opts.current_epoch) + '.h5')
eval_auc(model, opts, target=target, get_prob=get_prob)
opts.current_epoch += 1