本文整理汇总了Python中pylearn2.format.target_format.OneHotFormatter类的典型用法代码示例。如果您正苦于以下问题:Python OneHotFormatter类的具体用法?Python OneHotFormatter怎么用?Python OneHotFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OneHotFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_one_hot_formatter
def check_one_hot_formatter(seed, max_labels, dtype, ncases):
rng = numpy.random.RandomState(seed)
fmt = OneHotFormatter(max_labels=max_labels, dtype=dtype)
integer_labels = rng.random_integers(0, max_labels - 1, size=ncases)
one_hot_labels = fmt.format(integer_labels)
assert len(zip(*one_hot_labels.nonzero())) == ncases
for case, label in enumerate(integer_labels):
assert one_hot_labels[case, label] == 1
示例2: check_one_hot_formatter_symbolic
def check_one_hot_formatter_symbolic(seed, max_labels, dtype, ncases):
rng = numpy.random.RandomState(seed)
fmt = OneHotFormatter(max_labels=max_labels, dtype=dtype)
integer_labels = rng.random_integers(0, max_labels - 1, size=ncases)
x = theano.tensor.vector(dtype='int64')
y = fmt.theano_expr(x)
f = theano.function([x], y)
one_hot_labels = f(integer_labels)
assert len(zip(*one_hot_labels.nonzero())) == ncases
for case, label in enumerate(integer_labels):
assert one_hot_labels[case, label] == 1
示例3: generate_datasets
def generate_datasets(inputs):
targets = np.zeros(inputs.shape[0]).astype('int')
targets[::2] = 1 # every second target is class 1 others class 0
inputs[targets == 1] = inputs[targets == 1] + 1
target_formatter = OneHotFormatter(2)
targets_one_hot = target_formatter.format(targets)
train_set = VolumetricDenseDesignMatrix(topo_view=inputs[0:50],
y=targets_one_hot[0:50], axes=('b', 0, 1, 2, 'c'))
valid_set = VolumetricDenseDesignMatrix(topo_view=inputs[50:75],
y=targets_one_hot[50:75], axes=('b', 0, 1, 2, 'c'))
test_set = VolumetricDenseDesignMatrix(topo_view=inputs[75:100],
y=targets_one_hot[75:100], axes=('b', 0, 1, 2, 'c'))
return train_set, valid_set, test_set
示例4: nll
def nll(self, data):
X, Y = data
z = self.score(X)
z = z - z.max(axis=1).dimshuffle(0, 'x')
log_prob = z - T.log(T.exp(z).sum(axis=1).dimshuffle(0, 'x'))
Y = OneHotFormatter(self.dict_size).theano_expr(Y)
Y = Y.reshape((Y.shape[0], Y.shape[2]))
#import ipdb
#ipdb.set_trace()
log_prob_of = (Y * log_prob).sum(axis=1)
assert log_prob_of.ndim == 1
rval = as_floatX(log_prob_of.mean())
return - rval
示例5: test_one_hot_formatter_simple
def test_one_hot_formatter_simple():
def check_one_hot_formatter(seed, max_labels, dtype, ncases):
rng = numpy.random.RandomState(seed)
fmt = OneHotFormatter(max_labels=max_labels, dtype=dtype)
integer_labels = rng.random_integers(0, max_labels - 1, size=ncases)
one_hot_labels = fmt.format(integer_labels)
assert len(list(zip(*one_hot_labels.nonzero()))) == ncases
for case, label in enumerate(integer_labels):
assert one_hot_labels[case, label] == 1
rng = numpy.random.RandomState(0)
for seed, dtype in enumerate(all_types):
yield (check_one_hot_formatter, seed, rng.random_integers(1, 30), dtype, rng.random_integers(1, 100))
fmt = OneHotFormatter(max_labels=10)
assert fmt.format(numpy.zeros((1, 1), dtype="uint8")).shape == (1, 1, 10)
示例6: _transform_single_channel_data
def _transform_single_channel_data(self, X, y):
windowed_X = np.reshape(X, (-1, self.window_size))
windowed_y = np.reshape(y, (-1, self.window_size))
# Format the target into proper format
sum_y = np.sum(windowed_y, axis=1)
sum_y[sum_y > 0] = 1
# Duplicate the labels for all channels
dup_y = np.tile(sum_y, self.n_channels)
one_hot_formatter = OneHotFormatter(max_labels=self.n_classes)
hot_y = one_hot_formatter.format(dup_y)
return windowed_X, hot_y, None
示例7: __init__
def __init__(self, space, rng=None):
super(OneHotDistribution, self).__init__(space)
self.dim = space.get_total_dimension()
self.formatter = OneHotFormatter(self.dim, dtype=space.dtype)
self.rng = RandomStreams() if rng is None else rng
示例8: test_dtype_errors
def test_dtype_errors():
# Try to call theano_expr with a bad label dtype.
raised = False
fmt = OneHotFormatter(max_labels=50)
try:
fmt.theano_expr(theano.tensor.vector(dtype=theano.config.floatX))
except TypeError:
raised = True
assert raised
# Try to call format with a bad label dtype.
raised = False
try:
fmt.format(numpy.zeros(10, dtype='float64'))
except TypeError:
raised = True
assert raised
示例9: check_one_hot_formatter
def check_one_hot_formatter(seed, max_labels, dtype, ncases, nmultis):
rng = numpy.random.RandomState(seed)
fmt = OneHotFormatter(max_labels=max_labels, dtype=dtype)
integer_labels = rng.random_integers(0, max_labels - 1, size=ncases * nmultis).reshape(ncases, nmultis)
one_hot_labels = fmt.format(integer_labels, mode="merge")
# n_ones was expected to be equal to ncases * nmultis if integer_labels
# do not contain duplicated tags. (i.e., those labels like
# [1, 2, 2, 3, 5, 6].) Because that we are not depreciating this kind
# of duplicated labels, which allows different cases belong to
# different number of classes, and those duplicated tags will only
# activate one neuron in the k-hot representation, we need to use
# numpy.unique() here to eliminate those duplications while counting
# "1"s in the final k-hot representation.
n_ones = numpy.concatenate([numpy.unique(l) for l in integer_labels])
assert len(list(zip(*one_hot_labels.nonzero()))) == len(n_ones)
for case, label in enumerate(integer_labels):
assert numpy.sum(one_hot_labels[case, label]) == nmultis
示例10: OneHotDistribution
class OneHotDistribution(Distribution):
"""Randomly samples from a distribution of one-hot vectors."""
def __init__(self, space, rng=None):
super(OneHotDistribution, self).__init__(space)
self.dim = space.get_total_dimension()
self.formatter = OneHotFormatter(self.dim, dtype=space.dtype)
self.rng = RandomStreams() if rng is None else rng
def sample(self, n):
idxs = self.rng.random_integers((n, 1), low=0, high=self.dim - 1)
return self.formatter.theano_expr(idxs, mode='concatenate')
示例11: _transform_multi_channel_data
def _transform_multi_channel_data(self, X, y):
# Data partitioning
parted_X, parted_y = self._partition_data(X=X, y=y, partition_size=self.window_size)
transposed_X = np.transpose(parted_X, [0, 2, 1])
converted_X = np.reshape(transposed_X, (transposed_X.shape[0],
transposed_X.shape[1],
1,
transposed_X.shape[2]))
# Create view converter
view_converter = DefaultViewConverter(shape=self.sample_shape,
axes=('b', 0, 1, 'c'))
# Convert data into a design matrix
view_converted_X = view_converter.topo_view_to_design_mat(converted_X)
assert np.all(converted_X == view_converter.design_mat_to_topo_view(view_converted_X))
# Format the target into proper format
sum_y = np.sum(parted_y, axis=1)
sum_y[sum_y > 0] = 1
one_hot_formatter = OneHotFormatter(max_labels=self.n_classes)
hot_y = one_hot_formatter.format(sum_y)
return view_converted_X, hot_y, view_converter
示例12: ConditionalGeneratorTestCase
class ConditionalGeneratorTestCase(unittest.TestCase):
def setUp(self):
self.noise_dim = 10
self.num_labels = 10
self.condition_dtype = 'uint8'
self.condition_space = VectorSpace(dim=self.num_labels, dtype=self.condition_dtype)
self.condition_formatter = OneHotFormatter(self.num_labels, dtype=self.condition_dtype)
self.condition_distribution = OneHotDistribution(self.condition_space)
# TODO this nvis stuff is dirty. The ConditionalGenerator should handle it
self.mlp_nvis = self.noise_dim + self.num_labels
self.mlp_nout = 1
# Set up model
self.mlp = MLP(nvis=self.mlp_nvis, layers=[Linear(self.mlp_nout, 'out', irange=0.1)])
self.G = ConditionalGenerator(input_condition_space=self.condition_space,
condition_distribution=self.condition_distribution,
noise_dim=self.noise_dim,
mlp=self.mlp)
def test_conditional_generator_input_setup(self):
"""Check that conditional generator correctly sets up composite
input layer."""
# Feedforward: We want the net to ignore the noise and simply
# convert the one-hot vector to a number
weights = np.concatenate([np.zeros((self.mlp_nout, self.noise_dim)),
np.array(range(self.num_labels)).reshape((1, -1)).repeat(self.mlp_nout, axis=0)],
axis=1).T.astype(theano.config.floatX)
self.mlp.layers[0].set_weights(weights)
inp = (T.matrix(), T.matrix(dtype=self.condition_dtype))
f = theano.function(inp, self.G.mlp.fprop(inp))
assert_array_equal(
f(np.random.rand(self.num_labels, self.noise_dim).astype(theano.config.floatX),
self.condition_formatter.format(np.array(range(self.num_labels)))),
np.array(range(self.num_labels)).reshape(self.num_labels, 1))
def test_sample_noise(self):
"""Test barebones noise sampling."""
n = T.iscalar()
cond_inp = self.condition_distribution.sample(n)
sample_and_noise = theano.function([n], self.G.sample_and_noise(cond_inp, all_g_layers=True)[1])
print sample_and_noise(15)
示例13: setUp
def setUp(self):
self.noise_dim = 10
self.num_labels = 10
self.condition_dtype = 'uint8'
self.condition_space = VectorSpace(dim=self.num_labels, dtype=self.condition_dtype)
self.condition_formatter = OneHotFormatter(self.num_labels, dtype=self.condition_dtype)
self.condition_distribution = OneHotDistribution(self.condition_space)
# TODO this nvis stuff is dirty. The ConditionalGenerator should handle it
self.mlp_nvis = self.noise_dim + self.num_labels
self.mlp_nout = 1
# Set up model
self.mlp = MLP(nvis=self.mlp_nvis, layers=[Linear(self.mlp_nout, 'out', irange=0.1)])
self.G = ConditionalGenerator(input_condition_space=self.condition_space,
condition_distribution=self.condition_distribution,
noise_dim=self.noise_dim,
mlp=self.mlp)
示例14: test_bad_arguments
def test_bad_arguments():
# Make sure an invalid max_labels raises an error.
raised = False
try:
fmt = OneHotFormatter(max_labels=-10)
except ValueError:
raised = True
assert raised
raised = False
try:
fmt = OneHotFormatter(max_labels='10')
except ValueError:
raised = True
assert raised
# Make sure an invalid dtype identifier raises an error.
raised = False
try:
fmt = OneHotFormatter(max_labels=10, dtype='invalid')
except TypeError:
raised = True
assert raised
# Make sure an invalid ndim raises an error for format().
fmt = OneHotFormatter(max_labels=10)
raised = False
try:
fmt.format(numpy.zeros((2, 3), dtype='int32'))
except ValueError:
raised = True
assert raised
# Make sure an invalid ndim raises an error for theano_expr().
raised = False
try:
fmt.theano_expr(theano.tensor.imatrix())
except ValueError:
raised = True
assert raised
示例15: __init__
def __init__(self, max_labels, dim, **kwargs):
"""
Initialize an IndexSpace.
Parameters
----------
max_labels : int
The number of possible classes/labels. This means that
all labels should be < max_labels. Example: For MNIST
there are 10 numbers and hence max_labels = 10.
dim : int
The number of indices in one space e.g. for MNIST there is
one target label and hence dim = 1. If we have an n-gram
of word indices as input to a neurel net language model, dim = n.
kwargs: passes on to superclass constructor
"""
super(IndexSpace, self).__init__(**kwargs)
self.max_labels = max_labels
self.dim = dim
self.formatter = OneHotFormatter(self.max_labels)