我需要一种方法来获取 TensorFlow 中任何类型层(即 Dense、Conv2D 等)的输出张量的形状。根据文档,有output_shape解决问题的财产。但是,每次我访问它时,我都会得到AttributedError.
这是显示问题的代码示例:
import numpy as np
import tensorflow as tf
x = np.arange(0, 8, dtype=np.float32).reshape((1, 8))
x = tf.constant(value=x, dtype=tf.float32, verify_shape=True)
dense = tf.layers.Dense(units=2)
out = dense(x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(fetches=out)
print(res)
print(dense.output_shape)
Run Code Online (Sandbox Code Playgroud)
该print(dense.output_shape)语句将产生错误消息:
AttributeError: The layer has never been called and thus has no defined output shape.
Run Code Online (Sandbox Code Playgroud)
或print(dense.output)将产生:
AttributeError('Layer ' + self.name + ' has no inbound nodes.')
AttributeError: Layer dense_1 has no …Run Code Online (Sandbox Code Playgroud) 我接受过训练CatBoostClassifier来解决我的分类任务。现在我需要保存模型并在另一个应用程序中使用它进行预测。为此,我通过save_model方法保存模型并通过方法恢复它load_model。
但是,每次我调用predict恢复的模型时,都会收到错误消息:
CatboostError: There is no trained model to use predict(). Use fit() to train model. Then use predict().
Run Code Online (Sandbox Code Playgroud)
所以看起来我需要再次训练我的模型,而我需要恢复预训练模型并将其仅用于预测。
我在这里做错了什么?我应该使用一种特殊的方式来加载模型进行预测吗?
我的训练过程是这样的:
model = CatBoostClassifier(
custom_loss=['Accuracy'],
random_seed=42,
logging_level='Silent',
loss_function='MultiClass')
model.fit(
x_train,
y_train,
cat_features=None,
eval_set=(x_validation, y_validation),
plot=True)
...
model.save("model.cbm")
Run Code Online (Sandbox Code Playgroud)
我使用以下代码恢复模型:
model = CatBoostClassifier(
custom_loss=['Accuracy'],
random_seed=42,
logging_level='Silent',
loss_function='MultiClass')
model.load_model("model.cbm")
...
predict = self.model.predict(inputs)
Run Code Online (Sandbox Code Playgroud)