- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Tensorflow 2 中训练 LSTM 模型来预测两个输出:水流和水温。
因此,当温度和流量损失没有标签时,损失函数需要忽略它们。我已经阅读了大量 TF 文档,但我正在努力弄清楚如何最好地做到这一点。
到目前为止我已经尝试过
sample_weight_mode='temporal'
,然后在调用 fit
时包含 sample_weight
numpy 数组当我这样做时,我收到一个错误,要求我传递一个二维数组。但这让我感到困惑,因为有 3 个维度:n_samples
、sequence_length
和 n_outputs
。
这是我基本上想做的一些代码:
import tensorflow as tf
import numpy as np
# set up the model
simple_lstm_model = tf.keras.models.Sequential([
tf.keras.layers.LSTM(8, return_sequences=True),
tf.keras.layers.Dense(2)
])
simple_lstm_model.compile(optimizer='adam', loss='mae',
sample_weight_mode='temporal')
n_sample = 2
seq_len = 10
n_feat = 5
n_out = 2
# random in/out
x = np.random.randn(n_sample, seq_len, n_feat)
y_true = np.random.randn(n_sample, seq_len, n_out)
# set the initial mask as all ones (everything counts equally)
mask = np.ones([n_sample, seq_len, n_out])
# set the mask so that in the 0th sample, in the 3-8th time step
# the 1th variable is not counted in the loss function
mask[0, 3:8, 1] = 0
simple_lstm_model.fit(x, y_true, sample_weight=mask)
错误:
ValueError: Found a sample_weight array with shape (2, 10, 2). In order to use timestep-wise sample weighting, you should
pass a 2D sample_weight array.
有什么想法吗?我一定不明白 sample_weights
做了什么,因为对我来说,只有当 sample_weight
数组与输出具有相同维度时才有意义。我可以编写一个自定义损失函数并手动处理屏蔽,但似乎应该有一个更通用或内置的解决方案。
最佳答案
样本权重
是的,你理解错了。在本例中,您有 2
个样本、10
个时间步长,每个时间步长具有 5
个特征。您可以像这样传递 2D 张量,因此每个样本的每个时间步对总损失的贡献不同,所有特征的权重相同(通常是这种情况)。
这根本不是你想要的。您希望在计算后屏蔽某些损失值,这样它们就不会产生影响。
一种可能的解决方案是实现您自己的损失函数,该函数在获取mean
或sum
之前将损失张量乘以掩码。
基本上,您传递以某种方式连接在一起的mask
和tensor
并将其拆分到函数中以供使用。这就足够了:
def my_loss_function(y_true_mask, y_pred):
# Recover y and mask
y_true, mask = tf.split(y_true_mask, 2)
# You could user reduce_sum or other combinations
return tf.math.reduce_mean(tf.math.abs(y_true - y_pred) * mask)
现在你的代码(没有加权,因为不需要):
simple_lstm_model = tf.keras.models.Sequential(
[tf.keras.layers.LSTM(8, return_sequences=True), tf.keras.layers.Dense(2)]
)
simple_lstm_model.compile(optimizer="adam", loss=my_loss_function)
n_sample = 2
seq_len = 10
n_feat = 5
n_out = 2
x = np.random.randn(n_sample, seq_len, n_feat)
y_true = np.random.randn(n_sample, seq_len, n_out)
mask = np.ones([n_sample, seq_len, n_out])
mask[0, 3:8, 1] = 0
# Stack y and mask together
y_true_mask = np.stack([y_true, mask])
simple_lstm_model.fit(x, y_true_mask)
所以它有效。您还可以以其他方式堆叠这些值,但我希望您能感受到如何做到这一点。
请注意上面介绍的一些问题。如果你有很多零并取平均值
,你可能会得到一个非常小的损失值并抑制学习。另一方面,如果您使用 sum
它可能会爆炸。
关于python - 如何在 Tensorflow 2 LSTM 训练中屏蔽多输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60728883/
我需要创建一个 View (作为其他 View 的 mask ) 。圆圈是透明的,外部区域是半透明的。我可以使用 canvas.clipRegion(..) 来实现它,但是 http://develo
踏入爬虫的迷宫 多年以来,我一直是一个对编程充满了好奇心的人。探索着代码的世界,便如同探险家踏入密林深处,寻找未知的宝藏。最近,我将目光聚焦在了爬虫技术上,特别是百度搜索屏蔽问题上。 百度搜索的诱惑
我想将“www.adomain.com”重定向到“www.adomain.com/cms”。 cms 部分应该被屏蔽。 我无法让它安静地工作。因此“cms”始终是网址的一部分。 我尝试了这个解决方案:
我一直在我的 View Controller 中使用以下代码: UIView *view = [[CustomView alloc] init]; UIView *mask = [[CustomMas
如果是网页内容里面的alert,我们可以等网页加载完毕,也就是在webViewDidFinishLoad中执行下面的js代码,就可以屏蔽alert了
我正在使用 PyQt4 QLineEdit小部件接受密码。有一个setMasking属性,但不遵循如何设置屏蔽字符。 最佳答案 editor = QLineEdit() editor.setEchoM
我有一个包含员工信息的 Pandas 数据框,如下所示: df=pd.DataFrame({ 'Id':[1,2,3,4], 'Name':['Joe','Henry','Sam','
我正在为 iPhone 创建一个自定义开/关切换开关(类似于标准开关),并且我正在设置 slider 的蒙版,但调用 [[myView [layer] setMask:maskLayer] 设置
我如何能够在 Objective C 中屏蔽 nsimageview?例如,有一个带圆角的 nsimageview。 最佳答案 你不知道。如果你想以 NSImageView 不支持的方式绘制图像,则需
我正在开发一个可以更改边框或矩形 UIImage 的应用程序。边框会有所不同,但看起来 UIImage 是用剪刀剪掉的,或者有什么影响。 做到这一点的最佳方法是什么? 我的第一个想法是准备一堆具有我正
我需要屏蔽数据,就像在 Azure Pipelines 中一样,但位于 Azure 存储库文件内。有没有一种方法可以设置与脚本分开存储的变量,例如在 Azure 管道中: variable = $(S
我没有找到解决问题的简单方法。我想使用 TextInputDialog,您必须在其中键入用户密码,以重置数据库中的所有数据。 TextInputDialog 的问题是它没有屏蔽文本,我不知道有什么选择
我正在寻找一种自行开发的方法来扰乱生产数据以用于开发和测试。我已经构建了几个脚本来生成随机社会安全号码、轮类出生日期、打乱电子邮件等。但我在尝试打乱客户姓名时遇到了困难。我想保留真实姓名,这样我们仍然
我正在尝试使用过滤器来屏蔽 SSN 的前 5 位数字,它应该看起来像这样 XXX-XX-1234 到目前为止我想出了什么: // {{SocialSecurityNumber | ssn}} angu
我没有找到解决问题的简单方法。我想使用 TextInputDialog,您必须在其中键入用户密码,以重置数据库中的所有数据。 TextInputDialog 的问题是它没有屏蔽文本,我不知道有什么选择
我想屏蔽一个 Fortran 数组。这是我目前正在做的方式...... where (my_array <=15.0) mask_array = 1 elsewhere mask_ar
当用户在用户界面上输入时,我需要屏蔽数字,用户应该看到一个屏蔽的数字,但在 Java 代码上,我应该得到整个数字,包括屏蔽的字符用户应该看到什么 4545********9632但在 Java 代码(
我正在使用 javafx 文本字段。我需要输入数字以及应符合格式 ###.###.###.### 的小数其中小数点根本不可编辑,数字可以是任何数字;不允许其他输入。并非所有数字占位符都需要填写,例如它
我必须交付一个小型 C++ 软件。我想用一些实现(我使用了表达式模板)来掩盖头文件,以使最终用户难以辨认,因此他们无法修改它们。我无法将代码包含在静态或动态库中。有什么方法可以屏蔽头文件的内容吗? 谢
我正在开发一个与 gestureRecognizer 配合使用的应用程序。 使用手势可以选择 UIImage(例如 rectangle.png),并且可以使用 UIPopoverView 通过为所选图
我是一名优秀的程序员,十分优秀!