首页 > 解决方案 > Resolving Compile Errors in Custom Tensorflow Loss Function

问题描述

I am attempting to create a loss function for tensorflow that incorporates domain knowledge, but am encountering issues associated with the environment in which the function compiles and runs within tensorflow. I am looking for an explanation or documentation to understand what is possible to do within the scope of the loss function. I am using the 2.4.1 version of tensorflow.

Right now, attempts to extract the tensor dimensions do not work and the following compile error is raised:

AttributeError: 'Tensor' object has no attribute 'as_list'

I have also seen the following: AttributeError: 'Tensor' object has no attribute 'numpy' AttributeError: 'Tensor' object has no attribute 'make_ndarray'

It would help if I could debug the code, but I have not seen answers on debugging compilation or getting line numbers for the errors making the whole process fragile and error prone.

def domain_specific_loss_function( y_actual, y_predicted ):
        shape_tensor = tf.shape( y_actual ).as_list()
        batch_size = shape_tensor[0]
        dimension_count = shape_tensor[1]
        error_list = []
        for i in range(0, batch_size):
                error_list.append( 0.0 )
        for i in range( 0, batch_size ):
                is_peak = []
                for j in range(0, dimension_count):
                        is_peak.append( False )
                for j in range( 1, dimension_count -1 ):
                        yr_actual = y_actual[i]
                        if ((yr_actual[ j - 1 ] < yr_actual[ j ]) and (yr_actual[ j ] > yr_actual[ j + 1 ])):
                                is_peak[ j ] = True
                for j in range( 0, dimension_count ):
                        yr_actual = y_actual[i]
                        yr_predicted = y_predicted[i]
                        if (yr_actual[ j ] < yr_actual[ j ]):
                                if (not is_peak[ j ]):
                                        error_list[ j ] = 2*(yr_predicted[ j ] - yr_actual[ j ])
                                else:
                                        error_list[ j ] = yr_predicted[ j ] - yr_actual[ j ];
                        else:
                                if (not is_peak[ i ]):
                                        error_list[ j ] = 2*(yr_actual[ j ] - yr_predicted[ j ])
                                else:
                                        error_list[ j ] = yr_actual[ j ] - yr_predicted[ j ];
        return error_list

标签: pythontensorflowloss-functionlosscode-documentation

解决方案


推荐阅读