CNTK Python API:加载模型后的访问层

时间:2019-06-19 18:45:31

标签: python cntk

加载模型后,我无法访问图层。

我按如下方式创建了模型:

.gradle

然后我加载模型:

def create_model(vocab_dim, hidden_dim):

    input_seq_axis1 = Axis('inputAxis1')
    input_sequence_before = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis1, is_sparse = use_sparse)
    input_sequence_after = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis1, is_sparse = use_sparse)
    e=Sequential([
        C.layers.Embedding(hidden_dim),
        Stabilizer()
        ],name='Embedding')
    a = Sequential([
        e,  
        C.layers.Recurrence(C.layers.LSTM(hidden_dim//2),name='ForwardRecurrence'),
        ],name='ForwardLayer')
    b = Sequential([
        e,  
        C.layers.Recurrence(C.layers.LSTM(hidden_dim//2),go_backwards=True),
       ],name='BackwardLayer')
    latent_vector = C.splice(a(input_sequence_before), b(input_sequence_after))

    bias = C.layers.Parameter(shape = (vocab_dim, 1), init = 0, name='Bias')
    weights = C.layers.Parameter(shape = (vocab_dim, hidden_dim), init = C.initializer.glorot_uniform(), name='Weights')
    z = C.times_transpose(weights, latent_vector,name='Transpose') + bias
    z = C.reshape(z, shape = (vocab_dim))

    return z

我得到一个错误:TypeError(“参数ForwardRecurrence的类型SequenceOver [inputAxis1] [Tensor [100]]与传递的变量的类型SequenceOver [inputAxis1] [SparseTensor [50000]]不兼容”,)

看起来名称(z.ForwardLayer)所引用的图层代表了来自图层立即输入的函数。如何计算“ latent_vector”(我需要此变量来创建交叉熵和损失函数以继续训练)?

1 个答案:

答案 0 :(得分:1)

基于该错误,与您的ForwardLayer(100)相比,输入序列的尺寸太大(5000)。

通过z.ForwardLayer选择节点ForwardLayer时,仅选择非常特定的节点/层,而不选择与其连接的计算图的层/节点/其余部分。

您应该执行a = C.combine([z.ForwardLayer.owner])并且应该没事。