tensorflow将特征与标签配对
import tensorflow as tf #导入tensorflow
features = tf.constant([1, 2, 3, 4]) #创建实例,内容为特征值
labels = tf.constant([0, 0, 1, 0]) #创建实例,内容为标签值
#使用from_tensor_slices函数将特征与标签配对
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
print(dataset) #打印dataset
for e in dataset: #遍历dataset内容
print(e)
结果为:
#打印dataset结果
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
#遍历dataset结果,可以看到特征值与标签值都一一对应了
(<tf.Tensor: id=9, shape=(), dtype=int32, numpy=1>, <tf.Tensor: id=10, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=11, shape=(), dtype=int32, numpy=2>, <tf.Tensor: id=12, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=13, shape=(), dtype=int32, numpy=3>, <tf.Tensor: id=14, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=15, shape=(), dtype=int32, numpy=4>, <tf.Tensor: id=16, shape=(), dtype=int32, numpy=0>)