python – CNTK序列模型错误:检测到不同的小批量布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – CNTK序列模型错误:检测到不同的小批量布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2832字,纯文字阅读大概需要5分钟。
内容图文
![python – CNTK序列模型错误:检测到不同的小批量布局](/upload/InfoBanner/zyjiaocheng/735/4ab3dc23f21d409c9c67a369e59d3964.jpg)
我正在尝试使用CNTK训练模型,该模型接收两个输入序列并输出二维标量标签.我已经定义了这样的模型:
def create_seq_model(num_tokens):
with C.default_options(init=C.glorot_uniform()):
i1 = sequence.input(shape=num_tokens, is_sparse=True, name='i1')
i2 = sequence.input(shape=num_tokens, is_sparse=True, name='i2')
s1 = Sequential([Embedding(300), Fold(GRU(64))])(i1)
s2 = Sequential([Embedding(300), Fold(GRU(64))])(i2)
combined = splice(s1, s2)
model = Sequential([Dense(64, activation=sigmoid),
Dropout(0.1, seed=42),
Dense(2, activation=softmax)])
return model(combined)
我已将数据转换为CTF格式.当我尝试使用以下代码段训练它时(从示例here中进行了非常轻微的修改),我收到一个错误:
def train(reader, model, max_epochs=16):
criterion = create_criterion_function(model)
criterion.replace_placeholders({criterion.placeholders[0]: C.input(2, name='labels')})
epoch_size = 500000
minibatch_size=128
lr_per_sample = [0.003]*4+[0.0015]*24+[0.0003]
lr_per_minibatch= [x*minibatch_size for x in lr_per_sample]
lr_schedule = learning_rate_schedule(lr_per_minibatch, UnitType.minibatch, epoch_size)
momentum_as_time_constant = momentum_as_time_constant_schedule(700)
learner = fsadagrad(criterion.parameters,
lr=lr_schedule, momentum=momentum_as_time_constant,
gradient_clipping_threshold_per_sample=15,
gradient_clipping_with_truncation=True)
progress_printer = ProgressPrinter(freq=1000, first=10, tag='Training', num_epochs=max_epochs)
trainer = Trainer(model, criterion, learner, progress_printer)
log_number_of_parameters(model)
t = 0
for epoch in range(max_epochs):
epoch_end = (epoch+1) * epoch_size
while(t < epoch_end):
data = reader.next_minibatch(minibatch_size, input_map={
criterion.arguments[0]: reader.streams.i1,
criterion.arguments[1]: reader.streams.i2,
criterion.arguments[2]: reader.streams.labels
})
trainer.train_minibatch(data)
t += data[criterion.arguments[1]].num_samples
trainer.summarize_training_progress()
错误是这样的:
Different minibatch layouts detected (difference in sequence lengths or count or start flags) in data specified for the Function's arguments 'Input('i2', [#, *], [132033])' vs. 'Input('i1', [#, *], [132033])', though these arguments have the same dynamic axes '[*, #]'
我注意到如果我选择两个输入序列长度相同的示例,那么训练函数就可以工作.不幸的是,这代表了非常少量的数据.处理具有不同数据长度的序列的正确机制是什么?我是否需要填充输入(类似于Keras的pad_sequence())?
解决方法:
两个序列i1和i2被意外地视为具有相同的长度.这是因为sequence.input(…)的sequence_axis参数的默认值为default_dynamic_axis().解决这个问题的一种方法是告诉CNTK这两个序列的长度不同,只需给每个序列分配一个这样的序列:
i1_axis = C.Axis.new_unique_dynamic_axis('1')
i2_axis = C.Axis.new_unique_dynamic_axis('2')
i1 = sequence.input(shape=num_tokens, is_sparse=True, sequence_axis=i1_axis, name='i1')
i2 = sequence.input(shape=num_tokens, is_sparse=True, sequence_axis=i2_axis, name='i2')
内容总结
以上是互联网集市为您收集整理的python – CNTK序列模型错误:检测到不同的小批量布局全部内容,希望文章能够帮你解决python – CNTK序列模型错误:检测到不同的小批量布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。