1.定义变量
theano中的变量类型有字节、整数、浮点数、复数等多种形式, byte: bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4 16-bit integers: wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4 32-bit integers: iscalar, ivector, imatrix, irow, icol, itensor3, itensor4 64-bit integers: lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4 float: fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4 double: dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4 complex: cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4 以常用到的dscalar为例,定义浮点数使用dscalar 它表示的是有64位浮点数的标量
import theano
import theano.tensor as T
x =
T.dscalar('x')
x.
type
T.dscalar
输出都为TensorType(float64, scalar) 下面为简单的例子
from __future__ import print_function
import theano
from theano import pp
a = theano
.tensor.vector()
# declare variable
b = theano
.tensor.vector()
# declare variable
out = a **
2 + b **
2 +
2 * a * b
# build symbolic expression
f = theano
.function([a, b],
out)
# compile function
print(f([
1,
2], [
4,
5]))
# prints [ 25. 49.]
print(pp(
out))
#a**2+b**2+2*a*b
上面的例子中定义了两个矢量a和b,进行的运算是
a2+b2+2ab
,在theano中需要通过function函数来定义,function函数中一个输入一个输出矢量对应位置相加平方也就是
(1+4)2=25
和
(2+5)2=49
import theano
import numpy
from theano import pp
a = theano
.tensor.vector()
# declare variable
b = theano
.tensor.vector()
# declare variable
c = a **
2 + b **
2 +
2 * a * b
# build symbolic expression
numpy
.allclose(c
.eval({a : [
1,
2], b : [
4,
5]}), [
25,
49])
#True
2.计算梯度
T.grad=∂y∂x
,下面的程序比较简单就不做说明。
import numpy
import theano
import theano.tensor
as T
from theano
import pp
x = T.dscalar(
'x')
y = x **
2
gy = T.grad(y, x)
pp(gy)
'((fill((x ** TensorConstant{2}), TensorConstant{1.0}) * TensorConstant{2}) * (x ** (TensorConstant{2} - TensorConstant{1})))'
f = theano.function([x], gy)
f(
4)
numpy.allclose(f(
94.2),
188.4)
3. In设置缺省值
from theano import
In
from theano import
function
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, In(y, value=1)], z)
f(2) #array(3.0)
4. shared共享变量
from theano import shared
state = shared(
0)
inc = T.iscalar(
'inc')
accumulator = function([inc],
state, updates=[(
state,
state+inc)])
print(
state.get_value())
accumulator(
1)
print(
state.get_value())
accumulator(
300)
print(
state.get_value())
5.创建随机数
from theano.tensor.shared_randomstreams import RandomStreams
from theano import
function
srng = RandomStreams(seed=234)
rv_u = srng.uniform((
2,
2))
rv_n = srng.normal((
2,
2))
f =
function([], rv_u)
g =
function([], rv_n, no_default_updates=True) #Not updating rv_n.rng
nearly_zeros =
function([], rv_u + rv_u - 2 * rv_u)
6.logistic regression
import numpy
import theano
import theano
.tensor as T
rng = numpy
.random
N =
400 # training sample size
feats =
784 # number of input variables
# generate a dataset: D = (input_values, target_class)
D = (rng
.randn(N, feats), rng
.randint(size=N, low=
0, high=
2))
training_steps =
10000
# Declare Theano symbolic variables
x = T
.dmatrix(
"x")
y = T
.dvector(
"y")
# initialize the weight vector w randomly
#
# this and the following bias variable b
# are shared so they keep their values
# between training iterations (updates)
w = theano
.shared(rng
.randn(feats), name=
"w")
# initialize the bias term
b = theano
.shared(
0., name=
"b")
print(
"Initial model:")
print(w
.get_value())
print(b
.get_value())
# Construct Theano expression graph
p_1 =
1 / (
1 + T
.exp(-T
.dot(
x, w) - b))
# Probability that target = 1
prediction = p_1 >
0.5 # The prediction thresholded
xent = -
y * T
.log(p_1) - (
1-
y) * T
.log(
1-p_1)
# Cross-entropy loss function
cost = xent
.mean() +
0.01 * (w **
2)
.sum()
# The cost to minimize
gw, gb = T
.grad(cost, [w, b])
# Compute the gradient of the cost
# w.r.t weight vector w and
# bias term b
# (we shall return to this in a
# following section of this tutorial)
# Compile
train = theano
.function(
inputs=[
x,
y],
outputs=[prediction, xent],
updates=((w, w -
0.1 * gw), (b, b -
0.1 * gb)))
predict = theano
.function(inputs=[
x], outputs=prediction)
# Train
for i
in range(training_steps):
pred, err = train(D[
0], D[
1])
print(
"Final model:")
print(w
.get_value())
print(b
.get_value())
print(
"target values for D:")
print(D[
1])
print(
"prediction on D:")
print(predict(D[
0]))
转载请注明原文地址: https://ju.6miu.com/read-150352.html