本文整理汇总了Python中cntk.Axis.default_batch_axis方法的典型用法代码示例。如果您正苦于以下问题:Python Axis.default_batch_axis方法的具体用法?Python Axis.default_batch_axis怎么用?Python Axis.default_batch_axis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cntk.Axis
的用法示例。
在下文中一共展示了Axis.default_batch_axis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train_faster_rcnn_e2e
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def train_faster_rcnn_e2e(base_model_file_name, debug_output=False):
# Input variables denoting features and labeled ground truth rois (as 5-tuples per roi)
image_input = input_variable((num_channels, image_height, image_width), dynamic_axes=[Axis.default_batch_axis()], name=feature_node_name)
roi_input = input_variable((cfg["CNTK"].INPUT_ROIS_PER_IMAGE, 5), dynamic_axes=[Axis.default_batch_axis()])
dims_input = input_variable((6), dynamic_axes=[Axis.default_batch_axis()])
dims_node = alias(dims_input, name='dims_input')
# Instantiate the Faster R-CNN prediction model and loss function
loss, pred_error = create_faster_rcnn_predictor(base_model_file_name, image_input, roi_input, dims_node)
if debug_output:
print("Storing graphs and models to %s." % globalvars['output_path'])
plot(loss, os.path.join(globalvars['output_path'], "graph_frcn_train_e2e." + cfg["CNTK"].GRAPH_TYPE))
# Set learning parameters
e2e_lr_factor = globalvars['e2e_lr_factor']
e2e_lr_per_sample_scaled = [x * e2e_lr_factor for x in cfg["CNTK"].E2E_LR_PER_SAMPLE]
mm_schedule = momentum_schedule(cfg["CNTK"].MOMENTUM_PER_MB)
print("Using base model: {}".format(cfg["CNTK"].BASE_MODEL))
print("lr_per_sample: {}".format(e2e_lr_per_sample_scaled))
train_model(image_input, roi_input, dims_input, loss, pred_error,
e2e_lr_per_sample_scaled, mm_schedule, cfg["CNTK"].L2_REG_WEIGHT, globalvars['e2e_epochs'])
return create_eval_model(loss, image_input, dims_input)
示例2: train_faster_rcnn_e2e
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def train_faster_rcnn_e2e(cfg):
# Input variables denoting features and labeled ground truth rois (as 5-tuples per roi)
image_input = input_variable(shape=(cfg.NUM_CHANNELS, cfg.IMAGE_HEIGHT, cfg.IMAGE_WIDTH),
dynamic_axes=[Axis.default_batch_axis()],
name=cfg["MODEL"].FEATURE_NODE_NAME)
roi_input = input_variable((cfg.INPUT_ROIS_PER_IMAGE, 5), dynamic_axes=[Axis.default_batch_axis()])
dims_input = input_variable((6), dynamic_axes=[Axis.default_batch_axis()])
dims_node = alias(dims_input, name='dims_input')
# Instantiate the Faster R-CNN prediction model and loss function
loss, pred_error = create_faster_rcnn_model(image_input, roi_input, dims_node, cfg)
if cfg["CNTK"].DEBUG_OUTPUT:
print("Storing graphs and models to %s." % cfg.OUTPUT_PATH)
plot(loss, os.path.join(cfg.OUTPUT_PATH, "graph_frcn_train_e2e." + cfg["CNTK"].GRAPH_TYPE))
# Set learning parameters
e2e_lr_factor = cfg["MODEL"].E2E_LR_FACTOR
e2e_lr_per_sample_scaled = [x * e2e_lr_factor for x in cfg["CNTK"].E2E_LR_PER_SAMPLE]
mm_schedule = momentum_schedule(cfg["CNTK"].MOMENTUM_PER_MB)
print("Using base model: {}".format(cfg["MODEL"].BASE_MODEL))
print("lr_per_sample: {}".format(e2e_lr_per_sample_scaled))
train_model(image_input, roi_input, dims_input, loss, pred_error,
e2e_lr_per_sample_scaled, mm_schedule, cfg["CNTK"].L2_REG_WEIGHT, cfg["CNTK"].E2E_MAX_EPOCHS, cfg)
return create_faster_rcnn_eval_model(loss, image_input, dims_input, cfg)
示例3: __init__
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def __init__(self, eval_model, cfg):
# load model once in constructor and push images through the model in 'process_image()'
self._img_shape = (cfg.NUM_CHANNELS, cfg.IMAGE_HEIGHT, cfg.IMAGE_WIDTH)
image_input = input_variable(shape=self._img_shape,
dynamic_axes=[Axis.default_batch_axis()],
name=cfg["MODEL"].FEATURE_NODE_NAME)
dims_input = input_variable((1,6), dynamic_axes=[Axis.default_batch_axis()], name='dims_input')
self._eval_model = eval_model(image_input, dims_input)
示例4: __init__
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def __init__(self, eval_model, cfg):
# load model once in constructor and push images through the model in 'process_image()'
self._img_shape = (cfg.NUM_CHANNELS, cfg.IMAGE_HEIGHT, cfg.IMAGE_WIDTH)
image_input = input_variable(shape=self._img_shape,
dynamic_axes=[Axis.default_batch_axis()],
name=cfg["MODEL"].FEATURE_NODE_NAME)
roi_proposals = input_variable((cfg.NUM_ROI_PROPOSALS, 4), dynamic_axes=[Axis.default_batch_axis()],
name="roi_proposals")
self._eval_model = eval_model(image_input, roi_proposals)
self._cfg = cfg
示例5: eval_and_plot_faster_rcnn
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def eval_and_plot_faster_rcnn(eval_model, num_images_to_plot, test_map_file, img_shape,
results_base_path, feature_node_name, classes,
drawUnregressedRois=False, drawNegativeRois=False,
nmsThreshold=0.5, nmsConfThreshold=0.0, bgrPlotThreshold = 0.8):
# get image paths
with open(test_map_file) as f:
content = f.readlines()
img_base_path = os.path.dirname(os.path.abspath(test_map_file))
img_file_names = [os.path.join(img_base_path, x.split('\t')[1]) for x in content]
# prepare model
image_input = input_variable(img_shape, dynamic_axes=[Axis.default_batch_axis()], name=feature_node_name)
dims_input = input_variable((1,6), dynamic_axes=[Axis.default_batch_axis()], name='dims_input')
frcn_eval = eval_model(image_input, dims_input)
#dims_input_const = cntk.constant([image_width, image_height, image_width, image_height, image_width, image_height], (1, 6))
print("Plotting results from Faster R-CNN model for %s images." % num_images_to_plot)
for i in range(0, num_images_to_plot):
imgPath = img_file_names[i]
# evaluate single image
_, cntk_img_input, dims = load_resize_and_pad(imgPath, img_shape[2], img_shape[1])
dims_input = np.array(dims, dtype=np.float32)
dims_input.shape = (1,) + dims_input.shape
output = frcn_eval.eval({frcn_eval.arguments[0]: [cntk_img_input], frcn_eval.arguments[1]: dims_input})
out_dict = dict([(k.name, k) for k in output])
out_cls_pred = output[out_dict['cls_pred']][0]
out_rpn_rois = output[out_dict['rpn_rois']][0]
out_bbox_regr = output[out_dict['bbox_regr']][0]
labels = out_cls_pred.argmax(axis=1)
scores = out_cls_pred.max(axis=1).tolist()
if drawUnregressedRois:
# plot results without final regression
imgDebug = visualizeResultsFaster(imgPath, labels, scores, out_rpn_rois, img_shape[2], img_shape[1],
classes, nmsKeepIndices=None, boDrawNegativeRois=drawNegativeRois,
decisionThreshold=bgrPlotThreshold)
imsave("{}/{}_{}".format(results_base_path, i, os.path.basename(imgPath)), imgDebug)
# apply regression and nms to bbox coordinates
regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr, labels, dims)
nmsKeepIndices = apply_nms_to_single_image_results(regressed_rois, labels, scores,
nms_threshold=nmsThreshold,
conf_threshold=nmsConfThreshold)
img = visualizeResultsFaster(imgPath, labels, scores, regressed_rois, img_shape[2], img_shape[1],
classes, nmsKeepIndices=nmsKeepIndices,
boDrawNegativeRois=drawNegativeRois,
decisionThreshold=bgrPlotThreshold)
imsave("{}/{}_regr_{}".format(results_base_path, i, os.path.basename(imgPath)), img)
示例6: test_op_reduce_over_batch_axis
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def test_op_reduce_over_batch_axis(input_data, device_id, precision):
from .. import reduce_sum, reduce_max, reduce_min, reduce_mean, reduce_log_sum_exp, reduce_prod
from cntk import Axis
dt = PRECISION_TO_TYPE[precision]
data = AA(input_data, dtype=dt)
a = C.input_variable(shape=data.shape[1:],
dtype=sanitize_dtype_cntk(dt),
needs_gradient=True,
name='a')
ops = [
(reduce_sum, lambda x:np.sum(x, axis=0, keepdims=False), lambda x,f:np.ones_like(x)),
(reduce_max, lambda x:np.amax(x, axis=0, keepdims=False), lambda x,f:min_max_bwd(x,f, dt)),
(reduce_min, lambda x:np.amin(x, axis=0, keepdims=False), lambda x,f:min_max_bwd(x,f, dt)),
(reduce_mean, lambda x:np.mean(x, axis=0, keepdims=False), lambda x,f:np.ones_like(x)/x.shape[0]),
(reduce_log_sum_exp, lambda x:np.log(np.sum(np.exp(x), axis=0, keepdims=False)), lambda x,f:np.exp(x-f)),
(reduce_prod, lambda x:np.prod(x, axis=0, keepdims=False), lambda x,f:f / x)
]
for op,fwd,bwd in ops:
input_op = op(a, axis=Axis.default_batch_axis())
expected_forward = fwd(data)
expected_backward = bwd(data, expected_forward)
binding = {a: data}
actual_backward = input_op.grad(binding)
actual_forward = input_op.eval(binding)
assert np.allclose(actual_forward, expected_forward)
for ab,eb in zip (actual_backward, expected_backward):
assert np.allclose(ab, eb)
示例7: test_model_not_criterion_subset
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def test_model_not_criterion_subset():
input_dim = 2
proj_dim = 11
model1_dim = 3
model2_dim = 4
x = input_variable((input_dim,))
core = Embedding(proj_dim)
model1 = Dense(model1_dim)(sequence.last(core(x)))
model1_label = input_variable((model1_dim,), dynamic_axes=[Axis.default_batch_axis()])
ce_model1 = cross_entropy_with_softmax(model1, model1_label)
pe_model1 = classification_error(model1, model1_label)
model2 = Dense(model2_dim)(core(x))
model2_label = input_variable((model2_dim,))
ce_model2 = cross_entropy_with_softmax(model2, model2_label)
pe_model2 = classification_error(model2, model2_label)
ce = 0.5 * sequence.reduce_sum(ce_model2) + 0.5 * ce_model1
lr_schedule = learning_rate_schedule(0.003, UnitType.sample)
trainer_multitask = Trainer(model1, (ce, pe_model1), sgd(ce.parameters, lr=lr_schedule))
x_data = np.asarray([[2., 1.], [1., 2.]], np.float32)
model1_label_data = np.asarray([1., 0., 0.], np.float32)
model2_label_data = np.asarray([[0., 1., 0., 0.], [0., 0., 0., 1.]], np.float32)
trainer_multitask.train_minibatch({x : [x_data], model1_label : [model1_label_data], model2_label : [model2_label_data]})
示例8: test_eval_sparse_dense
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def test_eval_sparse_dense(tmpdir, device_id):
from cntk import Axis
from cntk.io import MinibatchSource, CTFDeserializer, StreamDef, StreamDefs
from cntk.device import cpu, gpu, set_default_device
from cntk.ops import input_variable, times
from scipy.sparse import csr_matrix
input_vocab_dim = label_vocab_dim = 69
ctf_data = '''\
0 |S0 3:1 |# <s> |S1 3:1 |# <s>
0 |S0 4:1 |# A |S1 32:1 |# ~AH
0 |S0 5:1 |# B |S1 36:1 |# ~B
0 |S0 4:1 |# A |S1 31:1 |# ~AE
0 |S0 7:1 |# D |S1 38:1 |# ~D
0 |S0 12:1 |# I |S1 47:1 |# ~IY
0 |S0 1:1 |# </s> |S1 1:1 |# </s>
2 |S0 60:1 |# <s> |S1 3:1 |# <s>
2 |S0 61:1 |# A |S1 32:1 |# ~AH
'''
ctf_file = str(tmpdir/'2seqtest.txt')
with open(ctf_file, 'w') as f:
f.write(ctf_data)
mbs = MinibatchSource(CTFDeserializer(ctf_file, StreamDefs(
features = StreamDef(field='S0', shape=input_vocab_dim, is_sparse=True),
labels = StreamDef(field='S1', shape=label_vocab_dim, is_sparse=True)
)), randomize=False, epoch_size = 2)
batch_axis = Axis.default_batch_axis()
input_seq_axis = Axis('inputAxis')
label_seq_axis = Axis('labelAxis')
input_dynamic_axes = [batch_axis, input_seq_axis]
raw_input = input_variable(
shape=input_vocab_dim, dynamic_axes=input_dynamic_axes,
name='raw_input', is_sparse=True)
mb_valid = mbs.next_minibatch(minibatch_size_in_samples=100,
input_map={raw_input : mbs.streams.features})
z = times(raw_input, np.eye(input_vocab_dim))
e_reader = z.eval(mb_valid)
# CSR with the raw_input encoding in ctf_data
one_hot_data = [
[3, 4, 5, 4, 7, 12, 1],
[60, 61]
]
data = [csr_matrix(np.eye(input_vocab_dim, dtype=np.float32)[d]) for d in
one_hot_data]
e_csr = z.eval({raw_input: data}, device=cntk_device(device_id))
assert np.all([np.allclose(a, b) for a,b in zip(e_reader, e_csr)])
# One-hot with the raw_input encoding in ctf_data
data = one_hot(one_hot_data, num_classes=input_vocab_dim)
e_hot = z.eval({raw_input: data}, device=cntk_device(device_id))
assert np.all([np.allclose(a, b) for a,b in zip(e_reader, e_hot)])
示例9: create_inputs
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def create_inputs(vocab_dim):
batch_axis = Axis.default_batch_axis()
input_seq_axis = Axis('inputAxis')
input_dynamic_axes = [batch_axis, input_seq_axis]
input_sequence = input_variable(shape=vocab_dim, dynamic_axes=input_dynamic_axes)
label_sequence = input_variable(shape=vocab_dim, dynamic_axes=input_dynamic_axes)
return input_sequence, label_sequence
示例10: train_sequence_classifier
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def train_sequence_classifier(debug_output=False):
input_dim = 2000
cell_dim = 25
hidden_dim = 25
embedding_dim = 50
num_output_classes = 5
# Input variables denoting the features and label data
features = input_variable(shape=input_dim, is_sparse=True)
label = input_variable(num_output_classes, dynamic_axes=[
Axis.default_batch_axis()])
# Instantiate the sequence classification model
classifier_output = LSTM_sequence_classifer_net(
features, num_output_classes, embedding_dim, hidden_dim, cell_dim)
ce = cross_entropy_with_softmax(classifier_output, label)
pe = classification_error(classifier_output, label)
rel_path = r"../../../../Tests/EndToEndTests/Text/SequenceClassification/Data/Train.ctf"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), rel_path)
reader = create_reader(path, True, input_dim, num_output_classes)
input_map = {
features : reader.streams.features,
label : reader.streams.labels
}
lr_per_sample = learning_rate_schedule(0.0005, UnitType.sample)
# Instantiate the trainer object to drive the model training
trainer = Trainer(classifier_output, (ce, pe),
sgd(classifier_output.parameters, lr=lr_per_sample))
# Get minibatches of sequences to train with and perform model training
minibatch_size = 200
training_progress_output_freq = 10
if debug_output:
training_progress_output_freq = training_progress_output_freq/3
for i in range(251):
mb = reader.next_minibatch(minibatch_size, input_map=input_map)
trainer.train_minibatch(mb)
print_training_progress(trainer, i, training_progress_output_freq)
import copy
evaluation_average = copy.copy(
trainer.previous_minibatch_evaluation_average)
loss_average = copy.copy(trainer.previous_minibatch_loss_average)
return evaluation_average, loss_average
示例11: train_sequence_classifier
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def train_sequence_classifier():
input_dim = 2000;
cell_dim = 25;
hidden_dim = 25;
embedding_dim = 50;
num_output_classes = 5;
# Input variables denoting the features and label data
features = input_variable(shape=input_dim, is_sparse=True)
label = input_variable(num_output_classes, dynamic_axes = [Axis.default_batch_axis()])
# Instantiate the sequence classification model
classifier_output = LSTM_sequence_classifer_net(features, num_output_classes, embedding_dim, hidden_dim, cell_dim)
ce = cross_entropy_with_softmax(classifier_output, label)
pe = classification_error(classifier_output, label)
rel_path = r"../../../../Tests/EndToEndTests/Text/SequenceClassification/Data/Train.ctf"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), rel_path)
feature_stream_name = 'features'
labels_stream_name = 'labels'
mb_source = text_format_minibatch_source(path, [
StreamConfiguration( feature_stream_name, input_dim, True, 'x' ),
StreamConfiguration( labels_stream_name, num_output_classes, False, 'y')], 0)
features_si = mb_source.stream_info(features)
labels_si = mb_source.stream_info(label)
# Instantiate the trainer object to drive the model training
lr = lr = learning_rates_per_sample(0.0005)
trainer = Trainer(classifier_output, ce, pe, [sgd_learner(classifier_output.owner.parameters(), lr)])
# Get minibatches of sequences to train with and perform model training
minibatch_size = 200
training_progress_output_freq = 10
i = 0;
while True:
mb = mb_source.get_next_minibatch(minibatch_size)
if len(mb) == 0:
break
# Specify the mapping of input variables in the model to actual minibatch data to be trained with
arguments = {features : mb[features_si].m_data, label : mb[labels_si].m_data}
trainer.train_minibatch(arguments)
print_training_progress(trainer, i, training_progress_output_freq)
i += 1
示例12: test_op_reduce_over_batch_axis
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def test_op_reduce_over_batch_axis(input_data, device_id, precision):
from .. import reduce_sum, reduce_max, reduce_min, reduce_mean, reduce_log_sum_exp, reduce_prod
from cntk import Axis
dt = PRECISION_TO_TYPE[precision]
data = AA(input_data, dtype=dt)
a = C.input_variable(shape=data.shape[1:],
dtype=sanitize_dtype_cntk(dt),
needs_gradient=True,
name='a')
def min_max_bwd(x, f):
forward_array = np.asarray(f, dtype=dt)
min_max_elements = forward_array.reshape(forward_array.size).tolist()
# place 1.0s where minimum or maximum elements are
backward = np.zeros_like(x)
for element in min_max_elements:
backward += np.asarray(x == element)
return backward
ops = [
(reduce_sum, lambda x:np.sum(x, axis=0, keepdims=False), lambda x,f:np.ones_like(x)),
(reduce_max, lambda x:np.amax(x, axis=0, keepdims=False), lambda x,f:min_max_bwd(x,f)),
(reduce_min, lambda x:np.amin(x, axis=0, keepdims=False), lambda x,f:min_max_bwd(x,f)),
(reduce_mean, lambda x:np.mean(x, axis=0, keepdims=False), lambda x,f:np.ones_like(x)/x.shape[0]),
(reduce_log_sum_exp, lambda x:np.log(np.sum(np.exp(x), axis=0, keepdims=False)), lambda x,f:np.exp(x-f)),
(reduce_prod, lambda x:np.prod(x, axis=0, keepdims=False), lambda x,f:f / x)
]
for op,fwd,bwd in ops:
input_op = op(a, axis=Axis.default_batch_axis())
expected_forward = fwd(data)
expected_backward = bwd(data, expected_forward)
binding = {a: data}
actual_backward = input_op.grad(binding)
actual_forward = input_op.eval(binding)
assert np.allclose(actual_forward, expected_forward)
for ab,eb in zip (actual_backward, expected_backward):
assert np.allclose(ab, eb)
示例13: test_recurrent_block
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def test_recurrent_block(block_type, block_outputs_count, block_size, W_mult, H_mult, expected_res):
input_shape = 4
sequenceAxis = Axis('sequenceAxis')
y = C.input_variable(input_shape, dynamic_axes=[Axis.default_batch_axis(), sequenceAxis])
data = np.reshape(np.arange(0,16, dtype=np.float32), (1,4,4))
rnn_block = block_type(block_size, init=0.1)
assert len(rnn_block.outputs) == block_outputs_count
rnn_net = Recurrence(rnn_block)(y)
assert rnn_net.b.shape == (W_mult*block_size,)
assert rnn_net.W.shape == (input_shape, W_mult*block_size)
assert rnn_net.H.shape == (block_size, H_mult*block_size)
res = rnn_net.eval(data)
expected = np.asarray(expected_res, dtype=np.float32)
np.testing.assert_array_almost_equal(res[0], expected, decimal=6)
示例14: sequence_to_sequence_translator
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def sequence_to_sequence_translator(debug_output=False, run_test=False):
input_vocab_dim = 69
label_vocab_dim = 69
# network complexity; initially low for faster testing
hidden_dim = 256
num_layers = 1
# Source and target inputs to the model
batch_axis = Axis.default_batch_axis()
input_seq_axis = Axis('inputAxis')
label_seq_axis = Axis('labelAxis')
input_dynamic_axes = [batch_axis, input_seq_axis]
raw_input = input_variable(
shape=(input_vocab_dim), dynamic_axes=input_dynamic_axes, name='raw_input')
label_dynamic_axes = [batch_axis, label_seq_axis]
raw_labels = input_variable(
shape=(label_vocab_dim), dynamic_axes=label_dynamic_axes, name='raw_labels')
# Instantiate the sequence to sequence translation model
input_sequence = raw_input
# Drop the sentence start token from the label, for decoder training
label_sequence = sequence.slice(raw_labels, 1, 0) # <s> A B C </s> --> A B C </s>
label_sentence_start = sequence.first(raw_labels) # <s>
is_first_label = sequence.is_first(label_sequence) # <s> 0 0 0 ...
label_sentence_start_scattered = sequence.scatter(
label_sentence_start, is_first_label)
# Encoder
encoder_outputH = stabilize(input_sequence)
for i in range(0, num_layers):
(encoder_outputH, encoder_outputC) = LSTMP_component_with_self_stabilization(
encoder_outputH.output, hidden_dim, hidden_dim, future_value, future_value)
thought_vectorH = sequence.first(encoder_outputH)
thought_vectorC = sequence.first(encoder_outputC)
thought_vector_broadcastH = sequence.broadcast_as(
thought_vectorH, label_sequence)
thought_vector_broadcastC = sequence.broadcast_as(
thought_vectorC, label_sequence)
# Decoder
decoder_history_hook = alias(label_sequence, name='decoder_history_hook') # copy label_sequence
decoder_input = element_select(is_first_label, label_sentence_start_scattered, past_value(
decoder_history_hook))
decoder_outputH = stabilize(decoder_input)
for i in range(0, num_layers):
if (i > 0):
recurrence_hookH = past_value
recurrence_hookC = past_value
else:
isFirst = sequence.is_first(label_sequence)
recurrence_hookH = lambda operand: element_select(
isFirst, thought_vector_broadcastH, past_value(operand))
recurrence_hookC = lambda operand: element_select(
isFirst, thought_vector_broadcastC, past_value(operand))
(decoder_outputH, encoder_outputC) = LSTMP_component_with_self_stabilization(
decoder_outputH.output, hidden_dim, hidden_dim, recurrence_hookH, recurrence_hookC)
decoder_output = decoder_outputH
# Softmax output layer
z = linear_layer(stabilize(decoder_output), label_vocab_dim)
# Criterion nodes
ce = cross_entropy_with_softmax(z, label_sequence)
errs = classification_error(z, label_sequence)
# network output for decoder history
net_output = hardmax(z)
# make a clone of the graph where the ground truth is replaced by the network output
ng = z.clone(CloneMethod.share, {decoder_history_hook.output : net_output.output})
# Instantiate the trainer object to drive the model training
lr_per_minibatch = learning_rate_schedule(0.5, UnitType.minibatch)
momentum_time_constant = momentum_as_time_constant_schedule(1100)
clipping_threshold_per_sample = 2.3
gradient_clipping_with_truncation = True
learner = momentum_sgd(z.parameters,
lr_per_minibatch, momentum_time_constant,
gradient_clipping_threshold_per_sample=clipping_threshold_per_sample,
gradient_clipping_with_truncation=gradient_clipping_with_truncation)
trainer = Trainer(z, ce, errs, learner)
# setup data
train_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Data", "cmudict-0.7b.train-dev-20-21.ctf")
valid_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Data", "tiny.ctf")
# readers
randomize_data = True
#.........这里部分代码省略.........
示例15: train_sequence_classifier
# 需要导入模块: from cntk import Axis [as 别名]
# 或者: from cntk.Axis import default_batch_axis [as 别名]
def train_sequence_classifier(debug_output=False):
input_dim = 2000
cell_dim = 25
hidden_dim = 25
embedding_dim = 50
num_output_classes = 5
# Input variables denoting the features and label data
features = input_variable(shape=input_dim, is_sparse=True)
label = input_variable(num_output_classes, dynamic_axes=[
Axis.default_batch_axis()])
# Instantiate the sequence classification model
classifier_output = LSTM_sequence_classifer_net(
features, num_output_classes, embedding_dim, hidden_dim, cell_dim)
ce = cross_entropy_with_softmax(classifier_output, label)
pe = classification_error(classifier_output, label)
rel_path = r"../../../../Tests/EndToEndTests/Text/SequenceClassification/Data/Train.ctf"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), rel_path)
feature_stream_name = 'features'
labels_stream_name = 'labels'
mb_source = text_format_minibatch_source(path, [
StreamConfiguration(feature_stream_name, input_dim, True, 'x'),
StreamConfiguration(labels_stream_name, num_output_classes, False, 'y')], 0)
features_si = mb_source[features]
labels_si = mb_source[label]
# Instantiate the trainer object to drive the model training
trainer = Trainer(classifier_output, ce, pe,
[sgd(classifier_output.parameters(), lr=0.0005)])
# Get minibatches of sequences to train with and perform model training
minibatch_size = 200
training_progress_output_freq = 10
i = 0
if debug_output:
training_progress_output_freq = training_progress_output_freq/3
while True:
mb = mb_source.get_next_minibatch(minibatch_size)
if len(mb) == 0:
break
# Specify the mapping of input variables in the model to actual
# minibatch data to be trained with
arguments = {features: mb[features_si],
label: mb[labels_si]}
trainer.train_minibatch(arguments)
print_training_progress(trainer, i, training_progress_output_freq)
i += 1
import copy
evaluation_average = copy.copy(
trainer.previous_minibatch_evaluation_average())
loss_average = copy.copy(trainer.previous_minibatch_loss_average())
return evaluation_average, loss_average