刚开始用
tensorflow-datasets
有点纳闷。我花了将近一个小时在谷歌上搜索它,但我仍然找不到在数据框中获取特征名称的方法。我想我错过了一些明显的东西。
import tensorflow_datasets as tfds
ds, ds_info = tfds.load('iris', split='train',
shuffle_files=True, with_info=True)
tfds.as_dataframe(ds.take(10), ds_info)
我想知道哪个特征是什么:sepal_length、sepal_width、petal_length、petal_width。但我坚持一个单一的
ndarray
.
我可以得到类名:
ds_info.features["label"].names
给我:
['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']
,但是
ds_info.features["features"]
什么都没给我:
Tensor(shape=(4,), dtype=float32)
总而言之,我的问题是:知道如何识别具有“sepal_length”、“sepal_width”、“petal_length”、“petal_width”等特征名称的输入 ndarray 内容吗?
这里是使用 tensorflow 数据集 API 的鸢尾花数据入门代码。
ds, ds_info = tfds.load(
'iris', split='train',
shuffle_files=True, with_info=True
)
ds = ds.map(lambda x : (x['features'], x['label']))
x, y = next(iter(ds))
x.shape, y.shape
# (TensorShape([4]), TensorShape([]))
ds = ds.batch(32, drop_remainder=True)
ds = ds.prefetch(tf.data.AUTOTUNE)
x, y = next(iter(ds))
x.shape, y.shape
# (TensorShape([32, 4]), TensorShape([32]))
构建虚拟模型进行训练。
model = keras.Sequential([
keras.layers.Dense(16, activation='relu'),
keras.layers.Dense(32, activation='relu'),
keras.layers.Dense(64, activation='relu'),
# 3 classes: https://www.tensorflow.org/datasets/catalog/iris
keras.layers.Dense(3, activation='softmax'),
])
model.compile(
loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
history = model.fit(
ds,
)
# 3s 9ms/step - loss: 1.0857 - accuracy: 0.3047
资源