blog_20160808_1_7193279 24行 Python
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
import theano
import theano.tensor as T
from theano import function
from theano import shared

hottness = shared(0)
visitWeight = T.iscalar("visitWeight")
calculateHottness = function([visitWeight], hottness, updates=[(hottness, hottness + visitWeight)])
minusValue = T.iscalar("minusValue")
decreaseHottness = function([minusValue], hottness, updates=[(hottness, hottness - minusValue)])


visitWeight1 = 5
calculateHottness(visitWeight1)
print("hottness=%d" % hottness.get_value())
visitWeight2 = 10
calculateHottness(visitWeight2)
print("hottness=%d" % hottness.get_value())
minusValue1 = 3
decreaseHottness(minusValue1)
print("hottness=%d" % hottness.get_value())

hottness.set_value(0)
print("hottness=%d" % hottness.get_value())
blog_20160808_2_638843 4行 Python
Raw
 1
 2
 3
 4
newHottness = hottness + visitWeight
score = T.scalar(dtype=hottness.dtype)
calculateScore = function([visitWeight, score], newHottness, givens=[(hottness, score)])
print("score=%d; hottness=%d!" % (calculateScore(5, 100), hottness.get_value()))
blog_20160808_3_7988884 4行 Python
Raw
 1
 2
 3
 4
rateScores = shared(0)
calculateRateScores = calculateHottness.copy(swap={hottness: rateScores})
calculateRateScores(5)
print("rateScores=%d" % rateScores.get_value())
blog_20160808_4_2307283 13行 Python
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
from theano.tensor.shared_randomstreams import RandomStreams
from theano import function

randStrm = RandomStreams(seed=299)
uniformValue = randStrm.uniform((2, 2))
normalValue = randStrm.normal((2, 2))
getUniformValue = function([], uniformValue)
getNormalValue = function([], normalValue, no_default_updates=True)
getRandomNumber = function([], uniformValue + uniformValue - 2 * uniformValue)

print(getUniformValue())
print(getNormalValue())
print(getRandomNumber())
blog_20160808_5_6102293 9行 Python
Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
from theano import In
from theano import function
from theano import tensor as T

x, y = T.dscalars("x", "y")
z = x + y
add = function([x, In(y, value=100)], z)

print("add=%d" % add(20))