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

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

Numpy 數組丟失轉換為 tf 數據集的維度

Numpy 數組丟失轉換為 tf 數據集的維度

阿晨1998 2022-07-26 16:58:02
我正在嘗試tf.data.Dataset使用以下代碼將 numpy 數組轉換為 a:train_dataset = tf.data.Dataset.from_tensor_slices((traininput, train[:, :, :, 1:4]))但是,我的數據集現在缺少它的第一個維度。numpy 數組都具有 1000、128、128、3 的形狀,并且數據集縮減為 128、128、3 的形狀。這會在嘗試訓練我的模型時導致錯誤:檢查輸入時出錯:expected input_2 to have 4 dimensions, but got array with shape (128, 128, 3) 我已嘗試根據加載 numpy 數據的 tensorflow 教程工作。為什么會發生這種情況,我該如何解決?正如建議的那樣,我在下面提供了一個 mcve:import tensorflow as tfimport numpy as npinp = np.random.rand(100, 128, 128, 3)out = np.random.rand(100, 126, 126, 3)model = tf.keras.Sequential(    [        tf.keras.layers.InputLayer(input_shape=(128, 128, 3)),        tf.keras.layers.Conv2D(              filters=32, kernel_size=3, strides=(2, 2), activation='relu'),        tf.keras.layers.Conv2DTranspose(                      filters=3,                      kernel_size=3,                      strides=(2, 2),                      padding="SAME",                      activation='relu'),    ])model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])train_dataset = tf.data.Dataset.from_tensor_slices((inp, out))model_history = model.fit(train_dataset, epochs=10)它以:Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (128, 128, 3)
查看完整描述

2 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

您需要batch在數據集上設置大小,以便它返回多個示例,而不僅僅是一個。這也會將維度數更改為 4。


import tensorflow as tf

import numpy as np

inp = np.random.rand(100, 128, 128, 3)

# *** Had to set the last dim below to 1 to avoid another error with the accuracy

out = np.random.rand(100, 126, 126, 1)

model = tf.keras.Sequential(

    [

        tf.keras.layers.InputLayer(input_shape=(128, 128, 3)),

        tf.keras.layers.Conv2D(

              filters=32, kernel_size=3, strides=(2, 2), activation='relu'),

        tf.keras.layers.Conv2DTranspose(

                      filters=3,

                      kernel_size=3,

                      strides=(2, 2),

                      padding="SAME",

                      activation='relu'),

    ]

)

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

train_dataset = tf.data.Dataset.from_tensor_slices((inp, out))

# *** Setting batch size of 10 below

train_dataset = train_dataset.batch(10)

model_history = model.fit(train_dataset, epochs=10)

注意:我必須更改out張量的最后一個維度以避免出現不同的錯誤:


ValueError: Can not squeeze dim[3], expected a dimension of 1, got 3 for 'metrics/accuracy/Squeeze' (op: 'Squeeze') with input shapes: [?,126,126,3]


查看完整回答
反對 回復 2022-07-26
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

我假設您有 100 張尺寸為 128x128 的圖像,它們是 3 通道(RGB)。而且您的網絡無法一次獲取所有圖像。它應該一步獲得一張圖像。所以你有2個選擇:

  1. 使用 for 循環遍歷數據集,從數據集中獲取一張圖像輸入和一張輸出圖像

  2. 使用批處理。告訴您網絡您將使用批次:tf.keras.layers.InputLayer(input_shape=(None, 128, 128, 3))-輸入占位符中的 Tensorflow 批次大小


查看完整回答
反對 回復 2022-07-26
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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