如何解决节点'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf'
img_height,img_width = 32,32
base_model = ResNet50(weights = 'imagenet',include_top = False,input_shape =(img_height,img_width,3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.7)(x)
predictions = Dense(num_classes,activation = 'softmax')(x)
model = Model(inputs = base_model.input,outputs = predictions)
model.compile(optimizer = Adam(),lr = 0.0001,loss = 'categorical_crossentropy',metrics = ['accuracy'])
model.fit(X_train,y_train,epochs = 20,batch_size = 128)
我正在尝试使用 Resnet 在 CIFAR-10 数据集上执行 image_classification 任务。我正在使用带有 TensorFlow 2.5.0 版的 colab CPU。
我在“model.fit”行中收到此错误:
InvalidArgumentError:节点“training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf”:连接到具有 3 个输出的源节点 conv5_block3_3_bn/cond 的无效输出 3。尝试使用 tf.compat.v1.experimental.output_all_intermediates(True)。
解决方法
我可以使用 TF2.5 在 colab 上执行代码而不会出现任何问题
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.applications import ResNet50
from tensorflow.keras import Input,Model
from tensorflow.keras.layers import GlobalAveragePooling2D,Dropout,Dense
from tensorflow.keras.optimizers import Adam
(X_train,y_train),(X_test,y_test) = tf.keras.datasets.cifar10.load_data()
X_train = X_train.astype("float32") / 255.0
X_test = X_test.astype("float32") / 255.0
y_train = tf.keras.utils.to_categorical(y_train,10)
y_test = tf.keras.utils.to_categorical(y_test,10)
num_classes = 10
img_height,img_width = 32,32
base_model = ResNet50(weights = 'imagenet',include_top = False,input_shape =(img_height,img_width,3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.7)(x)
predictions = Dense(num_classes,activation = 'softmax')(x)
model = Model(inputs = base_model.input,outputs = predictions)
model.compile(optimizer = Adam(),loss = 'categorical_crossentropy',metrics = ['accuracy'])
model.fit(X_train,y_train,epochs = 5,batch_size = 128)
输出:
2.5.0
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 6s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94773248/94765736 [==============================] - 2s 0us/step
Epoch 1/5
391/391 [==============================] - 72s 72ms/step - loss: 2.1970 - accuracy: 0.3832
Epoch 2/5
391/391 [==============================] - 27s 69ms/step - loss: 1.5985 - accuracy: 0.5207
Epoch 3/5
391/391 [==============================] - 28s 71ms/step - loss: 1.4202 - accuracy: 0.5837
Epoch 4/5
391/391 [==============================] - 27s 69ms/step - loss: 1.1143 - accuracy: 0.6464
Epoch 5/5
391/391 [==============================] - 27s 70ms/step - loss: 0.9299 - accuracy: 0.6993
<tensorflow.python.keras.callbacks.History at 0x7f7c8c112350>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。