ValueError: Tensor Tensor(“...”, shape=(?, 6), dtype=float32) is not an element of this graph.解决方法

ValueError: Tensor Tensor(“dense_2/Softmax:0”, shape=(?, 6), dtype=float32) is not an element of this graph. 的解决。

在下述代码的运行过程中,出现了标题所述的错误:

def test():
    time_start2 = time.time()
    model = load_model('../data/saved_model/model4444.h5')
    data = csiData1.load_labeled_CSI_data(directory)  # 将文件夹 directory 下的数据进行转化及去噪后,赋给 data
    i_test = [x for x in range(0, 6000) if x not in data['Pick']]  # 生成的随机数序列,用于选取测试数据
    X = data['X']  # 准备的数据矩阵
    Y = data['Y']  # 人体动作标签序列
    X = X.reshape(num_of_csi_files, 1, 30, 1000) / 143  # 测试数据为 3 维数据
    X = X[i_test, :, :, :]
    Y = Y[i_test]  # 按照随机序列选取出相应的标签序列子集
    test_result = []
    for i in range(len(X)):
        r = detect(X[i], model)
        print(i, '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
        test_result.append(r)  # 由于测试函数的调用过程中会出处大量提示信息,故将测试结果暂存起来,最后再统一打印
        print(i, '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
    num_succ = 0  # 用于计数成功识别的次数
    for i in range(len(X)):
        print(float(test_result[i]), float(Y[i]))
        print(i, '+++++++++++++++++++++++++++++')
        if float(test_result[i])==float(Y[i]):
            num_succ += 1
        print(i, '+++++++++++++++++++++++++++++')
    time_end2 = time.time()
    print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
    print("num_succ: "+str(num_succ)+";  num_test: "+str(len(X))+";  accuracy: "+str(1.0*num_succ/len(X)))
    print(" 测试数据用时 "+str(int((time_end2-time_start2)//60))+" 分 "+str(int((time_end2-time_start2)%60))+" 秒!")

if __name__ == '__main__':
    test()

查了一下资料,在模块中插入下述代码后,程序可以正常地运行了:

global graph
graph = tf.get_default_graph()
with graph.as_default():

修改后的代码如下:

def test():
    time_start2 = time.time()
    model = load_model('../data/saved_model/model4444.h5')
    data = csiData1.load_labeled_CSI_data(directory)  # 将文件夹 directory 下的数据进行转化及去噪后,赋给 data
    i_test = [x for x in range(0, 6000) if x not in data['Pick']]  # 生成的随机数序列,用于选取测试数据
    X = data['X']  # 准备的数据矩阵
    Y = data['Y']  # 人体动作标签序列
    X = X.reshape(num_of_csi_files, 1, 30, 1000) / 143  # 测试数据为 3 维数据
    X = X[i_test, :, :, :]
    Y = Y[i_test]  # 按照随机序列选取出相应的标签序列子集
    test_result = []
    global graph
    graph = tf.get_default_graph()
    with graph.as_default():
        for i in range(len(X)):
            r = detect(X[i], model)
            print(i, '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
            test_result.append(r)  # 由于测试函数的调用过程中会出处大量提示信息,故将测试结果暂存起来,最后再统一打印
            print(i, '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
    num_succ = 0  # 用于计数成功识别的次数
    for i in range(len(X)):
        print(float(test_result[i]), float(Y[i]))
        print(i, '+++++++++++++++++++++++++++++')
        if float(test_result[i])==float(Y[i]):
            num_succ += 1
        print(i, '+++++++++++++++++++++++++++++')
    time_end2 = time.time()
    print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
    print("num_succ: "+str(num_succ)+";  num_test: "+str(len(X))+";  accuracy: "+str(1.0*num_succ/len(X)))
    print(" 测试数据用时 "+str(int((time_end2-time_start2)//60))+" 分 "+str(int((time_end2-time_start2)%60))+" 秒!")

if __name__ == '__main__':
    # train()
    test()

注:虽然问题暂时得到了解决,但是笔者对于解决方法所涉及到的原理还不理解,望网友指正。

你可能感兴趣的:(python,机器学习)