【发布时间】:2021-05-29 07:51:35
【问题描述】:
我了解如何在 Tensorflow 1.x 中执行此操作 (link here)
但是对于Tensorflow 2.0,如何为numpy矩阵创建feature_columns?
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import tensorflow as tf
X = iris['data']
y = iris['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
ds_train = tf.data.Dataset.from_tensor_slices((X_train, y_train))
ds_test = tf.data.Dataset.from_tensor_slices((X_test, y_test))
model = CustomModel(feature_columns, num_classes=y_train.shape[1])
model.compile()
model.compile('adam', loss='categorical_crossentropy', metrics='accuracy')
根据CustomModel的文档字符串,它要求feature_columns: The Tensorflow feature columns for the dataset.
我以 sklearn 的 iris 数据集为例。我知道 tensorflow2.0 有一个 iris 数据集。如果我使用那个数据集,我就不会有这个问题。但这不是重点。鉴于我有 numpy 矩阵,我想知道如何创建特征列以输入 tensorflow 模型。
【问题讨论】:
标签: python numpy tensorflow tensorflow2.0