blob: a81a68ab43a634731423ec5d2b315378c2b94bbe [file] [log] [blame]
<!DOCTYPE html>
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html lang=" en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/versions/master/assets/img/mxnet-icon.png" rel="icon" type="image/png"><!-- Begin Jekyll SEO tag v2.6.1 -->
<title>Use TensorInspector to Help Debug Operators | Apache MXNet</title>
<meta name="generator" content="Jekyll v4.0.0" />
<meta property="og:title" content="Use TensorInspector to Help Debug Operators" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="A flexible and efficient library for deep learning." />
<meta property="og:description" content="A flexible and efficient library for deep learning." />
<link rel="canonical" href="https://mxnet.apache.org/versions/master/api/faq/tensor_inspector_tutorial" />
<meta property="og:url" content="https://mxnet.apache.org/versions/master/api/faq/tensor_inspector_tutorial" />
<meta property="og:site_name" content="Apache MXNet" />
<script type="application/ld+json">
{"url":"https://mxnet.apache.org/versions/master/api/faq/tensor_inspector_tutorial","headline":"Use TensorInspector to Help Debug Operators","description":"A flexible and efficient library for deep learning.","@type":"WebPage","@context":"https://schema.org"}</script>
<!-- End Jekyll SEO tag -->
<link rel="stylesheet" href="/versions/master/assets/docsearch.min.css" /><link rel="stylesheet" href="/versions/master/assets/main.css"><link type="application/atom+xml" rel="alternate" href="https://mxnet.apache.org/versions/master/feed.xml" title="Apache MXNet" /><!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
/* We explicitly disable cookie tracking to avoid privacy issues */
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '23']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
<script src="/versions/master/assets/js/jquery-3.3.1.min.js"></script>
<script src="/versions/master/assets/js/docsearch.min.js"></script><script src="/versions/master/assets/js/globalSearch.js" defer></script>
<script src="/versions/master/assets/js/clipboard.js" defer></script>
<script src="/versions/master/assets/js/copycode.js" defer></script></head>
<body><header class="site-header" role="banner">
<script>
$(document).ready(function () {
// HEADER OPACITY LOGIC
function opacity_header() {
var value = "rgba(4,140,204," + ($(window).scrollTop() / 300 + 0.4) + ")"
$('.site-header').css("background-color", value)
}
$(window).scroll(function () {
opacity_header()
})
opacity_header();
// MENU SELECTOR LOGIC
$('.page-link').each( function () {
if (window.location.href.includes(this.href)) {
$(this).addClass("page-current");
}
});
})
</script>
<div class="wrapper">
<a class="site-title" rel="author" href="/versions/master/"><img
src="/versions/master/assets/img/mxnet_logo.png" class="site-header-logo"></a>
<nav class="site-nav">
<input type="checkbox" id="nav-trigger" class="nav-trigger"/>
<label for="nav-trigger">
<span class="menu-icon">
<svg viewBox="0 0 18 15" width="18px" height="15px">
<path d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.032C17.335,0,18,0.665,18,1.484L18,1.484z M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.032C17.335,6.031,18,6.696,18,7.516L18,7.516z M18,13.516C18,14.335,17.335,15,16.516,15H1.484 C0.665,15,0,14.335,0,13.516l0,0c0-0.82,0.665-1.483,1.484-1.483h15.032C17.335,12.031,18,12.695,18,13.516L18,13.516z"/>
</svg>
</span>
</label>
<div class="gs-search-border">
<div id="gs-search-icon"></div>
<form id="global-search-form">
<input id="global-search" type="text" title="Search" placeholder="Search" />
<div id="global-search-dropdown-container">
<button class="gs-current-version btn" type="button" data-toggle="dropdown">
<span id="gs-current-version-label">master</span>
<svg class="gs-dropdown-caret" viewBox="0 0 32 32" class="icon icon-caret-bottom" aria-hidden="true">
<path class="dropdown-caret-path" d="M24 11.305l-7.997 11.39L8 11.305z"></path>
</svg>
</button>
<ul class="gs-opt-group gs-version-dropdown">
<li class="gs-opt gs-versions active">master</li>
<li class="gs-opt gs-versions">1.9.1</li>
<li class="gs-opt gs-versions">1.8.0</li>
<li class="gs-opt gs-versions">1.7.0</li>
<li class="gs-opt gs-versions">1.6.0</li>
<li class="gs-opt gs-versions">1.5.0</li>
<li class="gs-opt gs-versions">1.4.1</li>
<li class="gs-opt gs-versions">1.3.1</li>
<li class="gs-opt gs-versions">1.2.1</li>
<li class="gs-opt gs-versions">1.1.0</li>
<li class="gs-opt gs-versions">1.0.0</li>
<li class="gs-opt gs-versions">0.12.1</li>
<li class="gs-opt gs-versions">0.11.0</li>
</ul>
</div>
<span id="global-search-close">x</span>
</form>
</div>
<div class="trigger">
<div id="global-search-mobile-border">
<div id="gs-search-icon-mobile"></div>
<input id="global-search-mobile" placeholder="Search..." type="text"/>
<div id="global-search-dropdown-container-mobile">
<button class="gs-current-version-mobile btn" type="button" data-toggle="dropdown">
<svg class="gs-dropdown-caret" viewBox="0 0 32 32" class="icon icon-caret-bottom" aria-hidden="true">
<path class="dropdown-caret-path" d="M24 11.305l-7.997 11.39L8 11.305z"></path>
</svg>
</button>
<ul class="gs-opt-group gs-version-dropdown-mobile">
<li class="gs-opt gs-versions active">master</li>
<li class="gs-opt gs-versions">1.9.1</li>
<li class="gs-opt gs-versions">1.8.0</li>
<li class="gs-opt gs-versions">1.7.0</li>
<li class="gs-opt gs-versions">1.6.0</li>
<li class="gs-opt gs-versions">1.5.0</li>
<li class="gs-opt gs-versions">1.4.1</li>
<li class="gs-opt gs-versions">1.3.1</li>
<li class="gs-opt gs-versions">1.2.1</li>
<li class="gs-opt gs-versions">1.1.0</li>
<li class="gs-opt gs-versions">1.0.0</li>
<li class="gs-opt gs-versions">0.12.1</li>
<li class="gs-opt gs-versions">0.11.0</li>
</ul>
</div>
</div>
<a class="page-link" href="/versions/master/get_started">Get Started</a>
<a class="page-link" href="/versions/master/features">Features</a>
<a class="page-link" href="/versions/master/ecosystem">Ecosystem</a>
<a class="page-link" href="/versions/master/api">Docs & Tutorials</a>
<a class="page-link" href="/versions/master/trusted_by">Trusted By</a>
<a class="page-link" href="https://github.com/apache/mxnet">GitHub</a>
<div class="dropdown" style="min-width:100px">
<span class="dropdown-header">Apache
<svg class="dropdown-caret" viewBox="0 0 32 32" class="icon icon-caret-bottom" aria-hidden="true"><path class="dropdown-caret-path" d="M24 11.305l-7.997 11.39L8 11.305z"></path></svg>
</span>
<div class="dropdown-content" style="min-width:250px">
<a href="https://www.apache.org/foundation/">Apache Software Foundation</a>
<a href="https://www.apache.org/licenses/">License</a>
<a href="/versions/master/api/faq/security.html">Security</a>
<a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy</a>
<a href="https://www.apache.org/events/current-event">Events</a>
<a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a>
<a href="https://www.apache.org/foundation/thanks.html">Thanks</a>
</div>
</div>
<div class="dropdown">
<span class="dropdown-header">master
<svg class="dropdown-caret" viewBox="0 0 32 32" class="icon icon-caret-bottom" aria-hidden="true"><path class="dropdown-caret-path" d="M24 11.305l-7.997 11.39L8 11.305z"></path></svg>
</span>
<div class="dropdown-content">
<a class="dropdown-option-active" href="/">master</a>
<a href="/versions/1.9.1/">1.9.1</a>
<a href="/versions/1.8.0/">1.8.0</a>
<a href="/versions/1.7.0/">1.7.0</a>
<a href="/versions/1.6.0/">1.6.0</a>
<a href="/versions/1.5.0/">1.5.0</a>
<a href="/versions/1.4.1/">1.4.1</a>
<a href="/versions/1.3.1/">1.3.1</a>
<a href="/versions/1.2.1/">1.2.1</a>
<a href="/versions/1.1.0/">1.1.0</a>
<a href="/versions/1.0.0/">1.0.0</a>
<a href="/versions/0.12.1/">0.12.1</a>
<a href="/versions/0.11.0/">0.11.0</a>
</div>
</div>
</div>
</nav>
</div>
</header>
<main class="page-content" aria-label="Content">
<script>
</script>
<article class="post">
<header class="post-header wrapper">
<h1 class="post-title">Use TensorInspector to Help Debug Operators</h1>
<h3></h3></header>
<div class="post-content">
<div class="wrapper">
<div class="row">
<div class="col-3 docs-side-bar">
<h3 style="text-transform: capitalize; padding-left:10px">faq</h3>
<ul>
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/add_op_in_backend">A Beginner's Guide to Implementing Operators in MXNet Backend</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/cloud">MXNet on the Cloud</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/distributed_training">Distributed Training in MXNet</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/env_var">Environment Variables</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/float16">Float16</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/large_tensor_support">Using MXNet with Large Tensor Support</a></li>
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/model_parallel_lstm">Model Parallel</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/new_op">Create New Operators</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/perf">Some Tips for Improving MXNet Performance</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/recordio">Create a Dataset Using RecordIO</a></li>
<!-- page-category -->
<li><a href="/versions/master/api/faq/s3_integration">Use data from S3 for training</a></li>
<!-- page-category -->
<li><a href="/versions/master/api/faq/security">MXNet Security Best Practices</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/tensor_inspector_tutorial">Use TensorInspector to Help Debug Operators</a></li>
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/using_rtc">Using runtime compilation (RTC) to write CUDA kernels in MXNet</a></li>
<!-- page-category -->
<!-- page-category -->
<li><a href="/versions/master/api/faq/why_mxnet">Why MXNet came to be?</a></li>
<!-- page-category -->
<!-- page-category -->
<!-- page-category -->
<!-- resource-p -->
</ul>
</div>
<div class="col-9">
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->
<h1 id="use-tensorinspector-to-help-debug-operators">Use TensorInspector to Help Debug Operators</h1>
<h2 id="introduction">Introduction</h2>
<p>When developing new operators, developers need to deal with tensor objects extensively. This new utility, Tensor Inspector, mainly aims to help developers debug by providing unified interfaces to print, check, and dump the tensor value. To developers’ convenience, this utility works for all the three data types: Tensors, TBlobs, and NDArrays. Also, it supports both CPU and GPU tensors.</p>
<h2 id="usage">Usage</h2>
<p>This utility is located in <code class="highlighter-rouge">src/common/tensor_inspector.h</code>. To use it in any operator code, just include it using <code class="highlighter-rouge">#include "{path}/tensor_inspector.h"</code>, construct an <code class="highlighter-rouge">TensorInspector</code> object, and call the APIs on that object. You can run any script that uses the operator you just modified then.</p>
<p>The screenshot below shows a sample usage in <code class="highlighter-rouge">src/operator/nn/convolution-inl.h</code>.</p>
<p><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/faq/tensor_inspector_tutorial/tensor_inspector_example_usage.png" alt="tensor_inspector_example_usage" /></p>
<h2 id="functionalitiesapis">Functionalities/APIs</h2>
<h3 id="create-a-tensorinspector-object-from-tensor-tblob-and-ndarray-objects">Create a TensorInspector Object from Tensor, TBlob, and NDArray Objects</h3>
<p>You can create a <code class="highlighter-rouge">TensorInspector</code> object by passing in two things: 1) an object of type <code class="highlighter-rouge">Tensor</code>, <code class="highlighter-rouge">Tbob</code>, or <code class="highlighter-rouge">NDArray</code>, and 2) an <code class="highlighter-rouge">RunContext</code> object.</p>
<p>Essentially, <code class="highlighter-rouge">TensorInspector</code> can be understood as a wrapper class around <code class="highlighter-rouge">TBlob</code>. Internally, the <code class="highlighter-rouge">Tensor</code>, <code class="highlighter-rouge">Tbob</code>, or <code class="highlighter-rouge">NDArray</code> object that you passed in will be converted to a <code class="highlighter-rouge">TBlob</code> object. The <code class="highlighter-rouge">RunContext</code> object is used when the tensor is a GPU tensor; in such a case, we need to use the context information to copy the data from GPU memory to CPU/main memory.</p>
<p>Following are the three constructors:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Construct from Tensor object</span>
<span class="k">template</span><span class="o">&lt;</span><span class="k">typename</span> <span class="n">Device</span><span class="p">,</span> <span class="kt">int</span> <span class="n">dimension</span><span class="p">,</span> <span class="k">typename</span> <span class="n">DType</span> <span class="n">MSHADOW_DEFAULT_DTYPE</span><span class="o">&gt;</span>
<span class="n">TensorInspector</span><span class="p">(</span><span class="k">const</span> <span class="n">mshadow</span><span class="o">::</span><span class="n">Tensor</span><span class="o">&lt;</span><span class="n">Device</span><span class="p">,</span> <span class="n">dimension</span><span class="p">,</span> <span class="n">DType</span><span class="o">&gt;&amp;</span> <span class="n">ts</span><span class="p">,</span> <span class="k">const</span> <span class="n">RunContext</span><span class="o">&amp;</span> <span class="n">ctx</span><span class="p">);</span>
<span class="c1">// Construct from TBlob object</span>
<span class="n">TensorInspector</span><span class="p">(</span><span class="k">const</span> <span class="n">TBlob</span><span class="o">&amp;</span> <span class="n">tb</span><span class="p">,</span> <span class="k">const</span> <span class="n">RunContext</span><span class="o">&amp;</span> <span class="n">ctx</span><span class="p">);</span>
<span class="c1">// Construct from NDArray object</span>
<span class="n">TensorInspector</span><span class="p">(</span><span class="k">const</span> <span class="n">NDArray</span><span class="o">&amp;</span> <span class="n">arr</span><span class="p">,</span> <span class="k">const</span> <span class="n">RunContext</span><span class="o">&amp;</span> <span class="n">ctx</span><span class="p">)</span><span class="o">:</span>
</code></pre></div></div>
<h3 id="print-tensor-value-static">Print Tensor Value (Static)</h3>
<p>To print out the tensor value in a nicely structured way, you can use this API:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">print_string</span><span class="p">();</span>
</code></pre></div></div>
<p>This API will print the entire tensor to <code class="highlighter-rouge">std::cout</code> and preserve the shape (it supports all dimensions from 1 and up). You can copy the output and interpret it with any <code class="highlighter-rouge">JSON</code> loader. You can find some useful information about the tensor on the last line of the output. Refer to the case below, we are able to know that this is a float-typed tensor with shape 20x1x5x5.</p>
<p><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/faq/tensor_inspector_tutorial/tensor_inspector_to_string.png" alt="tensor_inspector_to_string" /></p>
<p>If instead of printing the tensor to <code class="highlighter-rouge">std::cout</code>, you just need a <code class="highlighter-rouge">string</code>, you can use this API:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="kt">void</span> <span class="nf">to_string</span><span class="p">();</span>
</code></pre></div></div>
<h3 id="interactively-print-tensor-value-dynamic">Interactively Print Tensor Value (Dynamic)</h3>
<p>Sometimes at compilation time, you may not know which part of a tensor to inspect. Also, it may be nice to pause the operator control flow to “zoom into” a specific, erroneous part of a tensor multiple times until you are satisfied. In this regard, you can use this API to interactively inspect the tensor:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">interactive_print</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">tag</span> <span class="o">=</span> <span class="s">""</span><span class="p">)</span> <span class="p">{</span>
</code></pre></div></div>
<p>This API will set a “break point” in your code. When that “break point” is reached, you will enter a loop that will keep asking you for further command input. In the API call, <code class="highlighter-rouge">tag</code> is an optional parameter to give the call a name, so that you can identify it when you have multiple <code class="highlighter-rouge">interactive_print()</code> calls in different parts of your code. A visit count will tell you how many times you stepped into this particular “break point”, should this operator be called more than once. Note that all <code class="highlighter-rouge">interactive_print()</code> calls are properly locked, so you can use it in many different places without issues.</p>
<p><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/faq/tensor_inspector_tutorial/tensor_inspector_interactive_print.png" alt="tensor_inspector_interactive_print" /></p>
<p>There are many useful commands available, as described in the previous screenshot: you can type “e” to print out the entire tensor, “d” to dump the tensor to file (see below), “b” to break from this command loop, and “s” to skip all future <code class="highlighter-rouge">interactive_print()</code>. Most importantly, in this screen, you can specify a part of the tensor that you are particularly interested in and want to print out. For example, for this 64x20x24x24 tensor, you can type in “0, 0” and presss enter to check the sub-tensor with shape 24x24 at coordinate (0, 0).</p>
<h3 id="check-tensor-value">Check Tensor Value</h3>
<p>Sometimes, developers might want to check if the tensor contains unexpected values which could be negative values, NaNs, infinities or others. To facilitate that, you can use these APIs:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">template</span><span class="o">&lt;</span><span class="k">typename</span> <span class="n">ValueChecker</span><span class="o">&gt;</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span> <span class="n">check_value</span><span class="p">(</span><span class="k">const</span> <span class="n">ValueChecker</span><span class="o">&amp;</span> <span class="n">checker</span><span class="p">,</span>
<span class="kt">bool</span> <span class="n">interactive</span> <span class="o">=</span> <span class="nb">false</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">tag</span> <span class="o">=</span> <span class="s">""</span><span class="p">);</span>
<span class="c1">// OR</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span> <span class="n">check_value</span><span class="p">(</span><span class="n">CheckerType</span> <span class="n">ct</span><span class="p">,</span>
<span class="kt">bool</span> <span class="n">interactive</span> <span class="o">=</span> <span class="nb">false</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">tag</span> <span class="o">=</span> <span class="s">""</span><span class="p">);</span>
</code></pre></div></div>
<p>In the first API, <code class="highlighter-rouge">ValueChecker checker</code> is a bool lambda function that takes in a single parameter which is of the same data type as the tensor. For example:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// use the same DType as in the tensor object</span>
<span class="p">[]</span> <span class="p">(</span><span class="n">DType</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="k">return</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">0</span><span class="p">};</span>
</code></pre></div></div>
<p>This checker is called on every value within the tensor. The return of the API is a <code class="highlighter-rouge">vector</code> of all the coordinates where the checker evaluates to <code class="highlighter-rouge">true</code>. The coordinates are themselves represented by <code class="highlighter-rouge">vector&lt;int&gt;</code>. If you set <code class="highlighter-rouge">interactive</code> to true, you will set a “break point” and enter a loop that asks for commands. This is similar to <code class="highlighter-rouge">interactive_print()</code>. You can type “p” to print the coordinates, “b” to break from the loop, and “s” to skip all future “break points” in <code class="highlighter-rouge">interactive_print()</code>. You can also specify a coordinate to print only a part of the tensor or type “e” to print out the entire tensor. Just like <code class="highlighter-rouge">interactive_print()</code>, this this interactive screen is also properly locked.</p>
<p><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/faq/tensor_inspector_tutorial/tensor_inspector_value_check.png" alt="tensor_inspector_value_check" /></p>
<p>Also, there are a bunch of built-int value checkers. Refer to the Enum below:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">enum</span> <span class="n">CheckerType</span> <span class="p">{</span>
<span class="n">NegativeChecker</span><span class="p">,</span> <span class="c1">// check if negative</span>
<span class="n">PositiveChecker</span><span class="p">,</span> <span class="c1">// check if positive</span>
<span class="n">ZeroChecker</span><span class="p">,</span> <span class="c1">// check for zero</span>
<span class="n">NaNChecker</span><span class="p">,</span> <span class="c1">// check if for NaN, will always return false if DType is not a float type</span>
<span class="n">InfChecker</span><span class="p">,</span> <span class="c1">// check for infinity, will always return false if DType is not a float type</span>
<span class="n">PositiveInfChecker</span><span class="p">,</span> <span class="c1">// check for positive infinity,</span>
<span class="c1">// will always return false if DType is not a float type</span>
<span class="n">NegativeInfChecker</span><span class="p">,</span> <span class="c1">// check for nagative infinity,</span>
<span class="c1">// will always return false if DType is not a float type</span>
<span class="n">FiniteChecker</span><span class="p">,</span> <span class="c1">// check if finite, will always return false if DType is not a float type</span>
<span class="n">NormalChecker</span><span class="p">,</span> <span class="c1">// check if it is neither infinity nor NaN</span>
<span class="n">AbnormalChecker</span><span class="p">,</span> <span class="c1">// chekck if it is infinity or nan</span>
<span class="p">};</span>
</code></pre></div></div>
<p>Remember the second API?</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span> <span class="n">check_value</span><span class="p">(</span><span class="n">CheckerType</span> <span class="n">ct</span><span class="p">,</span>
<span class="kt">bool</span> <span class="n">interactive</span> <span class="o">=</span> <span class="nb">false</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">tag</span> <span class="o">=</span> <span class="s">""</span><span class="p">);</span>
</code></pre></div></div>
<p>You can simply pass in a value from <code class="highlighter-rouge">CheckerType</code> where you would have passed in your own lambda if you were using the first API. Note that it’s the developer’s responsibility to pass in a valid value checker.</p>
<h3 id="dump-tensor-value">Dump Tensor Value</h3>
<p>Sometimes, you might want to dump the tensor to a file in binary mode. Then, you might want to use a python script to further analyze the tensor value. Or, you might do that simply because a binary dump has better precision and is faster to load than the output copy-pasted from <code class="highlighter-rouge">print_string()</code> and loaded as a <code class="highlighter-rouge">JSON</code> string. Either way, you can use this API:</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">dump_to_file</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">tag</span><span class="p">);</span>
</code></pre></div></div>
<p>This API will create a file with name “{tag}_{visit_count}.npy”, where tag is the name that we give to the call, and visit is the visit count, should the operated be called more than once.</p>
<p>The output format is <code class="highlighter-rouge">.npy</code>, version 1.0. This is the Numpy format and we can easily load it with the following code:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import numpy as np
a = np.load('abc_1.npy')
print(a)
</code></pre></div></div>
<p>Let’s see how it runs:</p>
<p><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/faq/tensor_inspector_tutorial/tensor_inspector_dump_to_file.png" alt="tensor_inspector_dump_to_file" /></p>
<p>Notice: in <code class="highlighter-rouge">interactive_print()</code>, you could also do value dumping with command “d”. You will be prompted to enter the <code class="highlighter-rouge">tag</code> value:</p>
<p><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/faq/tensor_inspector_tutorial/tensor_inspector_interactive_print.png" alt="tensor_inspector_interactive_print" /></p>
<h3 id="test-coverage-and-limitations">Test Coverage and Limitations</h3>
<p>This utility has been tested on Mac and Ubuntu with and without CUDNN and oneDNN. Supports for <code class="highlighter-rouge">Tensor</code>, <code class="highlighter-rouge">TBlob</code>, and <code class="highlighter-rouge">NDArray</code>, as well as for CPU and GPU have been manually tested.</p>
<p>Currently, this utility only supports non-empty tensors and tensors with known shapes i.e. <code class="highlighter-rouge">tb_.ndim() &gt; 0</code>. Also, this utility only supports dense <code class="highlighter-rouge">NDArray</code> objects, i.e. when the type is <code class="highlighter-rouge">kDefaultStorage</code>.</p>
</div>
</div>
</div>
</div>
</article>
</main><footer class="site-footer h-card">
<div class="wrapper">
<div class="row">
<div class="col-4">
<h4 class="footer-category-title">Resources</h4>
<ul class="contact-list">
<li><a href="/versions/master/community#stay-connected">Mailing lists</a></li>
<li><a href="/versions/master/community#github-issues">Github Issues</a></li>
<li><a href="https://github.com/apache/mxnet/projects">Projects</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home">Developer Wiki</a></li>
<li><a href="https://discuss.mxnet.io">Forum</a></li>
<li><a href="/versions/master/community">Contribute To MXNet</a></li>
</ul>
</div>
<div class="col-4"><ul class="social-media-list"><li><a href="https://github.com/apache/mxnet"><svg class="svg-icon"><use xlink:href="/versions/master/assets/minima-social-icons.svg#github"></use></svg> <span class="username">apache/mxnet</span></a></li><li><a href="https://www.twitter.com/apachemxnet"><svg class="svg-icon"><use xlink:href="/versions/master/assets/minima-social-icons.svg#twitter"></use></svg> <span class="username">apachemxnet</span></a></li><li><a href="https://youtube.com/apachemxnet"><svg class="svg-icon"><use xlink:href="/versions/master/assets/minima-social-icons.svg#youtube"></use></svg> <span class="username">apachemxnet</span></a></li></ul>
</div>
<div class="col-4 footer-text">
<p>A flexible and efficient library for deep learning.</p>
</div>
</div>
</div>
</footer>
<footer class="site-footer2">
<div class="wrapper">
<div class="row">
<div class="col-3">
<img src="/versions/master/assets/img/asf_logo.svg" class="footer-logo col-2">
</div>
<div class="footer-bottom-warning col-9">
</p><p>"Copyright © 2017-2022, The Apache Software Foundation. Licensed under the Apache License, Version 2.0. Apache MXNet, MXNet, Apache, the Apache
feather, and the Apache MXNet project logo are either registered trademarks or trademarks of the
Apache Software Foundation."</p>
</div>
</div>
</div>
</footer>
</body>
</html>