Project: s41467-025-62171-5 | Function: custom_loss (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Implements a custom weighted loss function for neural network training that applies differential weighting based on statistical outliers. This function assigns lower weights to data points within 3 standard deviations of the mean and higher weights to outliers, enabling robust learning for poly(A) tail length prediction.

**KEY TERMS:** {
  "loss_w": "weight matrix for robust loss calculation",
  "y_true": "actual tail length changes",
  "y_pred": "predicted tail length changes",
  "3 * std": "three standard deviation threshold for outlier detection"
}

**CONTEXT CODE:** import math
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.callbacks import Callback
from collections import OrderedDict

def custom_loss(y_true, y_pred):
	raise NotImplementedError("custom_loss function has been masked for testing purposes")

def custom_metric(y_true, y_pred):
	y_true_mean = tf.reduce_mean(y_true, axis=0) 
	y_pred_mean = tf.reduce_mean(y_pred, axis=0)
	r_numerator = tf.reduce_sum((y_true - y_true_mean) * (y_pred - y_pred_mean), axis=0)  
	x_square_sum = tf.reduce_sum(tf.square(y_true - y_true_mean), axis=0)  
	y_square_sum = tf.reduce_sum(tf.square(y_pred - y_pred_mean), axis=0)  
	r_denominator = tf.sqrt(x_square_sum * y_square_sum) + tf.keras.backend.epsilon()  
	r = r_numerator / r_denominator
	return(-tf.reduce_mean(tf.square(r)))

def resnet_block_v1(inputs, filters, kernel_size, dilation_rate = 1, strides = 1, kernel_regularizer = None, kernel_initializer = 'he_normal', activation_func = 'leaky_relu', dropout_rate = 0, trim = 'left', group_idx = 1, block_idx = 1):
	if trim is False:
		padding = 'same'
	else:
		padding = 'valid'
	name_prefix = 'resnet_v1_group_' + str(group_idx) + '_block_' + str(block_idx)
	res = inputs
	res = layers.Conv1D(
		filters = filters, 
		kernel_size = kernel_size, 
		kernel_regularizer = kernel_regularizer, 
		kernel_initializer = kernel_initializer,
		dilation_rate = dilation_rate, 
		padding = padding, 
		name = name_prefix + '_conv1D_1')(res)
	
	if activation_func == 'selu':
		res = layers.AlphaDropout(dropout_rate, name = name_prefix + '_alphadropout_1')(res)
		res = layers.LayerNormalization(name = name_prefix + '_layer_nor_1')(res)
	elif activation_func == 'silu':
		res = layers.Dropout(dropout_rate, name = name_prefix + '_dropout_1')(res)
		res = layers.LayerNormalization(name = name_prefix + '_layer_nor_1')(res)
	else:
		res = layers.Dropout(dropout_rate, name = name_prefix + '_dropout_1')(res)
		res = layers.BatchNormalization(name = name_prefix + '_batch_nor_1')(res)
	res = layers.Activation(activation_func, name = name_prefix + 'activate_1')(res)
	
	res = layers.Conv1D(
		filters = filters, 
		kernel_size = kernel_size, 
		kernel_regularizer = kernel_regularizer, 
		kernel_initializer = kernel_initializer,
		dilation_rate = dilation_rate, 
		padding = padding, 
		name = name_prefix + '_conv1D_2')(res)
	if activation_func == 'selu':
		res = layers.AlphaDropout(dropout_rate, name = name_prefix + '_alphadropout_2')(res)
		res = layers.LayerNormalization(name = name_prefix + '_layer_nor_2')(res)
	elif activation_func == 'silu':
		res = layers.Dropout(dropout_rate, name = name_prefix + '_dropout_2')(res)
		res = layers.LayerNormalization(name = name_prefix + '_layer_nor_2')(res)
	else:
		res = layers.Dropout(dropout_rate, name = name_prefix + '_dropout_2')(res)
		res = layers.BatchNormalization(name = name_prefix + '_batch_nor_2')(res)

	# crop the inputs so inputs are the same dimension as the output
	if trim == 'left': 
		inputs = layers.Cropping1D(cropping = ((kernel_size-1)*dilation_rate*2,0))(inputs)
	elif trim == 'right':
		inputs = layers.Cropping1D(cropping = (0, (kernel_size-1)*dilation_rate*2))(inputs)
	elif trim == 'both':
		inputs = layers.Cropping1D(cropping = (math.ceiling((kernel_size-1)*dilation_rate)*2, math.floor((kernel_size-1)*dilation_rate*2)))

	res = layers.Add(name = name_prefix + '_skip')([inputs, res])
	res = layers.Activation(activation_func, name = name_prefix + '_activate_2')(res)
	return(res)

def resnet_block_v2(inputs, filters, kernel_size, dilation_rate = 1, strides = 1, kernel_regularizer = None, kernel_initializer = 'he_normal', activation_func = 'leaky_relu', dropout_rate = 0, trim = 'left', group_idx = 1, block_idx = 1):
	if trim is False:
		padding = 'same'
	else:
		padding = 'valid'
	name_prefix = 'resnet_v2_group_' + str(group_idx) + '_block_' + str(block_idx)
	res = inputs

	if activation_func == 'selu':
		res = layers.AlphaDropout(dropout_rate, name = name_prefix + '_alphadropout_1')(res)
		res = layers.LayerNormalization(name = name_prefix + '_layer_nor_1')(res)
	elif activation_func == 'silu':
		res = layers.Dropout(dropout_rate, name = name_prefix + '_dropout_1')(res)
		res = layers.LayerNormalization(name = name_prefix + '_layer_nor_1')(res)
	else:
		res = layers.Dropout(dropout_rate, name = name_prefix + '_dropout_1')(res)
		res = layers.BatchNormalization(name = name_prefix + '_batch_nor_1')(res)
	res = layers.Activation(activation_func, name = name_prefix + 'activate_1')(res)

	res = layers.Conv1D(
		filters = filters, 
		kernel_size = kernel_size, 
		kernel_regularizer = kernel_regularizer, 
		kernel_initializer = kernel_initializer,
		dilation_rate = dilation_rate, 
		padding = padding, 
		name = name_prefix + '_conv1D_1')(res)
	
	if activation_func == 'selu':
		res = layers.AlphaDropout(dropout_rate, name = name_prefix + '_alphadropout_2')(res)


Complete the function implementation replacing "raise NotImplementedError".