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]

TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊
我假設(shè)您有 100 張尺寸為 128x128 的圖像,它們是 3 通道(RGB)。而且您的網(wǎng)絡(luò)無法一次獲取所有圖像。它應(yīng)該一步獲得一張圖像。所以你有2個選擇:
使用 for 循環(huán)遍歷數(shù)據(jù)集,從數(shù)據(jù)集中獲取一張圖像輸入和一張輸出圖像
使用批處理。告訴您網(wǎng)絡(luò)您將使用批次:
tf.keras.layers.InputLayer(input_shape=(None, 128, 128, 3))
-輸入占位符中的 Tensorflow 批次大小
添加回答
舉報