- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要更改内部变量以引用新对象,然后做一些事情,然后再次更改对象,然后对新值做一些事情。
class dml
{
void setTrain(matrix<T> train_x , matrix<T> train_y)
{
train_x_ = train_x;
train_y_ = train_y;
}
void train();
private:
matrix<T> train_x_;
matrix<T> train_y_;
}
因此,我将首先设置 train ,然后调用 train ,它使用我刚刚设置的值进行训练。然后稍后我可能会通过对新值调用 setTrain 来更改值,我想再次对该数据运行训练。(对于 ML 的人,我想实现小批量,我输入数据,训练它,然后输入下一批数据,然后训练它)我现在拥有的简单解决方案很愚蠢,因为它涉及复制一个非常大的矩阵。我的指针是想到的简单解决方案,但我不想使用它们。我的程序很复杂,指针和内存分配只会更麻烦。我想到的一个想法是:
private:
matrix<T>& train_x_;
matrix<T>& train_y_;
}
当然这是行不通的,因为引用永远不能指向空值,我必须在构造函数中初始化它们,因为它们不能重新绑定(bind),所以没有必要使用它们。
我认为解决方案是使用一些智能指针。但是智能指针用于管理堆中的内存,但我不是在堆中分配内存。矩阵对象是在堆栈中创建的,但它是使用 std::vector 实现的,它初始化堆中的所有对象。
我认为逻辑上 shared_ptr 应该起作用,因为我们正在共享一个变量。 std::make_shared
似乎是个好主意,但我读到,当您尝试从堆栈中的现有对象创建 shared_ptr 时,它也会创建一个拷贝。但这并没有解决必须复制这么大对象的问题。
另一个明显的事情是 std::move
,但是 move 在这里不适用,因为我可能需要在函数调用之外使用训练数据 agian。
我确定我遗漏了一些明显的东西。我现在有点 sleep 不足,所以任何解决方案都会对我有很大帮助!
澄清一下:
LargeObj a=9;
auto a_ptr=std::make_shared(a);
如果我这样做,那么 a_ptr 将只指向 a,而不创建拷贝?但这不是这里的一个答案所说的:
Create a boost::shared_ptr to an existing variable
int a = 3;//现有变量shared_ptr aCopy = make_shared(a);//创建值为a的拷贝
最佳答案
LargeObj a=9;
auto a_ptr=std::make_shared(a);If I do this, then a_ptr will just point to a, without creating a copy?
如果您通过复制现有对象(允许)来创建共享指针,那么您将拥有两个对象。这可以通过删除类中的相关函数和/或永远不要将对象定义为 shared_ptr<matrix<T>>
以外的任何对象来避免。 .
如果使用 std::shared_ptr
,您可以处理大对象并防止复制.
以下是对问题中示例的改编。它将对象创建为 std::shared_ptr
s 并显示如何将它们分配给示例中的类。
#include <memory>
#include <iostream>
#include <string>
template <typename T>
class matrix {
public:
matrix() = default;
matrix(const matrix&) = delete; // no copy
matrix(matrix&&) = default; // allow move
matrix& operator=(const matrix&) = delete; // no copy
matrix& operator=(matrix&&) = default; // allow move
};
template <typename T>
class dml
{
public:
void setTrain(std::shared_ptr<matrix<T>> train_x_ptr, std::shared_ptr<matrix<T>> train_y_ptr)
{
train_x_ = train_x_ptr;
train_y_ = train_y_ptr;
}
void train() {};
private:
std::shared_ptr<matrix<T>> train_x_{}; // starts out as an empty shared pointer (contains nullptr)
std::shared_ptr<matrix<T>> train_y_{}; // starts out as an empty shared pointer (contains nullptr)
};
int main()
{
// no explicit new, object is created on the heap (train_x_ptr is created on the stack,
// but will be copied to dml without copying the underlying object).
auto train_x_ptr{std::make_shared<matrix<int>>()};
auto train_y_ptr{std::make_shared<matrix<int>>()};
dml<int> d;
d.setTrain(train_x_ptr, train_y_ptr);
}
请注意 new
不直接使用。
I think the solution would be in using some of the smart pointers. But smart pointers are used to manage the memory in heap, but I am not allocating an memory in the heap. The matrix object is created in the stack, but it is implemented using a std::vector, which initializes all of its objects in the heap.
在此示例中,std::shared_ptr
将创建 matrix
在堆上并为您管理生命周期。同时,如果matrix
包含 std::vector
这些元素也将在某个地方的堆上,由 std::vector
管理.
I think logically shared_ptr should work, as we are sharing a varaible.
std::make_shared
seems to be a good idea, but I read that it too creates a copy when you try create a shared_ptr from an existing object in the stack. But this does not solve the problem of having to copy such a large object.
它不会创建拷贝。它确实解决了问题。
关于c++ - 如何设计可反弹的大对象的类依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125733/
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
.NET 框架:4.5.1 我在 Blend for visual studio 2015 中遇到一个奇怪的错误,我找不到它的来源。 如果我在 VS 中打开我的 WPF 解决方案,它会加载并运行良好。
我经常遇到这样的问题,与 Hierarchical RESTful URL design 非常相似 假设该服务仅提供用户上传文档。 POST, GET /accounts PUT, DELETE /a
在 Rails 应用程序中,我使用 devise 来管理我的用户,而我用来销毁 session 的链接不再有效。它正在工作,现在我添加了事件管理员,但没有。 我的链接是 :delete, :clas
我已经坚持了超过 24 小时,试图按照此处发布的其他解决方案进行操作,但我无法使其正常工作。我是 Rails 新手,需要帮助! 我想让我的/users/edit 页面正常工作,以便我可以简单地更改用户
Devise 在以下情况下不会使用户超时: 用户登录,关闭选项卡,然后在超时 + X 分钟内重新访问该 URL。用户仍处于登录状态。 如果选项卡已打开并且稍后刷新/单击,则超时可以正常工作。这意味着
我想使用这样的 slider 我希望该 slider 根据提供给它的值进行相应调整。到目前为止,我只能应用具有渐变效果的背景,但无法获得这种效果。请通过提供样式代码来帮助我。
您应该为每种方法创建一个请求/响应对象,还是应该为每个服务创建一个? 如果我在所有方法中使用它,我的服务请求对象中将只有 5 个不同的东西,因为我对几乎所有方法使用相同的输入。 响应对象将只有一个字典
我正在尝试在 REST 中对实体的附件进行建模。假设一个缺陷实体可以附加多个附件。每个附件都有描述和一些其他属性(上次修改时间、文件大小...)。附件本身是任何格式的文件(jpeg、doc ...)
我有以下表格: Blogs { BlogName } BlogPosts { BlogName, PostTitle } 博客文章同时建模一个实体和一个关系,根据 6nf(根据第三个宣言)这是无效的。
如果 A 类与 B、C 和 D 类中的每一个都有唯一的交互,那么交互的代码应该在 A 中还是在 B、C 和 D 中? 我正在编写一个小游戏,其中许多对象可以与其他对象进行独特的交互。例如,EMP点击
关于如何记住我与 Omniauth 一起工作似乎有些困惑。 根据这个wiki ,您需要在 OmniauthCallbacksController 中包含以下内容: remember_me(user)
设计问题: 使用 非线程安全 组件(集合,API,...)在/带有 多线程成分 ... 例子 : 组件 1 :多线程套接字服务器谁向消息处理程序发送消息... 组件 2 :非线程安全 消息处理程序 谁
我们目前正在设计一个 RESTful 应用程序。我们决定使用 XML 作为我们的基本表示。 我有以下关于在 XML 中设计/建模应用程序数据的问题。 在 XML 中进行数据建模的方法有哪些?从头开始然
我正在设计一个新的 XSD 来从业务合作伙伴那里获取积分信息。对于每笔交易,合作伙伴必须提供至少一种积分类型的积分值。我有以下几点:
设计支持多个版本的 API 的最佳方法是什么。我如何确保即使我的数据架构发生更改(微小更改),我的 api 的使用者也不会受到影响?任何引用架构、指南都非常有用。 最佳答案 Mark Nottingh
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我想用 php 创建一个网站,其工作方式与 https://www.bitcoins.lc/ 相同。确实,就每个页面上具有相同布局但内容会随着您更改链接/页面而改变而言,我如何在 php 中使用lay
我有一个关于编写 Swing UI 的问题。如果我想制作一个带有某些选项的软件,例如在第一个框架上,我有三个按钮(新建、选项、退出)。 现在,如果用户单击新按钮,我想将框架中的整个内容更改为其他内容。
我正在尝试找出并学习将应用程序拥有的一堆Docker容器移至Kubernetes的模式和最佳实践。诸如Pod设计,服务,部署之类的东西。例如,我可以创建一个其中包含单个Web和应用程序容器的Pod,但
我是一名优秀的程序员,十分优秀!