-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create rule S6922 #3624
Draft
github-actions
wants to merge
3
commits into
master
Choose a base branch
from
rule/add-RSPEC-S6922
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Create rule S6922 #3624
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"title": "arguments of \"tf.divide\" should have the same shape or be broadcastable to the same shape", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6922", | ||
"sqKey": "S6922", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "HIGH", | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
This rule raises an issue when the `tf.divide` function arguments have incompatible shapes. | ||
|
||
== Why is this an issue? | ||
|
||
The `tf.divide` function in TensorFlow is used to perform element-wise division between two tensors. It divides the corresponding elements of the first tensor by the corresponding elements of the second tensor, e.g.: | ||
|
||
[source,python] | ||
---- | ||
x = tf.constant([4,6,8]) | ||
y = tf.constant([2,3,4]) | ||
---- | ||
When you use `tf.divide(x, y)`, TensorFlow will perform element-wise division between `x` and `y`. So, the output would be: | ||
[source] | ||
---- | ||
[4 / 2, 6 / 3, 8 / 4] = [2.0 2.0, 2.0] | ||
---- | ||
Here, each element of `x` is divided by the corresponding element of `y`. | ||
|
||
One limitation to be aware of when using `tf.divide` is that both `x` and `y` must be of compatible shapes. In other words, they must have the same shape or be broadcastable to the same shape. Otherwise it will lead to `InvalidArgumentError`. | ||
|
||
== How to fix it | ||
Make sure that the `tf.divide` function arguments have compatible shapes. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
import tensorflow as tf | ||
|
||
x = tf.constant([4, 6, 8]) | ||
y = tf.constant([2, 3]) | ||
z = tf.divide(x, y) # Noncompliant: InvalidArgumentError: Incompatible shapes: [3] vs. [2] | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
import tensorflow as tf | ||
|
||
x = tf.constant([4, 6, 8]) | ||
y = tf.constant([2, 3, 4]) | ||
z = tf.divide(x, y) # OK: [2, 2, 2] | ||
---- | ||
|
||
//=== How does this work? | ||
|
||
//=== Pitfalls | ||
|
||
//=== Going the extra mile | ||
|
||
|
||
== Resources | ||
=== Documentation | ||
* Tensorflow documentation - https://www.tensorflow.org/api_docs/python/tf/math/divide[tf.divide] | ||
* Numpy documentation - https://numpy.org/doc/stable/user/basics.broadcasting.html#broadcasting[Broadcasting] | ||
//=== Articles & blog posts | ||
//=== Conference presentations | ||
//=== Standards | ||
//=== External coding guidelines | ||
//=== Benchmarks |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could add a word on broadcasting or a link to the documentation for broadcasting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a link to numpy documentation explaining broadcastable matrices