我正在尝试使用tensorflow 1.13.1在python中编写CNN。由于某些原因,即使将模型简化为仅一个仿射层,我仍然会遇到尺寸错误。这是相关代码:
tf.reset_default_graph()
X = tf.placeholder(tf.float32, [None, X_SHAPE[1], X_SHAPE[2], 1]) # X_SHAPE is the shape of the input image types I am workig with
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
def my_model(X, y, is_training):
output = X
output = tf.reshape(output, [-1, output.shape[1] * output.shape[2] *
output.shape[3]])
output = tf.layers.dense(output, 2) # makes the error
output = tf.contrib.layers.batch_norm(output)
return output
y_out = my_model(X, y, is_training)
total_loss = tf.losses.softmax_cross_entropy(tf.one_hot(y, 2), logits=y_out)
mean_loss = tf.reduce_mean(total_loss)
optimizer = tf.train.RMSPropOptimizer(1e-3)
# batch normalization in tensorflow requires this extra dependency
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(extra_update_ops):
train_step = optimizer.minimize(mean_loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print('Training')
run_model(sess,y_out,mean_loss,X_train,y_train,8,64,100,train_step,True)
print('Validation')
run_model(sess,y_out,mean_loss,X_val,y_val,1,64)
我得到的错误如下:
Traceback (most recent call last):
File "C:/Users/t8484200/Documents/fanta/dicom_snippet.py", line 189, in <module>
y_out = my_model(X, y, is_training)
File "C:/Users/t8484200/Documents/fanta/dicom_snippet.py", line 181, in my_model
output = tf.layers.dense(output, 2)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\core.py", line 188, in dense
return layer.apply(inputs)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1227, in apply
return self.__call__(inputs, *args, **kwargs)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 530, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 554, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\layers\core.py", line 975, in call
outputs = gen_math_ops.mat_mul(inputs, self.kernel)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 5629, in mat_mul
name=name)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 350, in _apply_op_helper
g = ops._get_graph_from_inputs(_Flatten(keywords.values()))
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 5713, in _get_graph_from_inputs
_assert_same_graph(original_graph_element, graph_element)
File "C:\Users\t8484200\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 5649, in _assert_same_graph
original_item))
ValueError: Tensor("dense/kernel:0", shape=(262144, 2), dtype=float32_ref) must be from the same graph as Tensor("Reshape:0", shape=(?, 262144), dtype=float32).
但是尺寸似乎还不错,所以我非常感谢您对此提供的帮助!
答案 0 :(得分:0)
您没有收到尺寸错误。尺寸只是作为有关两个张量的信息的一部分提到的。我使用tf 1.13.1运行了此代码,它对我有用。
例如,当我将前4行替换为:(相同的行,但顺序不同)时,我能够得到相同的错误
X = tf.placeholder(tf.float32, [None, X_SHAPE[1], X_SHAPE[2], 1]) # X_SHAPE is the shape of the input image types I am workig with
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
tf.reset_default_graph()
原因是在现有图形中创建了X,然后通过reset命令创建了新图形。然后在新图中创建密集张量,但不允许使用旧图中的X。 (看起来reshape命令在旧图中,可能是因为它没有创建新变量。)
因此,与定义占位符的位置相比,您需要检查将图形重置的位置。