小编mik*_*nim的帖子

使用 pybind11 包装 C++ 抽象类时出错

我正在尝试制作一个简单的示例,用 pybind 包装抽象 C++ 类。代码是:

\n
#include <pybind11/pybind11.h>\n#include <ostream>\n#include <iostream>\nnamespace py = pybind11;\nusing namespace std;\n\nclass Base\n{\npublic:\n    virtual void test() = 0;\n\n};\nclass Derived: public Base\n{\npublic:\n    void test() {cout << "Test";}\n};\n\n\nPYBIND11_MODULE(example,m) {\n    py::class_<Base, Derived>(m, "Base")\n        .def(py::init<>())\n        .def("test", &Derived::test);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行以下命令时

\n
c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` abstract_test.cpp -o example`python3-config --extension-suffix`\\n\n
Run Code Online (Sandbox Code Playgroud)\n

我收到错误:

\n
In file included from abstrakt_test.cpp:1:\n/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h: In instantiation of \xe2\x80\x98Return (Derived::* pybind11::method_adaptor(Return (Class::*)(Args ...)))(Args ...) [with Derived = Base; Return = void; Class = Derived; …
Run Code Online (Sandbox Code Playgroud)

c++ python pybind11

5
推荐指数
1
解决办法
3587
查看次数

使用带有 DQN 算法的张量板

对于强化学习,我读到张量板并不理想,因为它提供了每集和/或步骤的输入。由于强化学习有数千个步骤,因此它并没有给我们内容的概述。我在这里看到了这个修改后的张量板类:https://pythonprogramming.net/deep-q-learning-dqn-reinforcement-learning-python-tutorial/

班上:

class ModifiedTensorBoard(TensorBoard):
    # Overriding init to set initial step and writer (we want one log file for all .fit() calls)
    def __init__(self, name, **kwargs):
        super().__init__(**kwargs)
        self.step = 1
        self.writer = tf.summary.create_file_writer(self.log_dir)
        self._log_write_dir = os.path.join(self.log_dir, name)

    # Overriding this method to stop creating default log writer
    def set_model(self, model):
        pass

    # Overrided, saves logs with our step number
    # (otherwise every .fit() will start writing from 0th step)
    def on_epoch_end(self, epoch, logs=None):
        self.update_stats(**logs)

    # Overrided
    # We train …
Run Code Online (Sandbox Code Playgroud)

reinforcement-learning tensorflow tensorboard dqn

2
推荐指数
1
解决办法
1592
查看次数

从类方法函数调用变量到普通方法

我想从一个类方法调用一个变量到同一类内的另一个方法:

class A():
    @classmethod
    def b(cls):
        cls.g = 5
    def c(self):
        if self.g < 1:
            print("TestA")
        else:
            print("TestB")
Run Code Online (Sandbox Code Playgroud)

进行时:

x = A()
x.c()
Run Code Online (Sandbox Code Playgroud)

我得到:

AttributeError: 'A' object has no attribute 'g'
Run Code Online (Sandbox Code Playgroud)

我已经阅读并搜索了类似的案例,但没有找到。大多数处理从init方法调用变量,但这不适用于此方法。

python class

-1
推荐指数
1
解决办法
33
查看次数