正在嘗試使用https://github.com/jzuern/cifar-classifier中的代碼在 Google Colab 上實現 ResNet-CIFAR 10 模型。我使用的是我自己的自定義激活函數,而不是 ReLU 激活。這是代碼: def fonlaaf(x): return x/(1-tf.exp(-x)) def resnet_layer(inputs, num_filters=16, kernel_size=3, strides=1, activation='fonlaaf', batch_normalization=True, conv_first=True): """2D Convolution-Batch Normalization-Activation stack builder # Arguments inputs (tensor): input tensor from input image or previous layer num_filters (int): Conv2D number of filters kernel_size (int): Conv2D square kernel dimensions strides (int): Conv2D square stride dimensions activation (string): activation name batch_normalization (bool): whether to include batch normalization conv_first (bool): conv-bn-activation (True) or bn-activation-conv (False) # Returns x (tensor): tensor as input to the next layer """ conv = Conv2D(num_filters, kernel_size=kernel_size, strides=strides, padding='same', kernel_initializer='he_normal', kernel_regularizer=tf.keras.regularizers.l2(1e-4)) x = inputs if conv_first: x = conv(x) if batch_normalization: x = BatchNormalization()(x) if activation is not None: x = fonlaaf(x) else: if batch_normalization: x = BatchNormalization()(x) if activation is not None: x = fonlaaf(x) x = conv(x) return x得到以下錯誤:ValueError:模型的輸出張量必須是 TensorFlow 的輸出Layer(因此保存過去的層元數據)。找到:Tensor("dense/Softmax:0", shape=(?, 10), dtype=float32)以前對這個錯誤的回答大多沒有奏效。我在這里想念什么?
1 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
如錯誤所述,您必須傳遞圖層的輸出。由于 fonlaaf() 是一個沒有狀態的激活函數,您可以使用 Lambda 層。
代替,
def fonlaaf(x):
return x/(1-tf.exp(-x))
和
def fonlaaf(x):
return tf.keras.layers.Lambda(lambda x: x/(1-tf.exp(-x)))(x)
https://www.tensorflow.org/guide/keras/#custom_layers https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda
添加回答
舉報
0/150
提交
取消