LSTM for Function Maximization

I have got a beast of a post here so feel free to answer some, all, or none of my questions…

For background, I have a dataset that looks like this picture:

I am attempting to use an LSTM Neural Network to optimize Sharpe ratio by messing with portfolio allocations.

  1. I am using a batch size of 8, sequence length of 16, and have 16 features as shown above. As of right now I have a tensor of size [8, 16, 16] where it is 16 groups of 8 consecutive time periods. From my understanding it takes 8 months and makes an array out of that then stacks 16 of those on top of each other.

I am wondering if this seems logical to everyone as a way to format my data. It seems almost too simple.

  1. I am having difficulty with the forward function. I have tried quite a few things, so I wish I could attach more images. My goal is to have it output 8 values (maybe in a 1x8 tensor), those being the allocations for each asset class, however, I am struggling with making this happen. Wondering if anyone has any input on the appropriate way to make this happen. As of right now I’m getting very weird shape tensors back.

  2. Once I get the data and forward working, I can move on to the Loss Function. I am very confused about how to make this happen. Once I have the allocations I need to calculate what Sharpe that would have given over the period, however, it needs to all be done in a tensor form. Any pointers on making custom loss functions in general?

It did not let me attach more than one picture so I will do that here:

For question 2, here is my code:

For question 1, here is my code: