nilmtk / nilmtk-contrib

Apache License 2.0
114 stars 59 forks source link

AFHMM+SAC - ValueError: matmul: Input operand 0 does not have enough dimensions #40

Open levaphenyl opened 4 years ago

levaphenyl commented 4 years ago

When infering with AFHMM+SAC, one disaggregation thread can fail. This happens to the disaggregation thread that deals with the incomplete block, i.e. with the tail of the chunk. The failure happens during the optimization. The solver does not find any suitable constraint for the appliance and returns a Variable with a None value. This None instead of a np.array causes the later alternative minimization to fail.

Traceback ``` Generating predictions for : AFHMM_SAC Process Process-10: Traceback (most recent call last): File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap self.run() File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run self._target(*self._args, **self._kwargs) File ".venv/lib/python3.8/site-packages/nilmtk_contrib-0.1.2.dev1+git.de38dab-py3.8.egg/nilmtk_contrib/disaggregate/afhmm_sac.py", line 172, in disaggregate_thread app_usage= np.sum(s_[appliance_id]@means_vector[appliance_id],axis=1) ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1 ```

Reproductible with chunkwise training and testing on REFIT building 1 between 2014-03-08 and 2014-04-11.

Code ```python import nilmtk.api import nilmtk_contrib params = { "power": { "mains": [ "apparent", "active" ], "appliance": [ "active" ] }, "appliances": [ "dish washer", ], "artificial_aggregate": False, "chunk_size": 2**15, "sample_rate": 60, "DROP_ALL_NANS": True, "methods": { "AFHMM+SAC": nilmtk_contrib.disaggregate.AFHMM_SAC({ "default_num_states": 2, "chunk_wise_training": True, "time_period": 720, }), }, "train": { "datasets": { "REDD": { "path": "datasets/REDD/redd.h5", "buildings": { 1: { "start_time": "2011-04-18", "end_time": "2011-04-28" }, }}, }, }, "test": { "datasets": { "REFIT": { "path": "datasets/REFIT/refit.h5", "buildings": { 1: { "start_time": "2014-03-08", "end_time": "2014-04-11" }, }}, }, "metrics": [ "mae", ], }, } api_res = nilmtk.api.API(params) ```

Solution: Test if the Variable is empty before adding it to the constraints list s_ here. If the variable is empty, append a np.zeros with the right shape.