[求助贴]tensorflow报错问题,apply gradients报错.

深度学习

#1

报错显示: Traceback (most recent call last): File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py”, line 686, in _call_cpp_shape_fn_impl input_tensors_as_shapes, status) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py”, line 473, in exit c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 0 but is rank 1 for ‘get_train_op/train/update_encoder/dense/kernel/ApplyAdam’ (op: ‘ApplyAdam’) with input shapes: [128,128], [128,128], [128,128], [1], [1], [], [1], [1], [1], [128,128].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/home/lily/Downloads/model_global/attention_aq_2/model.py”, line 256, in model_train(X1_train, X2_train, y_train) File “/home/lily/Downloads/model_global/attention_aq_2/model.py”, line 172, in model_train graph = Graph(is_training=True) File “/home/lily/Downloads/model_global/attention_aq_2/model.py”, line 154, in init minimize_op = optimizer.apply_gradients(gradients, global_step=self.global_step, name=“train”) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/training/optimizer.py”, line 489, in apply_gradients update_ops.append(processor.update_op(self, grad)) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/training/optimizer.py”, line 102, in update_op update_op = optimizer._apply_dense(g, self._v) # pylint: disable=protected-access File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/training/adam.py”, line 156, in _apply_dense grad, use_locking=self._use_locking).op File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/training/gen_training_ops.py”, line 220, in apply_adam use_nesterov=use_nesterov, name=name) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py”, line 787, in _apply_op_helper op_def=op_def) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, line 2958, in create_op set_shapes_for_outputs(ret) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, line 2209, in set_shapes_for_outputs shapes = shape_func(op) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, line 2159, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py”, line 627, in call_cpp_shape_fn require_shape_fn) File “/home/lily/tf3.6/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py”, line 691, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Shape must be rank 0 but is rank 1 for ‘get_train_op/train/update_encoder/dense/kernel/ApplyAdam’ (op: ‘ApplyAdam’) with input shapes: [128,128], [128,128], [128,128], [1], [1], [], [1], [1], [1], [128,128].

这里更新参数我打印到了dense层,按照这个报错应该也是dense层报错,但是我dense层的输入只有一个三个维度,维度是[batch_size, 128,128], 后面对应的这个空的维度(标量)不知道在哪里. 我能够用tensorboard调出来在compute_gradient的图,但是无法更新到apply_gradients这一步.

求问有没有人遇到过这个问题.(开始认为是修改的底层的类的原因,现在全部用的tensorflow官方封装的api, 所以应该不存在底层的问题)


#2

bug终于调出来了, 在自己失误多打了一个逗号, 注意在配置参数文件的时候,一定不要手误打个逗号,这样python读进去就变成元组了…自己挖的坑真的是跪着也要填平…

这种错误总结出来一点就是,不要对自己的输入太自信,不仅要检查输入的x和y, 还得检查子集的输入变量, 否则检查的时候就会漏过很多问题. 如果说模型能够构图成功, 就不要像我一样用tensorboard检查构的图了,因为一般构图能够成功,就不是你最原始的输入的维度的问题, 可能是你中间输入的变量的问题.