caokyhieu / DELAFO-DeEp-Learning-Approach-for-portFolio-Optimization

MIT License
23 stars 13 forks source link

unable to print tensors between code #7

Closed turmeric-blend closed 3 years ago

turmeric-blend commented 3 years ago

hi, thanks for your work. I would like to print the output tensors to understand the code better, but it does not print out as expected. For example if I print(y_true) from utils.py:


def sharpe_ratio_loss(y_true, y_pred):
    print(y_true)          <------------------------
    epsilon = 1e-6
    max_bound = 1 - epsilon
    n_tickers = K.cast(K.shape(y_true)[1],K.floatx())
    constraint_value = 3e-3
    y_pred_reshape = K.expand_dims(y_pred, axis=-1)
    z = y_true * y_pred_reshape
    z = K.sum(z, axis=1)

    sum_w = K.clip(K.sum(y_pred_reshape, axis=1), epsilon, n_tickers* max_bound)
    ## constraint for number of tickers
#     num_constraint = C * (K.sum(y_pred) - 50)*(K.sum(y_pred)-10)
    rate = z/sum_w
    sharpeRatio = K.mean(rate, axis = 1)/K.maximum(K.std(rate, axis=1),epsilon)
    constraint =  K.sum((1.6 - y_pred) * y_pred,axis=1)
    return K.mean(constraint_value* constraint - sharpeRatio)

it returns Tensor("activation_1_target:0", shape=(?, ?), dtype=float32) instead of a tensor with values. Is there a way to solve this?

caokyhieu commented 3 years ago

@turmeric-blend you can try y_true = K.print_tensor(y_true)

turmeric-blend commented 3 years ago

it does not output anything :/

caokyhieu commented 3 years ago

As in this post :https://stackoverflow.com/questions/55022966/keras-custom-loss-function-not-printing-value-of-tensor, you just need add this line in sharpe_ratio_loss function, and run main.py file like my instruction, it will automatically print the value of y_true tensor when using sharpe_ratio_loss func.