Python张量流错误,张量必须来自同一个图

2024-03-28 22:08:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据分割文件中定义了一个conv\u net函数,如下所示

def conv_net(X, weights, biases, dropout):
    X = tf.reshape(X, shape=[-1, HEIGHT, WIDTH, NETWORK_DEPTH])
#error occurs on the below line - while calling the function in debugging mode
    conv1 = conv2d('conv1', X, weights['conv_weight1'], biases['conv_bias1'])
    conv1 = maxpool2d('max_pool1', conv1, k=2)

    conv2 = conv2d('conv2', conv1, weights['conv_weight2'], biases['conv_bias2'])
    conv2 = maxpool2d('max_pool2', conv2, k=2)

    conv3 = conv2d('conv3', conv2, weights['conv_weight3'], biases['conv_bias3'])
    conv3 = maxpool2d('max_pool3', conv3, k=2)

    conv4 = conv2d('conv4', conv3, weights['conv_weight4'], biases['conv_bias4'])
    conv4 = maxpool2d('max_pool4', conv4, k=2)

    fc1 = tf.reshape(conv4, shape=[-1, weights['fcl_weight1'].get_shape().as_list()[0]])
    fc1 = tf.nn.relu(tf.add(tf.matmul(fc1, weights['fcl_weight1']), biases['fcl_bias1']))
    fc1 = tf.nn.dropout(fc1, dropout)

    fc2 = tf.nn.relu(tf.add(tf.matmul(fc1, weights['fcl_weight2']), biases['fcl_bias2']))
    fc2 = tf.nn.dropout(fc2, dropout)

    out = tf.add(tf.matmul(fc2, weights['out_weight']), biases['out_bias'], name='softmax')
    return out

我在另一个.py文件中调用这个函数

print("Comp2")
logits = data_split.conv_net(data_split.X, data_split.weights, data_split.biases, keep_prob)
print("Comp2.0")
prediction = tf.nn.softmax(logits)

这是给我一个错误,当我运行logits行。你知道吗

ValueError: Tensor("conv_weight1:0", shape=(5, 5, 4, 16), dtype=float32_ref) must be from the same graph as Tensor("Reshape_12:0", shape=(?, 100, 100, 4), dtype=float32).

我试着从这个question中得到我的答案,但毫无帮助。你知道吗


Tags: tfnndropoutshapeweightsconvbiasesfcl
1条回答
网友
1楼 · 发布于 2024-03-28 22:08:35

data_split.py文件或“other”.py文件的某处,您有一个tf.Graph()定义。你知道吗

您在图形中定义了模型,例如:

g1 = tf.Graph()
with g1.as_default():
    model = conv_net(data_split.X)

但是传递模型的data_split.X已在g1范围之外定义,因此或在“默认图”(您可以通过tf.get_default_graph()或在另一个显式定义的图(与g1相同)中定义。你知道吗

解决方案是将输入定义和模型定义移到同一个图中(提示:在这种情况下,只需使用默认图而不显式创建`tf.图形'). 你知道吗

注意:如果不共享完整的代码,这是你能得到的最好的。你知道吗

相关问题 更多 >