本文整理汇总了C++中ComputationNodeBasePtr类的典型用法代码示例。如果您正苦于以下问题:C++ ComputationNodeBasePtr类的具体用法?C++ ComputationNodeBasePtr怎么用?C++ ComputationNodeBasePtr使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ComputationNodeBasePtr类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReplaceFinalCriterionNode
// add a new criterion node and at the same time orphan the previous one (it won't be removed)
// The newNode can have the same name and come with pre-connected inputs, which will be used to connect to existing nodes of the same name.
// BUGBUG: Can this operate on both new and existing nodes?
void ComputationNetwork::ReplaceFinalCriterionNode(wstring oldNodeName, ComputationNodeBasePtr newNode)
{
InvalidateCompiledNetwork();
// remove old criterion node
// BUGBUG: The old node is not removed from the network. Seems strangely inconsistent.
bool wasThere = RemoveFromNodeGroup(L"criterion", GetNodeFromName(oldNodeName));
if (!wasThere)
RuntimeError("ReplaceFinalCriterionNode: The node to be replaced is not a criterion node.");
// replace children
// This looks for nodes in the network that have the same name as its current inputs, and then relinks its inputs to those.
// I.e. this allows to move a node from network to another and reconnect by the names if its inputs.
for (int i = 0; i < newNode->GetNumInputs(); ++i)
{
if (m_nameToNodeMap.find(newNode->GetInputs()[i]->NodeName()) == m_nameToNodeMap.end())
RuntimeError("Child node %ls is not part of the network.", newNode->GetInputs()[i]->NodeName().c_str());
newNode->SetInput(i, m_nameToNodeMap[newNode->GetInputs()[i]->NodeName()]);
}
// add it to the network
AddNodeToNetIfNotYet(newNode);
// add new node to criterion node group
AddToNodeGroup(L"criterion", newNode);
}
示例2: GetNodeFromName
// replace a named node by newNode of the same type under the same name, including moving over all network links
// This is used in
// 1. Update nodes to quantized versions.
// 2. The KL-reg based adaptation to reduce feature copy (deprecated)
// need to update all the mappings as well childrens.
void ComputationNetwork::ReplaceNode(wstring nodeName, ComputationNodeBasePtr newNode)
{
ComputationNodeBasePtr oldNode = GetNodeFromName(nodeName);
if (newNode->NodeName() != nodeName) // TODO: This was not tested for earlier; I hope no code depends on this.
InvalidArgument("ChangeNode: newNode must have the same name as the old node.");
InvalidateCompiledNetwork();
// change all nodes that have old node as input to point to the new node instead
ChangeNodeInputs(oldNode, newNode);
// change all inputs of this new node to share the old one's inputs
for (int i = 0; i < oldNode->GetNumInputs(); i++)
{
newNode->SetInput(i, oldNode->GetInputs()[i]); // TODO: use AttachInput()?
//oldNode->SetInput(i, nullptr); // BUGBUG: old node should no longer point into the network
}
// replace the node in the network
RemoveNodeFromNet(oldNode);
AddNodeToNet(newNode);
// also update node groups
for (auto groupIter : GetAllNodeGroups())
{
auto& group = *groupIter;
for (int i = 0; i < group.size(); i++)
if (group[i] == oldNode)
group[i] = newNode;
}
}
示例3: InvalidateCompiledNetwork
// only copy a complete independent tree
// when node name exists
void ComputationNetwork::CopySubTree(const ComputationNetwork& fromNet,
const std::wstring fromName, std::wstring toNamePrefix,
const CopyNodeFlags flags)
{
InvalidateCompiledNetwork();
if (!(flags & CopyNodeFlags::copyNodeValue))
LogicError("CopySubTree: you cannot copy a tree without copying the node values.");
ComputationNodeBasePtr fromRoot = fromNet.GetNodeFromName(fromName);
if (!fromNet.EvalOrderExists(fromRoot))
const_cast<ComputationNetwork&>(fromNet).FormEvalOrder(fromRoot);
for (const auto& fromNode : fromNet.GetEvalOrder(fromRoot)) // BUGBUG: This probably will fail because the precomputed eval orders are invalid at this point.
{
wstring fromNodeName = fromNode->NodeName();
wstring toNodeName = toNamePrefix + fromNodeName;
ComputationNodeBasePtr toNode = CopyNode(fromNet, fromNodeName,
toNodeName,
CopyNodeFlags::copyNodeValue);
if (flags & CopyNodeFlags::copyNodeInputLinks)
{
// copy the children structure but use the new nodes generated
for (int i = 0; i < fromNode->GetNumInputs(); i++)
toNode->SetInput(i, GetNodeFromName(toNamePrefix + fromNode->GetInputs()[i]->NodeName()));
}
}
}
示例4: RenameNode
void ComputationNetwork::RenameNode(ComputationNodeBasePtr node, const std::wstring& newNodeName)
{
// TODO: check if new name exists
m_nameToNodeMap.erase(node->NodeName());
node->SetNodeName(newNodeName);
AddNodeToNet(node);
}
示例5: InvalidateCompiledNetwork
// change the node associated with nodeName to newNode; used in the KL-reg based adaptation to reduce feature copy
// need to update all the mappings as well childrens
void ComputationNetwork::ChangeNode(wstring nodeName, ComputationNodeBasePtr newNode)
{
InvalidateCompiledNetwork();
ComputationNodeBasePtr oldNode = GetNodeFromName(nodeName);
if (oldNode->OperationName() != newNode->OperationName())
InvalidArgument("newNode must have the same type as the old node.");
// change children
for (auto nodeIter = m_nameToNodeMap.begin(); nodeIter != m_nameToNodeMap.end(); nodeIter++)
{
ComputationNodeBasePtr node = nodeIter->second;
for (int i = 0; i < node->GetNumInputs(); i++)
if (node->GetInputs()[i] == oldNode)
node->SetInput(i, newNode);
}
// change name map
m_nameToNodeMap[nodeName] = newNode;
for (int i = 0; i < oldNode->GetNumInputs(); i++)
newNode->SetInput(i, oldNode->GetInputs()[i]);
// change other maps
for (auto groupIter : GetAllNodeGroups())
{
auto& group = *groupIter;
for (int i = 0; i < group.size(); i++)
if (group[i] == oldNode)
group[i] = newNode;
}
}
示例6: ReplaceFinalCriterionNode
void ComputationNetwork::ReplaceFinalCriterionNode(wstring oldNodeName, ComputationNodeBasePtr newNode)
{
InvalidateCompiledNetwork();
// Checks if the node is a criterion node.
int index = -1;
for (int i = 0; i < m_finalCriteria.size(); ++i)
{
if (m_finalCriteria[i]->NodeName() == oldNodeName)
{
index = i;
break;
}
}
if (index == -1)
RuntimeError("ReplaceFinalCriterionNode: the node to be replaced is not a criterion node.");
// Replaces children.
for (int i = 0; i < newNode->GetNumInputs(); ++i)
{
if (m_nameToNodeMap.find(newNode->GetInputs()[i]->NodeName()) == m_nameToNodeMap.end())
RuntimeError("Child node does not exist.");
newNode->SetInput(i, m_nameToNodeMap[newNode->GetInputs()[i]->NodeName()]);
}
// Addes it to criterion node list.
m_finalCriteria[index] = newNode;
m_nameToNodeMap[newNode->NodeName()] = newNode;
}
示例7: SetLearnableNodesBelowLearningRateMultiplier
// sets m_learningRateMultiplier in all LearnableParameters feeding into the passed rootNode
// Called from MEL
void ComputationNetwork::SetLearnableNodesBelowLearningRateMultiplier(const float learningRateMultiplier, const ComputationNodeBasePtr& rootNode)
{
// find nodes from all available nodes
if (rootNode == nullptr)
{
for (auto nodeIter = m_nameToNodeMap.begin(); nodeIter != m_nameToNodeMap.end(); nodeIter++)
{
ComputationNodeBasePtr node = nodeIter->second;
if (node->OperationName() == OperationNameOf(LearnableParameter))
node->SetLearningRateMultiplier(learningRateMultiplier);
}
}
else
{
// for calculating a specific node
if (!EvalOrderExists(rootNode))
const_cast<ComputationNetwork&>(*this).FormEvalOrder(rootNode);
for (const auto& node : GetAllNodesForRoot(rootNode))
{
if (node->OperationName() == OperationNameOf(LearnableParameter))
node->SetLearningRateMultiplier(learningRateMultiplier);
}
}
}
示例8: ChangeNodeInputs
// change all nodes that have fromNode as input to have toNode as input instead
void ComputationNetwork::ChangeNodeInputs(ComputationNodeBasePtr fromNode, ComputationNodeBasePtr toNode)
{
for (auto nodeIter = m_nameToNodeMap.begin(); nodeIter != m_nameToNodeMap.end(); nodeIter++)
{
ComputationNodeBasePtr node = nodeIter->second;
for (int i = 0; i < node->GetNumInputs(); i++)
if (node->GetInputs()[i] == fromNode)
node->SetInput(i, toNode);
}
}
示例9:
VariableLayout CNTKEvalExtended<ElemType>::ToVariableLayout(const ComputationNodeBasePtr n)
{
auto matrix = dynamic_pointer_cast<Matrix<ElemType>>(n->ValuePtr());
return VariableLayout
{
/* name */ n->GetName(),
/* type */ sizeof(ElemType) == sizeof(float) ? VariableLayout::Float32 : VariableLayout::Float64,
/* storage */ matrix ? matrix->GetMatrixType() == MatrixType::DENSE ? VariableLayout::Dense :
matrix->GetMatrixType() == MatrixType::SPARSE ? VariableLayout::Sparse :
VariableLayout::Undetermined :
VariableLayout::Undetermined,
/* dimension */ n->GetSampleLayout().GetNumElements()
};
}
示例10: GatherLoopNodesR
// traverse sub-graph feeding this node (which is a top-level node at start, e.g. training criterion) and list
// - all nodes that participate in a loop -> recurrentResult[loopId][]
// - all nodes that don't -> noRecurrentResult[]
// in order of traversal (depth-first).
// This is part of the FormRecurrentLoops() process, and only called from there from one place.
void ComputationNetwork::GatherLoopNodesR(const ComputationNodeBasePtr& node, unordered_set<ComputationNodeBasePtr>& visited,
map<int, list<ComputationNodeBasePtr>>& recurrentResult,
list<ComputationNodeBasePtr>& noRecurrentResult)
{
if (visited.find(node) != visited.end())
return; // do each node only once
visited.insert(node);
for (int i = 0; i < node->GetNumInputs(); i++)
GatherLoopNodesR(node->Input(i), visited, recurrentResult, noRecurrentResult);
if (node->m_loopId >= 0)
recurrentResult[node->m_loopId].push_back(node);
else
noRecurrentResult.push_back(node);
}
示例11: AddFeatureNode
void ComputationNetwork::AddFeatureNode(ComputationNodeBasePtr featureNode)
{
InvalidateCompiledNetwork();
wstring nodeName = featureNode->NodeName();
if (NodeNameExists(nodeName))
RuntimeError("AddFeatureNode: feature node already exists.");
m_nameToNodeMap[nodeName] = featureNode;
m_features.push_back(featureNode);
}
示例12: SetLearnableNodesBelowNeedGradient
// sets m_parameterUpdateRequired in all LearnableParameters feeding into the passed rootNode
// Called from MEL --TODO: correct?
void ComputationNetwork::SetLearnableNodesBelowNeedGradient(const bool needGradient, const ComputationNodeBasePtr& rootNode)
{
// find nodes from all available nodes
if (rootNode == nullptr)
{
for (auto nodeIter = m_nameToNodeMap.begin(); nodeIter != m_nameToNodeMap.end(); nodeIter++)
{
ComputationNodeBasePtr node = nodeIter->second;
if (node->OperationName() == OperationNameOf(LearnableParameter))
node->SetParameterUpdateRequired(needGradient);
}
}
else
{
// for calculating a specific node
for (const auto& node : GetEvalOrder(rootNode))
{
if (node->OperationName() == OperationNameOf(LearnableParameter))
node->SetParameterUpdateRequired(needGradient);
}
}
}
示例13: RenameNode
void ComputationNetwork::RenameNode(ComputationNodeBasePtr node, const std::wstring& newNodeName)
{
// make sure the new name is not already used
auto iter = m_nameToNodeMap.find(newNodeName);
if (iter != m_nameToNodeMap.end()) // found
RuntimeError("RenameNode: Target name already exists.");
InvalidateCompiledNetwork();
RemoveNodeFromNet(node); // take it out remporarily
node->SetNodeName(newNodeName); // change the name
AddNodeToNet(node); // and put it back
}
示例14: DetermineLoopForwardOrder
// recovers the processing order within a recurrent loop
// TODO: Once we only use the nested network for recurrent traversal, this will be no longer necessary.
void ComputationNetwork::DetermineLoopForwardOrder(unordered_set<ComputationNodeBasePtr>& visited,
unordered_set<ComputationNodeBasePtr>& recStack,
list<ComputationNodeBasePtr>& nodesStack,
ComputationNodeBasePtr cur)
{
if (visited.find(cur) == visited.end())
{
visited.insert(cur);
recStack.insert(cur);
if (GetRecurrenceSteppingDirection(cur) == 0) // recurrence stops at delay nodes
{
for (size_t i = 0; i < cur->GetNumInputs(); i++)
if (cur->Input(i)->m_loopId == cur->m_loopId)
DetermineLoopForwardOrder(visited, recStack, nodesStack, cur->Input(i));
}
recStack.erase(cur);
nodesStack.push_back(cur);
}
else if (recStack.find(cur) != recStack.end())
LogicError("%ls %ls operation is part of an infinite loop that cannot be unrolled.", cur->NodeName().c_str(), cur->OperationName().c_str());
}
示例15: SetBatchNormlizationNodesBelowEvalMode
void ComputationNetwork::SetBatchNormlizationNodesBelowEvalMode(const bool evalMode, const ComputationNodeBasePtr& rootNode /* = nullptr */)
{
vector<ComputationNodeBasePtr> nodes;
if (rootNode == nullptr)
{
for (auto pair : m_nameToNodeMap)
{
nodes.push_back(pair.second);
}
}
else
{
auto allnodes = rootNode->EnumerateNodes();
for (auto node : allnodes)
nodes.push_back(node);
}
for (auto& node : nodes)
{
if (node->OperationName() == OperationNameOf(BatchNormalizationNode))
{
auto pNode = dynamic_pointer_cast<BatchNormalizationNode<float>>(node);
if (!pNode)
{
auto pNode2 = dynamic_pointer_cast<BatchNormalizationNode<double>>(node);
if (!pNode2)
{
RuntimeError("Invalid node type: node name=%ls. We assume either BatchNormalizationNode<float> or BatchNormalizationNode<double>\n", node->NodeName().c_str());
}
}
else
{
pNode->SetEvalMode(evalMode);
}
}
}
}