6

Upon copying retrain.py from the TensorFlow GitHub repository

And then opening it in PyCharm, on lines 794 and 802 PyCharm shows the following warning:

Type 'Variable' doesn't have expected attribute '__sub__'

Here is a screenshot if that helps:

enter image description here

Can somebody please explain:

  1. What does this mean?

  2. How can this be resolved or the warning suppressed?

Clearly PyCharm thinks that layer_weights does not have an attribute "__sub__", but what does this mean and why would a __sub__ attribute be necessary? The function variable_summaries() does not refer to an attribute __sub__ (copied/pasted starting at line 735):

def variable_summaries(var):
    """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)
        tf.summary.scalar('max', tf.reduce_max(var))
        tf.summary.scalar('min', tf.reduce_min(var))
        tf.summary.histogram('histogram', var)
# end of function

Can somebody explain why an attribute __sub__ would be necessary?

After reading the this post, I'm under the impression that a comment to suppress this warning could be added, possibly something like:

@type whatGoesHere: ??
@attribute __sub__: comment here??    # is this correct?
@param whatGoesHere: ??

Is something like this doable, and what should the comment be?

I prefer to not disable PyCharm's warnings as I find them helpful in many cases. Can somebody please provide some enlightenment on the above so as to avoid disabling this warning in PyCharm?

- Edit:

Thanks for the explanation Shu. For the moment this seems to be the best way to deal with this in PyCharm without disabling that inspection entirely:

# this comment is necessary to suppress an unnecessary PyCharm warning
# noinspection PyTypeChecker
variable_summaries(layer_weights)

If eventually somebody could inform me of a better option that would be great.

Houda
  • 671
  • 6
  • 16
cdahms
  • 3,402
  • 10
  • 49
  • 75
  • If you would like to suppress the warnings, check the link below: https://www.jetbrains.com/help/pycharm/suppressing-inspections.html – Luis G Riera Nov 08 '18 at 04:50

1 Answers1

8

The - operator is called on var inside variable_summaries:

stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))

Normally Python would be looking for the __sub__ method of var when evaluating the expression var - mean. However, a tf.Variable instance is wrapped around state_ops.variable_op_v2 in order to support Ops on GPU/CPU and does not have a __sub__ method that Python would normally expect.

Therefore, this warning is inherent to the way TensorFlow heavily customizing standard Python operators on Tensorflow objects in order to support the expressions we are used to while enabling GPU/CPU computation with Tensorflow OPs.

I'd say you can safely ignore this warning on any Tensorflow object. Unfortunately I don't know how you can suppress this warning in PyCharm.

Shu
  • 331
  • 1
  • 4
  • Thanks for the explanation. I've added an edited to the bottom of my post above. – cdahms Feb 17 '18 at 08:14
  • "can suppress this warning in PyCharm", I know it's been two years, but for reference: Run inspections through: Code -> Inspect Code, right click on the result, choose "suppress for statement" or one of the other options. (I had the exact same question just now) – Douwe Jan 23 '20 at 15:25