我爱编程

theano篇

2018-06-11  本文已影响0人  逃淘桃

theano


import theano
import numpy as np
import theano.tensor as T
ones = theano.shared(np.float32([[1, 2, 3], [4, 5, 6],[7, 8, 9]]))
print(ones.get_value())
--->>[[1, 2, 3]
      [4, 5, 6]
      [7, 8, 9]]
result = T.concatenate([ones,ones], axis=0)
print(result.eval())
--->>
[[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]]
result = T.concatenate([ones, ones], axis=1)
print(result.eval())
--->>
[[ 1.  2.  3.  1.  2.  3.]
 [ 4.  5.  6.  4.  5.  6.]
 [ 7.  8.  9.  7.  8.  9.]]

当操作数为二维数组时,axis=0为第一维的方向,axis=1为第二维的方向。
3.theano.tensor.dot(a, b, axes):矩阵乘法

import theano
import numpy as np
import theano.tensor as T
ones = theano.shared(np.float32([[1, 2, 3],[4, 5, 6], [7, 8, 9]]))
print(ones.get_value())
--->>
[[ 1.  2.  3.]
 [ 4.  5.  6.]
 [ 7.  8.  9.]]
result = T.dot(ones, ones)
print(result.eval())
--->>
[[  30.   36.   42.]
 [  66.   81.   96.]
 [ 102.  126.  150.]]

4.theano.gradient.grad_clip(x,lower_bound,upper_bound):梯度裁剪
其中x是想要进行梯度裁剪的输入,lower_bround,upper_bound表示梯度的上限和下限。
5.theano.tensor.le(a,b):返回a,b中较小的值a<=b
6.theano.tensor.gt(a,b):返回逻辑上大于的值,表示为'int8'tensor, 也可以用来表示语法a>b
7.theano.tensor.lt(a,b):返回逻辑上较小的值,表示为一个'int8'的tensor,也可以用来表示语法a < b


2018-05-24

1.theano.tensor.switch(cond,ift,iff):满足条件(cond)输出x, 不满足输出y.
2.theano.scan(fn, sequences=None, outputs_info=None, non_sequences=None, n_steps=None, truncate_gradient=-1, go_backwards=False, mode=None, name=None, profile=False, allow_gc=None, strict=False)

result = 1
for i in range(k):
    result = result * A

使用theano.scan():
在此我们有三件事需要处理:第一,分配初始值result,第二是result值得计算,第三就是unchanging变量A,不变的变量传递给no_sequences

import theano
import theano.tensor as T

k = T.iscalar("k")
A = T.vector("A")

# Symbolic description of the result
result, updates = theano.scan(fn=lambda prior_result, A: prior_result * A,
                              outputs_info=T.ones_like(A),
                              non_sequences=A,
                              n_steps=k)
# we only care about A**k, but scan has provided us with A**1 through A**k,
# Discard the values that we don't care about. Scan is smart enough to
# notice this and not waste memory saving them.
final_result = result[-1]
# compiled function that returns A**k
power = theano.function(inputs=[A, k], outputs=final_result, updates=updates)

print(power(range(3), 2))
#===>[ 0.  1.  4.]

2018-06-11

1.theano.tensor常用的数据类型

import theano
x = theano.tensor.iscalar('x')# 声明一个int类型的变量x
y = theano.tensor.pow(x,3)# 定义y=x^3
f = theano.function([x],y)# 定义函数的自变量为x(输入),因变量为y(输出)
print(f(2))# 计算当x=2的时候,函数f(x)的值

example:

import theano
import theano.tensor as T

a = T.matrix()
b = T.matrix()
e = T.fscalar()
c = a * b
d = T.dot(a, b)
g = T.ivector()
f = g * e
f1 = theano.function([a, b], c)
f2 = theano.function([a, b], d)
f3 = theano.function([g, e], f)
A = [[1, 2], [3, 4]]# 2*2的矩阵
B = [[2, 4], [6, 8]]# 2*2的矩阵
C = [[1, 2], [3, 4], [5, 6]]# 3*2的矩阵
G = [1, 2, 3, 4]
print(f1(A, B))
print(f2(A, B))
# print(f1(C, B))
print(f2(C, B))
print(f3(G, 0.5))

输出为:

[[  2.   8.]
 [ 18.  32.]]
[[ 14.  20.]
 [ 30.  44.]]
[[ 14.  20.]
 [ 30.  44.]
 [ 46.  68.]]
[ 0.5  1.   1.5  2. ]

注意:函数输入必须是List带[ ]

上一篇 下一篇

猜你喜欢

热点阅读