How to create Dataset with samples of different size?

wongkenchiang97: <p>I’m trying to create a point cloud dataset, but the <strong>number of points between samples may be differ</strong>.</p>
<p>The class of Dataset:</p>
<pre><code class="lang-auto">class PFGDataset : public torch::data::Dataset&lt;PFGDataset&gt; {
public:
    enum Mode {
        kTrain,
        kTest
    };

    explicit PFGDataset(const std::string&amp; _root, Mode _mode = Mode::kTrain);
    ~PFGDataset();
    static std::pair&lt;torch::Tensor, torch::Tensor&gt; read_data(const std::string&amp; _root, bool _train);
    torch::data::Example&lt;&gt; get(size_t _index) override;
    torch::optional&lt;size_t&gt; size() const override;
    bool is_train() const noexcept;
    const torch::Tensor&amp; data() const;
    const torch::Tensor&amp; targets() const;

private:
    torch::Tensor data_;
    torch::Tensor targets_;
    Mode mode_;
};
</code></pre>
<p><code>read_data</code> method are used to initialize <code>data_</code> and <code>targets_</code> Tensor:</p>
<pre><code class="lang-auto">PFGDataset::PFGDataset(const std::string&amp; _root, Mode _mode)
    : mode_(_mode)
{
    auto data = read_data(_root, mode_ == PFGDataset::Mode::kTrain);
    data_ = std::move(data.first);
    targets_ = std::move(data.second);
}
</code></pre>
<p>The <code>read_data</code> method:</p>
<pre><code class="lang-auto">std::pair&lt;torch::Tensor, torch::Tensor&gt; PFGDataset::read_data(const std::string&amp; _root, bool _train)
{
    int i = 0;
    std::string ext = ".pcd";
    const auto num_samples = _train ? kTrainSize : kTestSize;
    const auto folder = _train ? _root + "/train" : _root + "/valid";
    auto num_points = 215146; // num_points of 1st sample
    auto point_clouds = torch::empty({ num_samples, num_points, 3 }, torch::kFloat); // xyz of points
    auto targets = torch::empty({ num_samples, num_points, 1 }, torch::kLong); // labels of points

    /*Load every labeled point cloud under directory*/
    std::cout &lt;&lt; "Load every labeled point cloud under directory..." &lt;&lt; std::endl;
    for (const auto&amp; p : fs::directory_iterator(folder)) {
        if (p.path().extension() != ext) {
            continue;
        }
        pcl::PointCloud&lt;pcl_ext::PointXYZLO&gt;::Ptr data_cloud;
        data_cloud = pcl::make_shared&lt;pcl::PointCloud&lt;pcl_ext::PointXYZLO&gt;&gt;();
        auto flag = pcl::io::loadPCDFile(p.path().string(), *data_cloud); // Num of points in each PCD files may be differ
        if (flag != -1) {
            /*Input Tensor with sizes{Num,3}*/
            auto xyz = pointCloudToTensor&lt;pcl_ext::PointXYZLO&gt;(data_cloud);
            std::cout &lt;&lt; "point_clouds[" &lt;&lt; i &lt;&lt; "][sizes]: " &lt;&lt; xyz.sizes() &lt;&lt; std::endl;
            point_clouds[i] = xyz; // xyz of points in i-th sample

            /*Target Tensor with sizes{Num,1}*/
            auto label_obj_map = Eigen::Map&lt;Eigen::MatrixXi, 0, Eigen::Stride&lt;sizeof(pcl_ext::PointXYZLO) / sizeof(int), 1&gt;&gt;(&amp;data_cloud-&gt;points[0].label, 2, data_cloud-&gt;points.size());
            Eigen::VectorXi label = label_obj_map.row(0);
            torch::Tensor tensor_label = torch::from_blob(label.data(), label.rows(), torch::TensorOptions().dtype(torch::kInt)).unsqueeze(-1);
            std::cout &lt;&lt; "tensor_label[" &lt;&lt; i &lt;&lt; "][sizes]: " &lt;&lt; tensor_label.sizes() &lt;&lt; std::endl;
            targets[i] = tensor_label.to(torch::kLong); // labels of points in i-th sample
        } else {
            continue;
        }

        if (i &gt;= num_samples - 1)
            break;
        i++;
    }

    return { point_clouds, targets };
}
</code></pre>
<p>The terminal output:</p>
<blockquote>
<p>Load every labeled point cloud under directory…<br>
point_clouds[0][sizes]: [215146, 3]<br>
tensor_label[0][sizes]: [215146, 1]<br>
point_clouds[1][sizes]: [213377, 3]<br>
terminate called after throwing an instance of ‘c10::Error’<br>
what():  <strong>The size of tensor a (215146) must match the size of tensor b (213377) at non-singleton dimension 0</strong></p>
</blockquote>
<p>Is there any way to <strong>allow</strong> the <strong>dataset to have samples of different size</strong>?</p>