theano 中的一个函数 sparse_block_dot;

Function:

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

Image Example

Input Parameter

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

Return

  1. - dot(W[i, j], h[i]) + b[j] #but b[j] is only added once
  2. - shape: (batch, oWin, oSize)

Applications

used form calculating theano.tensor.nnet.h_softmax;

Codes


  1. def h_softmax(x, batch_size, n_outputs, n_classes, n_outputs_per_class,
  2. W1, b1, W2, b2, target=None):
  3. "Two-level hierarchical softmax."
  4. # First softmax that computes the probabilities of belonging to each class
  5. class_probs = theano.tensor.nnet.softmax(tensor.dot(x, W1) + b1)
  6. if target is None: # Computes the probabilites of all the outputs
  7. # Second softmax that computes the output probabilities
  8. activations = tensor.tensordot(x, W2, (1, 1)) + b2
  9. output_probs = theano.tensor.nnet.softmax(
  10. activations.reshape((-1, n_outputs_per_class)))
  11. output_probs = output_probs.reshape((batch_size, n_classes, -1))
  12. output_probs = class_probs.dimshuffle(0, 1, 'x') * output_probs
  13. output_probs = output_probs.reshape((batch_size, -1))
  14. # output_probs.shape[1] is n_classes * n_outputs_per_class, which might
  15. # be greater than n_outputs, so we ignore the potential irrelevant
  16. # outputs with the next line:
  17. output_probs = output_probs[:, :n_outputs]
  18. else: # Computes the probabilities of the outputs specified by the targets
  19. target = target.flatten()
  20. # Classes to which belong each target
  21. target_classes = target // n_outputs_per_class
  22. # Outputs to which belong each target inside a class
  23. target_outputs_in_class = target % n_outputs_per_class
  24. # Second softmax that computes the output probabilities
  25. activations = sparse_block_dot(
  26. W2.dimshuffle('x', 0, 1, 2), x.dimshuffle(0, 'x', 1),
  27. tensor.zeros((batch_size, 1), dtype='int32'), b2,
  28. target_classes.dimshuffle(0, 'x'))
  29. output_probs = theano.tensor.nnet.softmax(activations.dimshuffle(0, 2))
  30. target_class_probs = class_probs[tensor.arange(batch_size),
  31. target_classes]
  32. output_probs = output_probs[tensor.arange(batch_size),
  33. target_outputs_in_class]
  34. output_probs = target_class_probs * output_probs
  35. 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. shift粘滞键后门创建/复原批处理

    创建shift粘滞键后门: 1 c: 2 3 cd \Windows\System32\ 4 5 rename sethc.exe bak_sethc.exe 6 7 xcopy cmd.exe se ...

  2. iOS开发系列--无限循环的图片浏览器

    --UIKit之UIScrollView 概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件 ...

  3. 事件分发时候的onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent调用顺序

    一直想弄清楚onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent的执行顺序,以及内部使用switch (event.getAction())中的执 ...

  4. H5 表格标签基本使用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. SQL语句查数据库中某一列是否有重复项

    Select 列名,COUNT(列名)FROM 表名GROUP BY 列名HAVING COUNT( 列名 ) 〉1

  6. 总结Cnblogs支持的常用Markdown语法

    一.什么是Markdown Markdown是一种可以使用普通文本编辑器编写的标记语言, Markdown的语法简洁明了.学习容易,而且功能比纯文本更强,因此有很多人用它写博客.世界上最流行的博客平台 ...

  7. crontab 启动 、运行 和编辑 查看

    cron服务是Linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond stop /sbi ...

  8. Nginx+PHP On windows

    前期准备 Nginx 下载 http://nginx.org/   PHP下载 PHP Windows binaries       问题 问题1: opened a DOS window to la ...

  9. BZOJ1149[CTSC2007]风玲Mobiles

    Description Input Output 输出仅包含一个整数.表示最少需要多少次交换能使风铃满足Ike的条件.如果不可能满足,输出-1. Sample Input 6 2 3 -1 4 5 6 ...

  10. [原]CentOS7.2最小安装环境部署Asp.NET Core笔记

    转载请注明原作者(think8848)和出处(http://think8848.cnblogs.com) 写在前面的话 不知不觉在cnblogs上注册已经10多年了,看我的园龄就直接暴露了我实际年龄, ...