标签: tensorflow-gradient

如何用张量板监测keras中的梯度消失和爆炸?

我想用keras监测张量板中的梯度变化,以确定梯度是否消失或爆炸.我该怎么办?

python keras tensorflow tensorboard tensorflow-gradient

9
推荐指数
1
解决办法
3069
查看次数

在Tensorflow训练期间如何打印渐变?

为了调试Tensorflow模型,我需要查看渐变是否已更改或其中是否存在nans。仅在Tensorflow中打印变量不起作用,因为您看到的是:

 <tf.Variable 'Model/embedding:0' shape=(8182, 100) dtype=float32_ref>
Run Code Online (Sandbox Code Playgroud)

我尝试使用tf.Print类,但无法使其工作,我想知道是否可以以这种方式实际使用它。在我的模型中,我有一个训练循环,可以打印每个时期的损耗值:

def run_epoch(session, model, eval_op=None, verbose=False):
    costs = 0.0
    iters = 0
    state = session.run(model.initial_state)
    fetches = {
            "cost": model.cost,
            "final_state": model.final_state,
    }
    if eval_op is not None:
        fetches["eval_op"] = eval_op

    for step in range(model.input.epoch_size):
        feed_dict = {}
        for i, (c, h) in enumerate(model.initial_state):
            feed_dict[c] = state[i].c
            feed_dict[h] = state[i].h

        vals = session.run(fetches, feed_dict)
        cost = vals["cost"]
        state = vals["final_state"]

        costs += cost
        iters += model.input.num_steps

    print("Loss:", costs)

    return costs
Run Code Online (Sandbox Code Playgroud)

插入print(model.gradients[0][1]) …

python variables machine-learning tensorflow tensorflow-gradient

4
推荐指数
1
解决办法
2495
查看次数

OpenAI 梯度检查点与 Tensorflow Eager Execution

我最近切换到 Tensorflow Eager(目前使用 TF 1.8.0)并且非常喜欢它。但是,我现在有一个相当大的模型,当使用计算 TF 中的梯度所需的渐变磁带运行时,该模型不适合我的 GPU 内存(GTX 1080Ti、12GB VRAM)。前向传递(即不使用渐变带)效果很好。

我考虑过使用OpenAI 的梯度检查点,希望这会有所帮助。然而,简单地按照 Git 中的描述使用它似乎对 Eager Execution 没有帮助,即

import tensorflow as tf
import tensorflow.contrib.eager as tfe
import memory_saving_gradients
tf.__dict__["gradients"] = memory_saving_gradients.gradients_memory
# using gradients_memory or gradients_speed does not change anything
# tf.__dict__["gradients"] = memory_saving_gradients.gradients_speed

[...]
with tfe.GradientTape() as g:
    output = run_large_model()
    loss = calculate_loss_on_output(output)
grads = g.gradient(full, model.variables)
optimizer.apply_gradients(zip(grads, model.variables))
Run Code Online (Sandbox Code Playgroud)

内存不足,与是否使用梯度检查点无关。

我的猜测是,梯度磁带仍然存储所有变量以及向后传递所需的信息,并且梯度检查点没有效果,因为 Eager 模式下的 TF 实际上并没有构建图形(据我所知 - 或者至少它是不同的)图形)。

您是否有任何经验或想法如何解决这个问题,或者我需要做什么才能在 TF Eager 模式下使用梯度检查点?

python gradient-descent tensorflow tensorflow-gradient

4
推荐指数
1
解决办法
1666
查看次数

Updating a tensor in tensorflow

I have defined an unsupervised problem in tensorflow, I need to update my B and my tfZ with every iteration, but I don't know how to update my tfZ using the tensorflow session.

tfY = tf.placeholder(shape=(15, 15), dtype=tf.float32)

with tf.variable_scope('test'):
    B = tf.Variable(tf.zeros([]))
    tfZ = tf.convert_to_tensor(Z, dtype=tf.float32)

def loss(tfY):
    r = tf.reduce_sum(tfZ*tfZ, 1)
    r = tf.reshape(r, [-1, 1])
    D = tf.sqrt(r - 2*tf.matmul(tfZ, tf.transpose(tfZ)) + tf.transpose(r) + 1e-9)
    return tf.reduce_sum(tfY*tf.log(tf.sigmoid(D+B))+(1-tfY)*tf.log(1-tf.sigmoid(D+B)))

LOSS = loss(Y)
GRADIENT = tf.gradients(LOSS, [B, tfZ])

sess = …
Run Code Online (Sandbox Code Playgroud)

python variable-assignment tensorflow tensorflow-gradient

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