提交 e1613241 authored 作者: Frederic's avatar Frederic

make import better.

上级 681c5c64
#!/usr/bin/env python
# Theano tutorial
# Solution to Exercise in section 'Loop'
# 1. First example
import numpy
import theano
import theano.tensor as tensor
import theano.tensor as tt
# 1. First example
theano.config.warn.subtensor_merge_bug = False
k = tensor.iscalar("k")
A = tensor.vector("A")
k = tt.iscalar("k")
A = tt.vector("A")
def inner_fct(prior_result, A):
......@@ -18,7 +19,7 @@ def inner_fct(prior_result, A):
# Symbolic description of the result
result, updates = theano.scan(fn=inner_fct,
outputs_info=tensor.ones_like(A),
outputs_info=tt.ones_like(A),
non_sequences=A, n_steps=k)
# Scan has provided us with A ** 1 through A ** k. Keep only the last
......@@ -34,16 +35,12 @@ print power(range(10), 2)
# 2. Second example
import numpy
import theano
import theano.tensor as tensor
coefficients = theano.tensor.vector("coefficients")
x = tensor.scalar("x")
coefficients = tt.vector("coefficients")
x = tt.scalar("x")
max_coefficients_supported = 10000
# Generate the components of the polynomial
full_range = theano.tensor.arange(max_coefficients_supported)
full_range = tt.arange(max_coefficients_supported)
components, updates = theano.scan(fn=lambda coeff, power, free_var:
coeff * (free_var ** power),
sequences=[coefficients, full_range],
......@@ -59,21 +56,17 @@ print calculate_polynomial1(test_coeff, 3)
# 3. Reduction performed inside scan
import numpy
import theano
import theano.tensor as tensor
theano.config.warn.subtensor_merge_bug = False
coefficients = theano.tensor.vector("coefficients")
x = tensor.scalar("x")
coefficients = tt.vector("coefficients")
x = tt.scalar("x")
max_coefficients_supported = 10000
# Generate the components of the polynomial
full_range = theano.tensor.arange(max_coefficients_supported)
full_range = tt.arange(max_coefficients_supported)
outputs_info = tensor.as_tensor_variable(numpy.asarray(0, 'float64'))
outputs_info = tt.as_tensor_variable(numpy.asarray(0, 'float64'))
components, updates = theano.scan(fn=lambda coeff, power, prior_value, free_var:
prior_value + (coeff * (free_var ** power)),
......
......@@ -4,7 +4,7 @@
import numpy
import theano
import theano.tensor as T
import theano.tensor as tt
theano.config.floatX = 'float32'
......@@ -17,8 +17,8 @@ rng.randint(size=N, low=0, high=2).astype(theano.config.floatX))
training_steps = 10000
# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector("y")
x = tt.matrix("x")
y = tt.vector("y")
w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0]
......@@ -27,12 +27,12 @@ y.tag.test_value = D[1]
#print w.get_value(), b.get_value()
# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probabily of having a one
p_1 = 1 / (1 + tt.exp(-tt.dot(x, w) - b)) # Probabily of having a one
prediction = p_1 > 0.5 # The prediction that is done: 0 or 1
xent = -y * T.log(p_1) - (1 - y) * T.log(1 - p_1) # Cross-entropy
cost = T.cast(xent.mean(), 'float32') + \
xent = -y * tt.log(p_1) - (1 - y) * tt.log(1 - p_1) # Cross-entropy
cost = tt.cast(xent.mean(), 'float32') + \
0.01 * (w ** 2).sum() # The cost to optimize
gw, gb = T.grad(cost, [w, b])
gw, gb = tt.grad(cost, [w, b])
# Compile expressions to functions
train = theano.function(
......
......@@ -13,7 +13,7 @@
import numpy
import theano
import theano.tensor as T
import theano.tensor as tt
from theano import sandbox, Out
......@@ -28,8 +28,8 @@ rng.randint(size=N, low=0, high=2).astype(theano.config.floatX))
training_steps = 10000
# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector("y")
x = tt.matrix("x")
y = tt.vector("y")
w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0]
......@@ -38,21 +38,21 @@ y.tag.test_value = D[1]
#print w.get_value(), b.get_value()
# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probabily of having a one
p_1 = 1 / (1 + tt.exp(-tt.dot(x, w) - b)) # Probabily of having a one
prediction = p_1 > 0.5 # The prediction that is done: 0 or 1
xent = -y * T.log(p_1) - (1 - y) * T.log(1 - p_1) # Cross-entropy
cost = T.cast(xent.mean(), 'float32') + \
xent = -y * tt.log(p_1) - (1 - y) * tt.log(1 - p_1) # Cross-entropy
cost = tt.cast(xent.mean(), 'float32') + \
0.01 * (w ** 2).sum() # The cost to optimize
gw, gb = T.grad(cost, [w, b])
gw, gb = tt.grad(cost, [w, b])
"""
# Compile expressions to functions
train = theano.function(
inputs=[x, y],
outputs=[Out(theano.sandbox.cuda.basic_ops.gpu_from_host(T.cast(prediction, 'float32')),borrow=True), Out(theano.sandbox.cuda.basic_ops.gpu_from_host(T.cast(xent, 'float32')), borrow=True)],
outputs=[Out(theano.sandbox.cuda.basic_ops.gpu_from_host(tt.cast(prediction, 'float32')),borrow=True), Out(theano.sandbox.cuda.basic_ops.gpu_from_host(tt.cast(xent, 'float32')), borrow=True)],
updates={w: w - 0.01 * gw, b: b - 0.01 * gb},
name="train")
predict = theano.function(inputs=[x], outputs=Out(theano.sandbox.cuda.basic_ops.gpu_from_host(T.cast(prediction, 'float32')), borrow=True),
predict = theano.function(inputs=[x], outputs=Out(theano.sandbox.cuda.basic_ops.gpu_from_host(tt.cast(prediction, 'float32')), borrow=True),
name="predict")
"""
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论