加偏置点处的第一个卷积层出错

时间:2016-02-12 09:10:13

标签: python tensorflow deep-learning

我坚持有关CNN的错误。

尝试在tensorflow上实现以下结构。

enter image description here

我编写的代码正如教程(Deep MNIST for Experts)所示。定义了权重和偏差初始化,如下所示。

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)


def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

第一个卷积层如下。与教程几乎相同。

with tf.name_scope('conv1') as scope:
  W_conv1 = weight_variable([4, 1, 128, 256])
  b_conv1 = bias_variable([256])

  x_image = tf.reshape(images_placeholder, [-1,599,1,128])

  print tf.Tensor.get_shape(x_image)
  print tf.Tensor.get_shape(conv2d(x_image, W_conv1))

  h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)

然而,我得到的错误如下。如何解决这个错误?

  File "CNN_model.py", line 43, in inference
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/math_ops.py", line 403, in binary_op_wrapper
    return func(x, y, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 44, in add
    return _op_def_lib.apply_op("Add", x=x, y=y, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 297, in apply_op
    g = ops._get_graph_from_inputs(_Flatten(keywords.values()), graph=g)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 2879, in _get_graph_from_inputs
    assert_same_graph([original_input, op_input])
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 698, in assert_same_graph
    raise ValueError("Items must be from the same graph.")
  ValueError: Items must be from the same graph.

1 个答案:

答案 0 :(得分:0)

该错误不是来自您展示的代码。看起来你正在创建多个图形(可能是因为ipython笔记本单元的重复调用?)并试图在同一计算中使用来自不同图形的东西。