Tensorflow: tf.select()

官方api翻译:

tf.select(condition, t, e, name=None)

根据condition选择t或e中的元素。

t和e张量都必须具有相同的形状,并且输出也将具有该形状。 如果t和e是标量,那么condition tensor必须是标量。 如果t和e是向量或更高级别,那么条件必须是大小与t的第一维相匹配的向量,或者必须具有与t相同的形状。

condition张量充当一个掩码,根据每个元素的值选择输出中相应的元素/行应该从t(如果为true)或e(如果为false)中取出。

如果condition是一个向量,t和e是更高级别的矩阵,那么它选择从t和e复制哪一行(外部维度)。

如果条件具有与t和e相同的形状,那么它会选择从t和e复制哪个元素。

说了一大堆,还是直接看列子吧:

import tensorflow as tf
sess=tf.Session()
condition=[[True,False],[True,False]]
a=[[1,2],[3,4]]
b=[[5,6],[7,8]]
c=tf.select(condition,a,b)
print(sess.run(c))
sess.close()

结果:


tf.select()通常与tf.greater()结合使用。具体可参考上篇tf.greater().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Conditional layer in TensorFlow's Keras API allows you to conditionally apply different layers based on some input. It is useful when you want to create a model that has different behavior depending on certain conditions. The `tf.keras.layers.Conditional` layer is not a built-in layer in TensorFlow. However, you can achieve similar functionality using other available layers and custom model construction techniques. One way to implement conditional behavior is by using the `tf.keras.layers.Concatenate` layer along with the `tf.keras.layers.Lambda` layer. Here's an example of how you can create a conditional layer using these components: ```python import tensorflow as tf # Define your conditional inputs condition = tf.keras.Input(shape=(1,), dtype=tf.bool) input_1 = tf.keras.Input(shape=(10,)) input_2 = tf.keras.Input(shape=(20,)) # Create a conditional layer concatenated_inputs = tf.keras.layers.Concatenate(axis=-1)([input_1, input_2]) output = tf.keras.layers.Lambda(lambda x: tf.where(condition, x[:, :10], x[:, 10:]))(concatenated_inputs) # Create the model model = tf.keras.Model(inputs=[condition, input_1, input_2], outputs=output) ``` In this example, we have a condition input that determines which part of the concatenated inputs should be selected. The `Lambda` layer uses a lambda function to conditionally select either the first 10 elements or the last 20 elements from the concatenated inputs based on the condition input. You can customize this example based on your specific requirements and adjust the condition and inputs according to your model's needs.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拦路雨g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值