使用虹膜数据集示例:
@Immutable
private static final class DependentBigtablePath extends BigtablePath {
private final BigtablePath delegate;
private final Function<String, String> derivationFunction;
private DependentBigtablePath( BigtablePath delegate, Function<String, String> derivationFunction) {
this.delegate = Preconditions.checkNotNull(delegate);
this.derivationFunction =Preconditions.checkNotNull(derivationFunction);
}
...
}
使用的进口:
train_ds_url = "http://download.tensorflow.org/data/iris_training.csv"
我下载了数据集,然后使用import tensorflow as tf
import pandas as pd
import numpy as np
tf.enable_eager_execution()
表示pd.read
,train_plantfeatures
数组。
train_categories
此后,我使用categories='Plants'
train_path = tf.keras.utils.get_file(train_ds_url.split('/')[-1], train_ds_url)
train = pd.read_csv(train_path, names=ds_columns, header=0)
train_plantfeatures, train_categories = train, train.pop(categories)
来创建分类表示。
tf.contrib.keras.utils.to_categorical
当我尝试使用y_categorical = tf.contrib.keras.utils.to_categorical(train_categories, num_classes=3)
和tf.data.Dataset
from_tensor_slices
我收到了:
ValueError:无法将非矩形Python序列转换为Tensor。
没有急切模式的相同实现效果很好。这里是Colab示例
答案 0 :(得分:4)
from_tensor_slices()
方法接收一个Numpy数组作为输入。但是在这种情况下,变量train_plantfeatures
是熊猫DataFrame
。
type(train_plantfeatures)
`Out:` pandas.core.frame.DataFrame
要执行此操作,请添加.values
以将熊猫从Numpy转换为Numpy:
dataset = tf.data.Dataset.from_tensor_slices((train_plantfeatures.values,
y_categorical))
对test_plantfeatures
变量执行相同操作:
dataset_test = tf.data.Dataset.from_tensor_slices((test_plantfeatures.values,
y_categorical_test))