在Keras上训练时,有什么方法可以调试张量中的值? [英] Is there any way to debug a value inside a tensor while training on Keras?

查看:356
本文介绍了在Keras上训练时,有什么方法可以调试张量中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TensorFlow后端在Keras上实现自己的层.

I'm trying to implement my own layer on Keras with a TensorFlow backend.

训练时有什么方法可以在张量中打印值? 例如,我要在以下代码中打印xself.kernel:

Is there any way to print the value inside tensors while training? For example, I want to print x and self.kernel in the following code:

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(input_shape[1], self.output_dim),
                                      initializer='uniform',
                                      trainable=True)
        super(MyLayer, self).build(input_shape)

    def call(self, x):

        # print x
        # print self.kernel

        return K.dot(x, self.kernel)

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim)

推荐答案

您可以使用keras.backend.print_tensor,它只是一个身份转换,具有打印张量值和可选消息的副作用.例如:

You can use keras.backend.print_tensor, which is just an identity transform that has the side-effect of printing the value of the tensor, and optionally a message. For example:

import keras.backend as K

def call(self, x):

    return K.dot(K.print_tensor(x, message='Value of x'),
                 K.print_tensor(self.kernel, 
                                message='Value of kernel'))

有关更多信息,请参见 https://keras.io/backend/#print_tensor .

See https://keras.io/backend/#print_tensor for more information.

这篇关于在Keras上训练时,有什么方法可以调试张量中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆