擅长:python、mysql、java
<p>Keras的后端有<code>print_tensor</code>,这使您能够做到这一点。你可以这样使用它:</p>
<pre><code>import keras.backend as K
def loss_fn(y_true, y_pred):
y_true = K.print_tensor(y_true, message='y_true = ')
y_pred = K.print_tensor(y_pred, message='y_pred = ')
...
</code></pre>
<p>函数返回一个相同的张量。当对该张量求值时,它将打印其内容,前面是<code>message</code>。
从<a href="https://keras.io/backend/#print_tensor" rel="noreferrer">Keras docs</a>:</p>
<blockquote>
<p>Note that print_tensor returns a new tensor identical to x which should be used in the following code. Otherwise the print operation is not taken into account during evaluation.</p>
</blockquote>
<p>所以,以后一定要用张量。</p>