libtorch c++ save dict of tensor - c++

How can I save a torch::Dict in a file?
The method torch::save gives error (probably a custom serialize is required). Any alternative?
#include "torch/script.h" // One-stop header.
#include "torch/serialize.h" // One-stop header.
#include <iostream>
int main(int argc, const char* argv[])
{
std::string dict_path{"/home/dev/my_dict_tensor_velocity.t"};
std::vector<double> vector_velocity{1.0, 2.2, 3.3, 4.4, 5.5, 6.6};
torch::Tensor tensor_velocity = torch::from_blob(vector_velocity.data(), at::IntArrayRef{2,3}, torch::TensorOptions().dtype(torch::kF64)).clone();
torch::Dict<std::string, torch::Tensor> my_dictionary;
my_dictionary.insert_or_assign("velocity",tensor_velocity );
torch::save(my_dictionary,dict_path);
}

Related

How to store different classes in a vector?

I have two classes with different member type. I wanted to store these two different classes in a vector and then try to print out all values inside of that object. I written the following code:
#include <variant>
#include <string>
#include <vector>
#include <iostream>
class Box
{
public:
std::string m_name;
};
class Lox
{
public:
int m_number;
};
int main(int argc, char* argv[])
{
auto v = std::vector<std::variant<Box, Lox>>{"Milad", 30};
v.push_back(Lox{ 21 });
v.push_back(Box{ "ROme" });
assert(std::get<Lox>(v[0]).m_number == 21);
assert(std::get<Box>(v[1]).m_name == "ROme");
return 0;
}
These are my problems with MSVC compiler:
'variant': is not a member of 'std'
'variant': undeclared identifier
'Box': illegal use of this type as an expression

C++ std::vector assign methods

has anyone an idea how I can realize this with working code?
My wish is to fill a vector with executable methods of a class...
#include <iostream>
#include <vector>
#include <any>
class foo {
public:
void boo() {
std::cout << "WM 2018" << std::endl;
}
};
int main(int argc, char const *argv[])
{
std::vector<std::any> vec;
vec.push_back( (new foo)->boo() );
vec[0]();
return 0;
}
It seems you want std::vector<std::function<void()>>:
std::vector<std::function<void()>> vec{[](){ foo{}.boo();}};
Demo

Log4cplus not compiling as descirbed

#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/initializer.h>
#include <log4cplus/layout.h>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <string>
int main(int argc, char* argv[]) {
log4cplus::SharedAppenderPtr appender(new log4cplus::FileAppender("test.log"));
appender->setName("mainLog");
std::string pattern = "[%-5p][%D{%Y/%m/%d %H:%M:%S:%q}][%-l][%t] %m%n";
std::auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
appender->setLayout(layout);
mainLog = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("mainLog"));
mainLog.addAppender(appender);
LOG4CPLUS_INFO(mainLog, LOG4CPLUS_TEXT("Execution started!"));
return 0;
}
The above code should work according to the every guide I have managed to find on the subject, but it won't compile. The line:
appender->setLayout(layout);
underscores layout and says:
cannot convert argument 1 from 'std::auto_ptr<log4cplus::Layout>' to 'std::unique_ptr<log4cplus::Layout,std::default_delete<_Ty>>
What gives?

error: 'int main(int, char**)' previously defined here in C++

I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.

class extending GtkWindow

i'm trying to learn c++, but i can not find if it's possible to extend a class in this way:
main.cc
#include "mWindow.h"
using namespace std;
int main( int argc, char* argv[] ) {
gtk_init( &argc, &argv );
mWindow win = mWindow();
gtk_main();
return 0;
}
mWindow.cc
#include "mWindow.h"
mWindow::mWindow() {
gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (this, "my window");
gtk_widget_show_all (GTK_WIDGET(this));
}
mWindow.h
#ifndef MWINDOW_H_INCLUDED
#define MWINDOW_H_INCLUDED
#include <gtk/gtk.h>
using namespace std;
class mWindow : public GtkWindow {
public:
mWindow();
};
#endif
I suggest you take a look at gtkmm (http://www.gtkmm.org/) if you want to use GTK+ in conjunction with C++, i.e. there is no need to try to reinvent the wheel and write your own C++ interface for GTK+ (which is a C library).
thanks,
I was trying to use C libraries as if they were C++.
This is how I solved with gtkmm:
main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window);
return 0;
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm-2.4/gtkmm.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
show_all_children();
}
also add the command to complete successfully, at least on Arch Linux:
g++ $(pkg-config --cflags --libs gtkmm-2.4) main.cc examplewindow.cc examplewindow.h -o executable
another small indication, what i shouldl use as dynamic arrays or vectors and for hashmap?