节点“训练/Adam/梯度/梯度/转换块3\u 3\u bn/状态梯度/无状态IF”

2024-10-03 11:17:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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数据集上执行图像分类任务。我使用的是带有TensorFlow 2.5.0版的colab CPU

我在“model.fit”行中遇到此错误:

InvalidArgumentError: Node 'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf': Connecting to invalid output 3 of source node conv5_block3_3_bn/cond which has 3 outputs. Try using tf.compat.v1.experimental.output_all_intermediates(True).


Tags: imginputoutputbasemodeltrainwidthoutputs
1条回答
网友
1楼 · 发布于 2024-10-03 11:17:57

我能够使用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>

相关问题 更多 >