x = torch.randn(3, requires_grad=False)
print(x.requires_grad)
with torch.no_grad():
x.requires_grad_()
print(x.requires_grad)
y=x ** 2
print(x.requires_grad)
torch.autograd.grad(y,x, create_graph=True)[0]
output:
False
True
True
when I set up x.requires_grad_(), the print result is True for x, which means x has gradient. However it stills give me an error "element 0 of tensors does not require grad and does not have a grad_fn" for torch.autograd.grad(y,x, create_graph=True). May I ask what is the reason?
Related
I am trying to have optional parameter (string array). So, My parameter for a function looks like f(names:dynamic=dynamic([])) to set default if i am not passing this parameter. However below check doen't work. Any pointers on how to do null check and if present apply that filter else retrieve all values.
| where isempty(_x) or x in(_x)
you could try using set_has_element().
for example:
let T = range x from 1 to 5 step 1
;
let F = (_x: dynamic = dynamic(null)) {
T
| where isnull(_x) or set_has_element(_x, x)
| order by x desc
}
;
F(dynamic([1,2,4]))
x
4
2
1
Though, please note the following, regarding stored functions: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions#features-that-are-currently-unsupported-by-user-defined-functions
1.
x in (_x) is a valid syntax
let _x = dynamic([1,2,4]);
print is_2_in_x = 2 in (_x)
,is_3_in_x = 3 in (_x)
is_2_in_x
is_3_in_x
true
false
Fiddle
2.
Figuring out if array is null/empty is complicated and the use of isnull() and/or isempty() is not sufficient for the task.
Please note the 3 last options in the demo below for potential solutions.
let F = (_x_as_str:string = "", _x: dynamic = dynamic(null))
{
print _x_as_str = _x_as_str
,isnull = isnull(_x)
,isempty = isempty(_x)
,array_length = array_length(_x)
,is_1st_null = isnull(_x[0])
,is_1st_empty = isempty(_x[0])
,coalesce_array_length = coalesce(array_length(_x), 0)
}
;
union F("dynamic([1,2,4])",dynamic([1,2,4]))
,F()
,F("dynamic(null)", dynamic(null))
,F("dynamic([])" , dynamic([]))
_x_as_str
isnull
isempty
array_length
is_1st_null
is_1st_empty
coalesce_array_length
dynamic([1,2,4])
false
false
3
false
false
3
true
true
true
true
0
dynamic([])
false
false
0
true
true
0
dynamic(null)
true
true
true
true
0
Fiddle
I want write R code for Pythagoras theorem.
The Pythagorean Theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.
(sideA)^2+(SideB)^2=hypotenuse^2
Now I wrote the R code as below:
pythag<-function(sidea,sideb){
if (sidea>=0&sideb>=0)
hypoteneuse=sqrt(sidea^2+sideb^2)
else if (sidea<0|sideb<0)
hypoteneuse<-"Values Need to be Positive"
else if (!is.vector(x))
hypoteneuse<-"I need numeric values to make this work"
print(hypoteneuse)
}
pythag(4,5)
pythag("A","B")
pythag(-4,-5)
In case of pythag(4,5) it is ok, also pythag(-4,-5) is giving comment "Values Need to be Positive".
But in case of pythag("A","B") I want comment "I need numeric values to make this work", but unfortunately my code does't work for this.
You can try like this:
get_hypotenuse_length <- function(height, base)
{
sides <- c(height, base)
if(any(sides < 0))
{
message("sides must be positive")
} else if(!is.numeric(x = sides))
{
message("sides can not be non-numeric")
} else
{
sqrt(x = sum(sides ^ 2))
}
}
Here's an annotated version. It is creating the function which takes the values a and b and calculates c. It is first testing if the values are numeric, if they are not numeric it will print your error message, otherwise it will ignore what is within those curly brackets and move on to the next test. The second test is checking that both are greater than zero (seeing as a triangle can't have a side of length zero or negative length). If it satifies the condition that both are >0 then it will calculate c, if not it will give the error stating that there are negative values.
# Feed it the values a and b (length of the two sides)
pythag <- function(a,b){
# Test that both are numeric - return error if either is not numeric
if(is.numeric(a) == FALSE | is.numeric(b) == FALSE){
return('I need numeric values to make this work')}
# Test that both are positive - return length of hypoteneuese if true...
if(a > 0 & b > 0){
return(sqrt((a^2)+(b^2)))
}else{
# ... give an error either is not positive
return('Values Need to be Positive')
}
}
Here's a more streamlined version:
pythag <- function(a,b){
if(is.numeric(a) == FALSE | is.numeric(b) == FALSE){return('I need numeric values to make this work')}
if(a > 0 & b > 0){return(sqrt((a^2)+(b^2)))}
else{return('Values Need to be Positive')}
}
And this is what it returns with your examples:
> pythag(4,5)
[1] 6.403124
> pythag("A","B")
[1] "I need numeric values to make this work"
> pythag(-4,-5)
[1] "Values Need to be Positive"
if x = c("sideA", "sideB"), then it will still be a vector so your test is.vector(x) will return true:
> is.vector(x)
[1] TRUE
But you want to test if it's numbers, so if it's numeric:
> is.numeric(x)
[1] FALSE
In julia language (ver 1.1.0), I am experimenting what would happen when I mutate a dictionary key.
Before mutation, both the variable x and [1,2,3] is recognized.
x = [1,2,3]; d = Dict(x=>"x")
haskey(d, x)
# true
haskey(d, [1,2,3])
# true
Once I mutate x, neither the variable x nor [1,2,3,4] is recognized.
push!(x, 4)
haskey(d, x)
# false
haskey(d, [1,2,3,4])
# false
haskey(d, [1,2,3])
# false
Value-wise, the key is "equal" to x, so I guess this has something to do with the hash function, but could not understand the source code.
collect(keys(d))[1] == x == [1,2,3,4]
# true
Can someone explain what makes this behavior, or suggest resources that I should look at?
The key function to look into is ht_keyindex.
There you can see that in order for the key to be found it must both:
Match hash value (via hashindex).
Match identity or value.
There is a non-negligible probability that after mutating x it will have the same hashindex value and the key would be found. For example here you could set index 4 of x to 5 and all seemingly would work:
julia> x[4] = 5
5
julia> x
4-element Array{Int64,1}:
1
2
3
5
julia> haskey(d, x)
true
Therefore - as in any programming language supporting dictionaries in a similar way - mutating keys of the dictionary should not be done. The above discussion should be in practice only a theoretical one.
In my bisection algorithm, it says that there's a "missing value where true/false is needed", and it points to this line:
if (sign(f(c)) == sign(f(a)) ) {
a <- c
}
Why? There's nothing wrong with that line, and if I replicate it manually, it works just fine. Yet when I run the function, it produces
Error in if (sign(f(c)) == sign(f(a))) { :
missing value where TRUE/FALSE needed
EDIT: Full code is
Bisection <- function(f, a,b, tol = 0.005, maxiter = 1000) {
i <- 1
while (i < maxiter) {
c <- (a+b)/2
if (f(c) == 0 | (b-a)/2 < tol) { return(c)}
i <- i + 1
if (sign(f(c)) == sign(f(a)) ) {
a <- c
}
else {b <- c}}
return(NA)
}
Always, 0 and 100 are used as a and b.
I'm calling the function on different functions f, that are the same except for a different parameter, and it is only for one very particular parameter that the bisection function fails. For all other parameters, the bisection function works fine.
Generally, the missing value where TRUE/FALSE needed means R is encountering an NA value - in this case, I would expect an NA value in either a, c, or whatever f() returns for these values.
It could be that when you run these lines manually, the values stored in your global environment for a and c are just fine (non-missing), but whatever values are passed into your function (or computed within it) contains missing values. I'd recommend checking that.
I'm trying to write a basic function to take an integer and evaluate to a bool that will check whether the integer is a prime or not.
I've used an auxiliary function to keep track of the current divisor I'm testing, like so:
fun is_divisible(n : int, currentDivisor : int) =
if currentDivisor <= n - 1 then
n mod currentDivisor = 0 orelse is_divisible(n, currentDivisor + 1)
else
true;
fun is_prime(n : int) : bool =
if n = 2 then
true
else
not(is_divisible(n, 2));
It looks right to me but I test it on 9 and get false and then on 11 and get false as well.
Sorry for all the questions today and thanks!
The problem is that if your is_divisible reaches the last case it should return false because it means that all the iterated divisors have resulted in a remainder larger than zero except for the last one which is the number it self. So you should rename is_divisible and return false instead of true