小编Leo*_*eed的帖子

TensorFlow 的 Print 或 K.print_tensor 不打印损失函数中的中间张量

我为 Keras 模型编写了一个相当复杂的损失函数,它nan在训练时不断返回。因此,我需要在训练时打印中间张量。我知道您不能在损失函数中执行 K.eval,因为张量未初始化。但是,我已经尝试过K.print_tensor()tf.Print()并且都没有奏效。

我几乎想做这样的事情:

def mean_squared_error(y_true, y_pred):
    print("mean_squared_error")
    loss = K.mean(K.square(y_pred - y_true), axis=-1)
    loss = tf.Print(loss, [loss])
    return loss
model.compile(optimizer=self.optimizer, loss=mean_squared_error)
Run Code Online (Sandbox Code Playgroud)

在实践中,我会mean_squared_error用我的自定义损失替换。“mean_squared_error”会被打印出来,但不会打印我尝试使用 TensorFlow 打印(也不是 Keras 打印)打印的值。我还尝试了与如何在 Keras 训练期间在损失函数内部打印完全相同的代码我仍然没有看到控制台中打印出任何内容。

另外,我写了一个单独的文件来测试一些东西。

import tensorflow as tf
import keras.backend as K

input1 = K.constant(1)
input2 = K.constant(2)
input3 = K.constant(3)

node1 = tf.add(input1, input2)
print_output = K.print_tensor(node1)
output = tf.multiply(print_output, input3)
Run Code Online (Sandbox Code Playgroud)

也没有打印任何内容。

我是否错误地使用了 TensorFlowPrint和 Keras print_tensor?或者结果打印在其他地方?我尝试使用我的控制台测试 stderr …

python keras tensorflow

5
推荐指数
1
解决办法
2660
查看次数

Python - 继续在'elif'语句中报告语法错误

我是一名正在学习Python的初学程序员.在构建经典游戏'Pong!'时,我遇到了这个问题,其中Python一直告诉我,我在elif语句上有语法错误.但是,我只是找不到我的问题所在...这是我的一些代码:

def draw(canvas):
    global score1, score2, pad1_pos, pad2_pos, ball_vel, RIGHT, c
        # update ball
    '''reflections on the left wall'''
    if (ball_pos[0] == BALL_RADIUS + PAD_WIDTH) and (ball_pos in [range(pad1_pos, pad1_pos + PAD_HEIGHT)]):
        print ball_pos
        ball_vel[0] = - ball_vel[1]
        ball_vel[1] = ball_vel[1]
        c += 10

    '''scoring of paddle1'''
    elif (ball_pos[0] == BALL_RADIUS + PAD_WIDTH) and not (ball_pos in [range(pad1_pos, pad1_pos + PAD_HEIGHT)]):
        score2 += 1
        spawn_ball()
        RIGHT = False

    '''reflections on the right wall'''
    elif ball_pos[0] == (WIDTH - 1) - …
Run Code Online (Sandbox Code Playgroud)

python

-6
推荐指数
1
解决办法
118
查看次数

标签 统计

python ×2

keras ×1

tensorflow ×1