假设我正在构建一个维度部分已知的模型:
# Multilayer Perceptron
from keras.layers import Input
from keras.layers import Dense
import tensorflow as tf
inputs = Input(shape=(10,))
hidden1 = Dense(10, activation='relu')(inputs)
hidden2 = tf.math.add(hidden1, 5)
hidden3 = tf.math.add(hidden2, 5)
hidden4 = my_custom_op(hidden2, hidden3)
output = Dense(10, activation='linear')(hidden4)
my_custom_op
是一个大而复杂的张量函数,它在不同的地方使用断言来确保满足关于形状和秩的假设.为了再现这个问题,我将只做如下处理:
def my_custom_op(hidden_x, hidden_y):
tf.assert_equal(tf.shape(hidden_x), tf.shape(hidden_y))
return hidden
当我运行此命令时,会出现以下错误:
TypeError:无法为名称生成TypeSpec:
我不明白这个错误消息告诉我什么.如果我运行tf.assert_equal(2, 2)
,我没有得到异常,所以我假设这与维度还未知有关.
但是,当维度已知时,它们运行的不是断言的要点吗?如果不是,这是否意味着我不能在my_custom_op
中使用断言,因为它们在构建图时会导致这些错误?
以下是完整的错误消息:
TypeError: Could not build a TypeSpec for name: "tf.debugging.assert_equal_1/assert_equal_1/Assert/Assert"
op: "Assert"
input: "tf.debugging.assert_equal_1/assert_equal_1/All"
input: "tf.debugging.assert_equal_1/assert_equal_1/Assert/Assert/data_0"
input: "tf.debugging.assert_equal_1/assert_equal_1/Assert/Assert/data_1"
input: "Shape"
input: "tf.debugging.assert_equal_1/assert_equal_1/Assert/Assert/data_3"
input: "Shape_1"
attr {
key: "T"
value {
list {
type: DT_STRING
type: DT_STRING
type: DT_INT32
type: DT_STRING
type: DT_INT32
}
}
}
attr {
key: "summarize"
value {
i: 3
}
}
of unsupported type <class 'tensorflow.python.framework.ops.Operation'>.