1 回答

TA貢獻1868條經(jīng)驗 獲得超4個贊
要么使用:
y = tf.data.Dataset.from_tensors(dict(y_train))
或這個:
y = tf.data.Dataset.from_tensor_slices(y_train)
或者只使用雙括號,這樣您的數(shù)據(jù)框仍然是一個數(shù)據(jù)框,那么您就不需要更改任何內(nèi)容:
y_train = df[['height']].copy()
y = tf.data.Dataset.from_tensor_slices(dict(y_train))
重現(xiàn)該問題的完整代碼,以及替代方案:
import tensorflow as tf
import pandas as pd
df = pd.DataFrame(data={'integers': [1, 2, 3, 4], 'floats': [4., 3., 2., 1]})
y_train = df['floats'].copy()
y = tf.data.Dataset.from_tensor_slices(dict(y_train)) # not good
y = tf.data.Dataset.from_tensor_slices(y_train) # good
print(next(iter(y)))
y = tf.data.Dataset.from_tensors(y_train) # good
print(next(iter(y)))
y_train = df[['floats']].copy()
y = tf.data.Dataset.from_tensor_slices(dict(y_train)) # good
next(iter(y))
{'floats': <tf.Tensor: shape=(4,), dtype=float64, numpy=array([5., 4., 3., 2.])>}
添加回答
舉報