本文整理汇总了Python中blocks.bricks.NDimensionalSoftmax类的典型用法代码示例。如果您正苦于以下问题:Python NDimensionalSoftmax类的具体用法?Python NDimensionalSoftmax怎么用?Python NDimensionalSoftmax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NDimensionalSoftmax类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ShallowFusionReadout
class ShallowFusionReadout(Readout):
def __init__(self, lm_costs_name, lm_weight,
normalize_am_weights=False,
normalize_lm_weights=False,
normalize_tot_weights=True,
am_beta=1.0,
**kwargs):
super(ShallowFusionReadout, self).__init__(**kwargs)
self.lm_costs_name = lm_costs_name
self.lm_weight = lm_weight
self.normalize_am_weights = normalize_am_weights
self.normalize_lm_weights = normalize_lm_weights
self.normalize_tot_weights = normalize_tot_weights
self.am_beta = am_beta
self.softmax = NDimensionalSoftmax()
self.children += [self.softmax]
@application
def readout(self, **kwargs):
lm_costs = -kwargs.pop(self.lm_costs_name)
if self.normalize_lm_weights:
lm_costs = self.softmax.log_probabilities(
lm_costs, extra_ndim=lm_costs.ndim - 2)
am_pre_softmax = self.am_beta * super(ShallowFusionReadout, self).readout(**kwargs)
if self.normalize_am_weights:
am_pre_softmax = self.softmax.log_probabilities(
am_pre_softmax, extra_ndim=am_pre_softmax.ndim - 2)
x = am_pre_softmax + self.lm_weight * lm_costs
if self.normalize_tot_weights:
x = self.softmax.log_probabilities(x, extra_ndim=x.ndim - 2)
return x
示例2: create_rnn
def create_rnn(hidden_dim, vocab_dim,mode="rnn"):
# input
x = tensor.imatrix('inchar')
y = tensor.imatrix('outchar')
#
W = LookupTable(
name = "W1",
#dim = hidden_dim*4,
dim = hidden_dim,
length = vocab_dim,
weights_init = initialization.IsotropicGaussian(0.01),
biases_init = initialization.Constant(0)
)
if mode == "lstm":
# Long Short Term Memory
H = LSTM(
hidden_dim,
name = 'H',
weights_init = initialization.IsotropicGaussian(0.01),
biases_init = initialization.Constant(0.0)
)
else:
# recurrent history weight
H = SimpleRecurrent(
name = "H",
dim = hidden_dim,
activation = Tanh(),
weights_init = initialization.IsotropicGaussian(0.01)
)
#
S = Linear(
name = "W2",
input_dim = hidden_dim,
output_dim = vocab_dim,
weights_init = initialization.IsotropicGaussian(0.01),
biases_init = initialization.Constant(0)
)
A = NDimensionalSoftmax(
name = "softmax"
)
initLayers([W,H,S])
activations = W.apply(x)
hiddens = H.apply(activations)#[0]
activations2 = S.apply(hiddens)
y_hat = A.apply(activations2, extra_ndim=1)
cost = A.categorical_cross_entropy(y, activations2, extra_ndim=1).mean()
cg = ComputationGraph(cost)
#print VariableFilter(roles=[WEIGHT])(cg.variables)
#W1,H,W2 = VariableFilter(roles=[WEIGHT])(cg.variables)
layers = (x, W, H, S, A, y)
return cg, layers, y_hat, cost
示例3: softmax_layer
def softmax_layer(h, y, frame_length, hidden_size):
hidden_to_output = Linear(name="hidden_to_output", input_dim=hidden_size, output_dim=frame_length)
initialize([hidden_to_output])
linear_output = hidden_to_output.apply(h)
linear_output.name = "linear_output"
softmax = NDimensionalSoftmax()
y_hat = softmax.apply(linear_output, extra_ndim=1)
y_hat.name = "y_hat"
cost = softmax.categorical_cross_entropy(y, linear_output, extra_ndim=1).mean()
cost.name = "cost"
return y_hat, cost
示例4: softmax_layer
def softmax_layer(h, y, vocab_size, hidden_size):
hidden_to_output = Linear(name='hidden_to_output', input_dim=hidden_size,
output_dim=vocab_size)
initialize([hidden_to_output])
linear_output = hidden_to_output.apply(h)
linear_output.name = 'linear_output'
softmax = NDimensionalSoftmax()
y_hat = softmax.apply(linear_output, extra_ndim=1)
y_hat.name = 'y_hat'
cost = softmax.categorical_cross_entropy(
y, linear_output, extra_ndim=1).mean()
cost.name = 'cost'
return y_hat, cost
示例5: SoftmaxEmitter
class SoftmaxEmitter(AbstractEmitter, Initializable, Random):
"""A softmax emitter for the case of integer outputs.
Interprets readout elements as energies corresponding to their indices.
Parameters
----------
initial_output : int or a scalar :class:`~theano.Variable`
The initial output.
"""
def __init__(self, initial_output=0, **kwargs):
super(SoftmaxEmitter, self).__init__(**kwargs)
self.initial_output = initial_output
self.softmax = NDimensionalSoftmax()
self.children = [self.softmax]
@application
def probs(self, readouts):
return self.softmax.apply(readouts, extra_ndim=readouts.ndim - 2)
@application
def emit(self, readouts):
probs = self.probs(readouts)
batch_size = probs.shape[0]
pvals_flat = probs.reshape((batch_size, -1))
generated = self.theano_rng.multinomial(pvals=pvals_flat)
return generated.reshape(probs.shape).argmax(axis=-1)
@application
def cost(self, readouts, outputs):
# WARNING: unfortunately this application method works
# just fine when `readouts` and `outputs` have
# different dimensions. Be careful!
return self.softmax.categorical_cross_entropy(
outputs, readouts, extra_ndim=readouts.ndim - 2)
@application
def costs(self, readouts):
return -self.softmax.log_probabilities(
readouts, extra_ndim=readouts.ndim - 2)
@application
def initial_outputs(self, batch_size):
return self.initial_output * tensor.ones((batch_size,), dtype='int64')
def get_dim(self, name):
if name == 'outputs':
return 0
return super(SoftmaxEmitter, self).get_dim(name)
示例6: __init__
def __init__(self, input1_size, input2_size, lookup1_dim=200, lookup2_dim=200, hidden_size=512):
self.hidden_size = hidden_size
self.input1_size = input1_size
self.input2_size = input2_size
self.lookup1_dim = lookup1_dim
self.lookup2_dim = lookup2_dim
x1 = tensor.lmatrix('durations')
x2 = tensor.lmatrix('syllables')
y = tensor.lmatrix('pitches')
lookup1 = LookupTable(dim=self.lookup1_dim, length=self.input1_size, name='lookup1',
weights_init=initialization.Uniform(width=0.01),
biases_init=Constant(0))
lookup1.initialize()
lookup2 = LookupTable(dim=self.lookup2_dim, length=self.input2_size, name='lookup2',
weights_init=initialization.Uniform(width=0.01),
biases_init=Constant(0))
lookup2.initialize()
merge = Merge(['lookup1', 'lookup2'], [self.lookup1_dim, self.lookup2_dim], self.hidden_size,
weights_init=initialization.Uniform(width=0.01),
biases_init=Constant(0))
merge.initialize()
recurrent_block = LSTM(dim=self.hidden_size, activation=Tanh(),
weights_init=initialization.Uniform(width=0.01)) #RecurrentStack([LSTM(dim=self.hidden_size, activation=Tanh())] * 3)
recurrent_block.initialize()
linear = Linear(input_dim=self.hidden_size, output_dim=self.input1_size,
weights_init=initialization.Uniform(width=0.01),
biases_init=Constant(0))
linear.initialize()
softmax = NDimensionalSoftmax()
l1 = lookup1.apply(x1)
l2 = lookup2.apply(x2)
m = merge.apply(l1, l2)
h = recurrent_block.apply(m)
a = linear.apply(h)
y_hat = softmax.apply(a, extra_ndim=1)
# ValueError: x must be 1-d or 2-d tensor of floats. Got TensorType(float64, 3D)
self.Cost = softmax.categorical_cross_entropy(y, a, extra_ndim=1).mean()
self.ComputationGraph = ComputationGraph(self.Cost)
self.Model = Model(y_hat)
示例7: softmax_layer
def softmax_layer(self, h, y):
"""
Perform Softmax over the hidden state in order to
predict the next word in the sequence and compute
the loss.
:param h The hidden state sequence
:param y The target words
"""
hidden_to_output = Linear(name='hidden_to_output', input_dim=self.hidden_size,
output_dim=self.vocab_size)
initialize(hidden_to_output, sqrt(6.0 / (self.hidden_size + self.vocab_size)))
linear_output = hidden_to_output.apply(h)
linear_output.name = 'linear_output'
softmax = NDimensionalSoftmax(name="lm_softmax")
y_hat = softmax.log_probabilities(linear_output, extra_ndim=1)
y_hat.name = 'y_hat'
cost = softmax.categorical_cross_entropy(y, linear_output, extra_ndim=1).mean()
cost.name = 'cost'
return y_hat, cost
示例8: __init__
def __init__(self, lm_costs_name, lm_weight,
normalize_am_weights=False,
normalize_lm_weights=False,
normalize_tot_weights=True,
am_beta=1.0,
**kwargs):
super(ShallowFusionReadout, self).__init__(**kwargs)
self.lm_costs_name = lm_costs_name
self.lm_weight = lm_weight
self.normalize_am_weights = normalize_am_weights
self.normalize_lm_weights = normalize_lm_weights
self.normalize_tot_weights = normalize_tot_weights
self.am_beta = am_beta
self.softmax = NDimensionalSoftmax()
self.children += [self.softmax]
示例9: GMMMLP
class GMMMLP(Initializable):
"""An mlp brick that branchs out to output
sigma and mu for GMM
Parameters
----------
mlp: MLP brick
the main mlp to wrap around.
dim:
output dim
"""
def __init__(self, mlp, dim, k, const=1e-5, **kwargs):
super(GMMMLP, self).__init__(**kwargs)
self.dim = dim
self.const = const
self.k = k
input_dim = mlp.output_dim
self.mu = MLP(activations=[Identity()],
dims=[input_dim, dim],
name=self.name + "_mu")
self.sigma = MLP(activations=[SoftPlus()],
dims=[input_dim, dim],
name=self.name + "_sigma")
self.coeff = MLP(activations=[Identity()],
dims=[input_dim, k],
name=self.name + "_coeff")
self.coeff2 = NDimensionalSoftmax()
self.mlp = mlp
self.children = [self.mlp, self.mu,
self.sigma, self.coeff, self.coeff2]
#self.children.extend(self.mlp.children)
@application
def apply(self, inputs):
state = self.mlp.apply(inputs)
mu = self.mu.apply(state)
sigma = self.sigma.apply(state)
coeff = self.coeff2.apply(self.coeff.apply(state),
extra_ndim=state.ndim - 2) + self.const
return mu, sigma, coeff
@property
def output_dim(self):
return self.dim
示例10: __init__
def __init__(self, mlp, dim, k, const=1e-5, **kwargs):
super(GMMMLP, self).__init__(**kwargs)
self.dim = dim
self.const = const
self.k = k
input_dim = mlp.output_dim
self.mu = MLP(activations=[Identity()],
dims=[input_dim, dim],
name=self.name + "_mu")
self.sigma = MLP(activations=[SoftPlus()],
dims=[input_dim, dim],
name=self.name + "_sigma")
self.coeff = MLP(activations=[Identity()],
dims=[input_dim, k],
name=self.name + "_coeff")
self.coeff2 = NDimensionalSoftmax()
self.mlp = mlp
self.children = [self.mlp, self.mu,
self.sigma, self.coeff, self.coeff2]
示例11: FRNNEmitter
class FRNNEmitter(AbstractEmitter, Initializable, Random):
"""An RNN emitter for the case of real outputs.
Parameters
----------
"""
def __init__(self, mlp, target_size, frame_size, k, frnn_hidden_size, frnn_step_size, const=1e-5, **kwargs):
super(FRNNEmitter, self).__init__(**kwargs)
self.mlp = mlp
self.target_size = target_size
self.frame_size = frame_size
self.k = k
self.frnn_hidden_size = frnn_hidden_size
self.const = const
self.input_dim = self.mlp.output_dim
self.frnn_step_size = frnn_step_size
# adding a step if the division is not exact.
self.number_of_steps = frame_size // frnn_step_size
self.last_steps = frame_size % frnn_step_size
if self.last_steps != 0:
self.number_of_steps += 1
self.mu = MLP(activations=[Identity()], dims=[frnn_hidden_size, k * frnn_step_size], name=self.name + "_mu")
self.sigma = MLP(
activations=[SoftPlus()], dims=[frnn_hidden_size, k * frnn_step_size], name=self.name + "_sigma"
)
self.coeff = MLP(activations=[Identity()], dims=[frnn_hidden_size, k], name=self.name + "_coeff")
self.coeff2 = NDimensionalSoftmax()
self.frnn_initial_state = Linear(
input_dim=self.input_dim, output_dim=frnn_hidden_size, name="frnn_initial_state"
)
# self.frnn_hidden = Linear(
# input_dim=frnn_hidden_size,
# output_dim=frnn_hidden_size,
# activation=Tanh(),
# name="frnn_hidden")
self.frnn_activation = Tanh(name="frnn_activation")
self.frnn_linear_transition_state = Linear(
input_dim=frnn_hidden_size, output_dim=frnn_hidden_size, name="frnn_linear_transition_state"
)
self.frnn_linear_transition_input = Linear(
input_dim=self.frnn_step_size, output_dim=frnn_hidden_size, name="frnn_linear_transition_input"
)
# self.frnn_linear_transition_output = Linear (
# input_dim = frnn_hidden_size,
# output_dim = self.rnn_hidden_dim,
# name="frnn_linear_transition_output")
self.children = [
self.mlp,
self.mu,
self.sigma,
self.coeff,
self.coeff2,
self.frnn_initial_state,
self.frnn_activation,
self.frnn_linear_transition_state,
self.frnn_linear_transition_input,
]
@application
def emit(self, readouts):
"""
keep_parameters is True if mu,sigma,coeffs must be stacked and returned
if false, only the result is given, the others will be empty list.
"""
# initial state
state = self.frnn_initial_state.apply(self.mlp.apply(readouts))
results = []
for i in range(self.number_of_steps):
last_iteration = i == self.number_of_steps - 1
# First generating distribution parameters and sampling.
mu = self.mu.apply(state)
sigma = self.sigma.apply(state) + self.const
coeff = self.coeff2.apply(self.coeff.apply(state), extra_ndim=state.ndim - 2) + self.const
shape_result = coeff.shape
shape_result = tensor.set_subtensor(shape_result[-1], self.frnn_step_size)
ndim_result = coeff.ndim
mu = mu.reshape((-1, self.frnn_step_size, self.k))
sigma = sigma.reshape((-1, self.frnn_step_size, self.k))
coeff = coeff.reshape((-1, self.k))
#.........这里部分代码省略.........
示例12: __init__
def __init__(self, initial_output=0, **kwargs):
super(SoftmaxEmitter, self).__init__(**kwargs)
self.initial_output = initial_output
self.softmax = NDimensionalSoftmax()
self.children = [self.softmax]
示例13: __init__
def __init__(self, config_dict, init_type="xavier", **kwargs):
super(CharRNNModel, self).__init__(**kwargs)
self.batch_size = config_dict["batch_size"]
self.num_subwords = config_dict["num_subwords"]
self.num_words = config_dict["num_words"]
self.subword_embedding_size = config_dict["subword_embedding_size"]
self.input_vocab_size = config_dict["input_vocab_size"]
self.output_vocab_size = config_dict["output_vocab_size"]
self.subword_RNN_hidden_state_size = config_dict["subword_RNN_hidden_state_size"]
self.table_width = config_dict["table_width"]
self.max_out_dim = config_dict["max_out_dim"]
self.max_out_K = config_dict["max_out_K"]
self.lookup = LookupTable(length=self.input_vocab_size, dim=self.subword_embedding_size, name="input_lookup")
self.lookup.weights_init = Uniform(width=self.table_width)
self.lookup.biases_init = Constant(0)
if init_type == "xavier":
linear_init = XavierInitializationOriginal(self.subword_embedding_size, self.subword_RNN_hidden_state_size)
lstm_init = XavierInitializationOriginal(self.subword_embedding_size, self.subword_RNN_hidden_state_size)
else: # default is gaussian
linear_init = IsotropicGaussian()
lstm_init = IsotropicGaussian()
# The `inputs` are then split in this order: Input gates, forget gates, cells and output gates
self.linear_forward = Linear(
input_dim=self.subword_embedding_size,
output_dim=self.subword_RNN_hidden_state_size * 4,
name="linear_forward",
weights_init=linear_init,
biases_init=Constant(0.0),
)
self.language_model = LSTM(
dim=self.subword_RNN_hidden_state_size,
activation=Tanh(),
name="language_model_RNN",
weights_init=lstm_init,
biases_init=Constant(0.0),
)
self.max_out = LinearMaxout(
self.subword_RNN_hidden_state_size,
self.max_out_dim,
self.max_out_K,
name="max_out",
weights_init=IsotropicGaussian(),
biases_init=Constant(0.0),
)
self.softmax_linear = Linear(
self.max_out_dim,
self.output_vocab_size,
name="soft_max_linear",
weights_init=IsotropicGaussian(),
biases_init=Constant(0.0),
)
self.softmax = NDimensionalSoftmax()
self.children = [
self.lookup,
self.linear_forward,
self.language_model,
self.max_out,
self.softmax_linear,
self.softmax,
]
示例14: SimpleRecurrent
rnn = SimpleRecurrent(
name='hidden',
dim=hidden_layer_dim,
activation=Tanh(),
weights_init=initialization.Uniform(width=0.01))
rnn.initialize()
linear_output = Linear(
name='linear_output',
input_dim=hidden_layer_dim,
output_dim=charset_size,
weights_init=initialization.Uniform(width=0.01),
biases_init=Constant(0))
linear_output.initialize()
softmax = NDimensionalSoftmax(name='ndim_softmax')
activation_input = lookup_input.apply(x)
hidden = rnn.apply(linear_input.apply(activation_input))
activation_output = linear_output.apply(hidden)
y_est = softmax.apply(activation_output, extra_ndim=1)
cost = softmax.categorical_cross_entropy(y, activation_output, extra_ndim=1).mean()
from blocks.graph import ComputationGraph
from blocks.algorithms import GradientDescent, Adam
cg = ComputationGraph([cost])
step_rules = [RMSProp(learning_rate=0.002, decay_rate=0.95), StepClipping(1.0)]
示例15: SimpleSpeechRecognizer
# ******************* Model *******************
recognizer = SimpleSpeechRecognizer(transition=transition,
dims_transition=conf.dims_transition,
num_features=num_features, num_classes=num_classes)
#recognizer = SpeechRecognizer(
# num_features=num_features, dims_bottom=[],
# dims_bidir=conf.dims_transition, dims_top=[num_classes],
# bidir_trans=GatedRecurrent, bottom_activation=None)
# ******************* output *******************
y_hat = recognizer.apply(x,x_m)
y_hat.name = 'outputs'
y_hat_softmax = NDimensionalSoftmax().apply(y_hat, extra_ndim = y_hat.ndim - 2)
y_hat_softmax.name = 'outputs_softmax'
# there is a cost function for monitoring and for training, because one is more stable to compute
# gradients and seems also to be more memory efficient, but does not compute the true cost.
if conf.task=='CTC':
cost_train = ctc.pseudo_cost(y, y_hat, y_m, x_m).mean()
cost_train.name = "cost_train"
cost_monitor = ctc.cost(y, y_hat_softmax, y_m, x_m).mean()
cost_monitor.name = "cost_monitor"
elif conf.task=='framewise':
cost_train = categorical_crossentropy_batch().apply(y_hat_softmax, y, x_m)
cost_train.name='cost'
cost_monitor = cost_train
else: