Closed MarMarhoun closed 8 months ago
Hi @MarMarhoun and thanks for reaching out.
I run the code snippet you sent on my side and it appears that the data
tensor you are trying to use as an input for the SB algorithm has a shape (252, 6)
.
For SB to work, the input matrix you provide must be square, i.e. it must have 2 dimensions and both must be the same. The problem you encountered comes from the difference between the two dimensions of the matrix.
Hope this could help.
All the best!
Greetings @bqth29, Yes, I know this issue is there any way I can solve this issue?
I tried the following code to run the simulated bifurcation algorithm. However, I received an issue during the implementation.
Here is the code:
!pip install yfinance
!pip install plotly
!pip install tqdm
!pip install numpy
!pip install pandas
!pip install simulated-bifurcation
!pip install deap
!pip install plotly_express
import yfinance as yf
import torch
import random # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import yfinance as yf
import matplotlib.pyplot as plt
import json
import numpy as np
import simulated_bifurcation as sb
def get_data(asset_name, period='1y', interval='1d'):
data = yf.download(tickers=asset_name, period=period, interval=interval)
return data
def optimize_sb(matrix, **kwargs):
sb.set_env(time_step=.1, pressure_slope=.01, heat_coefficient=.06)
best_vector, best_value = sb.maximize(matrix, **kwargs)
return best_vector, best_value
if __name__ == "__main__":
asset_name = 'AAPL'
data = get_data(asset_name)
matrix = torch.tensor(data.to_numpy())
agents = 100
max_steps = 10000
sampling_period = 30
convergence_threshold = 50
device = 'cuda' # To enable the GPU
best_vector, best_value = optimize_sb(matrix, agents=agents, device=device,
max_steps=max_steps, sampling_period=sampling_period, ballistic= True,
convergence_threshold=convergence_threshold, use_window=True, heated=True, best_only=True)
Here is the issue:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-efec6dd548a5> in <cell line: 2>()
1 sb.set_env(time_step=.1, pressure_slope=.01, heat_coefficient=.06)
----> 2 best_vector, best_value = sb.maximize(matrix, agents=100, device='cuda',
3 max_steps=10000, sampling_period=30, ballistic= True,
4 convergence_threshold=50, use_window=True, heated=True, best_only=True)
6 frames
/usr/local/lib/python3.10/dist-packages/simulated_bifurcation/polynomial/base_multivariate_polynomial.py in __check_square_matrix(matrix)
252 raise ValueError(f"Matrix requires two dimension, got {matrix.ndim}.")
253 if matrix.shape[0] != matrix.shape[1]:
--> 254 raise ValueError("Matrix must be square.")
255
256 def __check_vector_shape(self, vector: torch.Tensor) -> None:
ValueError: Matrix must be square.
Hi @MarMarhoun,
It appears that your input matrix does not suit the SB algorithm because it is not square. Could you explain us what your code is trying to achieve to see if we can apply some operations to said matrix to make it square and compatible with SB?
All the best!
I used this code to perform an opt task and I read on the docs that the An Ising problem, given a null-diagonal square symmetrical matrix.
The following error appears: