我在概念上有一个关于Theano的简单问题,但是我还没有找到答案(我会先前承认并没有真正理解共享变量在Theano中是如何工作的,尽管有很多小时的教程).
我正在尝试实施"反卷积网络"; 具体来说,我有一个3张输入(每个输入是一个2D图像)和一个4张量的代码; 对于第i个输入代码[i]表示一组代码字,它们一起编码输入i.
我一直在努力弄清楚如何在代码字上做渐变下降.以下是我的代码的相关部分:
idx = T.lscalar()
pre_loss_conv = conv2d(input = codes[idx].dimshuffle('x', 0, 1,2),
filters = dicts.dimshuffle('x', 0,1, 2),
border_mode = 'valid')
loss_conv = pre_loss_conv.reshape((pre_loss_conv.shape[2], pre_loss_conv.shape[3]))
loss_in = inputs[idx]
loss = T.sum(1./2.*(loss_in - loss_conv)**2)
del_codes = T.grad(loss, codes[idx])
delc_fn = function([idx], del_codes)
train_codes = function([input_index], loss, updates = [
[codes, T.set_subtensor(codes[input_index], codes[input_index] -
learning_rate*del_codes[input_index]) ]])
Run Code Online (Sandbox Code Playgroud)
(这里代码和dicts是共享的张量变量).Theano对此不满意,尤其是定义
del_codes = T.grad(loss, codes[idx])
Run Code Online (Sandbox Code Playgroud)
我得到的错误信息是: theano.gradient.DisconnectedInputError:要求grad方法计算相对于不是成本计算图的一部分的变量的梯度,或者仅由不可微运算符使用:Subtensor {int64} .0
我猜它想要一个符号变量而不是代码[idx]; 但后来我不知道如何让一切都连接起来以获得预期的效果.我猜我需要将最后一行更改为类似的内容
learning_rate*del_codes) ]])
Run Code Online (Sandbox Code Playgroud)
有人可以给我一些关于如何正确定义这个功能的指示吗?我想我可能错过了与Theano合作的基本知识,但我不确定是什么.
提前致谢!
-Justin
更新:凯尔的建议非常好.这是我使用的具体代码
current_codes = T.tensor3('current_codes')
current_codes = codes[input_index]
pre_loss_conv …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Gentoo系统上从源代码安装tensorflow(我认为我需要这样做才能使用CUDA 9.1)。
我能够构建tensorflow,然后以以下用户身份安装它:
pip3 install --no-cache-dir --user /tmp/tensorflow_pkg/tensorflow-1.6.0rc1-cp35-cp35m-linux_x86_64.whl
Run Code Online (Sandbox Code Playgroud)
当我尝试导入tensorflow时,我得到:
>RuntimeError Traceback (most recent call last)
>RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
>
>ImportError Traceback (most recent call last)
>ImportError: numpy.core.multiarray failed to import
>
>ImportError Traceback (most recent call last)
>ImportError: numpy.core.umath failed to import
>
>ImportError Traceback (most recent call last)
>ImportError: numpy.core.umath failed to import
Run Code Online (Sandbox Code Playgroud)
所以我的猜测是tensorflow是针对不同于我的系统默认版本(1.13.3)的numpy版本构建的。
问题是如何解决?我对这个过程有很多陌生的知识,因此,如果有以下任何建议,我将不胜感激:
任何帮助表示赞赏!
我在virtualenv中安装gym-atari时遇到了麻烦.从我所知,看起来问题是zlib,但zlib安装在系统上(cmake也是如此).有人有主意吗?这是在Gentoo系统上,它的价值:
(tensorflow)alaya ~ # pip install gym[atari]
Requirement already satisfied: gym[atari] in /opt/tensorflow/lib/python2.7/site-packages
Requirement already satisfied: six in /usr/lib64/python2.7/site-packages (from gym[atari])
Requirement already satisfied: requests>=2.0 in /usr/lib64/python2.7/site-packages (from gym[atari])
Requirement already satisfied: pyglet>=1.2.0 in /opt/tensorflow/lib/python2.7/site-packages (from gym[atari])
Requirement already satisfied: numpy>=1.10.4 in /opt/tensorflow/lib/python2.7/site-packages (from gym[atari])
Requirement already satisfied: PyOpenGL; extra == "atari" in /usr/lib64/python2.7/site-packages (from gym[atari])
Collecting atari-py>=0.0.17; extra == "atari" (from gym[atari]) Using cached atari-py-0.0.18.tar.gz
Requirement already satisfied: Pillow; extra == "atari" in /usr/lib64/python2.7/site-packages (from gym[atari])
Building …Run Code Online (Sandbox Code Playgroud) 我有一个处理图像的循环,我希望在每 100 次迭代(比如)使用 matplotlib 在单个输出窗口中显示图像。所以我正在尝试编写一个函数,它将一个 numpy 张量作为输入并显示相应的图像。
这是我所拥有的不起作用的东西:
def display(image):
global im
# If im has been initialized, update it with the current image; otherwise initialize im and update with current image.
try:
im
im.set_array(image)
plt.draw()
except NameError:
im = plt.imshow(image, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
plt.show(block=False)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
我一开始试图通过 FuncAnimation 传递它,但这似乎旨在让动画调用一个函数来执行更新,而不是在 matplotlib 上调用函数来显示结果。
上面的代码打开一个窗口,但它似乎没有更新。任何人都可以指出我正确的方向吗?
非常感谢,
贾斯汀
我在使用Python释放内存时遇到了麻烦.情况基本上是这样的:我将一个大型数据集拆分为4个文件.每个文件包含5000个numpy形状阵列(3072,412)的列表.我试图将每个数组的第10到第20列提取到一个新列表中.
我想要做的是顺序读取每个文件,提取我需要的数据,并释放我正在使用的内存,然后再继续下一个.但是,删除对象,将其设置为无并将其设置为0,然后调用gc.collect()似乎不起作用.这是我正在使用的代码片段:
num_files=4
start=10
end=20
fields = []
for j in range(num_files):
print("Working on file ", j)
source_filename = base_filename + str(j) + ".pkl"
print("Memory before: ", psutil.virtual_memory())
partial_db = joblib.load(source_filename)
print("GC tracking for partial_db is ",gc.is_tracked(partial_db))
print("Memory after loading partial_db:",psutil.virtual_memory())
for x in partial_db:
fields.append(x[:,start:end])
print("Memory after appending to fields: ",psutil.virtual_memory())
print("GC Counts before del: ", gc.get_count())
partial_db = None
print("GC Counts after del: ", gc.get_count())
gc.collect()
print("GC Counts after collection: ", gc.get_count())
print("Memory after freeing partial_db: …Run Code Online (Sandbox Code Playgroud) python ×4
gentoo ×2
linux ×2
numpy ×2
bazel ×1
matplotlib ×1
memory-leaks ×1
openai-gym ×1
tensorflow ×1
theano ×1
virtualenv ×1