How do you transpose a tensor in LibTorch? - c++
I can't seem to figure out how to transpose a tensor in LibTorch, the (C++ version of PyTorch).
torch::Tensor one_T = torch::rand({6, 6});
int main() {
std::cout << one_T.transpose << "\n";
}
My error...
/home/iii/tor/m_gym/multiv_normal.cpp:53:24: note: mismatched types ‘const std::set<Types ...>’ and ‘at::Tensor (at::Tensor::*)(at::Dimname, at::Dimname) const’
53 | std::cout << one_T.transpose << "\n";
| ^~~~~~~~~
/home/iii/tor/m_gym/multiv_normal.cpp:53:24: note: mismatched types ‘const std::set<Types ...>’ and ‘at::Tensor (at::Tensor::*)(int64_t, int64_t) const’ {aka ‘at::Tensor (at::Tensor::*)(long int, long int) const’}
In file included from /home/iii/tor/m_gym/libtorch/include/c10/util/Logging.h:28,
from /home/iii/tor/m_gym/libtorch/include/c10/core/TensorImpl.h:17,
from /home/iii/tor/m_gym/libtorch/include/ATen/core/TensorBody.h:20,
from /home/iii/tor/m_gym/libtorch/include/ATen/core/Tensor.h:3,
from /home/iii/tor/m_gym/libtorch/include/ATen/Tensor.h:3,
from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/function_hook.h:3,
from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/cpp_hook.h:2,
from /home/iii/tor/m_gym/libtorch/include/torch/csrc/autograd/variable.h:6,
one_T.transpose(0, 1)
The 0, 1 is the type of default transpose used in PyTorch and Numpy without being specified.
Related
How to get the date from a calendar widget in gtkmm3?
So I don't understand how to get this thing working. I already read the Class reference but the only two references to get_date() return void. How can I get the date from a calendar widget?. I now that I'm calling the method wrong and this cause the errors. This is my Notebook.hpp #pragma once #include <gtkmm.h> class NotebookMain : public Gtk::Window{ public: NotebookMain(); virtual ~NotebookMain(); protected: // Signal handlers void on_notebook_switch_page(Gtk::Widget *page, guint page_num); void on_button_add_appointment(); // Member widgets Gtk::Notebook notebookMain; Gtk::Label label1, labelTest; Gtk::Box boxDatebook, boxCalendar Gtk::Calendar calendar; Gtk::Button buttonAddAppointment; }; This is my Notebook.cpp #include <iostream> #include "../include/NotebookMain.hpp" NotebookMain::NotebookMain() : label1("Hello from inside Tab 2!"), labelTest("Hello from inside Appointments"){ this->set_title("Test"); this->set_default_size(640, 480); // Create Calendar Box boxCalendar.set_orientation(Gtk::ORIENTATION_VERTICAL); boxCalendar.pack_start(calendar); buttonAddAppointment.set_label("Add Appointment"); boxCalendar.pack_start(buttonAddAppointment); // Create Appointments Box boxAppointments.set_orientation(Gtk::ORIENTATION_VERTICAL); boxAppointments.pack_start(labelTest); // Create Datebok Box boxDatebook.set_orientation(Gtk::ORIENTATION_HORIZONTAL); boxDatebook.pack_start(boxCalendar); boxDatebook.pack_end(boxAppointments); // Set Notebook notebookMain.set_border_width(0); // Add Notebook tabs notebookMain.append_page(boxDatebook, "Datebook"); notebookMain.append_page(label1, "Tab 2"); // Switch tabs notebookMain.signal_switch_page().connect(sigc::mem_fun(*this, &NotebookMain::on_notebook_switch_page)); // Get Date buttonAddAppointment.signal_clicked().connect(sigc::mem_fun(*this, &NotebookMain::on_button_add_appointment)); // Add Main Window (Notebook) this->add(notebookMain); this->show_all_children(); } NotebookMain::~NotebookMain(){} void NotebookMain::on_notebook_switch_page(Gtk::Widget * /* page */, guint page_num){ std::cout << "Switched to tab with index : " << page_num << std::endl; } void NotebookMain::on_button_add_appointment(){ std::cout << "Button clicked\n"; std::cout << calendar.get_date() << "\n"; } And the errors src/NotebookMain.cpp: In member function ‘void NotebookMain::on_button_add_appointment()’: src/NotebookMain.cpp:51:23: error: no matching function for call to ‘Gtk::Calendar::get_date()’ 51 | calendar.get_date(); | ^ In file included from /usr/include/gtkmm-3.0/gtkmm.h:196, from src/../include/NotebookMain.hpp:3, from src/NotebookMain.cpp:3: /usr/include/gtkmm-3.0/gtkmm/calendar.h:299:8: note: candidate: ‘void Gtk::Calendar::get_date(guint&, guint&, guint&) const’ 299 | void get_date(guint& year, guint& month, guint& day) const; | ^~~~~~~~ /usr/include/gtkmm-3.0/gtkmm/calendar.h:299:8: note: candidate expects 3 arguments, 0 provided /usr/include/gtkmm-3.0/gtkmm/calendar.h:305:8: note: candidate: ‘void Gtk::Calendar::get_date(Glib::Date&) const’ 305 | void get_date(Glib::Date& date) const; | ^~~~~~~~ /usr/include/gtkmm-3.0/gtkmm/calendar.h:305:8: note: candidate expects 1 argument, 0 provided make: *** [makefile:9: NotebookMain.o] Error 1
This solved the problem. I need to pass the arguments, and then those variables will stored the value from the calendar. unsigned int year, month, day; calendar.get_date(year, month, day); Pro tip: Read the documentation right!.
Getting Blob data in caffe
I am trying to plot layer out of caffe as follow. int ncols = (int)(sqrt (blob.channels())); int nrows; if(blob.channels()%ncols!=0){ nrows = ncols+1; } int Rows = nrows*blob.height(); int Cols = ncols*blob.width(); cv::Mat image = cv::Mat::zeros(Rows+nrows, Cols+ncols, CV_32FC1); ///////Plotting output of individual layer if(blob.height()>1 && blob.width()>1){ cv::Size ss(blob.width(), blob.height()); Dtype* data = blob.mutable_cpu_data(); int r=0; int c=0; for(int k=0; k < blob.channels(); k++) { cv::Mat channel(ss, CV_32FC1, data); channel.copyTo(image(cv::Rect(c*blob.width()+1, r*blob.height()+1, blob.width(), blob.height()))); c++; if(c>0 &&c%ncols==0){ r++; c=0; } channel.release(); data += ss.area(); } } For that I have error as CXX src/caffe/net.cpp src/caffe/net.cpp: In instantiation of ‘void caffe::Net<Dtype>::ForwardDebugInfo(int) [with Dtype = float]’: src/caffe/net.cpp:1040:1: required from here src/caffe/net.cpp:632:49: error: passing ‘const caffe::Blob<float>’ as ‘this’ argument discards qualifiers [-fpermissive] Dtype* data = blob.mutable_cpu_data(); ^ In file included from ./include/caffe/layer.hpp:8:0, from src/caffe/net.cpp:11: ./include/caffe/blob.hpp:225:10: note: in call to ‘Dtype* caffe::Blob<Dtype>::mutable_cpu_data() [with Dtype = float]’ Dtype* mutable_cpu_data(); ^ src/caffe/net.cpp: In instantiation of ‘void caffe::Net<Dtype>::ForwardDebugInfo(int) [with Dtype = double]’: src/caffe/net.cpp:1040:1: required from here src/caffe/net.cpp:632:49: error: passing ‘const caffe::Blob<double>’ as ‘this’ argument discards qualifiers [-fpermissive] Dtype* data = blob.mutable_cpu_data(); ^ In file included from ./include/caffe/layer.hpp:8:0, from src/caffe/net.cpp:11: ./include/caffe/blob.hpp:225:10: note: in call to ‘Dtype* caffe::Blob<Dtype>::mutable_cpu_data() [with Dtype = double]’ Dtype* mutable_cpu_data(); ^ Makefile:575: recipe for target '.build_debug/src/caffe/net.o' failed make: *** [.build_debug/src/caffe/net.o] Error 1 What does that error means? Earlier version of caffe, it was fine. I did it before. Now what could be the error?
That error translates as "You pass a const object as this argument to a non-const method mutable_cpu_data" const Dtype* cpu_data() const; Dtype* mutable_cpu_data(); "Passing an object as this argument" suggests use of operators . or -> to access object's method and use of operator(). If you do that, you potentially can change const object, so it's an error, unless permissive mode engaged.
Libtorrent set_settings error
I'm trying to set the session settings but I'm getting an error I don't quite understand, I am able to get the session settings OK and according to the Libtorrent docs I simply pass the session_settings structure to set_settings once I have changed any values. using namespace libtorrent; session* Session; session_status* Session_Status; session_settings* Session_Settings; bool Start_Client_Sess() { Session = new session; Session_Status = new session_status; Session_Settings = new session_settings; Session->settings( ); std::cout << "upload_rate_limit " << Session_Settings->upload_rate_limit << " \n"; std::cout << "dht_announce_interval " << Session_Settings->dht_announce_interval << " \n"; Session_Settings->upload_rate_limit = 200; Session_Settings->dht_announce_interval = 1800; Session->set_settings( Session_Settings ); // error } Error: 1>Source\Client_F.cpp(66): error C2664: 'void libtorrent::session::set_settings(const libtorrent::session_settings &)' : cannot convert argument 1 from 'libtorrent::session_settings *' to 'const libtorrent::session_settings &' 1> Reason: cannot convert from 'libtorrent::session_settings *' to 'const libtorrent::session_settings' 1> No constructor could take the source type, or constructor overload resolution was ambiguous
As the error message says, you don't have to pass a pointer to this function. Session->set_settings( *Session_Settings );
Error while XOR ing C++
I have problem, when i compile this code #include <iostream> using namespace std; int main(){ const unsigned long box[256]= { 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96, 0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0x636920D8, 0x71574E69, 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5, 0x9C30D539, 0x2AF26013, 0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF, 0x8E79DCB0, 0x603A180E, 0x6C9E0E8B, 0xB01E8A3E, 0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60, 0xE65525F3, 0xAA55AB94, 0x57489862, 0x63E81440, 0x55CA396A, 0x2AAB10B6, 0xB4CC5C34, 0x1141E8CE, 0xA15486AF, 0x7C72E993, 0xB3EE1411, 0x636FBC2A, 0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E, 0xAFD6BA33, 0x6C24CF5C, 0x7A325381, 0x28958677, 0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193, 0x61D809CC, 0xFB21A991, 0x487CAC60, 0x5DEC8032, 0xEF845D5D, 0xE98575B1, 0xDC262302, 0xEB651B88, 0x23893E81, 0xD396ACC5, 0x0F6D6FF3, 0x83F44239, 0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E, 0x21C66842, 0xF6E96C9A, 0x670C9C61, 0xABD388F0, 0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3, 0x6EEF0B6C, 0x137A3BE4, 0xBA3BF050, 0x7EFB2A98, 0xA1F1651D, 0x39AF0176, 0x66CA593E, 0x82430E88, 0x8CEE8619, 0x456F9FB4, 0x7D84A5C3, 0x3B8B5EBE, 0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6, 0x4ED3AA62, 0x363F7706, 0x1BFEDF72, 0x429B023D, 0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B, 0x075372C9, 0x80991B7B, 0x25D479D8, 0xF6E8DEF7, 0xE3FE501A, 0xB6794C3B, 0x976CE0BD, 0x04C006BA, 0xC1A94FB6, 0x409F60C4, 0x5E5C9EC2, 0x196A2463, 0x68FB6FAF, 0x3E6C53B5, 0x1339B2EB, 0x3B52EC6F, 0x6DFC511F, 0x9B30952C, 0xCC814544, 0xAF5EBD09, 0xBEE3D004, 0xDE334AFD, 0x660F2807, 0x192E4BB3, 0xC0CBA857, 0x45C8740F, 0xD20B5F39, 0xB9D3FBDB, 0x5579C0BD, 0x1A60320A, 0xD6A100C6, 0x402C7279, 0x679F25FE, 0xFB1FA3CC, 0x8EA5E9F8, 0xDB3222F8, 0x3C7516DF, 0xFD616B15, 0x2F501EC8, 0xAD0552AB, 0x323DB5FA, 0xFD238760, 0x53317B48, 0x3E00DF82, 0x9E5C57BB, 0xCA6F8CA0, 0x1A87562E, 0xDF1769DB, 0xD542A8F6, 0x287EFFC3, 0xAC6732C6, 0x8C4F5573, 0x695B27B0, 0xBBCA58C8, 0xE1FFA35D, 0xB8F011A0, 0x10FA3D98, 0xFD2183B8, 0x4AFCB56C, 0x2DD1D35B, 0x9A53E479, 0xB6F84565, 0xD28E49BC, 0x4BFB9790, 0xE1DDF2DA, 0xA4CB7E33, 0x62FB1341, 0xCEE4C6E8, 0xEF20CADA, 0x36774C01, 0xD07E9EFE, 0x2BF11FB4, 0x95DBDA4D, 0xAE909198, 0xEAAD8E71, 0x6B93D5A0, 0xD08ED1D0, 0xAFC725E0, 0x8E3C5B2F, 0x8E7594B7, 0x8FF6E2FB, 0xF2122B64, 0x8888B812, 0x900DF01C, 0x4FAD5EA0, 0x688FC31C, 0xD1CFF191, 0xB3A8C1AD, 0x2F2F2218, 0xBE0E1777, 0xEA752DFE, 0x8B021FA1, 0xE5A0CC0F, 0xB56F74E8, 0x18ACF3D6, 0xCE89E299, 0xB4A84FE0, 0xFD13E0B7, 0x7CC43B81, 0xD2ADA8D9, 0x165FA266, 0x80957705, 0x93CC7314, 0x211A1477, 0xE6AD2065, 0x77B5FA86, 0xC75442F5, 0xFB9D35CF, 0xEBCDAF0C, 0x7B3E89A0, 0xD6411BD3, 0xAE1E7E49, 0x00250E2D, 0x2071B35E, 0x226800BB, 0x57B8E0AF, 0x2464369B, 0xF009B91E, 0x5563911D, 0x59DFA6AA, 0x78C14389, 0xD95A537F, 0x207D5BA2, 0x02E5B9C5, 0x83260376, 0x6295CFA9, 0x11C81968, 0x4E734A41, 0xB3472DCA, 0x7B14A94A, 0x1B510052, 0x9A532915, 0xD60F573F, 0xBC9BC6E4, 0x2B60A476, 0x81E67400, 0x08BA6FB5, 0x571BE91F, 0xF296EC6B, 0x2A0DD915, 0xB6636521, 0xE7B9F9B6, 0xFF34052E, 0xC5855664, 0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A }; for (int x = 0;x<=255;x++){ cout<< sbox[x]^ sbox[x]<<endl; } return 0; } I get an error which says: [Error] invalid operands of types ‘const long unsigned int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’". What is wrong with my code?
Order of operations: << is higher than ^. Put parentheses around sbox[x]^ sbox[x]: cout << (sbox[x]^sbox[x]) << endl;
Displaying a value in a text box-Error
I have size_t sums[4] = {0, 0, 0, 0}; and a text box in which I am trying to display the value that I get after I've done some operations on it. The value is in sums[i] , however now I want to display the value in the textbox, I am doing this: *TextBox4 << "hello" << size_t sums; frame->Connect(TEXT_BOX4, wxEVT_COMMAND_BUTTON_CLICKED | wxEVT_COMMAND_ENTER, (wxObjectEventFunction) & MyFrame::OnGenerateKey); TextBox4 = new wxTextCtrl(this, TEXT_BOX4, wxT(""), wxPoint(200, 200), wxSize(200, 20), wxTE_PROCESS_ENTER | wxTE_READONLY | wxTE_LEFT, wxDefaultValidator, wxT("WxTextBox4")); however it generates the following errors base.cpp:212:22: error: ambiguous overload for 'operator<<' in '*((MyFrame*)this)->MyFrame::TextBox4 << sum' base.cpp:212:22: note: candidates are: C:\wxWidgets-2.8.12\include/wx/textctrl.h:419:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(const wxString&) C:\wxWidgets-2.8.12\include/wx/textctrl.h:420:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(int) C:\wxWidgets-2.8.12\include/wx/textctrl.h:421:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(long int) C:\wxWidgets-2.8.12\include/wx/textctrl.h:422:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(float) C:\wxWidgets-2.8.12\include/wx/textctrl.h:423:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(double) C:\wxWidgets-2.8.12\include/wx/textctrl.h:424:17: note: wxTextCtrl& wxTextCtrlBase::operator<<(wxChar) Please help me.
First of all you can not print a static array like that, you need to iterate over it. Second - you should only print sums without the type: *TextBox4 << "hello" << sums; Still this may not be enough to help you - I need more context.