How to print state of model in Federated learning - tensorflow-federated

I would like to print (before training) the state of model :
with print(state['model']),
I found this error :
TypeError: 'ServerState' object is not subscriptable

tff.leraning.framework.ServerState is a Python attrs class, whos fields are accessed via the Instance.method (__getattr_) syntax, rather than the Instance['key'] lookup (__getitem__).
Try replacing print(state['model']) with print(state.model).

Related

How to use the instance name as a string in Modelica code?

I have a Modelica simulation model composed by some models connected to each other.
I would like to save some data of some of the model instances in my simulation model at a given time using the built-in function
Modelica.Utilities.Streams.writeRealMatrix();
To be sure which instance writes which file, I would like to include the instance name in the writeRealMatrix() output file name, e.g., in case I have an instance called myModel, using the name:
myModelOut.mat.
To do this, I need a way to get the instance name and put it into a string.
I know that Modelica allows using instance names in model icons, through a Text record, using the keyword "%name", but I don't know how to do the same in a regular string (I mean outside any record or icon annotation).
Does anyone know if there is a way to do this?
Thank you in advance.
In your case I think the function getInstanceName() should be a good approach. Using it will need you to edit the model, but given you are writing information from with the class using writeRealMatrix() this shouldn't be an issue.
I have created a small example package with a constant block, that stores its name into final parameter of type String. The example then writes the string to the console at the termination of the simulation:
package GetName
block ConstantNamed "Generate constant signal of type Real"
extends Modelica.Blocks.Sources.Constant;
final parameter String name = getInstanceName();
end ConstantNamed;
model Example
extends Modelica.Icons.Example;
ConstantNamed myConst(k=23) annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
equation
when terminal() then
Modelica.Utilities.Streams.print("### Here is the models full path: '" + myConst.name + "'");
end when;
end Example;
annotation (uses(Modelica(version="4.0.0")));
end GetName;
This should result in a simulation log containing the path of the instance of ConstantNamed, which is Example.myConst:
Note: The print function is added to Example in the above code. It could be added to the ConstantNamed as well. For the case from the question, the print shouldn't be necessary anyways...
Besides that, in case you are using Dymola, there is the ModelManagement library, which contains some functions like ModelManagement.Structure.AST.Classes.ComponentsInClass. But these are more intended to be applied from "outside" to a given model.

the initialize computation to construct the server state

In the federated learning context, and like this tutorial shows, initial weights of global model (at server level) are initialized randomly with : state = iterative_process.initialize(). I want to have the hand to put these initial weights by downloading them from another model (load_model()). So please how can I proceed, I'm newer in TFF.
Thanks
You can either manually create a state object with the same structure and any values you choose, or use tff.structure.update_struct.
If you already have a my_model, and instance of either tff.learning.Model or a tf.keras.Model with weights you want, you can write something like
state = tff.structure.update_struct(state, model=tff.learning.ModelWeights.from_model(my_model))

How to access object field in qaf step from stored variable

In my previous question I was looking for a way to access and store return value of the function in qaf step. I was provided with the following:
When create new user using "{'name':'user1','password':'user123'}"
And store into 'newUser'
Then system should have user '${newUser}'
Now, I'd like to know how to get value from object/collection stored.
If it is a simple object named newUser which has field Id. How would I pass Id on next step?
And, if return is List, how to get by index from stored list?
Resolved issue on my own. If anyone faces same unknowns, here is how I solved it.
For requirements to work around response data, parsing same stored objects in properties by specific fields or collecting data from other structures such as Maps or Lists, create common functions with #QAFTestStep annotation to get data for class member name, map by key or list by index and so on... Add those in common steps and then write stepname text in gherkin format with parameters specified. Let me know if someone needs help, always ready to help out...

Fitnesse-Fixture for method that return data types like data set

I am trying to test the .NET class using fitnesse. Is it possible with fitnesse to test a method that returns (not a single value either string or integer) a data set.
Yes. For example, you can return a json object and view the contents of the json object as a return value of a function.
|script |className |
| $variable= |functionName|
For a json object returned, you should be able to view its contents assigned to $variable.
The steps associated with other data structures are similar
A dataset return value is automatically handled with an Array fixture:
|SomeClass|
|SomeMethodThatReturnsDataSet|
|column1|column2|column3|
|a|b|c|
|d|e|f|
See http://fitsharp.github.io/Fit/FixtureWrapper.html for more examples.
You can return IEnumerable class and check the table.
See whole tutorial there: https://github.com/imanushin/NetRunner/wiki/Net-Runner-tutorial

Error codes + error descriptions

I have the following classes in my project:
Car
Truck
Bicycle
Plane
User initializes each class with input. For example, for Car, they initialize with model, make, etc.
I have validation functions that use key value validation to validate all of the properties in each model.
Then, for each validate function, I set the NSError input parameter for that function.
The problem is I have over 20 error codes matching to over 20 error descriptions. For example, if the user did not put a valid Car Model, they get an error code 1000 with error description, "Please input valid car model." Right now, I use a long switch statement inside a function in the parent class of all of these models to match each error code to an error description and create the appropriate nserror object for each sub class validation function. Is there a better and more manageable way to handle the mapping of over 20 error codes to error descriptions?
Also, another question, how specific should an error code be? For example, if car model is nil or car model is not a valid model (not nil, just not valid), should there be a difference in error code between the two.
I don't fully understand your situation, but you should be adding the error description to the at the same time you add the error code, i.e. when you create the NSError object.
[NSError errorWithDomain:#"aDomain" code:1 userInfo:#{NSLocalizedDescriptionKey: #"Please input valid car model."}];
Then when you need to present the error to the user, just grab the description from the error:
NSString *errorText = error.userInfo[NSLocalizedDescriptionKey];
You can use a dictionary. Convert the error codes into NSNumber instances and set the associated values to the error descriptions. Then, when you create the error just get the description from the dictionary.

Resources