ljvmiranda921/pyswarms

TypeError: '<' not supported between instances of 'generator' and 'float'

agcala opened this issue · 1 comments

agcala commented

Windows 10 22H2
Pyswarm 1.3.0
Python 3.9.13

II am trying to adapt the toy example of Neural Network architecture to a real life case where I need to use a data generator to feed the data cycle. Apart from the obvious change on the layers I had to change main routine f(x) as follow:

    n_particles = x.shape[0]
    ploss=np.zeros(n_particles)
    for k in range(ntrain):
        X,Y=next(dgen)
        j=[forward_prop(x[i]) for i in range(n_particles)]
        ploss=ploss+np.array(j)
    return np.array(ploss)/ntrain 

Here basically what I am doing is feeding batches of data to the network and letting the network find the loss for each batch which I then add together and find an average loss by particle which I then return to the optimizer. ntrain is the amount of times I need to call the data generator dgen to consume all the data. At the end I have a list of every particle loss in j which then I accumulate in the list ploss to be able to calculate the average loss by particle. Needless to say I tested the changes with just the first batch of data and everything seemed to work alright.
When I try the whole data set, which is too big to be read at once, (not to mention the particle swarm itself take a lot of memory, ) I get the following error

 dimensions: 7570
 2023-06-11 05:40:01,733 - pyswarms.single.global_best - INFO - Optimize for 1000 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
 pyswarms.single.global_best:   0%|                                                                           |0/10002023-06-11 05:40:04.507691: I 
 tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network 
 Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
 2023-06-11 05:40:05.020539: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device 
 /job:localhost/replica:0/task:0/device:GPU:0 with 1359 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 750 Ti, pci bus id: 
 0000:01:00.0, compute capability: 5.0
  pyswarms.single.global_best:   0%|                                                                           |0/1000
  Traceback (most recent call last):
  File "o:\horses\programs\swarmmlp.py", line 148, in <module>
  cost, pos = optimizer.optimize(f, iters=1000)
  File "k:\anaconda\lib\site-packages\pyswarms\single\global_best.py", line 210, in optimize
  self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
  File "k:\anaconda\lib\site-packages\pyswarms\backend\operators.py", line 66, in compute_pbest
  mask_cost = swarm.current_cost < swarm.pbest_cost
  TypeError: '<' not supported between instances of 'generator' and 'float'

I need to add here I changed some of the numpy arithmetic with tensor ones to provide some additional speed. But again I tested everything with one batch of data and worked fine.
Now I went to the supposed cause of the error , the script operrators.py and couldn't find any relation to a data generator there,
Any ideas?

agcala commented

I solved it.