在keras中批量更新期间如何缩放渐变? [英] How to scale the gradient during batch update in keras?

查看:52
本文介绍了在keras中批量更新期间如何缩放渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标准的keras模型,并且正在批量培训(使用train_on_batch函数).现在,我要获取批次中每个元素的梯度并将其缩放(将每个样品梯度乘以我拥有的特定于样品的值),然后在对每个梯度进行缩放后,可以将其求和并用于更新现有的权重.无论如何,给定的keras功能可以做到这一点吗?如果没有,我是否有办法使用张量流来操纵它? (给定了模型,其余的都是用喀拉拉邦写的)

I am using a standard keras model and I am training on batch (using the train_on_batch function). Now, I want to take the gradient of each element in the batch and scale it (multiply each sample gradient with a sample-specific value that I have) and after each gradient has been scaled, then it can be summed and used to update the existing weights. Is there anyway to do this given keras functions? And if not, is there a way for me to manipulate this using tensorflow? (given the model and the rest was written in keras)

函数看起来像这样:(循环是为了说明批次中的所有样品都发生了这种情况)

The function looks like this: (the loop is to illustrate it happens for all samples in the batch)

grad = 0, w= #array of size batch_size
for i in batch_size:
    grad <- grad + w_i*grad_i

推荐答案

  • 在模型的fit方法中使用sample_weights自变量.
  • 或者,如果使用生成器,则使生成器不仅返回X_train, y_train,而且返回X_train, y_train, sample_weights.
    • Use the sample_weights argument in the fit method of a model.
    • Or, if using a generator, make the generator return not only X_train, y_train, but X_train, y_train, sample_weights.
    • 在两种情况下,sample_weights应该是一维矢量,且采样数与数据相同.

      In both cases, sample_weights should be a 1D vector with the same number of samples as the data.

      这篇关于在keras中批量更新期间如何缩放渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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