第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Numpy 數(shù)組丟失轉(zhuǎn)換為 tf 數(shù)據(jù)集的維度

Numpy 數(shù)組丟失轉(zhuǎn)換為 tf 數(shù)據(jù)集的維度

阿晨1998 2022-07-26 16:58:02
我正在嘗試tf.data.Dataset使用以下代碼將 numpy 數(shù)組轉(zhuǎn)換為 a:train_dataset = tf.data.Dataset.from_tensor_slices((traininput, train[:, :, :, 1:4]))但是,我的數(shù)據(jù)集現(xiàn)在缺少它的第一個維度。numpy 數(shù)組都具有 1000、128、128、3 的形狀,并且數(shù)據(jù)集縮減為 128、128、3 的形狀。這會在嘗試訓(xùn)練我的模型時導(dǎo)致錯誤:檢查輸入時出錯:expected input_2 to have 4 dimensions, but got array with shape (128, 128, 3) 我已嘗試根據(jù)加載 numpy 數(shù)據(jù)的 tensorflow 教程工作。為什么會發(fā)生這種情況,我該如何解決?正如建議的那樣,我在下面提供了一個 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貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊

您需要batch在數(shù)據(jù)集上設(shè)置大小,以便它返回多個示例,而不僅僅是一個。這也會將維度數(shù)更改為 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張量的最后一個維度以避免出現(xiàn)不同的錯誤:


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]


查看完整回答
反對 回復(fù) 2022-07-26
?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊

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

  1. 使用 for 循環(huán)遍歷數(shù)據(jù)集,從數(shù)據(jù)集中獲取一張圖像輸入和一張輸出圖像

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


查看完整回答
反對 回復(fù) 2022-07-26
  • 2 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號