Is there any thing like 'TensorList’ in pytorch? - list

I would like to put some tensor in a list, and I know if I would like to put nn.Module class into a list, I must use ModuleList to wrap that list.
So, Is there anything like 'TensorList’ in pytorch, that I must use to wrap the list containing tensors?

What are these tensors? Are these tensors parameters of your nn.Module? If so, you need to use the proper container.
For example, using nn.ParameterList. This way calling your module's .paramters() methods will yield these tensors as well. Otherwise you'll get errors like this one.

Related

How to automatically initialize component parameters?

While doing a game engine that uses .lua files in order to read parameter values, I got stuck when I had to read these values and assign them to the parameters of each component in C++. I tried to investigate the way Unity does it, but I didn't find it (and I'm starting to doubt that Unity has to do it at all).
I want the parameters to be initialized automatically, without the user having to do the process of
myComponentParameter = readFromLuaFile("myParameterName")
for each one of the parameters.
My initial idea is to use the std::variant type, and storing an array of variants in order to read them automatically. My problems with this are:
First of all, I don't know how to know the type that std::variant is storing at the moment (tried with std::variant::type, but it didn't work for the template), in order to cast from the untyped .lua value to the C++ value. For reference, my component initialization looks like this:
bool init(luabridge::LuaRef parameterTable)
{
myIntParameter = readVariable<int>(parameterTable, "myIntParameter");
myStringParameter = readVariable<std::string>(parameterTable, "myStringParameter");
return true;
}
(readVariable function is already written in this question, in case you're curious)
The second problem is that the user would have to write std::get(myIntParameter); whenever they want to access to the value stored by the variant, and that sounds like something worse than making the user read the parameter value.
The third problem is that I can't create an array of std::variant<any type>, which is what I would like to do in order to automatically initialize the parameters.
Is there any good solution for this kind of situation where I want the init function to not be necessary, and the user doesn't need to manually set up the parameter values?
Thanks in advance.
Let's expand my comment. In a nutshell, you need to get from
"I have some things entered by the user in some file"
to:
"the client code can read the value without std::get"
…which roughly translates to:
"input validation was done, and values are ready for direct use."
…which implies you do not store your variables in variants.
In the end it is a design question. One module somewhere must have the knowledge of which variable names exist, and the type of each, and the valid values.
The input of that module will be unverified values.
The output of the module will probably be some regular c++ struct.
And the body of that module will likely have a bunch of those:
config.foo = readVariable<int>("foo");
config.bar = readVariable<std::string>("bar");
// you also want to validate values there - all ints may not be valid values for foo,
// maybe bar must follow some specific rules, etc
assuming somewhere else it was defined as:
struct Configuration {
int fooVariable;
std::string bar;
};
Where that module lives depends on your application. If all expected types are known, there is no reason to ever use a variant, just parse right away.
You would read to variants if some things do not make sense until later. For instance if you want to read configuration values that will be used by plugins, so you cannot make sense of them yet.
(actually even then simply re-parsing the file later, or just saving values as text for later parsing would work)

Google Mock: obtain AssertionResult from EXPECT_THAT

I'm trying to write a google-test utility function which performs some setup steps base on it's input arguments and finally compares the contents of two containers irrespective of order. I would then like to return a ::testing::AssertionResult from this function based on whether the container contents are equal.
For the comparison itself I would like to use functionalities provided by google-mock in order to avoid errors in my own test-code and reduce developement overhead. Something like the following would work:
EXPECT_THAT(actual_container,
testing::UnorderedElementsAreArray(expected_container))
<< "Container content equal.";
The trouble with this is that I'm not sure if it is even possible to
obtain an AssertionResult which can be returned from a function in a similar manner.
Does anyone have a solution to this or a suggestion for an alternative approach?

Use variable of a constructor

I am not a C++ expert hence I will try to make this as clear as possible, do not hesitate to ask me about any detail you would need.
I am using a program to handle ply file.
I am writing my code in MyFunction.cpp, and calling a function from AnotherFunction.cpp on my file like this : MeshFunction(myplyfile) (MeshFunction definition is in the file AnotherFunction.cpp).
Let's say that MeshFunction is using a vector of elements at a certain point and that I would like to get it in order to use it back in MyFunction.cpp; how is that possible ?
Thank you very much!
Yours faithfully,
L
Update the interface of MeshFunction to accept std::vector<Element>& elements as another arugment.
Make sure to fill elements with the necessary data in the implementation of MeshFunction.
Supply the argument when calling the function.

Iterate and update BindingList items using a one-liner

I have a BindingList I want to update certain items,but in order to use the Foreach available only for the List<> I have to initialize a new List with the BindingList items. like this:
new List<ScanData>(ScanDataList)
.FindAll(i => i.Badge == badge)
.ForEach(x =>x.EmpName = empname);
And that's the simplest way I found to do it, but I don't want to start with the New keyword, is there any other simpler way to Iterate over the BindingList items and update them using a one-liner like the above? (I put it in three lines for readability).
Id like just to remove the New keyword but that just doesn't work,
if a new function helps that is also acceptable, if its generic for any BindingList would be perfect.
Note: Im using compact framework 2.0
I don't want to initialize a variable Im not gonna use.
Thanks.
This question is a bit silly. There's no reason you have to do it in one line of code and avoid declaring a variable. If you use the new operator you are initializing an instance of an object, regardless of whether you are declaring a variable for it or not.
That being said, I do not know what your ScanDataList is... There's a linq expression equivalent to FindAll called Where which may be more efficient than FindAll (because it doesn't have to create a new list, it just lazily iterates). If your ScanDataList is already IEnumerable then you can probably do something like this...
ScanDataList.Where(i => i.Badge == badge).ToList().ForEach(x=>x.EmpName = empname);
Even if your ScanDataList is not enumerable, you could implement an extension method of your own to help you accomplish this, but it seems like a lot of work for something that can easily be achieved without arbitrary unnecessary constraints (no new, no variables, etc).
So to clarify, I would probably use .Where LINQ expression because it is probably a bit more efficient because it doesn't need to create a new List. However, using that same logic, I'd probably avoid ToList() and separate your code into two lines with something like.
foreach(Employee emp in ScanDataList.Where(i => i.badge == badge))
emp.EmpName = empname;
This way, no additional list is created.

List design (Object oriented) suggestion needed

I'm trying to implement a generic class for lists for an embedded device using C++. Such a class will provide methods to update the list, sort the list, filter the list based on some user specified criteria, group the list based on some user specified criteria etc. But there are quite a few varieties of lists I want this generic class to support and each of these varieties can have different display aspects. Example: One variety of list can have strings and floating point numbers in each of its elements. Other variety could have a bitmap, string and special character in each of it's elements. etc.
I wrote down a class with the methods of interest (sort, group, etc). This class has an object of another class (say DisplayAspect) as its member. But the number of member variables and the type of each member variable of class DisplayAspect is unknown. What would be a better way to implement this?
Why not use the std::list, C++ provides that and it provides all the functionality you mentioned(It is templated class, So it supports all data types you can think of).
Also, there is no point reinventing the wheel as the code you write will almost will never be as efficient as std::list.
In case you still want to reinvent this wheel, You should write a template list class.
First, you should probably use std::list as your list, as others have stated. It seems to me that you are having problems more with what to put in the list, however, so I'm focusing on that part of the question.
Since you want to also store multiple bits of information in each element of the list, you will need to create multiple classes, one to store each combination. You don't describe why you are storing mutiple bits of information, but you'd want to use a logical name for each class. So if, for example, you were storing a name and a price (string and a double), you could give the class some name like Product.
You mention creating a class called DisplayAspect.
If this is because you want to have one piece of code print all of these lists, then you should use inheritance and polymorphism to accomplish this goal. One way to accomplish that is to make your DisplayAspect class an abstract class with the needed functions (printItem() for example) pure virtual and have each of the classes you created for the combinations of data be subclasses of this DisplayAspect class.
If, on the other hand, you created the DisplayAspect class so that you could reuse your list code, you should look into template classes. std::list is an example of a template class and it will hold any type you'd like to put into it and in that case, you could drop your DisplayAspect class.
Others (e.g., #Als) have already given the obvious, direct, answer to the question you asked. If you really want a linked list, they're undoubtedly correct: std::list is the obvious first choice.
I, however, am going to suggest that you probably don't want a linked list at all. A linked list is only rarely a useful data structure. Given what you've said you want (sorting, grouping), and especially your target (embedded system, so you probably don't have a lot of memory to waste) a linked list probably isn't a very good choice for what you're trying to do. At least right off, it sounds like something closer to an array probably makes a lot more sense.
If you end up (mistakenly) deciding that a linked list really is the right choice, there's a fair chance you only need a singly linked list though. For that, you might want to look at Boost Slist. While it's a little extra work to use (it's intrusive), this will generally have lower overhead, so it's at least not quite a poor of a choice as many generic linked lists.