节点'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf'

Posted

技术标签:

【中文标题】节点\'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf\'【英文标题】:Node 'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf'节点'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf' 【发布时间】:2021-06-06 06:34:35 【问题描述】:
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': 连接到具有 3 个输出的源节点 conv5_block3_3_bn/cond 的无效输出 3。尝试使用 tf.compat.v1.experimental.output_all_intermediates(True)。

【问题讨论】:

【参考方案1】:

我可以使用 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>

【讨论】:

以上是关于节点'training/Adam/gradients/gradients/conv5_block3_3_bn/cond_grad/StatelessIf'的主要内容,如果未能解决你的问题,请参考以下文章

给出一个二叉树的节点,返回该节点的前驱节点

给出一个二叉树的节点,返回该节点的前驱节点

web前端练习25----Dom1,元素节点(自己,父节点,子节点,兄弟节点),属性节点,文本节点

获取TreeView单击节点父节点的ID

DOM节点和DOM元素的区别

二叉树