如何在CNTK中设置特定操作的设备?

时间:2017-07-24 22:41:52

标签: python cntk

我在cntk中有模型,它包含一个嵌入层和一个跟随LSTM。我想将嵌入查找操作放在CPU上,将其余部分放在GPU上 在tensorflow中,我会做

with tf.device("/cpu:0"):

cntk中的等价物是什么,我可以把它包装成:

C.layers.Embedding(embedding_size)

1 个答案:

答案 0 :(得分:1)

在CNTK中,设备控制是特定功能/网络的前向/后向传递,而不是单独的操作。如果要在CPU中执行特定操作而在GPU中执行其余操作,则必须执行以下操作:

features = C.input_variable(input_dim)
labels = C.input_variable(label_dim)
embedding_input = C.input_variable(embedding_size, needs_gradient=True)
embedding = C.layers.Embedding(embedding_size)(features)
loss = rest_of_your_network(embedding_input, labels)
emb_state, embedding_value = embedding.forward({features: some_data}, keep_for_backward=set(embedding.output), device=C.cpu(), as_numpy=False)
loss_state, loss_value = loss.forward({embedding_input: embedding_value}, keep_for_backward=set(loss.output), device=C.gpu(0), as_numpy=False)
loss_grad_dict = loss.backward(loss_state, {loss.output: np.ones_like(loss_value)}, set(loss.parameters + [embedding_input]))
emb_grad_dict = embedding.backward(emb_state, {embedding.output: loss_grad_dict[embedding_input]}, set(embedding.parameters))
# use these dictionaries to update the parameters with a learner