Can I Creat Model during Runtime?

tancl: <p>Currently the examples in C++ such as the mnist, and the custom-dataset, dcgan creating the models in the C++ and compile to run the training and validation. This means the model is “fixed” once the source is compiled and any changes on the network structure would require to recompile the code again.</p>
<p>Any examples to have a compiled executable to accept network model such as mnist CNN in a file format (json/prototxt/etc…) and trained the model from the external file? This will allow user to modify the network structure in the file and retrain the model without recompiling the code.</p>
<p>A close example is the caffe (not caffe2) which have the model defined in prototxt.</p>
<p>Thanks.</p>
<p>Regard,<br>
CL</p>

yf225: <p>One way to achieve this is by defining the model in TorchScript and then training it in C++:</p>
<ol>
<li>
<p>Make sure your PyTorch and libtorch installation are of the same version / commit. One way to ensure it is to install the nightly builds:</p>
<ol>
<li>PyTorch: pip install torch_nightly -f <a href="https://download.pytorch.org/whl/nightly/cu90/torch_nightly.html" rel="nofollow noopener">https://download.pytorch.org/whl/nightly/cu90/torch_nightly.html</a> (change “cu90” to your CUDA version)</li>
<li>libtorch: wget <a href="https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip" rel="nofollow noopener">https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip</a> &amp;&amp;<br>
unzip libtorch-shared-with-deps-latest.zip</li>
</ol>
</li>
<li>
<p>Run the following Python script to export a model:</p>
</li>
</ol>
<pre><code class="lang-auto">    import torch
    
    class Model(torch.jit.ScriptModule):
        def __init__(self):
            super(Model, self).__init__()
            self.linear = torch.nn.Linear(2, 2)
    
        @torch.jit.script_method
        def forward(self, x):
            a = 0
            while a &lt; 4: # While loop example
                print(a)
                a += 1
            x = self.linear(x)
            return x
    
    model = Model()
    model.save('model1.pt')
</code></pre>
<ol start="3">
<li>Create a cpp file called example-app.cpp that contains the following:</li>
</ol>
<pre><code class="lang-auto">    #include &lt;torch/torch.h&gt;
    #include &lt;torch/script.h&gt;
    #include &lt;iostream&gt;
    
    int main() {
      std::string module_path = "../model1.pt";
      // NOTE: depending on the libtorch version, you might need to change this line to `torch::jit::script::Module module = torch::jit::load(module_path);`
      // See https://discuss.pytorch.org/t/how-to-load-previously-saved-model-in-cpp-frontend/52336/6 for details.
      std::shared_ptr&lt;torch::jit::script::Module&gt; module = torch::jit::load(module_path);
    
      // TorchScript module's forward method only accepts std::vector&lt;torch::jit::IValue&gt; as input
      std::vector&lt;torch::jit::IValue&gt; inputs;
      inputs.push_back(torch::randn({2, 2}).set_requires_grad(true));
    
      std::cout &lt;&lt; "gradient of self.linear.weight: " &lt;&lt; "\n";
      std::cout &lt;&lt; module-&gt;get_module("linear")-&gt;get_parameter("weight").grad() &lt;&lt; "\n";
    
      torch::Tensor prediction = module-&gt;forward(inputs).toTensor();
      prediction.sum().backward();
    
      // Verify that backward works
      std::cout &lt;&lt; "gradient of self.linear.weight: " &lt;&lt; "\n";
      std::cout &lt;&lt; module-&gt;get_module("linear")-&gt;get_parameter("weight").grad() &lt;&lt; "\n";
    }
</code></pre>
<ol start="4">
<li>Create <code>CMakeLists.txt</code>, put in the following:</li>
</ol>
<pre><code class="lang-auto">    cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
    project(example-app)
    
    find_package(Torch REQUIRED)
    
    add_executable(example-app example-app.cpp)
    target_link_libraries(example-app "${TORCH_LIBRARIES}")
    set_property(TARGET example-app PROPERTY CXX_STANDARD 11)
</code></pre>
<ol start="5">
<li><code>mkdir build &amp;&amp; cd build</code></li>
<li>
<code>cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..</code>   # /absolute/path/to/libtorch should be the <em>absolute</em> path to the unzipped LibTorch distribution</li>
<li><code>make</code></li>
<li>Run <code>example-app</code> to see output</li>
</ol>
<p>Later on, when you want to modify the network structure of your model, you can change the model description code in Python, export the model again, and then re-train the model using the same C++ code. This way you don’t need to recompile the C++ code.</p>

tancl: <p>Hi, Thanks for your great explanation, it will be one of the solution.</p>
<p>Just something more demanding perhaps, is it possible to create the model.pt then from a simple text editor w/o using the python? it will be good to have everything in C++. The text file which contains the model architecture could be as simple as normal text, or json, or google prototxt format?</p>
<p>Thanks again.</p>
<p>Regards,<br>
Chin Luh</p>