theano 中的一个函数 sparse_block_dot;

Function:

for b in range(batch_size):
for j in range(o.shape[1]):
for i in range(h.shape[1]):
o[b, j, :] += numpy.dot(h[b, i], W[iIdx[b, i], oIdx[b, j]])

Image Example

Input Parameter

- W (iBlocks, oBlocks, iSize, oSize) – weight matrix
- h (batch, iWin, iSize) – input from lower layer (sparse)
- inputIdx (batch, iWin) – indexes of the input blocks
- b (oBlocks, oSize) – bias vector
- outputIdx (batch, oWin) – indexes of the output blocks

Return

- dot(W[i, j], h[i]) + b[j] #but b[j] is only added once

- shape: (batch, oWin, oSize)

Applications

used form calculating theano.tensor.nnet.h_softmax;

Codes


def h_softmax(x, batch_size, n_outputs, n_classes, n_outputs_per_class,
W1, b1, W2, b2, target=None):
"Two-level hierarchical softmax." # First softmax that computes the probabilities of belonging to each class
class_probs = theano.tensor.nnet.softmax(tensor.dot(x, W1) + b1) if target is None: # Computes the probabilites of all the outputs # Second softmax that computes the output probabilities
activations = tensor.tensordot(x, W2, (1, 1)) + b2
output_probs = theano.tensor.nnet.softmax(
activations.reshape((-1, n_outputs_per_class)))
output_probs = output_probs.reshape((batch_size, n_classes, -1))
output_probs = class_probs.dimshuffle(0, 1, 'x') * output_probs
output_probs = output_probs.reshape((batch_size, -1))
# output_probs.shape[1] is n_classes * n_outputs_per_class, which might
# be greater than n_outputs, so we ignore the potential irrelevant
# outputs with the next line:
output_probs = output_probs[:, :n_outputs] else: # Computes the probabilities of the outputs specified by the targets target = target.flatten() # Classes to which belong each target
target_classes = target // n_outputs_per_class # Outputs to which belong each target inside a class
target_outputs_in_class = target % n_outputs_per_class # Second softmax that computes the output probabilities
activations = sparse_block_dot(
W2.dimshuffle('x', 0, 1, 2), x.dimshuffle(0, 'x', 1),
tensor.zeros((batch_size, 1), dtype='int32'), b2,
target_classes.dimshuffle(0, 'x')) output_probs = theano.tensor.nnet.softmax(activations.dimshuffle(0, 2))
target_class_probs = class_probs[tensor.arange(batch_size),
target_classes]
output_probs = output_probs[tensor.arange(batch_size),
target_outputs_in_class]
output_probs = target_class_probs * output_probs return output_probs

theano sparse_block_dot的更多相关文章

  1. Deconvolution Using Theano

    Transposed Convolution, 也叫Fractional Strided Convolution, 或者流行的(错误)称谓: 反卷积, Deconvolution. 定义请参考tuto ...

  2. Theano printing

    Theano printing To visualize the internal relation graph of theano variables. Installing conda insta ...

  3. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

  4. Theano Inplace

    Theano Inplace inplace Computation computation that destroy their inputs as a side-effect. Example i ...

  5. broadcasting Theano vs. Numpy

    broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...

  6. theano scan optimization

    selected from Theano Doc Optimizing Scan performance Minimizing Scan Usage performan as much of the ...

  7. ubuntu系统theano和keras的安装

    说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...

  8. theano学习

    import numpy import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar( ...

  9. Theano 学习笔记(一)

    Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...

随机推荐

  1. 深入理解javascript选择器API系列第三篇——h5新增的3种selector方法

    × 目录 [1]方法 [2]非实时 [3]缺陷 前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuer ...

  2. Linux-学习前言

    本随笔会持续,不定期更新.我有上网找与Linux相关的博客,发现很多人只写了几篇就没更新了,没有坚持下来!希望我能keep  on. 最近一个月是考试月,可能更新会比较少.

  3. mount常用挂载命令

    挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式: mount [-t vfstype] [-o optio ...

  4. 第三方Android 模拟器流畅速度快,适合开发人员

    “工欲善其事,必先利其器.” 使用Android模拟器开发和调试应用肯定比使用真机方便.但相比XCODE的IOS模拟器,Android SDK自带的AVD实在不争气,不过一些第三方的模拟器却表现不俗! ...

  5. Linux计划任务.md

    crond crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程 ...

  6. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  7. [LeetCode] Same Tree 判断相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. [个人翻译]Redis 集群教程(上)

    官方原文地址:https://redis.io/topics/cluster-tutorial  水平有限,如果您在阅读过程中发现有翻译的不合理的地方,请留言,我会尽快修改,谢谢.        这是 ...

  9. 撸一段 SQL ? 还是撸一段代码?

    记得刚入公司带我的研发哥们能写一手漂亮的 SQL,搜索准确.执行快.效率高. 配合Web项目中的查询展示数据的需求,基本是分分钟完成任务. 那段时间基本是仰视的态度,每天都去讨教一点手写 SQL 的要 ...

  10. xwalk_core_library-15.44.384 .13.aar 百度云分享

    xwalk_core_library-15.44.384.13.aar 这玩意下载很慢 特意放到百度,需要的可以通过百度下载 http://pan.baidu.com/s/1nt0Cmbn 下完后放到 ...