小编mar*_*xor的帖子

Tensorflow:在类中创建图并在外部运行

我相信我很难理解图形在张量流中如何工作以及如何访问它们.我的直觉是'with graph:'下的线条将图形形成为单个实体.因此,我决定创建一个在实例化时构建图形的类,并且拥有一个运行图形的函数,如下所示;

class Graph(object):

    #To build the graph when instantiated
    def __init__(self, parameters ):
        self.graph = tf.Graph()
        with self.graph.as_default():
             ...
             prediction = ... 
             cost       = ...
             optimizer  = ...
             ...
    # To launch the graph
    def launchG(self, inputs):
        with tf.Session(graph=self.graph) as sess:
             ...
             sess.run(optimizer, feed_dict)
             loss = sess.run(cost, feed_dict)
             ...
        return variables
Run Code Online (Sandbox Code Playgroud)

接下来的步骤是创建一个主文件,它将汇集参数以传递给类,构建图形然后运行它;

#Main file
...
parameters_dict = { 'n_input': 28, 'learnRate': 0.001, ... }

#Building graph
G = Graph(parameters_dict)
P = G.launchG(Input)
...
Run Code Online (Sandbox Code Playgroud)

这对我来说非常优雅,但它显然不起作用(显然).实际上,似乎launchG函数无法访问图中定义的节点,这给出了我的错误;

---> 26 sess.run(optimizer, feed_dict)

NameError: …
Run Code Online (Sandbox Code Playgroud)

python session class graph tensorflow

9
推荐指数
1
解决办法
7843
查看次数

Python ctypes返回一个函数指针数组

我正在使用包含单个调用的.dll,它返回一个函数指针数组.GetMyApi()返回一个指向结构的指针,该结构是一个函数指针数组.功能本身具有不同的单独输入和输出.到目前为止我尝试了什么:

[我不能轻易改变的C代码] C:

typedef struct My_Api_V2
{
    int                 (__cdecl *IsValidInt)(int i);
    int                 (__cdecl *InvalidInt)();
    int                 (__cdecl *IsValidSize)(size_t i);
} my_Api_V2;

const my_Api_V2* GetMyApi(int version);   // This function is accessed from DLL
Run Code Online (Sandbox Code Playgroud)

Python努力:

from ctypes import *

my_dll = cdll.LoadLibrary(path_to_my_dll)
my_api = my_dll.GetMyApi
my_api.argtypes[c_int]  #version number
my_api.restypes = c_void_p

firstfuncptr = my_api(2)
firstfunc = prototype(firstfuncptr)
firstfunc.argtypes[c_int]
firstfunc.restypes = c_int

test = firstfunc(23)
Run Code Online (Sandbox Code Playgroud)

此时,我只是想让函数列表的第一个函数返回工作.任何帮助指向我更好的方向的人都表示赞赏.

python ctypes function-pointers

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

内置qsort函数和稳定排序函数有什么区别?

从引用的各种来源我知道内置的C函数,stable_sort是稳定的但qsort是不稳定的.如果是这种情况,我们为什么要使用qsort呢?这不是多余的吗?为什么不使用stable_sort呢?

c++ sorting qsort stable-sort

4
推荐指数
2
解决办法
505
查看次数