亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決 Resnet 模型中輸出張量的值錯誤?

如何解決 Resnet 模型中輸出張量的值錯誤?

慕村225694 2022-01-18 21:20:50
正在嘗試使用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


查看完整回答
反對 回復 2022-01-18
  • 1 回答
  • 0 關注
  • 289 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號