我有一个在我的data_split文件中定义的conv_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)必须与Tensor相同的图形(“Reshape_12:0”,shape =(?,100,100, 4),dtype = float32)。
我试图从这个question得到我的答案 - 但没有帮助。
在data_split.py
文件或“其他”.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.Graph')。
注意:如果不共享完整代码,这是您可以获得的最佳代码。