我想使用 jupyter 笔记本中的散景小部件来更新散景图。我的(有点hacky)代码如下所示:
from bokeh.plotting import figure
from bokeh.io import output_notebook, push_notebook, show
from bokeh.models import CustomJS, Slider
output_notebook()
power = 0.5
x = [1,2,3]
y = [i**power for i in x]
fig = figure()
plt = fig.circle(x, y)
def update_plot(power):
x = plt.data_source.data['x']
plt.data_source.data['y'] = [i**power for i in x]
push_notebook(handle=bokeh_handle)
bokeh_handle = show(fig, notebook_handle=True)
##### new notebook cell #####
callback = CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
var kernel = IPython.notebook.kernel;
cmd = "update_plot(" + cb_obj.value + ")"; …Run Code Online (Sandbox Code Playgroud) 嗨,我正在关注一个神经网络教程,作者似乎在各处使用共享变量。据我了解,theanos中的共享变量只是内存中的空间,可以由gpu和cpu堆共享。无论如何,我有两个矩阵,它们声明为共享变量,并且我想使用函数对它们执行一些操作。(问题1)如果有人可以解释为什么函数对常规def函数有用的话,我会喜欢的。无论如何,我正在像这样设置我的定义:
import theano
import theano.tensor as T
from theano import function
import numpy as np
class Transform:
def __init__(self, dimg):
dimg = dimg.astype(theano.config.floatX)
self.in_t = theano.shared(dimg, name='dimg', borrow=True)
def rotate(self, ox, oy, radians):
value = np.zeros((2 * self.in_t.get_value().shape[0],
2 * self.in_t.get_value().shape[1]))
out_t = theano.shared(value,
name='b',
dtype=theano.config.floatX),
borrow=True)
din = theano.tensor.dmatrix('a')
dout = theano.tensor.dmatrix('b')
def atest():
y = x + y
return y
f = function(inputs=[],
givens={x: self.in_t,
y: self.out_t},
outputs=atest)
return f()
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何在常规函数输出调用中使用共享变量。我了解可以通过function([],.. update =(shared_var_1,upate_function))进行更新。但是,如何在常规功能中访问它们?