PCGrad icon indicating copy to clipboard operation
PCGrad copied to clipboard

Anyone with a problem like this? (AttributeError: 'NoneType' object has no attribute 'op')

Open SehwanMoon opened this issue 5 years ago • 7 comments

Anyone with a problem like this? (AttributeError: 'NoneType' object has no attribute 'op')

losses Out[159]: [<tf.Tensor 'add_17:0' shape=() dtype=float32>, <tf.Tensor 'add_17:0' shape=() dtype=float32>]

train_step = optimizer.minimize(losses) Traceback (most recent call last):

File "", line 1, in train_step = optimizer.minimize(losses)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\training\optimizer.py", line 399, in minimize grad_loss=grad_loss)

File "F:\Pan_cancer\OmiVAE-master\PCGrad_tf.py", line 40, in compute_gradients if grad is not None], axis=0), loss)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 459, in map_fn maximum_iterations=n)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3209, in while_loop result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2941, in BuildLoop pred, body, original_loop_vars, loop_vars, shape_invariants)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2878, in _BuildLoop body_result = body(*packed_vars_for_body)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3179, in body = lambda i, lv: (i + 1, orig_body(*lv))

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 448, in compute packed_fn_values = fn(packed_values)

File "F:\Pan_cancer\OmiVAE-master\PCGrad_tf.py", line 39, in for grad in tf.gradients(x, var_list)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 532, in gradients gate_gradients, aggregation_method, stop_gradients)

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 588, in _GradientsHelper from_ops = [t.op for t in xs]

File "C:\Users\USER\Anaconda3\envs\tensorflow3\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 588, in from_ops = [t.op for t in xs]

AttributeError: 'NoneType' object has no attribute 'op'

SehwanMoon avatar Sep 16 '20 09:09 SehwanMoon

Use the code below right before where the tf.gradients is called, hopefully this will fix it:

        if var_list is None:
          var_list = (
          variables.trainable_variables() +
          ops.get_collection(ops.GraphKeys.TRAINABLE_RESOURCE_VARIABLES))
        else:
          var_list = nest.flatten(var_list)

mnabian avatar Sep 23 '20 04:09 mnabian

Use the code below right before where the tf.gradients is called, hopefully this will fix it:

        if var_list is None:
          var_list = (
          variables.trainable_variables() +
          ops.get_collection(ops.GraphKeys.TRAINABLE_RESOURCE_VARIABLES))
        else:
          var_list = nest.flatten(var_list)

It cannot and some new variables are involved causing errors.

Coder-Yu avatar Sep 26 '20 04:09 Coder-Yu

+1,how to solve this problem?

scriptboy1990 avatar Apr 20 '21 08:04 scriptboy1990

I got the same issue

silver1886 avatar Dec 02 '21 04:12 silver1886

+1, I got the same issue

Xls1994 avatar Feb 18 '22 02:02 Xls1994

who can give an answer?really thanks

momodagithub avatar Apr 13 '22 11:04 momodagithub

The default value of var_list is None. To compute the gradients between each loss and var_list, var_list should be specified first.

        if var_list is None:
            var_list = (
                    variables.trainable_variables() +
                    ops.get_collection(ops.GraphKeys.TRAINABLE_RESOURCE_VARIABLES))
        else:
            var_list = nest.flatten(var_list)
        # pylint: disable=protected-access
        var_list += ops.get_collection(ops.GraphKeys._STREAMING_MODEL_PORTS)
        # pylint: enable=protected-access
        if not var_list:
            raise ValueError("No variables to optimize.")

EveDong avatar Jul 21 '22 12:07 EveDong