Beispiel #1
0
    def _train(self):
        print('....')
        with tf.Session(config=tf.ConfigProto(
                device_count={'GPU': 0})) as self.sess:
            #with tf.Session() as self.sess:
            self.sess.run(tf.global_variables_initializer())
            allDataLength = len(self.train)
            global_step = 0
            print('total step:%d' %
                  (Config.epochs *
                   (int(allDataLength / Config.batch_size) + 1)))
            for i in range(Config.epochs):
                num_batchs = int(allDataLength / Config.batch_size) + 1
                for j in range(num_batchs):
                    global_step += 1
                    now_batch = self._get_batch(self.train, j)
                    start = time.time()
                    batch_dict = {
                        self.ph['single_index']: now_batch[1],
                        self.ph['numerical_index']: now_batch[2],
                        self.ph['value']: now_batch[-1],
                        self.ph['label']: get_label(now_batch[0], 2),
                        self.ph['numerical_value']: now_batch[3],
                        self.train_phase: True
                    }
                    if Config.multi_features:
                        for idx, s in enumerate(Config.multi_features):
                            batch_dict[self.ph['multi_index_%s' %
                                               s]] = now_batch[4]
                            batch_dict[self.ph['multi_value_%s' %
                                               s]] = now_batch[5]
                    end = time.time()
                    start = time.time()
                    _out, _loss, _ = self.sess.run(
                        (self.softmax_output, self.loss, self.optimizer),
                        feed_dict=batch_dict)
                    end = time.time()

                    if global_step % 10 == 0:
                        __out, __loss, __ = self.sess.run(
                            (self.softmax_output, self.loss, self.optimizer),
                            feed_dict=self.valid_dict)
                        self.global_step.append(global_step)
                        self.global_train_auc.append(_loss)
                        self.global_valid_auc.append(__loss)
                        print(
                            'step:', global_step, 'train loss:', _loss,
                            'valid loss:', __loss, 'valid_auc:',
                            auc_score(__out, get_label(self.valid_batch[0], 2),
                                      2))
Beispiel #2
0
def train(model):
    print('begin to train model......')
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        all_data_length = len(train_data)
        global_step = 0
        print('total step:%d' % (conf.epochs *
                                 (int(all_data_length / conf.batch_size) + 1)))
        for i in range(conf.epochs):
            num_batchs = int(all_data_length / conf.batch_size) + 1
            for j in range(num_batchs):
                global_step += 1
                now_batch = tools.get_batch(train_data, j, single_size,
                                            numerical_size, multi_size,
                                            conf.batch_size,
                                            conf.use_numerical_embedding)

                batch_dict = {
                    model.ph['label']: tools.get_label(now_batch[0], 2),
                    model.ph['single_index']: now_batch[1],
                    model.ph['numerical_index']: now_batch[2],
                    model.ph['numerical_value']: now_batch[3],
                    model.ph['value']: now_batch[-1],
                    model.train_phase: True
                }

                if conf.MULTI_FEATURES:
                    for idx, s in enumerate(conf.MULTI_FEATURES):
                        batch_dict[model.ph['multi_index_%s' %
                                            s]] = now_batch[4]
                        batch_dict[model.ph['multi_value_%s' %
                                            s]] = now_batch[5]

                _out, _loss, _ = sess.run(
                    [model.softmax_output, model.loss, model.optimizer],
                    feed_dict=batch_dict)
                end = time.time()

                if global_step % 10 == 0:
                    _out2, _loss2, _ = sess.run(
                        [model.softmax_output, model.loss, model.optimizer],
                        feed_dict=valid_dict)
                    print(
                        'step:', global_step, 'train loss:', _loss,
                        'valid loss:', _loss2, 'valid_auc:',
                        tools.auc_score(_out2,
                                        tools.get_label(valid_batch[0], 2), 2))
Beispiel #3
0
                            multi_size=multi_size)
    test_label = get_label(test_batch[0], 2)

    test_dict = {
        ph['single_index']: test_batch[1],
        ph['numerical_index']: test_batch[2],
        ph['numerical_value']: test_batch[3],
        ph['value']: test_batch[-1],
        ph['label']: test_label,
        train_phase: False
    }
    if Config.multi_features:
        for idx, s in enumerate(Config.multi_features):
            test_dict[ph['multi_index_%s' % s]] = test_batch[4]
            test_dict[ph['multi_value_%s' % s]] = test_batch[5]

    prediction, loss = sess.run((prediction, loss), feed_dict=test_dict)
    print(loss)
    print(prediction)
    auc_for_test = auc_score(prediction, get_label(test_batch[0], 2), 2)
    print(auc_for_test)
    res = []
    test_out_ = [x[1] for x in prediction]
    print(test_out_)
    # res.append([test_out_])
    # print(res)
    res_df = pd.DataFrame(test_out_)
    res_df.to_csv("./ex_data/results.csv")

print('Its OK')
Beispiel #4
0
    def _train(self):
        print('....')
        #设置GPU用量
        #config = tf.ConfigProto()
        #config.gpu_options.per_process_gpu_memory_fraction = 0.9 # 占用GPU100%的显存
        #session = tf.Session(config=config)
        #设置最小的GPU使用量
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        '''
        #测试集
        self.test_batch = self._get_batch(self.test, -1)
        self.test_label = get_label(self.test_batch[0], 2)
        self.test_dict = {
                                self.ph['single_index']: self.test_batch[1],
                                self.ph['numerical_index']: self.test_batch[2],
                                self.ph['numerical_value']: self.test_batch[3],
                                self.ph['value']: self.test_batch[-1],
                                self.ph['label']: self.test_label,
                                self.train_phase: False
                            }
        if Config.multi_features:
            for idx, s in enumerate(Config.multi_features):
                self.test_dict[self.ph['multi_index_%s' % s]] = self.test_batch[4]
                self.test_dict[self.ph['multi_value_%s' % s]] = self.test_batch[5]        
        '''

        saver = tf.train.Saver(max_to_keep=10)  #保存模型
        with tf.Session() as self.sess:
            self.sess.run(tf.global_variables_initializer())
            allDataLength = len(self.train)
            global_step = 0
            print('total step:%d' %
                  (Config.epochs *
                   (int(allDataLength / Config.batch_size) + 1)))
            for i in range(Config.epochs):
                num_batchs = int(allDataLength / Config.batch_size) + 1
                for j in range(num_batchs):
                    global_step += 1
                    now_batch = self._get_batch(self.train, j)
                    start = time.time()
                    batch_dict = {
                        self.ph['single_index']: now_batch[1],
                        self.ph['numerical_index']: now_batch[2],
                        self.ph['value']: now_batch[-1],
                        self.ph['label']: get_label(now_batch[0], 2),
                        self.ph['numerical_value']: now_batch[3],
                        self.train_phase: True
                    }
                    if Config.multi_features:
                        for idx, s in enumerate(Config.multi_features):
                            batch_dict[self.ph['multi_index_%s' %
                                               s]] = now_batch[4]
                            batch_dict[self.ph['multi_value_%s' %
                                               s]] = now_batch[5]
                    end = time.time()
                    start = time.time()
                    _out, _loss, _ = self.sess.run(
                        (self.softmax_output, self.loss, self.optimizer),
                        feed_dict=batch_dict)  #训练集
                    end = time.time()

                    if global_step % 10 == 0:
                        #__out, __loss, __ = self.sess.run((self.softmax_output, self.loss, self.optimizer),feed_dict=self.valid_dict) #验证集
                        #print('step:',global_step,'train loss:',_loss,'valid loss:',__loss)
                        #if global_step % 50 == 0:
                        __out, __loss = self.sess.run(
                            (self.softmax_output, self.loss),
                            feed_dict=self.valid_dict)  #验证集
                        auc, accuracy, precision, recall = auc_score(
                            __out, get_label(self.valid_batch[0], 2), 2)
                        print('step:', global_step, 'train loss:', _loss,
                              'valid loss:', __loss, 'valid_auc:', auc,
                              'accuracy:', accuracy, ' precision: ', precision,
                              ' recall: ', recall)
                        #写入文件
                        with open('OUT/DeepFM_loss_result.csv', 'a') as f:
                            f.write('step: ' + str(global_step) +
                                    ' train loss: ' + str(_loss) +
                                    ' valid loss: ' + str(__loss) +
                                    ' valid_auc: ' + str(auc) + ' accuracy: ' +
                                    str(accuracy) + ' precision: ' +
                                    str(precision) + ' recall: ' +
                                    str(recall) + '\n')

                #每轮输出一次测试结果,并保存模型
                saver.save(self.sess, 'ckpt/mnist.ckpt', global_step=i + 1)
                '''