Recommend/MR/mrcnn5.py
def testing(self, test_dataset, step_num):
        test_X, test_y = test_dataset
        test_batches = self.get_batches(test_X, test_y)

        """Perform an evaluation of `model` on the examples from `dataset`."""
#         avg_loss = tf.keras.metrics.Mean('loss', dtype=tf.float32)
        #         avg_mae = tf.keras.metrics.Mean('mae', dtype=tf.float32)

        batch_num = (len(test_X) // self.batch_size)
        for batch_i in range(batch_num):
            x, y = next(test_batches)
            categories = np.zeros([self.batch_size, 18])
            for i in range(self.batch_size):
                categories[i] = x.take(6, 1)[i]

            titles = np.zeros([self.batch_size, self.sentences_size])
            for i in range(self.batch_size):
                titles[i] = x.take(5, 1)[i]

            logits = self.model([np.reshape(x.take(0, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(2, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(3, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(4, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(1, 1), [self.batch_size, 1]).astype(np.float32),
                                 categories.astype(np.float32),
                                 titles.astype(np.float32)], training=False)
            test_loss = self.ComputeLoss(np.reshape(y, [self.batch_size, 1]).astype(np.float32), logits)
            self.avg_loss(test_loss)
            # 保存测试损失
            self.losses['test'].append(test_loss)
            self.ComputeMetricsMAE(np.reshape(y, [self.batch_size, 1]).astype(np.float32), logits)
            # avg_loss(self.compute_loss(labels, logits))
            # avg_mae(self.compute_metrics(labels, logits))

        print('Model test set loss: {:0.6f} mae: {:0.6f}'.format(self.avg_loss.result(), self.ComputeMetricsMAE.result()))
        # print('Model test set loss: {:0.6f} mae: {:0.6f}'.format(avg_loss.result(), avg_mae.result()))
        #         summary_ops_v2.scalar('loss', avg_loss.result(), step=step_num)
        #         summary_ops_v2.scalar('mae', self.ComputeMetricsMAE.result(), step=step_num)
        # summary_ops_v2.scalar('mae', avg_mae.result(), step=step_num)

        if self.avg_loss.result() < self.best_loss:
            self.best_loss = self.avg_loss.result()
            print("best loss = {}".format(self.best_loss))
            self.checkpoint.save(self.checkpoint_prefix)
Perform an evaluation of `model` on the examples from `dataset`.
Perform an evaluation of `model` on the examples from `dataset`.
Perform an evaluation of ` model ` on the examples from ` dataset ` .
def testing(self, test_dataset, step_num):
        test_X, test_y = test_dataset
        test_batches = self.get_batches(test_X, test_y)
        batch_num = (len(test_X) // self.batch_size)
        for batch_i in range(batch_num):
            x, y = next(test_batches)
            categories = np.zeros([self.batch_size, 18])
            for i in range(self.batch_size):
                categories[i] = x.take(6, 1)[i]
            titles = np.zeros([self.batch_size, self.sentences_size])
            for i in range(self.batch_size):
                titles[i] = x.take(5, 1)[i]
            logits = self.model([np.reshape(x.take(0, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(2, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(3, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(4, 1), [self.batch_size, 1]).astype(np.float32),
                                 np.reshape(x.take(1, 1), [self.batch_size, 1]).astype(np.float32),
                                 categories.astype(np.float32),
                                 titles.astype(np.float32)], training=False)
            test_loss = self.ComputeLoss(np.reshape(y, [self.batch_size, 1]).astype(np.float32), logits)
            self.avg_loss(test_loss)
            self.losses['test'].append(test_loss)
            self.ComputeMetricsMAE(np.reshape(y, [self.batch_size, 1]).astype(np.float32), logits)
        print('Model test set loss: {:0.6f} mae: {:0.6f}'.format(self.avg_loss.result(), self.ComputeMetricsMAE.result()))
        if self.avg_loss.result() < self.best_loss:
            self.best_loss = self.avg_loss.result()
            print("best loss = {}".format(self.best_loss))
            self.checkpoint.save(self.checkpoint_prefix)
def testing ( self , test_dataset , step_num ) : test_X , test_y = test_dataset test_batches = self . get_batches ( test_X , test_y ) batch_num = ( len ( test_X ) // self . batch_size ) for batch_i in range ( batch_num ) : x , y = next ( test_batches ) categories = np . zeros ( [ self . batch_size , 18 ] ) for i in range ( self . batch_size ) : categories [ i ] = x . take ( 6 , 1 ) [ i ] titles = np . zeros ( [ self . batch_size , self . sentences_size ] ) for i in range ( self . batch_size ) : titles [ i ] = x . take ( 5 , 1 ) [ i ] logits = self . model ( [ np . reshape ( x . take ( 0 , 1 ) , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , np . reshape ( x . take ( 2 , 1 ) , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , np . reshape ( x . take ( 3 , 1 ) , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , np . reshape ( x . take ( 4 , 1 ) , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , np . reshape ( x . take ( 1 , 1 ) , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , categories . astype ( np . float32 ) , titles . astype ( np . float32 ) ] , training = False ) test_loss = self . ComputeLoss ( np . reshape ( y , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , logits ) self . avg_loss ( test_loss ) self . losses [ 'test' ] . append ( test_loss ) self . ComputeMetricsMAE ( np . reshape ( y , [ self . batch_size , 1 ] ) . astype ( np . float32 ) , logits ) print ( 'Model test set loss: {:0.6f} mae: {:0.6f}' . format ( self . avg_loss . result ( ) , self . ComputeMetricsMAE . result ( ) ) ) if self . avg_loss . result ( ) < self . best_loss : self . best_loss = self . avg_loss . result ( ) print ( "best loss = {}" . format ( self . best_loss ) ) self . checkpoint . save ( self . checkpoint_prefix )
Perform an evaluation of `model` on the examples from `dataset`.
Perform an evaluation of ` model ` on the examples from ` dataset ` .
"""Perform an evaluation of `model` on the examples from `dataset`.""" #         avg_loss = tf.keras.metrics.Mean('loss', dtype=tf.float32) #         avg_mae = tf.keras.metrics.Mean('mae', dtype=tf.float32) # 保存测试损失 # avg_loss(self.compute_loss(labels, logits)) # avg_mae(self.compute_metrics(labels, logits)) # print('Model test set loss: {:0.6f} mae: {:0.6f}'.format(avg_loss.result(), avg_mae.result())) #         summary_ops_v2.scalar('loss', avg_loss.result(), step=step_num) #         summary_ops_v2.scalar('mae', self.ComputeMetricsMAE.result(), step=step_num) # summary_ops_v2.scalar('mae', avg_mae.result(), step=step_num)
self
test_dataset
step_num