PERSISTENT声明必须在任何使用变量之前

时间:2019-03-01 00:49:06

标签: matlab compiler-errors persistent

在进行matlab作业时,我遇到了一个非常奇怪的错误。这是我的代码:

import tensorflow as tf
template = tf.convert_to_tensor([[1, 1, 0.5, 0.5, 0.3, 0.3],                                                                       
                                 [2, 2, 0.75, 0.5, 0.3, 0.3],                                                                      
                                 [3, 3, 0.5, 0.75, 0.3, 0.3],                                                                      
                                 [4, 4, 0.75, 0.75, 0.3, 0.3]])   

patch = tf.convert_to_tensor([[1, 1, 1, 0.17, 0.4, 0.4],                                                                           
                              [3, 3, 3, 0.22, 0.53, 0.6]])

ind = tf.constant([1,3])
rn_t = tf.range(0, template.shape[0])

def index1d(t, val):
    return tf.reduce_min(tf.where(tf.equal([t], val)))

def index1dd(t,val):
    return tf.argmax(tf.cast(tf.equal(t,val), tf.int64), axis=0)

r = tf.map_fn(lambda x: tf.where(tf.equal(index1d(ind, x), 0), patch[index1dd(ind, x)] , template[x]), rn_t, dtype=tf.float32)

with tf.Session() as sess:
   print(sess.run([r]))

运行时,这会给我错误:

function [z,times] = Divide(x,y)

    persistent times;

    if (y == 0)
        if (isempty(times))
            times = 1;
        else
            times = times + 1;
        end
    end

    z = x/y;
end

这很奇怪,因为它告诉我在将变量声明为持久性(!?)之前需要将变量声明为持久性。我不知道我在做什么错,因此,如果我应该使用一些奇怪的解决方法,请告诉我。

1 个答案:

答案 0 :(得分:3)

错误消息的意思是:在将“ times”声明为持久变量之前,您已经使用了“ times”。当您在返回变量中使用“时间”时。

解决方案之一可能是为“ times”保留两个不同的变量,一个为持久变量,另一个为返回变量。

在此处粘贴我的更改以供参考。祝你好运!

function [z,times] = Divide(x,y)
    persistent p_times;

    if (y == 0)
        if (isempty(p_times))
            p_times = 1;
        else
            p_times = p_times + 1;
        end
    end

    times = p_times;
    z = x/y;
end