tf.data.Dataset.from_tensor_slices,张量和渴望模式

时间:2018-08-13 20:19:20

标签: python tensorflow keras tensor tensorflow-datasets

使用虹膜数据集示例:

@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.readtrain_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示例

1 个答案:

答案 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))