- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有一些一维数据(例如时间序列),其中所有序列都有固定长度l:
# [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] index
example = [ 0, 1, 1, 0, 23, 22, 20, 14, 9, 2, 0, 0] # l = 12
我们想要使用n个类执行语义分割:
# [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] index
labeled = [
[ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], # class 1
[ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0], # class 2
[ 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0], # class 3
#[ ... ],
[ 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1], # class n
]
那么单个示例的输出形状为 [n, l]
(即 data_format
不是 "channels_last"
)并且批量输出的形状为 [b, n, l]
,其中b
是批处理中的示例数。
这些类是独立的,因此我的理解是,这里使用 sigmoid 交叉熵作为损失,而不是 softmax 交叉熵。
<小时/>我有一些关于 tf.nn.sigmoid_cross_entropy_with_logits
的预期格式和使用的小相关问题。 :
由于网络输出的张量与批量标签的形状相同,我应该在输出 logits 的假设下训练网络,还是采用 keras 方法(请参阅 keras 的 binary_crossentropy
)并假设它输出概率?
鉴于一维分割问题,我应该调用 tf.nn.sigmoid_cross_entropy_with_logits
上:
data_format='channels_first'
(如上所示),或data_format='channels_last'
(示例.T)如果我希望为每个 channel 单独分配标签?
传递给优化器的损失操作应该是:
tf.nn.sigmoid_cross_entropy_with_logits(labels, logits)
, tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels, logits))
,或tf.losses.sigmoid_cross_entropy
?这个Colab ,突出了我的困惑并表明 data_format
事实上确实很重要......,但文档没有明确说明预期的内容。
c = 5 # number of channels (label classes)
p = 10 # number of positions ('pixels')
# data_format = 'channels_first', shape = [classes, pixels]
# 'logits' for 2 examples
pred_1 = np.array([[random.random() for v in range(p)]for n in range(c)]).astype(float)
pred_2 = np.array([[random.random() for v in range(p)]for n in range(c)]).astype(float)
# 'ground truth' for the above 2 examples
targ_1 = np.array([[0 if random.random() < 0.8 else 1 for v in range(p)]for n in range(c)]).astype(float)
targ_2 = np.array([[0 if random.random() < 0.8 else 1 for v in range(p)]for n in range(c)]).astype(float)
# batched form of the above examples
preds = np.array([pred_1, pred_2])
targs = np.array([targ_1, targ_2])
# data_format = 'channels_last', shape = [pixels, classes]
t_pred_1 = pred_1.T
t_pred_2 = pred_2.T
t_targ_1 = targ_1.T
t_targ_2 = targ_2.T
t_preds = np.array([t_pred_1, t_pred_2])
t_targs = np.array([t_targ_1, t_targ_2])
# calculate individual losses for 'channels_first'
loss_1 = tf.nn.sigmoid_cross_entropy_with_logits(labels=targ_1, logits=pred_1)
loss_2 = tf.nn.sigmoid_cross_entropy_with_logits(labels=targ_2, logits=pred_2)
# calculate batch loss for 'channels_first'
b_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=targs, logits=preds)
# calculate individual losses for 'channels_last'
t_loss_1 = tf.nn.sigmoid_cross_entropy_with_logits(labels=t_targ_1, logits=t_pred_1)
t_loss_2 = tf.nn.sigmoid_cross_entropy_with_logits(labels=t_targ_2, logits=t_pred_2)
# calculate batch loss for 'channels_last'
t_b_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=t_targs, logits=t_preds)
# get actual tensors
with tf.Session() as sess:
# loss for 'channels_first'
l1 = sess.run(loss_1)
l2 = sess.run(loss_2)
# batch loss for 'channels_first'
bl = sess.run(b_loss)
# loss for 'channels_last'
t_l1 = sess.run(t_loss_1)
t_l2 = sess.run(t_loss_2)
# batch loss for 'channels_last'
t_bl = sess.run(t_b_loss)
# calculate individual losses for 'channels_first'
rm_loss_1 = tf.reduce_mean(loss_1)
rm_loss_2 = tf.reduce_mean(loss_2)
# calculate batch loss for 'channels_first'
rm_b_loss = tf.reduce_mean(b_loss)
# calculate individual losses for 'channels_last'
rm_t_loss_1 = tf.reduce_mean(t_loss_1)
rm_t_loss_2 = tf.reduce_mean(t_loss_2)
# calculate batch loss for 'channels_last'
rm_t_b_loss = tf.reduce_mean(t_b_loss)
# get actual tensors
with tf.Session() as sess:
# loss for 'channels_first'
rm_l1 = sess.run(rm_loss_1)
rm_l2 = sess.run(rm_loss_2)
# batch loss for 'channels_first'
rm_bl = sess.run(rm_b_loss)
# loss for 'channels_last'
rm_t_l1 = sess.run(rm_t_loss_1)
rm_t_l2 = sess.run(rm_t_loss_2)
# batch loss for 'channels_last'
rm_t_bl = sess.run(rm_t_b_loss)
# calculate individual losses for 'channels_first'
tf_loss_1 = tf.losses.sigmoid_cross_entropy(multi_class_labels=targ_1, logits=pred_1)
tf_loss_2 = tf.losses.sigmoid_cross_entropy(multi_class_labels=targ_2, logits=pred_2)
# calculate batch loss for 'channels_first'
tf_b_loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=targs, logits=preds)
# calculate individual losses for 'channels_last'
tf_t_loss_1 = tf.losses.sigmoid_cross_entropy(multi_class_labels=t_targ_1, logits=t_pred_1)
tf_t_loss_2 = tf.losses.sigmoid_cross_entropy(multi_class_labels=t_targ_2, logits=t_pred_2)
# calculate batch loss for 'channels_last'
tf_t_b_loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=t_targs, logits=t_preds)
# get actual tensors
with tf.Session() as sess:
# loss for 'channels_first'
tf_l1 = sess.run(tf_loss_1)
tf_l2 = sess.run(tf_loss_2)
# batch loss for 'channels_first'
tf_bl = sess.run(tf_b_loss)
# loss for 'channels_last'
tf_t_l1 = sess.run(tf_t_loss_1)
tf_t_l2 = sess.run(tf_t_loss_2)
# batch loss for 'channels_last'
tf_t_bl = sess.run(tf_t_b_loss)
# loss _should_(?) be the same for 'channels_first' and 'channels_last' data_format
# test example_1
e1 = (l1 == t_l1.T).all()
# test example 2
e2 = (l2 == t_l2.T).all()
# loss calculated for each example and then batched together should be the same
# as the loss calculated on the batched examples
ea = (np.array([l1, l2]) == bl).all()
t_ea = (np.array([t_l1, t_l2]) == t_bl).all()
# loss calculated on the batched examples for 'channels_first' should be the same
# as loss calculated on the batched examples for 'channels_last'
eb = (bl == np.transpose(t_bl, (0, 2, 1))).all()
e1, e2, ea, t_ea, eb
# (True, False, False, False, True) <- changes every time, so True is happenstance
l_e1 = tf_l1 == rm_l1
l_e2 = tf_l2 == rm_l2
l_eb = tf_bl == rm_bl
l_t_e1 = tf_t_l1 == rm_t_l1
l_t_e2 = tf_t_l2 == rm_t_l2
l_t_eb = tf_t_bl == rm_t_bl
l_e1, l_e2, l_eb, l_t_e1, l_t_e2, l_t_eb
# (False, False, False, False, False, False)
最佳答案
tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(...)) 和 tf.losses.sigmoid_cross_entropy(...)
(使用默认参数)都是计算同样的事情。问题出在您的测试中,您使用 ==
来比较两个 float 。相反,使用 np.isclose
检查两个 float 是否相等的方法:
# loss _should_(?) be the same for 'channels_first' and 'channels_last' data_format
# test example_1
e1 = np.isclose(l1, t_l1.T).all()
# test example 2
e2 = np.isclose(l2, t_l2.T).all()
# loss calculated for each example and then batched together should be the same
# as the loss calculated on the batched examples
ea = np.isclose(np.array([l1, l2]), bl).all()
t_ea = np.isclose(np.array([t_l1, t_l2]), t_bl).all()
# loss calculated on the batched examples for 'channels_first' should be the same
# as loss calculated on the batched examples for 'channels_last'
eb = np.isclose(bl, np.transpose(t_bl, (0, 2, 1))).all()
e1, e2, ea, t_ea, eb
# (True, True, True, True, True)
还有:
l_e1 = np.isclose(tf_l1, rm_l1)
l_e2 = np.isclose(tf_l2, rm_l2)
l_eb = np.isclose(tf_bl, rm_bl)
l_t_e1 = np.isclose(tf_t_l1, rm_t_l1)
l_t_e2 = np.isclose(tf_t_l2, rm_t_l2)
l_t_eb = np.isclose(tf_t_bl, rm_t_bl)
l_e1, l_e2, l_eb, l_t_e1, l_t_e2, l_t_eb
# (True, True, True, True, True, True)
关于python - TensorFlow Sigmoid 交叉熵与一维数据的 Logits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53612973/
我有一个 Haskell 程序,它用 Metropolis 模拟 Ising 模型 算法。主要操作是一个模板操作,它需要下一个的总和 2D 中的邻居,然后将其与中心元素相乘。那么 元素可能已更新。 在
对于卷积,我想在一个维度上应用圆形填充,在所有其他维度上应用零填充。我该怎么做? 对于卷积,有 28 个 channel ,并且数据在球形箱中描述。半径时间有 20 个箱子,极地时间有 20 个箱子,
假设您有一个数组并想创建另一个数组,该数组的值连续等于第一个数组的 10 个元素的标准差。在 for 循环的帮助下,它可以像下面的代码一样轻松编写。我想要做的是避免使用 for 循环来加快执行时间。有
我有一个长度为size * size的一维数组,表示值的平方字段。 我的目标是将数组旋转到位(previous question)。我目前在获取正确的内圈索引方面遇到问题。我的算法有什么错误? 这是我
stride = 1 和 1 个过滤器的 1D CNN 的输出长度不应该等于输入长度而不需要填充吗? 我认为是这种情况,但创建了一个具有这些规范的 Keras 模型,当输入形状为 (17910,1)
我有一个零散布的一维数组。想创建第二个数组,其中包含最后一个零的位置,如下所示: >>> a = np.array([1, 0, 3, 2, 0, 3, 5, 8, 0, 7, 12]) >>> fo
我试着去理解卡尔曼滤波器是如何工作的,因为多维变量太混乱了,一开始我从一维的例子开始。 我发现有3个不同的来源解释温度计的情况,但所有这些情况实现的方程略有不同,我没有得到重点。 我实现了解决方案2,
我有一个二维数组,我需要将它转换为一个列表(同一对象)。我不想使用 for 或 foreach 循环来执行此操作,它们将获取每个元素并将其添加到列表中。还有其他方法吗? 最佳答案 好吧,你可以让它使用
我正在使用 Keras 构建一个 CNN,将以下 Conv1D 作为我的第一层: cnn.add(Conv1D( filters=512, kernel_size=3, str
在 Python 中使用 TensorFlow,我正在制作一个以一维数组作为输入的神经网络。我想在网络中添加一个卷积层,但似乎无法让它工作。 我的训练数据如下所示: n_samples = 20 le
我有一个简单的一维数组,但我想使用 jQuery 将长列表分成两列。我怎样才能实现这个目标? var articles = ['article10','article9','article8','ar
我有一个刚从文件中读入的 float 的一维 vector 。 std::vector result(s.size() / sizeof(float)); 我想像这样使用这些数据 myTable[rl
使用 numpy 将 data reshape 为 fencepost 的最有效方法是什么? data = np.array([1, 2, 3, 4, 5])
我正在尝试通过删除 for 循环并仅在处理大型数据集时使用 numpy 数组来优化一些代码。 我想采用一维 numpy 数组,例如: a = [1, 2, 3, 4, 5] 并生成一个 2D nump
我正在尝试为两个数组 Pages 和 Price 赋值。 #include int main() { static int pages[3]; static int price[3];
我是深度学习、keras API 和卷积网络的新手,如果这些错误是幼稚的,请事先致歉。我正在尝试构建一个用于分类的简单卷积神经网络。输入数据 X 有 286 个样本,每个样本有 500 个时间点,4
假设我有 Numpy 数组 p 和一个 Scipy 稀疏矩阵 q 这样 >>> p.shape (10,) >>> q.shape (10,100) 我想做 p 和 q 的点积。当我尝试使用 nump
我需要一些行为类似于 std::vector 的东西(界面/功能/等)但我需要它是平坦的,即它不能动态分配缓冲区。显然,这通常不起作用,因为可用大小必须在编译时确定。但我希望类型能够处理N没有额外分配
作为我正在运行的一些模拟的一部分,我需要最终对一些非常长的(实数)数字序列执行以下操作。这是要点: 给定一个长的一维 NumPy 数组,对于数组中的每个位置,我想对该位置前后的值进行平均,取平均值之间
这个问题在这里已经有了答案: Concatenating two one-dimensional NumPy arrays (6 个答案) 关闭 5 年前。 我想将 numpy 数组存储到另一个 n
我是一名优秀的程序员,十分优秀!