ValueError:Tensor(“ flatten_1 / Const:0”,shape =(1,),dtype = int32)必须与Tensor(“ strided_slice:0”,shape =(3,),dtype = int32)来自同一张图

时间:2019-12-15 23:16:46

标签: tensorflow

我正在尝试使用用TensorFlow库编写的函数。我遇到了一个错误,但是我对 Tensorflow 并不熟悉,我很难用代码解决问题。

功能如下:

def calculate_SI(featuremap, label,mode='dontCare'):
    tf.reset_default_graph()
    # featuremap = tf.convert_to_tensor(featuremap)
    def cond(count_true,i, size_loop):
        return tf.less(i, size_loop)

    def body(count_true,i, size_loop):

        norm = tf.subtract(square, 2 * tf.tensordot(array, array[i, :], axes=1))

        delta = tf.get_variable("delta", [number], dtype=tf.float32, initializer=tf.constant_initializer(0))
        delta = tf.scatter_update(delta, i - 1, 0)
        delta = tf.scatter_update(delta, i, np.inf)
        norm = tf.math.add(norm, delta)

        min_index_norm = tf.argmin(norm)
        equal = tf.equal(label[min_index_norm], label[i])

        count_true = tf.cond(equal, lambda: tf.add(count_true, 1),
                             lambda: count_true)

        return (count_true,tf.add(i, 1), size_loop)

    with tf.Session() as sess:
        [number, size] = featuremap.shape
        array_plhdr = tf.placeholder(dtype=tf.float32, shape=[number, size])
        array = tf.get_variable('array', [number, size])
        label = tf.convert_to_tensor(label)

        square = tf.math.reduce_sum(tf.math.square(array), axis=1)

        size_loop = tf.constant(number)
        i = tf.constant(0)
        count_true = tf.constant(0)
        count_true, i ,_= tf.while_loop(cond, body, [count_true,i, size_loop])  # ,parallel_iterations=100

        sess.run(tf.initialize_all_variables())
        sess.run(array.assign(array_plhdr), {array_plhdr: featuremap})
        count,_ = sess.run([count_true,i])

        print(count)
        return count,number

在循环中调用此函数以查找卷积神经网络层的分离索引。 在函数的第一次调用中,一切正常,我得到了结果。但是,第二次调用该错误时就会出现。这是循环:

train=[]
test=[]

for i in [0,1]:

   sub_model = submodel(CNN,i)

   x_train_out = sub_model.predict(x_train)
   x_test_out = sub_model.predict(x_test)

   print('calculate SI test')
   result2, number2 = calculate_SI(x_test_out, y_test_one_hot)
   print(i, result2, number2, float(result2 / number2))
   test.append(float(result2 / number2)) 
   K.clear_session()


   x_train_out = None
   x_test_out = None
   sub_model = None 

print(test)
#print(train)

预先感谢您。

0 个答案:

没有答案
相关问题