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

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

無法獲得 TensorFlow 自定義層的正確形狀

無法獲得 TensorFlow 自定義層的正確形狀

瀟瀟雨雨 2023-10-31 14:31:38
我正在嘗試使用自定義層在 TensorFlow 中訓練模型。我在最后一層遇到問題,我正在嘗試構建一個層來獲取一批圖像 [None,100,100,1] 并返回 10 個不同方形區域的總和,因此輸出應該是 [None] 的形狀,10]。我嘗試了一些不同的方法但沒有成功。我試過了:        output = tf.concat([tf.reshape(tf.math.reduce_sum(inputs[34:42, 28:40,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[34:42, 44:56,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[34:42, 60:72,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 20:32,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 36:48,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 52:64,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[46:54, 68:80,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[58:66, 28:40,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[58:66, 44:56,0]), [1,]),                            tf.reshape(tf.math.reduce_sum(inputs[58:66, 60:72,0]), [1,])], axis= 0)和類似的求和函數,但無法將形狀的第一維設置為“無”。我嘗試過“作弊”,將輸入重塑為正確的形狀,然后乘以 0 并添加大小為 [10] 的張量。這得到了正確的形狀,但模型沒有訓練。有沒有正確的方法來做到這一點?我在這個問題上被困了好幾個星期,但沒有運氣。如果我放置一個不做我想要的事情的不同層,模型訓練得很好,但它具有正確的輸出形狀:class output_layer(tf.keras.Model):    def __init__(self, shape_input):        self.shape_input = shape_input        super(output_layer, self).__init__()    def call(self, inputs):        inputs = tf.math.multiply(inputs,tf.math.conj(inputs))        temp = tf.math.reduce_sum(inputs, axis=2)        temp = tf.reshape(temp, [-1,10,10])        temp = tf.math.reduce_sum(temp, axis=2)                output = tf.cast(temp, tf.float32)        output = tf.keras.activations.softmax(output, axis=-1)        return output如果有人能幫助我,我將非常感激!
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

我修改了你的代碼并提出以下內容:


output = tf.concat(

                  [tf.math.reduce_sum(inputs[:, 34:42, 28:40,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 34:42, 44:56,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 34:42, 60:72,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 46:54, 20:32,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 46:54, 36:48,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 46:54, 52:64,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 46:54, 68:80,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 58:66, 28:40,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 58:66, 44:56,:], axis=[1,2]),

                   tf.math.reduce_sum(inputs[:, 58:66, 60:72,:], axis=[1,2])], axis=-1)

請注意,我更改inputs[34:42, 28:40, 0]為inputs[:, 34:42, 28:40,:]. 您可以用于:想要保持相同的尺寸。我還指定了應減少哪個軸,因此,僅保留沒有要減少的規格的尺寸 - 在本例中,它是第一個也是最后一個尺寸。在你的情況下,tf.math.reduce_sum將產生形狀[無,1]。與此同時,我將 的軸更改tf.concat為 -1,這是最后一層,因此它產生形狀 [None, 10]。


為了完整起見,您可以創建自己的圖層。為此,您必須繼承 tf.keras.layers.Layer。


然后,您可以將其用作任何其他層。


class ReduceZones(tf.keras.layers.Layer):

    def __init__(self):

        super(ReduceZones, self).__init__()

      

    def build(self, input_shapes):

        return

      

    def call(self, inputs):

        output = tf.concat(

                [tf.math.reduce_sum(inputs[:, 34:42, 28:40,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 34:42, 44:56,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 34:42, 60:72,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 46:54, 20:32,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 46:54, 36:48,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 46:54, 52:64,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 46:54, 68:80,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 58:66, 28:40,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 58:66, 44:56,:], axis=[1,2]),

                 tf.math.reduce_sum(inputs[:, 58:66, 60:72,:], axis=[1,2])], axis=-1)

        return output


查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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