python - 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
解决方案
推荐阅读
- flutter - Flutter:如何为透明容器添加阴影?
- boxing - 如果我将一个 int 值分配给一个类的对象内的变量,那么我可以将它称为装箱吗
- javascript - 为 JS 滑块添加滑动支持
- javascript - 如何在 Javascript 中通过 TagName 或 Id 删除 Dom 元素
- typescript - 类属性作为函数的类型
- jenkins - 在 Jenkins 中按名称获取环境变量
- protocols - Analytics Measurement Protocol - 错误的地理位置
- php - orm关系中如何获取用户信息?
- python - 以“词”分割句子,将多个大写词(假定为专有名词)视为一个
- java - 从电子邮件中修剪域并使值大于 10 时设置为最大 10 位并忽略“。” 如果介于两者之间。我们应该怎么做?