If I have a federated value, say {int32}#CLIENTS that I'd like to cast to {float32}#CLIENTS is there an easy way to do this? Thanks!
Tensor manipulating generally needs to occur inside a function decorated with tff.tf_computation. Since the types mentioned have placements (#CLIENTS) this likely is inside a tff.federated_computation decorated function, so the casting method would need to be called with tff.federated_map.
Something like this:
#tff.tf_computation
def cast_to_float(x):
return tf.cast(x, tf.float32)
#tff.federated_computation(tff.FederatedType(int32, tff.CLIENTS))
def my_func(a):
a_float = tff.federated_map(cast_to_float, a)
return a_float
print(my_func.type_signature)
>>> ({int32}#CLIENTS -> {float32}#CLIENTS)
It can be done using tf.cast(), however using #tff.federated_computation().
Related
I have a general questions about how to treat params on a method. Let's say, we have a method that receives a number and multiplies it by 2.
def multiplier(num)
num*2
end
What happens when num is nil? Who is responsible to handle the error? The method itself or the one who calls the method? What is considered the best oop practice?
This is not related to OOP in any way, because it applies to other paradigms as well. That said, there are different schools of thought about the problem. Which one is the "best" depends on who you're talking to.
One is "defensive programming everywhere". This means each method is responsible for rigorous checks of its input.
Another is "caller is responsible for providing correct data".
Another is what I call a "security perimeter": rigorous checks only when we deal with external input, but once data is "inside of the system", it can be trusted.
And anything in between.
I agree with the other answers (#sergio-tulentsev, #Зелёный). And there is another thing to consider.
In a lot of cases it is a good practice not to expect an object of particular type, but an object which acts like a particular type. I mean, in your case, the method could expect not only a number to be passed as a parameter, but any object that could be treated like a number. That would not only make your method more powerful, but also solve the nil problem. Of course it depends on your needs.
The method version I am talking about might look like this:
def multiplier(num)
# trying to convert whatever is passed to a number
num.to_f * 2
end
In the case of nil it will return 0.0
I'd like to know how can I limit the set of values that I can pass to function as an argument (or to class as a property). Or, in other words, the logic that I want to implement, actually, is to make function or a class accept only particular values. I've come up with an idea to use enum for it. But the caveat here is that I can't use pure integers with the 'case' like so:
enum Measure {
case 1, 2, 3
}
Is there any way to implement what I want?
enum Measure:Int{
case ONE = 1
case TWO = 2
case THREE = 3
}
//accept it as argument
func myMethod(measure:Measure){
switch measure {
case .ONE:...
case .TWO:...
case .THREE
}
}
//call the method with some raw data
myMethod(Measure(rawValue:1)!)
//or call the method with
myMethod(Measure.ONE)
But why are you trying to implement it. Swift by default does not allow to pass more or fewer arguments than defined on the definition of that function or class.
So if you have a simple function which takes just one argument, then no one can pass less or more than one argument while calling your function. And if he would try to do so then the swift compiler won't allow him/her to do so.
So logically the conclusion is you don't need to develop anything like that.
If your scenario is different then what I m thinking please let me know by adding comment or writing another question in a simpler or understandable way.
I have some ruby code that operates on an ActiveRecord object using a couple of methods, and the end game is to return the object itself. I want to use two methods that have return values other than the object itself (boolean values). I like using the shorthand Symbol#to_proc syntax, i.e.
Object.tap(&:do_work)
Is it possible to pass multiple procs? i.e.
Object.tap(&:do_work, &:do_more_work)
The above syntax does not work. Is this possible or do I have to do something like:
Object.tap(&:do_work).tap(&:do_more_work)
If you are looking for a good architectural solution then you need to implement FIFO stack for that.
Or you can do it like you mentioned above:
Object.tap(&:do_work).tap(&:do_more_work)
In Grails you can declare a controller action like this:
def create(Integer foo, Integer bar) {
}
And if your HTTP request has parameters named foo and bar with values that can be converted to an Integer, the parameters will be assigned these values. I'm wondering how Grails can do this, because my understanding is that at the JVM bytecode level, a method's formal parameter names are not available. Is this witchcraft or am I misunderstanding something?
Basically what happens is that there's an AST transform that adds a new method with no args and the same name. This new method has logic in it to do the data binding based on the declared types of your "real" method, and then call your method. That's why the types are required (otherwise there's no way to do a conversion) and why you cannot have method overloads.
The inability to have overloaded methods is easy to work around though. Say you wanted an action
def foo(String bar)
and another
def foo(String bar, Integer wahoo)
In this scenario just keep the 2nd method and check to see if wahoo is null.
It's also important to use object parameter types and not primitives. If you use int/long/boolean/etc. and there is no provided parameter, you would get a NPE (since zero is not an acceptable conversion from null for numbers, and either is false for booleans).
You can get a decent sense for what's going on if you decompile the class using JD-GUI or another decompiler.
The fact that Grails controllers are Groovy classes helps quite a lot. Looking through the source code for controllers you can see where it makes heavy use of AST transformations, in particular the MethodNode. So, before it becomes bytecode the "witchcraft" is done. :)
I have some code that wants to do grab some extra return values from a function and pass them forward latter on:
local ok, ... = coroutine.resume(co)
do_stuff(ok)
return ...
However, this won't run since the ... on the variable assignment is a syntax error.
I could work around this limitation by using the old "functions arguments and variables are equivalent" trick and an immediately-invoked function
return (function(ok, ...)
do_stuff(ok)
return ...
)(coroutine.resume(co))
but I imagine doing so wouldn't be very idiomatic or efficient. Are there more reasonable ways to solve this problem of handling the remaining values returned from the resume call?
EDIT: By the way, this needs to work with nil values in the extra arguments
EDIT2: Looks like using the immediately invoked function was the best way all along.
IMHO, the best way is passing vararg as parameter to helper function as you have done in your question.
The alternative way is "pack-unpack":
-- Lua 5.2 only
local t = table.pack(coroutine.resume(co))
do_stuff(t[1])
return table.unpack(t, 2, t.n)
The idiomatic way to do this with an unknown number of return values is to wrap the function call in a table constructor:
local t = { coroutine.resume(co) }
do_stuff(table.remove(t, 1))
return unpack(t) -- table.unpack(t) in lua 5.2
While this also involves creating a temporary object, it should be a lot quicker than using a closure, and it's certainly a lot neater.