tf.data.Dataset.from_tensor_slices的作用

刚接触TensorFlow不久,对于很多api看不懂,翻看了网上很多人的解说都是挺模糊的,然后决定还是自己写个小demo来的直观,这里分享出来给一起学习TensorFlow的小伙伴

    import tensorflow as tf
    import numpy as np
    
    features, labels = (np.random.sample((6,3)),np.random.sample((6,1)))
    print((features, labels))
    data = tf.data.Dataset.from_tensor_slices((features,labels))
    print(list(data.as_numpy_iterator()))

打印信息如下

(array([[0.98895762, 0.26454848, 0.0183561 ],
       [0.51641159, 0.8714609 , 0.2763759 ],
       [0.74152886, 0.74101885, 0.39290169],
       [0.27559503, 0.36266356, 0.23744076],
       [0.5434669 , 0.95196242, 0.00877751],
       [0.28143582, 0.47334865, 0.71569477]]), array([[0.37186106],
       [0.68210544],
       [0.19374422],
       [0.75647886],
       [0.25159219],
       [0.31047784]]))
    2021-05-07 19:21:02.650948: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
    2021-05-07 19:21:02.651337: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    [(array([0.98895762, 0.26454848, 0.0183561 ]), array([0.37186106])), (array([0.51641159, 0.8714609 , 0.2763759 ]), array([0.68210544])), (array([0.74152886, 0.74101885, 0.39290169]), array([0.19374422])), (array([0.27559503, 0.36266356, 0.23744076]), array([0.75647886])), (array([0.5434669 , 0.95196242, 0.00877751]), array([0.25159219])), (array([0.28143582, 0.47334865, 0.71569477]), array([0.31047784]))]

这样应该相当直观了.

你可能感兴趣的:(tf.data.Dataset.from_tensor_slices的作用)