def _build_validate_function(self):
print 'building validate function'
t1 = datetime.datetime.now()
data = self.val_data
captions = self.val_data_captions
self._index_im_val = T.vector(dtype='int32') # index to the minibatch
self._index_cap_val = T.vector(dtype='int32')
self._cap_len_val = T.scalar(dtype='int32')
self._validate_function = theano.function(inputs=[self._index_im_val, self._index_cap_val, self._cap_len_val, self._run_steps],
outputs=[self._kl_final, self._logpxz, self._log_likelihood],
updates=self._updates_train,
givens={
self._x: data[self._index_im_val],
self._y: captions[self._index_cap_val,0:self._cap_len_val]
})
t2 = datetime.datetime.now()
print (t2-t1)
python类scalar()的实例源码
def _build_validate_function(self, isVal=True):
print 'building validate function'
t1 = datetime.datetime.now()
if isVal:
data = self.val_data
else:
data = self.test_data
self._index_val = T.scalar(dtype='int32') # index to the minibatch
self._validate_function = theano.function(inputs=[self._index_val, self._run_steps],
outputs=[self._kl_final, self._logpxz, self._log_likelihood],
updates=self._updates_train,
givens={
self._x: data[(self._index_val * batch_size):((self._index_val + 1) * batch_size)].astype(floatX)
})
t2 = datetime.datetime.now()
print (t2-t1)
def build_model(self):
import theano.tensor as T
self.x = T.ftensor4('x')
self.y = T.lvector('y')
self.lr = T.scalar('lr')
net = build_model_resnet50(input_shape=(None, 3, 224, 224))
if self.verbose: print('Total number of layers:', len(lasagne.layers.get_all_layers(net['prob'])))
self.output_layer = net['prob']
from lasagne.layers import get_output
self.output = lasagne.layers.get_output(self.output_layer, self.x, deterministic=False)
self.cost = lasagne.objectives.categorical_crossentropy(self.output, self.y).mean()
from lasagne.objectives import categorical_accuracy
self.error = 1-categorical_accuracy(self.output, self.y, top_k=1).mean()
self.error_top_5 = 1-categorical_accuracy(self.output, self.y, top_k=5).mean()
def build_model(self):
import theano.tensor as T
self.x = T.ftensor4('x')
self.y = T.lvector('y')
self.lr = T.scalar('lr')
net = build_model_vgg16(input_shape=(None, 3, 224, 224), verbose=self.verbose)
self.output_layer = net['prob']
from lasagne.layers import get_output
self.output = lasagne.layers.get_output(self.output_layer, self.x, deterministic=False)
self.cost = lasagne.objectives.categorical_crossentropy(self.output, self.y).mean()
from lasagne.objectives import categorical_accuracy
self.error = 1-categorical_accuracy(self.output, self.y, top_k=1).mean()
self.error_top_5 = 1-categorical_accuracy(self.output, self.y, top_k=5).mean()
def build_model(self):
import theano.tensor as T
self.x = T.ftensor4('x')
self.y = T.lvector('y')
self.lr = T.scalar('lr')
net = build_model_resnet152(input_shape=(None, 3, 224, 224))
self.output_layer = net['prob']
from lasagne.layers import get_output
self.output = lasagne.layers.get_output(self.output_layer, self.x, deterministic=False)
self.cost = lasagne.objectives.categorical_crossentropy(self.output, self.y).mean()
from lasagne.objectives import categorical_accuracy
self.error = 1-categorical_accuracy(self.output, self.y, top_k=1).mean()
self.error_top_5 = 1-categorical_accuracy(self.output, self.y, top_k=5).mean()
def compile_iter_fns(self, *args, **kwargs):
import theano
import time
start=time.time()
# f_pred_prob = theano.function([x, mask], pred, name='f_pred_prob')
self.f_pred = theano.function([self.x, self.mask], self.pred.argmax(axis=1), name='f_pred')
# f_cost = theano.function([x, mask, y], cost, name='f_cost')
import theano.tensor as tensor
grads = tensor.grad(self.cost, wrt=list(self.tparams.values()))
# f_grad = theano.function([x, mask, y], grads, name='f_grad')
lr = tensor.scalar(name='lr')
from theanompi.models.lstm import adadelta
self.f_grad_shared, self.f_update = adadelta(lr, self.tparams, grads,
self.x, self.mask, self.y, self.cost)
if self.rank==0: print('compile time %.3f' % (time.time()-start))
def __init__(self, batch_size, emb_X, lstm_params, output_size):
super().__init__(batch_size)
self.inputs = [T.imatrix('input'), T.matrix('mask')]
self.target = T.matrix('target')
l = InputLayer((batch_size, None), self.inputs[0])
l_mask = InputLayer((batch_size, None), self.inputs[1])
l = EmbeddingLayer(l, emb_X.shape[0], emb_X.shape[1], W=emb_X)
for lstm_param in lstm_params:
l = LSTMLayer(
l, lstm_param, grad_clipping=100, nonlinearity=tanh, mask_input=l_mask, only_return_final=True
)
l = DenseLayer(l, output_size, nonlinearity=identity)
self.pred = get_output(l, deterministic=True)
self.loss = T.mean(aggregate(squared_error(get_output(l), self.target)))
params = get_all_params(l, trainable=True)
self.update_params = [T.scalar('learning_rate')]
self.updates = rmsprop(self.loss, params, learning_rate=self.update_params[0])
self.metrics = {'train': [rmse], 'val': [rmse]}
self.network = l
self.compile()
def train_one_batch(self):
self.actions = tensor.vector(name='actions', dtype='int64')
self.y = tensor.vector(name='y', dtype=theano.config.floatX)
cost = self.output_vector[self.actions].sum() / self.actions.shape[0]
coef = (self.y - self.output_vector[self.actions]).sum() / self.actions.shape[0]
grads = tensor.grad(cost, wrt=self.params.values())
grads = [coef*t for t in grads]
lr = tensor.scalar(name='lr')
f_update = self._adadelta(lr, self.params, grads)
def update_function(states, actions, y, yita):
f_update(numpy.array(yita, dtype=theano.config.floatX))
return
return update_function
def setupVariables(self):
floatX = theano.config.floatX # @UndefinedVariable
# params
self.learning_rate = T.scalar('learning_rate',dtype=floatX)
self.momentum = T.scalar('momentum',dtype=floatX)
# input
self.tvIndex = T.lscalar() # index to a [mini]batch
#self.tvIndex.tag.test_value = 10
self.tvX = self.descrNet.inputVar
# targets
self.tvY = T.ivector('y')
self.tvYr = T.tensor4('yr')
self.tvPairIdx = T.imatrix('pairIdx')
self.tvPairLabels = T.ivector('pairLabels')
self.tvTripletIdx = T.imatrix('tripletIdx')
self.tvTripletThresh = T.scalar('tripletThresh')
self.tvTripletPoolIdx = T.imatrix('tripletPoolIdx')
self.tvTripletPoolThresh = T.scalar('tripletPoolThresh')
self.tvPosTripletPoolSize = T.iscalar('posTripletPoolSize')
self.tvNegTripletPoolSize = T.iscalar('negTripletPoolSize')
def get_update(Ws_s, bs_s):
x, fx = train.get_model(Ws_s, bs_s)
# Ground truth (who won)
y = T.vector('y')
# Compute loss (just log likelihood of a sigmoid fit)
y_pred = sigmoid(fx)
loss = -( y * T.log(y_pred) + (1 - y) * T.log(1 - y_pred)).mean()
# Metrics on the number of correctly predicted ones
frac_correct = ((fx > 0) * y + (fx < 0) * (1 - y)).mean()
# Updates
learning_rate_s = T.scalar(dtype=theano.config.floatX)
momentum_s = T.scalar(dtype=theano.config.floatX)
updates = train.nesterov_updates(loss, Ws_s + bs_s, learning_rate_s, momentum_s)
f_update = theano.function(
inputs=[x, y, learning_rate_s, momentum_s],
outputs=[loss, frac_correct],
updates=updates,
)
return f_update
def test_local_gpu_elemwise_careduce():
x = theano.tensor.matrix()
o = (x * x).sum()
f = theano.function([x], o, mode=mode_with_gpu)
topo = f.maker.fgraph.toposort()
assert len(topo) == 3
assert topo[1].op.pre_scalar_op == theano.scalar.sqr
data = numpy.random.rand(3, 4).astype(theano.config.floatX)
utt.assert_allclose(f(data), (data * data).sum())
o = (x * x).sum(axis=1)
f = theano.function([x], o, mode=mode_with_gpu)
topo = f.maker.fgraph.toposort()
assert len(topo) == 3
assert topo[1].op.pre_scalar_op == theano.scalar.sqr
utt.assert_allclose(f(data), (data * data).sum(axis=1))
def test_printing_scan():
# Skip test if pydot is not available.
if not theano.printing.pydot_imported:
raise SkipTest('pydot not available')
def f_pow2(x_tm1):
return 2 * x_tm1
state = theano.tensor.scalar('state')
n_steps = theano.tensor.iscalar('nsteps')
output, updates = theano.scan(f_pow2,
[],
state,
[],
n_steps=n_steps,
truncate_gradient=-1,
go_backwards=False)
f = theano.function([state, n_steps],
output,
updates=updates,
allow_input_downcast=True)
theano.printing.pydotprint(output, scan_graphs=True)
theano.printing.pydotprint(f, scan_graphs=True)
def test_output_order_sorted(self):
'''
Tests that the output keys are sorted correctly.
'''
x = T.scalar('x')
y = T.scalar('y')
z = T.scalar('z')
e1 = T.scalar('1')
e2 = T.scalar('2')
f = theano.function([x, y, z, e1, e2], outputs={'x': x, 'y': y, 'z': z,
'1': e1, '2': e2})
assert '1' in str(f.outputs[0])
assert '2' in str(f.outputs[1])
assert 'x' in str(f.outputs[2])
assert 'y' in str(f.outputs[3])
assert 'z' in str(f.outputs[4])
def test_output_list_still_works(self):
'''
Test that theano.function works if outputs is a list.
'''
x = T.scalar('x')
f = theano.function([x], outputs=[x * 3, x * 2, x * 4, x])
result = f(5.0)
assert result[0] == 15.0
assert result[1] == 10.0
assert result[2] == 20.0
assert result[3] == 5.0
def test_debug_mode_dict(self):
'''
Tests that debug mode works where outputs is a dictionary.
'''
x = T.scalar('x')
f = theano.function([x], outputs={'1': x, '2': 2 * x,
'3': 3 * x}, mode="DEBUG_MODE")
result = f(3.0)
assert result['1'] == 3.0
assert result['2'] == 6.0
assert result['3'] == 9.0
def test_key_string_requirement(self):
'''
Tests that an exception is thrown if a non-string key is used in
the outputs dictionary.
'''
x = T.scalar('x')
try:
theano.function([x], outputs={1.0: x})
raise Exception("Did not throw exception with 1.0 as only key")
except AssertionError:
pass
try:
theano.function([x], outputs={1.0: x, "a": x**2})
raise Exception("Did not throw exception with 1.0 as one key")
except AssertionError:
pass
try:
theano.function([x], outputs={(1, "b"): x, 1.0: x**2})
raise Exception("Did not throw exception with tuple as key")
except AssertionError:
pass
def test_jacobian_disconnected_inputs():
"""
Test that disconnected inputs are properly handled by jacobian.
"""
v1 = tensor.vector()
v2 = tensor.vector()
jacobian_v = theano.gradient.jacobian(1 + v1, v2, disconnected_inputs='ignore')
func_v = theano.function([v1, v2], jacobian_v)
val = numpy.arange(4.0).astype(theano.config.floatX)
assert numpy.allclose(func_v(val, val), numpy.zeros((4, 4)))
s1 = tensor.scalar()
s2 = tensor.scalar()
jacobian_s = theano.gradient.jacobian(1 + s1, s2, disconnected_inputs='ignore')
func_s = theano.function([s2], jacobian_s)
val = numpy.array(1.0).astype(theano.config.floatX)
assert numpy.allclose(func_s(val), numpy.zeros(1))
def make_node(self, x, index):
x = as_sparse_variable(x)
assert x.format in ["csr", "csc"]
assert len(index) == 2
input_op = [x]
for ind in index:
if isinstance(ind, slice):
raise Exception("GetItemScalar called with a slice as index!")
# in case of indexing using int instead of theano variable
elif isinstance(ind, integer_types):
ind = theano.tensor.constant(ind)
input_op += [ind]
# in case of indexing using theano variable
elif ind.ndim == 0:
input_op += [ind]
else:
raise NotImplemented()
return gof.Apply(self, input_op, [tensor.scalar(dtype=x.dtype)])
def make_node(self, x, y):
x, y = as_sparse_variable(x), tensor.as_tensor_variable(y)
assert x.format in ["csr", "csc"]
# upcast the tensor. Is the cast of sparse done implemented?
dtype = scalar.upcast(x.type.dtype, y.type.dtype)
# The magic number two here arises because L{scipy.sparse}
# objects must be matrices (have dimension 2)
# Broadcasting of the sparse matrix is not supported.
# We support nd == 0 used by grad of SpSum()
assert y.type.ndim in [0, 2]
out = SparseType(dtype=dtype,
format=x.type.format)()
return gof.Apply(self, [x, y], [out])
def structured_monoid(tensor_op):
# Generic operation to perform many kinds of monoid element-wise
# operations on the non-zeros of a sparse matrix.
# The first parameter must always be a sparse matrix. The other parameters
# must be scalars which will be passed as argument to the tensor_op.
def decorator(f):
def wrapper(*args):
x = as_sparse_variable(args[0])
assert x.format in ["csr", "csc"]
xs = [scalar.as_scalar(arg) for arg in args[1:]]
data, ind, ptr, shape = csm_properties(x)
data = tensor_op(data, *xs)
return CSM(x.format)(data, ind, ptr, shape)
wrapper.__name__ = str(tensor_op.scalar_op)
return wrapper
return decorator
def make_node(self, a, b):
a = as_sparse_variable(a)
assert a.format in ["csr", "csc", "bsr"]
if not _is_sparse_variable(a):
raise TypeError('First argument must be of type SparseVariable '
'or SparseConstant')
dtype_out = scalar.upcast(a.type.dtype, b.type.dtype)
if b.type.ndim != 2:
raise NotImplementedError('non-matrix b')
if _is_sparse_variable(b):
return gof.Apply(self, [a, b],
[SparseType(a.type.format, dtype_out)()])
else:
return gof.Apply(self, [a, b],
[tensor.tensor(dtype_out,
(False, b.type.broadcastable[1]))])
def perform(self, node, inputs, outputs):
(a_indices, a_indptr, b, g_ab) = inputs
(out,) = outputs
g_a_data = numpy.zeros(a_indices.shape, dtype=g_ab.dtype)
for j in xrange(len(a_indptr) - 1):
ind0 = a_indptr[j]
ind1 = a_indptr[j + 1]
for i_idx in xrange(ind0, ind1):
i = a_indices[i_idx]
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
dot_val = numpy.dot(g_ab[i], b[j].T)
if isinstance(dot_val, scipy.sparse.spmatrix):
dot_val = dot_val[0, 0]
g_a_data[i_idx] = dot_val
out[0] = g_a_data
def make_node(self, alpha, x, y, z):
if not _is_sparse_variable(x) and not _is_sparse_variable(y):
# If x and y are tensor, we don't want to use this class
# We should use Dot22 and Gemm in that case.
raise TypeError(x)
dtype_out = scalar.upcast(alpha.type.dtype, x.type.dtype,
y.type.dtype, z.type.dtype)
alpha = tensor.as_tensor_variable(alpha)
z = tensor.as_tensor_variable(z)
assert z.ndim == 2
assert alpha.type.broadcastable == (True,) * alpha.ndim
if not _is_sparse_variable(x):
x = tensor.as_tensor_variable(x)
assert y.format in ["csr", "csc"]
assert x.ndim == 2
if not _is_sparse_variable(y):
y = tensor.as_tensor_variable(y)
assert x.format in ["csr", "csc"]
assert y.ndim == 2
return gof.Apply(self, [alpha, x, y, z],
[tensor.tensor(dtype=dtype_out,
broadcastable=(False, False))])
def test_c(self):
if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.")
for dtype in ["floatX", "complex64", "complex128", "int8", "uint8"]:
self.with_linker(gof.CLinker(), scalar.add, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.mul, dtype=dtype)
for dtype in ["floatX", "int8", "uint8"]:
self.with_linker(gof.CLinker(), scalar.minimum, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.maximum, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.and_, dtype=dtype,
tensor_op=tensor.all)
self.with_linker(gof.CLinker(), scalar.or_, dtype=dtype,
tensor_op=tensor.any)
for dtype in ["int8", "uint8"]:
self.with_linker(gof.CLinker(), scalar.or_, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.and_, dtype=dtype)
self.with_linker(gof.CLinker(), scalar.xor, dtype=dtype)
def test_infer_shape(self, dtype=None, pre_scalar_op=None):
if dtype is None:
dtype = theano.config.floatX
for xsh, tosum in self.cases:
x = self.type(dtype, [(entry == 1) for entry in xsh])('x')
if pre_scalar_op is not None:
x = pre_scalar_op(x)
if tosum is None:
tosum = list(range(len(xsh)))
xv = numpy.asarray(numpy.random.rand(*xsh), dtype=dtype)
d = {}
if pre_scalar_op is not None:
xv = x.eval({x.owner.inputs[0]: xv})
d = {pre_scalar_op: pre_scalar_op}
self._compile_and_check([x],
[self.op(scalar.add, axis=tosum, *d)(x)],
[xv], self.op,
["local_cut_useless_reduce"],
warn=0 not in xsh)
def setUp(self):
self.test_vals = [numpy.array(x, dtype=config.floatX) for x in [
0,
1,
numpy.nan,
numpy.inf,
-numpy.inf,
[numpy.nan, numpy.inf, -numpy.inf, 0, 1, -1],
]]
self.scalar = tensor.scalar()
self.vector = tensor.vector()
self.mode = get_default_mode()
if isinstance(self.mode, theano.compile.debugmode.DebugMode):
# Disable the check preventing usage of NaN / Inf values.
self.mode = copy(self.mode)
self.mode.check_isfinite = False
def test_mean_default_dtype(self):
"""
Test the default dtype of a mean().
"""
# We try multiple axis combinations even though axis should not matter.
axes = [None, 0, 1, [], [0], [1], [0, 1]]
for idx, dtype in enumerate(imap(str, theano.scalar.all_types)):
axis = axes[idx % len(axes)]
x = tensor.matrix(dtype=dtype)
m = x.mean(axis=axis)
if dtype in tensor.discrete_dtypes and axis != []:
assert m.dtype == 'float64'
else:
assert m.dtype == dtype, (m, m.dtype, dtype)
f = theano.function([x], m)
data = numpy.random.rand(3, 4) * 10
data = data.astype(dtype)
f(data)
def test_prod_without_zeros_default_dtype(self):
"""
Test the default dtype of a ProdWithoutZeros().
"""
# We try multiple axis combinations even though axis should not matter.
axes = [None, 0, 1, [], [0], [1], [0, 1]]
for idx, dtype in enumerate(imap(str, theano.scalar.all_types)):
axis = axes[idx % len(axes)]
x = ProdWithoutZeros(axis=axis)(tensor.matrix(dtype=dtype))
assert x.dtype == dict(
int8='int64',
int16='int64',
int32='int64',
uint8='uint64',
uint16='uint64',
uint32='uint64',
).get(dtype, dtype)
def test_prod_without_zeros_custom_dtype(self):
"""
Test ability to provide your own output dtype for a ProdWithoutZeros().
"""
# We try multiple axis combinations even though axis should not matter.
axes = [None, 0, 1, [], [0], [1], [0, 1]]
idx = 0
for input_dtype in imap(str, theano.scalar.all_types):
x = tensor.matrix(dtype=input_dtype)
for output_dtype in imap(str, theano.scalar.all_types):
axis = axes[idx % len(axes)]
prod_woz_var = ProdWithoutZeros(
axis=axis, dtype=output_dtype)(x)
assert prod_woz_var.dtype == output_dtype
idx += 1
if ('complex' in output_dtype or
'complex' in input_dtype):
continue
f = theano.function([x], prod_woz_var)
data = numpy.random.rand(2, 3) * 3
data = data.astype(input_dtype)
f(data)
def test_infer_shape(self):
for s_left, s_right in [((5, 6), (5, 6)),
((5, 6), (5, 1)),
((5, 6), (1, 6)),
((5, 1), (5, 6)),
((1, 6), (5, 6)),
((2, 3, 4, 5), (2, 3, 4, 5)),
((2, 3, 4, 5), (2, 3, 1, 5)),
((2, 3, 4, 5), (1, 3, 4, 5)),
((2, 1, 4, 5), (2, 3, 4, 5)),
((2, 3, 4, 1), (2, 3, 4, 5))]:
dtype = theano.config.floatX
t_left = TensorType(dtype, [(entry == 1) for entry in s_left])()
t_right = TensorType(dtype, [(entry == 1) for entry in s_right])()
t_left_val = numpy.zeros(s_left, dtype=dtype)
t_right_val = numpy.zeros(s_right, dtype=dtype)
self._compile_and_check([t_left, t_right],
[Elemwise(scalar.add)(t_left, t_right)],
[t_left_val, t_right_val], Elemwise)