gpt4 book ai didi

if-statement - keras( tensorflow 后端)使用 K.switch() 进行条件分配

转载 作者:行者123 更新时间:2023-12-02 03:27:25 28 4
gpt4 key购买 nike

我正在尝试实现类似的东西

if np.max(subgrid) == np.min(subgrid):
middle_middle = cur_subgrid + 1
else:
middle_middle = cur_subgrid

由于条件只能在运行时确定,因此我使用 Keras 语法如下

middle_middle = K.switch(K.max(subgrid) == K.min(subgrid), lambda: tf.add(cur_subgrid,1), lambda: cur_subgrid)

但我收到此错误:

<ipython-input-112-0504ce070e71> in col_loop(j, gray_map, mask_A)
56
57
---> 58 middle_middle = K.switch(K.max(subgrid) == K.min(subgrid), lambda: tf.add(cur_subgrid,1), lambda: cur_subgrid)
59
60 print ('ml',middle_left.shape)

/nfs/isicvlnas01/share/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in switch(condition, then_expression, else_expression) 2561 The selected tensor. 2562 """
-> 2563 if condition.dtype != tf.bool: 2564 condition = tf.cast(condition, 'bool') 2565 if not callable(then_expression):

AttributeError: 'bool' object has no attribute 'dtype'

middle_middlecur_subgrid 和 subgrid 都是 NxN 张量。如有任何帮助,我们将不胜感激。

最佳答案

我认为问题在于,使用 K.max(subgrid) == K.min(subgrid) 您正在创建一个比较两个张量对象的 python bool 值,不是包含两个输入张量的比较值的tensorflow bool 张量

换句话说,你所写的内容将被评估为

K.switch(False, lambda: tf.add(cur_subgrid,1), lambda: cur_subgrid)

而不是

comparison = ... # Some tensor, that at runtime will contain True if min and max are the same, False otherwise. 
K.switch(comparison , lambda: tf.add(cur_subgrid,1), lambda: cur_subgrid)

所以你需要做的是使用keras.backend.equal()而不是==:

K.switch(K.equal(K.max(subgrid),K.min(subgrid)), lambda: tf.add(cur_subgrid,1), lambda: cur_subgrid)

关于if-statement - keras( tensorflow 后端)使用 K.switch() 进行条件分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com