Python 如何打印Keras';使用Tensorflow后端时的SGD优化器

Python 如何打印Keras';使用Tensorflow后端时的SGD优化器,python,tensorflow,keras,Python,Tensorflow,Keras,我想写一个Keras的SGD变体,它允许在指定的迭代中离散地改变步长。我正在使用Tensorflow后端 为了帮助调试,我试图让优化器的get\u updates方法将消息打印给我,但我似乎做不到。我试过标准的print语句和tf.print,但都不起作用。直接来自Keras SGD optimizer cldass的相关代码如下所示: @interfaces.legacy_get_updates_support def get_updates(self, loss, params):

我想写一个Keras的SGD变体,它允许在指定的迭代中离散地改变步长。我正在使用Tensorflow后端

为了帮助调试,我试图让优化器的
get\u updates
方法将消息打印给我,但我似乎做不到。我试过标准的print语句和tf.print,但都不起作用。直接来自Keras SGD optimizer cldass的相关代码如下所示:

@interfaces.legacy_get_updates_support
def get_updates(self, loss, params):
    print (" -------------------------> Getting updates <------------------------------------------")
    grads = self.get_gradients(loss, params)
    self.updates = [K.update_add(self.iterations, 1)]
    tf.Print(self.iterations,
             [self.iterations],
             message="-------------------------------> GETTING UPDATES <----------------------------------------")

    lr = self.lr
    if self.initial_decay > 0:
        lr = lr * (1. / (1. + self.decay * K.cast(self.iterations,
                                                  K.dtype(self.decay))))

    # momentum
    shapes = [K.int_shape(p) for p in params]
    moments = [K.zeros(shape) for shape in shapes]
    self.weights = [self.iterations] + moments
    for p, g, m in zip(params, grads, moments):
        v = self.momentum * m - lr * g  # velocity
        self.updates.append(K.update(m, v))

        if self.nesterov:
            new_p = p + self.momentum * v - lr * g
        else:
            new_p = p + v

        # Apply constraints.
        if getattr(p, 'constraint', None) is not None:
            new_p = p.constraint(new_p)

        self.updates.append(K.update(p, new_p))
    return self.updates
@interfaces.legacy\u获取更新\u支持
def get_更新(自身、丢失、参数):

打印(“--------------------------->获取更新获取更新我想我知道这里发生了什么

  • 我的
    print
    语句仅在调用
    get\u updates
    时生成输出。它只调用一次,并返回用于实际计算更新的图(子图?)

  • 我的
    tf.Print
    不会产生任何输出,因为我从未将其显式地放入计算图中


  • 我想我知道这里发生了什么

  • 我的
    print
    语句仅在调用
    get\u updates
    时生成输出。它只调用一次,并返回用于实际计算更新的图(子图?)

  • 我的
    tf.Print
    不会产生任何输出,因为我从未将其显式地放入计算图中


  • 为了在图形模式下使用
    tf.print
    ,您可以使用
    tf.print
    作为
    tf.print
    的替代品,只需在
    模型函数中执行张量之前强制执行
    tf.print
    操作。您可以查看详细信息。

    为了使用
    tf.print
    在图形模式下,您可以使用
    tf.print
    作为
    tf.print
    的替代品,您只需在
    模型函数中执行张量之前强制执行
    tf.print
    操作。您可以查看详细信息