繁体   English   中英

如何在卷积中使用tf.where?

[英]How to use tf.where with convolutions?

我想创建一个根据分类结果在某个点之后拆分为其他几个图的图。 我认为tf.condtf.where可能正确使用,但我不确定如何使用。

无法在此处复制我的所有代码,但我创建了一小段说明此问题。

import os
import sys
import tensorflow as tf
GPU_INDEX = 2

net_class = tf.constant([[0.1, 0.2, 0.3, 0.4], [0.4, 0.3, 0.2, 0.1],[0.2, 0.4, 0.3, 0.1], [0.3, 0.2, 0.4, 0.1],[0.1, 0.3, 0.3, 0.4]]) # 3,0,1,2,3
classes = tf.argmax(net_class, axis=1)
cls_0_idx = tf.squeeze(tf.where(tf.equal(classes, 0)))
cls_3_idx = tf.squeeze(tf.where(tf.equal(classes, 3)))

cls_0 = tf.gather(params=net_class, indices=cls_0_idx)
cls_3 = tf.gather(params=net_class, indices=cls_3_idx)

params_0 = tf.constant([1.0,1,1,1])
params_3 = tf.constant([3.0,3,3,3])


output = tf.stack([tf.nn.conv1d(cls_0, params_0, 1,  padding='VALID'), tf.nn.conv1d(cls_3, params_3, 1,  padding='VALID')])

sess = tf.Session()
cls_0_idx_val = sess.run(output)

print(output)

在这里,我尝试提取分类为0或3的输入索引,并使用不同的变量将它们乘以输出(每个类的权重相同,这就是为什么要使用卷积的原因)。

我收到以下错误:

ValueError: Shape must be rank 4 but is rank 2 for 'conv1d/Conv2D' (op: 'Conv2D') with input shapes: ?, [1,4].

我知道为什么会收到错误消息(因为tf.where无法“知道”错误tf.where的大小),但问题是如何解决? (类不相等,在我的“实际”问题中甚至可能为空)

我想你应该

  1. tf.squeeze设置axis设置为1

  2. tf.nn.conv1d更改为简单乘法

  3. tf.stack更改为tf.concat

那么您将获得以下内容:

net_class = tf.constant([[0.1, 0.2, 0.3, 0.4], [0.4, 0.3, 0.2, 0.1],[0.2, 0.4, 0.3, 0.1], [0.3, 0.2, 0.4, 0.1],[0.1, 0.3, 0.3, 0.4]]) # 3,0,1,2,3
classes = tf.argmax(net_class, axis=1)
cls_0_idx = tf.squeeze(tf.where(tf.equal(classes, 0)), -1)
cls_3_idx = tf.squeeze(tf.where(tf.equal(classes, 3)), -1)

cls_0 = tf.gather(params=net_class, indices=cls_0_idx)
cls_3 = tf.gather(params=net_class, indices=cls_3_idx)

params_0 = tf.constant([1.0,1,1,1])
params_3 = tf.constant([3.0,3,3,3])
output = tf.concat([cls_0 * params_0, cls_3 * params_3], axis = 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM