簡體   English   中英

在 TensorFlow 2.0 中使用 tf.Dataset 進行訓練

[英]Training using tf.Dataset in TensorFlow 2.0

我很難使用tf.Dataset而不是 pd.DataFrame 訓練我的 TensorFlow pd.DataFrame (工作正常)。

我在下面創建了一個虛擬示例,鑒於我在網上/在TensorFlow 網站上閱讀的內容,我希望它可以工作。

!pip install tensorflow==2.0.0 > /dev/null

import numpy as np
import tensorflow as tf

features, target = np.random.rand(100, 30), np.random.randint(0, 2, 100)
dataset = tf.data.Dataset.from_tensor_slices((features, target))

model = tf.keras.Sequential([
    tf.keras.layers.Dense(30, activation='relu', input_shape=(30,)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.5),

    tf.keras.layers.Dense(30, activation='relu'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.5),

    tf.keras.layers.Dense(1, activation='sigmoid')
])

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

model.fit(
    dataset, 
    epochs=10,
)

返回以下錯誤消息

...

ValueError: Error when checking input: expected dense_input to have shape (30,) but got array with shape (1,)

上面有什么明顯的錯誤嗎? 為什么 TensorFlow 抓取形狀為(1,)的輸入?

嘗試使用tf.data.Dataset.from_tensors代替tf.data.Dataset.from_tensor_slices

此處解釋的差異: https://stackoverflow.com/a/55370549/10418812

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM