gpt4 book ai didi

python - 无法将keras模型转换为tensorflow : type object 'Dense' has no attribute 'from_config'

转载 作者:行者123 更新时间:2023-12-01 09:30:04 25 4
gpt4 key购买 nike

我正在努力将我的 Keras 模型转换为 TensorFlow 估计器。我收到以下错误:

AttributeError: type object 'Dense' has no attribute 'from_config'

这是我的代码:

from tensorflow import keras
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense

classifier = keras.models.Sequential()
classifier.add(tf.layers.Dense(units = 6, kernel_initializer = keras.initializers.he_uniform(), activation = tf.nn.relu, input_shape =(11,)))
classifier.add(tf.layers.Dense(units = 6, kernel_initializer = keras.initializers.he_uniform(), activation = tf.nn.relu))
classifier.add(tf.layers.Dense(units = 1, kernel_initializer = tf.keras.initializers.he_uniform(), activation = tf.nn.softmax))

classifier.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001),
loss=tf.keras.losses.binary_crossentropy,
metric=tf.keras.metrics.categorical_accuracy)


my_estimator = tf.keras.estimator.model_to_estimator(keras_model=classifier)

错误来自最后一行代码我想这是因为 keras Dense 没有良好的属性,但是我怎样才能找到具有 from_config 的等效属性?

Keras==2.1.6
tensorflow==1.7.0

最佳答案

看起来您使用了错误包中的Dense层:它应该是tf.keras.layers.Dense而不是tf.layers。密集

请注意,虽然它们具有相同的类名和许多相似的参数,但实际上它们没有任何共同点:tf.layers.Dense是一个高级tensorflow API,与keras无关。这就是为什么您无法将它们添加到分类器中。

关于python - 无法将keras模型转换为tensorflow : type object 'Dense' has no attribute 'from_config' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50047605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com