def init_graph(self):
    if (self.sess is not None):
        return
    self.graph = tf.Graph()
    with self.graph.as_default():
        tf1.set_random_seed(self.random_seed)
        self.feat_index = tf1.placeholder(tf.int32, shape=[None, None], name='feat_index')
        self.feat_value = tf1.placeholder(tf.float32, shape=[None, None], name='feat_value')
        self.label = tf1.placeholder(tf.float32, shape=[None, 1], name='label')
        self.dropout_keep_fm = tf1.placeholder(tf.float32, shape=[None], name='dropout_keep_fm')
        self.dropout_keep_deep = tf1.placeholder(tf.float32, shape=[None], name='dropout_keep_deep')
        self.train_phase = tf1.placeholder(tf.bool, name='train_phase')
        self.weights = self._initialize_weights()
        self.embeddings = tf.nn.embedding_lookup(self.weights['feature_embeddings'], self.feat_index)
        feat_value = tf.reshape(self.feat_value, shape=[(- 1), self.field_size, 1])
        self.embeddings = tf.multiply(self.embeddings, feat_value)
        self.y_first_order = tf.nn.embedding_lookup(self.weights['feature_bias'], self.feat_index)
        self.y_first_order = tf.reduce_sum(tf.multiply(self.y_first_order, feat_value), 2)
        self.y_first_order = tf.nn.dropout(self.y_first_order, rate=(1 - self.dropout_keep_fm[0]))
        self.summed_features_emb = tf.reduce_sum(self.embeddings, 1)
        self.summed_features_emb_square = tf.square(self.summed_features_emb)
        self.squared_features_emb = tf.square(self.embeddings)
        self.squared_sum_features_emb = tf.reduce_sum(self.squared_features_emb, 1)
        self.y_second_order = (0.5 * tf.subtract(self.summed_features_emb_square, self.squared_sum_features_emb))
        self.y_second_order = tf.nn.dropout(self.y_second_order, rate=(1 - self.dropout_keep_fm[1]))
        self.y_deep = tf.reshape(self.embeddings, shape=[(- 1), (self.field_size * self.embedding_size)])
        self.y_deep = tf.nn.dropout(self.y_deep, rate=(1 - self.dropout_keep_deep[0]))
        for i in range(0, len(self.deep_layers)):
            self.y_deep = tf.add(tf.matmul(self.y_deep, self.weights[('layer_%d' % i)]), self.weights[('bias_%d' % i)])
            if self.batch_norm:
                self.y_deep = self.batch_norm_layer(self.y_deep, train_phase=self.train_phase, scope_bn=('bn_%d' % i))
            self.y_deep = self.deep_layers_activation(self.y_deep)
            self.y_deep = tf.nn.dropout(self.y_deep, rate=(1 - self.dropout_keep_deep[(1 + i)]))
        if (self.use_fm and self.use_deep):
            concat_input = tf.concat([self.y_first_order, self.y_second_order, self.y_deep], axis=1)
        elif self.use_fm:
            concat_input = tf.concat([self.y_first_order, self.y_second_order], axis=1)
        elif self.use_deep:
            concat_input = self.y_deep
        self.out = tf.add(tf.matmul(concat_input, self.weights['concat_projection']), self.weights['concat_bias'])
        if (self.loss_type == 'logloss'):
            self.out = tf.nn.sigmoid(self.out, name='out')
            self.loss = tf1.losses.log_loss(self.label, self.out)
        elif (self.loss_type == 'mse'):
            self.loss = tf.nn.l2_loss(tf.subtract(self.label, self.out))
        if (self.l2_reg > 0):
            self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights['concat_projection'])
            if self.use_deep:
                for i in range(len(self.deep_layers)):
                    self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights[('layer_%d' % i)])
        if (self.optimizer_type == 'adam'):
            self.optimizer = tf1.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(self.loss)
        elif (self.optimizer_type == 'adagrad'):
            self.optimizer = tf1.train.AdagradOptimizer(learning_rate=self.learning_rate, initial_accumulator_value=1e-08).minimize(self.loss)
        elif (self.optimizer_type == 'gd'):
            self.optimizer = tf1.train.GradientDescentOptimizer(learning_rate=self.learning_rate).minimize(self.loss)
        elif (self.optimizer_type == 'momentum'):
            self.optimizer = tf1.train.MomentumOptimizer(learning_rate=self.learning_rate, momentum=0.95).minimize(self.loss)
        self.saver = tf1.train.Saver()
        init = tf1.global_variables_initializer()
        self.sess = self._init_session()
        self.sess.run(init)
        total_parameters = 0
        for variable in self.weights.values():
            shape = variable.get_shape()
            variable_parameters = 1
            for dim in shape:
                variable_parameters *= dim.value
            total_parameters += variable_parameters
        if (self.verbose > 0):
            print(('#params: %d' % total_parameters))
def init_graph(self):
    if (self.sess is not None):
        return
    self.graph = tf.Graph()
    with self.graph.as_default():
        tf1.set_random_seed(self.random_seed)
        self.feat_index = tf1.placeholder(tf.int32, shape=[None, None], name='feat_index')
        self.feat_value = tf1.placeholder(tf.float32, shape=[None, None], name='feat_value')
        self.label = tf1.placeholder(tf.float32, shape=[None, 1], name='label')
        self.dropout_keep_fm = tf1.placeholder(tf.float32, shape=[None], name='dropout_keep_fm')
        self.dropout_keep_deep = tf1.placeholder(tf.float32, shape=[None], name='dropout_keep_deep')
        self.train_phase = tf1.placeholder(tf.bool, name='train_phase')
        self.weights = self._initialize_weights()
        self.embeddings = tf.nn.embedding_lookup(self.weights['feature_embeddings'], self.feat_index)
        feat_value = tf.reshape(self.feat_value, shape=[(- 1), self.field_size, 1])
        self.embeddings = tf.multiply(self.embeddings, feat_value)
        self.y_first_order = tf.nn.embedding_lookup(self.weights['feature_bias'], self.feat_index)
        self.y_first_order = tf.reduce_sum(tf.multiply(self.y_first_order, feat_value), 2)
        self.y_first_order = tf.nn.dropout(self.y_first_order, rate=(1 - self.dropout_keep_fm[0]))
        self.summed_features_emb = tf.reduce_sum(self.embeddings, 1)
        self.summed_features_emb_square = tf.square(self.summed_features_emb)
        self.squared_features_emb = tf.square(self.embeddings)
        self.squared_sum_features_emb = tf.reduce_sum(self.squared_features_emb, 1)
        self.y_second_order = (0.5 * tf.subtract(self.summed_features_emb_square, self.squared_sum_features_emb))
        self.y_second_order = tf.nn.dropout(self.y_second_order, rate=(1 - self.dropout_keep_fm[1]))
        self.y_deep = tf.reshape(self.embeddings, shape=[(- 1), (self.field_size * self.embedding_size)])
        self.y_deep = tf.nn.dropout(self.y_deep, rate=(1 - self.dropout_keep_deep[0]))
        for i in range(0, len(self.deep_layers)):
            self.y_deep = tf.add(tf.matmul(self.y_deep, self.weights[('layer_%d' % i)]), self.weights[('bias_%d' % i)])
            if self.batch_norm:
                self.y_deep = self.batch_norm_layer(self.y_deep, train_phase=self.train_phase, scope_bn=('bn_%d' % i))
            self.y_deep = self.deep_layers_activation(self.y_deep)
            self.y_deep = tf.nn.dropout(self.y_deep, rate=(1 - self.dropout_keep_deep[(1 + i)]))
        if (self.use_fm and self.use_deep):
            concat_input = tf.concat([self.y_first_order, self.y_second_order, self.y_deep], axis=1)
        elif self.use_fm:
            concat_input = tf.concat([self.y_first_order, self.y_second_order], axis=1)
        elif self.use_deep:
            concat_input = self.y_deep
        self.out = tf.add(tf.matmul(concat_input, self.weights['concat_projection']), self.weights['concat_bias'])
        if (self.loss_type == 'logloss'):
            self.out = tf.nn.sigmoid(self.out, name='out')
            self.loss = tf1.losses.log_loss(self.label, self.out)
        elif (self.loss_type == 'mse'):
            self.loss = tf.nn.l2_loss(tf.subtract(self.label, self.out))
        if (self.l2_reg > 0):
            self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights['concat_projection'])
            if self.use_deep:
                for i in range(len(self.deep_layers)):
                    self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights[('layer_%d' % i)])
        if (self.optimizer_type == 'adam'):
            self.optimizer = tf1.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(self.loss)
        elif (self.optimizer_type == 'adagrad'):
            self.optimizer = tf1.train.AdagradOptimizer(learning_rate=self.learning_rate, initial_accumulator_value=1e-08).minimize(self.loss)
        elif (self.optimizer_type == 'gd'):
            self.optimizer = tf1.train.GradientDescentOptimizer(learning_rate=self.learning_rate).minimize(self.loss)
        elif (self.optimizer_type == 'momentum'):
            self.optimizer = tf1.train.MomentumOptimizer(learning_rate=self.learning_rate, momentum=0.95).minimize(self.loss)
        self.saver = tf1.train.Saver()
        init = tf1.global_variables_initializer()
        self.sess = self._init_session()
        self.sess.run(init)
        total_parameters = 0
        for variable in self.weights.values():
            shape = variable.get_shape()
            variable_parameters = 1
            for dim in shape:
                variable_parameters *= dim.value
            total_parameters += variable_parameters
        if (self.verbose > 0):
            print(('#params: %d' % total_parameters))