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

CNTK Python API: access layers after loading model

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

我创建的模型如下:

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

然后我加载模型:

def load_my_model(vocab_dim, hidden_dim):

    z=load_model("models/lm_epoch0.dnn")
    input_sequence_before = z.arguments[0]
    input_sequence_after = z.arguments[1]
    a=z.ForwardLayer
    b=z.BackwardLayer
    latent_vector = C.splice(a(input_sequence_before), b(input_sequence_after))

我得到一个错误:TypeError("argument ForwardRecurrence's type SequenceOver[inputAxis1][Tensor[100]] is incompatible with the type SequenceOver[inputAxis1][SparseTensor[50000]] of the passed Variable",)

看起来由名称 (z.ForwardLayer) 引用的层表示来自层直接输入的函数。我如何计算 "latent_vector"(我需要这个变量来创建交叉熵和损失函数以继续训练)?

根据错误,与 ForwardLayer 预期的 (100) 相比,您输入序列的维度 (5000) 太大。

当你通过z.ForwardLayerselect节点ForwardLayer时,你只是select那个非常具体的node/layer,而不是layers/nodes/rest的计算连接到它的图表。

你应该做 a = C.combine([z.ForwardLayer.owner]) 你应该没问题。