本文整理汇总了Python中theano.tensor.dscalars函数的典型用法代码示例。如果您正苦于以下问题:Python dscalars函数的具体用法?Python dscalars怎么用?Python dscalars使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dscalars函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_scalars
def test_scalars(self):
try:
import theano.tensor as T
from theano import function
except:
return
# Set up variables and function
vals = [1, 2, 3, 4, 5]
f = lambda a, b, c, d, e : a + (b * c) - d ** e
# Set up our objects
Cs = [ch.Ch(v) for v in vals]
C_result = f(*Cs)
# Set up Theano's equivalents
Ts = T.dscalars('T1', 'T2', 'T3', 'T4', 'T5')
TF = f(*Ts)
T_result = function(Ts, TF)
# Make sure values and derivatives are equal
self.assertEqual(C_result.r, T_result(*vals))
for k in range(len(vals)):
theano_derivative = function(Ts, T.grad(TF, Ts[k]))(*vals)
#print C_result.dr_wrt(Cs[k])
our_derivative = C_result.dr_wrt(Cs[k])[0,0]
#print theano_derivative, our_derivative
self.assertEqual(theano_derivative, our_derivative)
开发者ID:MPI-IS,项目名称:chumpy,代码行数:29,代码来源:test_ch.py
示例2: sample_gradient
def sample_gradient():
print "微分"
x, y = T.dscalars("x", "y")
z = (x+2*y)**2
# dz/dx
gx = T.grad(z, x)
fgx = theano.function([x,y], gx)
print fgx(1.0, 1.0)
# dz/dy
gy = T.grad(z, y)
fgy = theano.function([x,y], gy)
print fgy(1.0, 1.0)
# d{sigmoid(x)}/dx
x = T.dscalar("x")
sig = sigmoid(x)
dsig = T.grad(sig, x)
f = theano.function([x], dsig)
print f(0.0)
print f(1.0)
# d{sigmoid(<x,w>)}/dx
w = T.dscalar("w")
sig = sigmoid(T.dot(x,w))
dsig = T.grad(sig, x)
f = theano.function([x, w], dsig)
print f(1.0, 2.0)
print f(3.0, 4.0)
print
开发者ID:norikinishida,项目名称:snippets,代码行数:27,代码来源:sample.py
示例3: test_examples_6
def test_examples_6(self):
from theano import Param
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, Param(y, default=1)], z)
assert f(33) == array(34.0)
assert f(33, 2) == array(35.0)
开发者ID:AI-Cdrone,项目名称:Theano,代码行数:8,代码来源:test_tutorial.py
示例4: defaultValue
def defaultValue(*arg):
x, y, w = T.dscalars('x', 'y', 'w')
z = x + y + w
f = th.function([x, th.In(y, value=1), th.In(w, value=2, name='wName')], z)
if len(arg) == 3:
print(f(arg[0], wName = arg[1], y = arg[2]))
elif len(arg) == 2:
print(f(arg[0], arg[1]))
else:
print(f(arg[0]))
开发者ID:thbeucher,项目名称:DQN,代码行数:10,代码来源:theanoL.py
示例5: test_examples_7
def test_examples_7(self):
from theano import Param
x, y, w = T.dscalars('x', 'y', 'w')
z = (x + y) * w
f = function([x, Param(y, default=1), Param(w, default=2, name='w_by_name')], z)
assert f(33) == array(68.0)
assert f(33, 2) == array(70.0)
assert f(33, 0, 1) == array(33.0)
assert f(33, w_by_name=1) == array(34.0)
assert f(33, w_by_name=1, y=0) == array(33.0)
开发者ID:AI-Cdrone,项目名称:Theano,代码行数:10,代码来源:test_tutorial.py
示例6: test_default_values
def test_default_values(self):
# Check that default values are restored
# when an exception occurs in interactive mode.
a, b = T.dscalars('a', 'b')
c = a + b
func = theano.function([theano.In(a, name='first'), theano.In(b, value=1, name='second')], c)
x = func(first=1)
try:
func(second=2)
except TypeError:
assert(func(first=1) == x)
开发者ID:athiwatp,项目名称:Theano,代码行数:11,代码来源:test_function_module.py
示例7: test_deepcopy_trust_input
def test_deepcopy_trust_input(self):
a = T.dscalar() # the a is for 'anonymous' (un-named).
x, s = T.dscalars('xs')
f = function([x, In(a, value=1.0, name='a'),
In(s, value=0.0, update=s + a * x, mutable=True)],
s + a * x)
f.trust_input = True
try:
g = copy.deepcopy(f)
except NotImplementedError as e:
if e[0].startswith('DebugMode is not picklable'):
return
else:
raise
self.assertTrue(f.trust_input is g.trust_input)
f(np.asarray(2.))
self.assertRaises((ValueError, AttributeError), f, 2.)
g(np.asarray(2.))
self.assertRaises((ValueError, AttributeError), g, 2.)
开发者ID:EugenePY,项目名称:Theano,代码行数:20,代码来源:test_function_module.py
示例8: test
def test():
# multiple inputs, multiple outputs
a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = T.abs_(diff)
sqr_diff = diff ** 2
f = function([a, b], [diff, abs_diff, sqr_diff])
h, i, j = f([[0, 1], [2, 3]], [[4, 5], [6, 7]])
# default value for function arguments
a, b = T.dscalars('a', 'b')
z = a + b
f = function([a, Param(b, default=1)], z)
print f(1, b=2)
print f(1)
print f(1, 2)
# shared variable
state = shared(0)
inc = T.lscalar('inc') # state is int64 by default
accumulator = function([inc], state, updates=[(state, state + inc)])
print accumulator(300)
print state.get_value()
开发者ID:ZiangYan,项目名称:learn-new-tools,代码行数:23,代码来源:test.py
示例9: brachistochrone_functional
def brachistochrone_functional():
# define all symbols
lx, ly = T.dscalars('lx', 'ly')
fseq = T.dvector('fseq')
N = fseq.size + 1
delta_x = lx / N
iseq = T.arange(N-1)
# functional term
functional_ithterm = lambda i: T.switch(T.eq(i, 0),
T.sqrt(0.5*(delta_x**2+(fseq[0]-ly)**2)/(ly-0.5*(fseq[0]+ly))),
T.sqrt(0.5*(delta_x**2+(fseq[i]-fseq[i-1])**2)/(ly-0.5*(fseq[i]+fseq[i-1])))
)
# defining the functions
functional_parts, _ = theano.map(fn=lambda k: functional_ithterm(k), sequences=[iseq])
functional = functional_parts.sum() + T.sqrt(0.5*(delta_x**2+(0-fseq[N-2])**2)/(ly-0.5*(0+fseq[N-2])))
gfunc = T.grad(functional, fseq)
# compile the functions
time_fcn = theano.function(inputs=[fseq, lx, ly], outputs=functional)
grad_time_fcn = theano.function(inputs=[fseq, lx, ly], outputs=gfunc)
return time_fcn, grad_time_fcn
开发者ID:stephenhky,项目名称:BrachistochroneWithTheano,代码行数:24,代码来源:BrachistochroneModel.py
示例10: f
'''
Created on Jun 1, 2015
@author: xujian
'''
import theano
from theano import Param
import theano.tensor as T
from samba.dcerpc.atsvc import Third
a,b,c = T.dscalars('a','b','c')
z=(a+b)*c
f=theano.function([a,Param(b,default=0),Param(c,default=1,name="third_var")],z)
print f(1)
print f(1,2)
print f(1,third_var=2)
开发者ID:xujian-lele,项目名称:python-study,代码行数:17,代码来源:default-values-in-function.py
示例11: abs
'''
Executing multiple functions
'''
a,b = T.dmatrices('a','b')
diff = a-b
abs_diff = abs(a-b)
diff_sq = diff**2
mult = function([a,b],[diff,abs_diff,diff_sq])
print mult([[0,1],[1,2]],[[-1,2],[5,7]])
#print pp(diff)
#print pp(abs_diff)
'''
Setting a default value for an argument
So, if arg not give, take default value; else take the given value
'''
x, y = T.dscalars("x","y")
z = x+y
add = function([x,Param(y,default=1)],z)
print add(33.0)
print add(2,6)
'''
Setting names to parameters
'''
x,y,w = T.dscalars("x","y","w")
z = (x+y)*w
add_par = function([x,Param(y,default=1),Param(w,default=2,name="debalu")],z)
print add_par(33)
print add_par(33,6,debalu=5)
开发者ID:Dawny33,项目名称:Data-Mining-and-ML,代码行数:30,代码来源:logistic_and_stuff.py
示例12: defaultValue
def defaultValue():
x, y, z = T.dscalars('x', 'y', 'z')
return function([x, In(y, value=1), In(z, value=2, name='namedZ')], (x + y) * z)
开发者ID:fyabc,项目名称:TheanoProject,代码行数:3,代码来源:examples.py
示例13: tuto
def tuto():
print "\nLogistic Function 1"
print "---------------------"
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x))
logistic = theano.function([x], s)
print logistic([[0, 1], [-1, -2]])
print "\nLogistic Function 2"
print "---------------------"
s2 = (1 + T.tanh(x / 2)) / 2
logistic2 = theano.function([x], s2)
print logistic2([[0, 1], [-1, -2]])
print "\nComputing More than one Thing at the Same Time"
print "------------------------------------------------"
a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff**2
f = theano.function([a, b], [diff, abs_diff, diff_squared])
print f([[1, 1], [1, 1]], [[0, 1], [2, 3]])
print "\nSetting a Default Value for an Argument"
print "---------------------------------------"
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, In(y, value=1)], z)
print f(33)
print f(33, 2)
print "A Real Example: Logistic Regression"
print "-----------------------------------"
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]))
开发者ID:mducoffe,项目名称:TD_Deep_Learning,代码行数:89,代码来源:tuto_theano.py
示例14: test_1_examples_param_default
def test_1_examples_param_default():
x, y = T.dscalars('x', 'y')
f = theano.function([x, theano.Param(y, default=1)], x + y)
assert f(1, 2) == 3
assert f(1) == 2
开发者ID:consciousnesss,项目名称:learn_theano,代码行数:5,代码来源:test_1_examples.py
示例15: dscalars
from time import clock
from numpy import ones
from theano import Mode
from theano import function
from theano.tensor import dscalars
from theano.tensor import dmatrices
from theano.tensor import lt
from theano.tensor import mean
from theano.tensor import switch
from theano.ifelse import ifelse
a_dscalar, b_dscalar = dscalars('a', 'b')
x_dmatrix, y_dmatrix = dmatrices('x', 'y')
z_switch_dmatrix = switch(lt(a_dscalar, b_dscalar), mean(x_dmatrix), mean(y_dmatrix))
z_ifelse_dmatrix = ifelse(lt(a_dscalar, b_dscalar), mean(x_dmatrix), mean(y_dmatrix))
# Both ops build a condition over symbolic variables. IfElse takes a boolean condition and two variables as inputs.
# Switch evaluates both output variables, ifelse is lazy and only evaluates one variable with respect to the condition.
# Unless linker='vm' or linker='cvm' are used, ifelse will compute both variables and take the same computation time as switch.
f_switch = function([a_dscalar, b_dscalar, x_dmatrix, y_dmatrix], z_switch_dmatrix, mode=Mode(linker='vm'))
f_ifelse = function([a_dscalar, b_dscalar, x_dmatrix, y_dmatrix], z_ifelse_dmatrix, mode=Mode(linker='vm'))
var1 = 0.
var2 = 1.
big_mat1 = ones((10000, 1000))
big_mat2 = ones((10000, 1000))
开发者ID:SnakeHunt2012,项目名称:alchemy-stove,代码行数:31,代码来源:switch_vs_ifelse.py
示例16: abs
Computing multiple things
"""
# shorthand definition of vars
a, b = T.dmatrices('a', 'b')
diff = a - b
# NOTE: we do not need to use a, b again - diff is already a formed expression
abs_diff = abs(diff)
diff_squared = diff ** 2
f = theano.function([a, b], [diff, abs_diff, diff_squared])
print(f([[1, 1], [1, 1],], [[0, 1], [2, 3]])) # has 3 outputs
"""
Default values
"""
from theano import In
x, y = T.dscalars('x', 'y')
z = x + y
# NOTE: you can do even more complex stuff with In(), not only default values
# NOTE: default values always come AFTER the other inputs with no defaults
f = theano.function([x, In(y, value=1)], z)
print(f(33)) # 34
print(f(33, 2)) # 35
w = T.dscalar('w')
z2 = (x + y)*w
f2 = theano.function([x, In(y, value=1), In(w, value=2, name='w_by_name')], z2)
print(f2(33)) # 68
print(f2(33, 2)) # 70
print(f2(33, 0, 1)) # 33
# NOTE: the above w_by_name allows us to specify w's value at another position
# than that of the original w (just like in regular languages). w_by_name overrides
开发者ID:taimir,项目名称:deep_learning,代码行数:31,代码来源:more_examples.py
示例17: function
import numpy
import theano.tensor as T
from theano import function
from theano import In
x, y, w = T.dscalars('x', 'y', 'w')
z = (x + 2 * y) * w
f = function([In(x, value = 0), In(y, value = 0, name='y_name'), In(w, value = 1)], z)
print 'f(): ' + str(f())
print 'f(4): ' + str(f(4))
print 'f(y_name=3): ' + str(f(y_name=3))
print 'f(4, 3, 2): ' + str(f(4, 3, 2))
print 'f(4, 3): ' + str(f(4, 3))
print 'f(y_name=3, w = 4): ' + str(f(y_name=3, w = 4))
开发者ID:dyut,项目名称:UseTheano,代码行数:14,代码来源:ParamsWithDefaultValue.py
示例18:
#coding: utf-8
import theano
import theano.tensor as T
## まとめて宣言
x, y = T.dscalars("x", "y")
z = (x+2*y)**2
## zをxについて微分
gx = T.grad(z, x)
## zをyについて微分
gy = T.grad(z, y)
## まとめて
v1 = [x, y]
v2 = [x, y]
## 配列を足してもできる
v = v1+v2
# vの中の変数について順番に微分
grads = T.grad(z, v)
print grads
fgy = theano.function([x, y], grads[3])
fgx = theano.function([x, y], grads[2])
开发者ID:MasazI,项目名称:Theano_Exercise,代码行数:30,代码来源:theano_grad.py
示例19: xrange
#!/usr/bin/env python
from theano import function
import theano.tensor as T
from theano.tensor import shared_randomstreams
import numpy as np
import numpy.random
from scipy.special import gammaincinv
from numpy.linalg import norm
# tensor stand-in for np.random.RandomState
rngT = shared_randomstreams.RandomStreams()
rng = numpy.random.RandomState()
# {{{ Fastfood Params }}}
n, d = T.dscalars('n', 'd')
# transform dimensions to be a power of 2
d0, n0 = d, n
l = T.ceil(T.log2(d)) # TODO cast to int
d = 2**l
k = T.ceil(n/d) # TODO cast to int
n = d*k
# generate parameter 'matrices'
B = rng.choice([-1, 1], size=(k, d))
G = rng.normal(size=(k, d), dtype=np.float64)
PI = np.array([rng.permutation(d) for _ in xrange(k)]).T
S = np.empty((k*d, 1), dtype=np.float64)
# generate scaling matrix, S
for i in xrange(k):
for j in xrange(d):
p1 = rng.uniform(size=d)
p2 = d/2
开发者ID:kafluette,项目名称:fastfood,代码行数:31,代码来源:fastfood.py
示例20: create_spatialglimpse_function
def create_spatialglimpse_function(img_h=480, img_w=640, fovH=64, fovW=64):
fovHalfH = fovH / 2
fovHalfW = fovW / 2
glimpseInpImg = T.dtensor3('glimpseInpImg')
glimpseInpLoc_y, glimpseInpLoc_x = T.dscalars('gilY', 'gilX') # each lies between -1 and 1
glimpseLocOnImg_y = T.cast(((glimpseInpLoc_y + 1) / 2.0) * img_h, 'int32')
glimpseLocOnImg_x = T.cast(((glimpseInpLoc_x + 1) / 2.0) * img_w, 'int32')
y1 = T.max((glimpseLocOnImg_y - fovHalfH, 0))
y2 = T.min((glimpseLocOnImg_y + fovHalfH, img_h))
x1 = T.max((glimpseLocOnImg_x - fovHalfW, 0))
x2 = T.min((glimpseLocOnImg_x + fovHalfW, img_w))
y3 = T.max((glimpseLocOnImg_y - fovH, 0))
y4 = T.min((glimpseLocOnImg_y + fovH, img_h))
x3 = T.max((glimpseLocOnImg_x - fovW, 0))
x4 = T.min((glimpseLocOnImg_x + fovW, img_w))
y5 = T.max((glimpseLocOnImg_y - 2*fovH, 0))
y6 = T.min((glimpseLocOnImg_y + 2*fovH, img_h))
x5 = T.max((glimpseLocOnImg_x - 2*fovW, 0))
x6 = T.min((glimpseLocOnImg_x + 2*fovW, img_w))
glimpse1= glimpseInpImg[:, y1:y2, x1:x2]
if T.lt(glimpse1.shape[1], fovH):
pad = T.zeros((glimpse1.shape[0], fovH - glimpse1.shape[1], glimpse1.shape[2]))
if T.eq(y1, 0):
glimpse1 = T.concatenate((pad, glimpse1), 1)
else:
glimpse1 = T.concatenate((glimpse1, pad), 1)
if T.lt(glimpse1.shape[2], fovW):
pad = T.zeros((glimpse1.shape[0], glimpse1.shape[1], fovW - glimpse1.shape[2]))
if T.eq(x1, 0):
glimpse1 = T.concatenate((pad, glimpse1), 2)
else:
glimpse1 = T.concatenate((glimpse1, pad), 2)
glimpse2 = glimpseInpImg[:, y3:y4, x3:x4]
if T.lt(glimpse2.shape[1], 2*fovH):
pad = T.zeros((glimpse2.shape[0], 2*fovH - glimpse2.shape[1], glimpse2.shape[2]))
if T.eq(y3, 0):
glimpse2 = T.concatenate((pad, glimpse2), 1)
else:
glimpse2 = T.concatenate((glimpse2, pad), 1)
if T.lt(glimpse2.shape[2], 2*fovW):
pad = T.zeros((glimpse2.shape[0], glimpse2.shape[1], 2*fovW - glimpse2.shape[2]))
if T.eq(x3, 0):
glimpse2 = T.concatenate((pad, glimpse2), 2)
else:
glimpse2 = T.concatenate((glimpse2, pad), 2)
glimpse2 = T.signal.pool.pool_2d(glimpse2, (2, 2), ignore_border=True, mode='average_exc_pad')
glimpse3 = glimpseInpImg[:, y5:y6, x5:x6]
if T.lt(glimpse3.shape[1], 4*fovH):
pad = T.zeros((glimpse3.shape[0], 4*fovH - glimpse3.shape[1], glimpse3.shape[2]))
if T.eq(y5, 0):
glimpse3 = T.concatenate((pad, glimpse3), 1)
else:
glimpse3 = T.concatenate((glimpse3, pad), 1)
if T.lt(glimpse3.shape[2], 4*fovW):
pad = T.zeros((glimpse3.shape[0], glimpse3.shape[1], 4*fovW - glimpse3.shape[2]))
if T.eq(x5, 0):
glimpse3 = T.concatenate((pad, glimpse3), 2)
else:
glimpse3 = T.concatenate((glimpse3, pad), 2)
glimpse3 = pool.pool_2d(glimpse3, (4, 4), ignore_border=True, mode='average_exc_pad')
glimpse1 = T.cast(glimpse1, 'uint8')
glimpse2 = T.cast(glimpse2, 'uint8')
glimpse3 = T.cast(glimpse3, 'uint8')
fun = theano.function([glimpseInpImg, glimpseInpLoc_y, glimpseInpLoc_x], [glimpse1, glimpse2, glimpse3])
return fun
开发者ID:harshhemani,项目名称:deeplearning,代码行数:74,代码来源:SpatGlimp.py
注:本文中的theano.tensor.dscalars函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论