【问题标题】:How to read data from npy file as TensorFlow dataset如何从 npy 文件中读取数据作为 TensorFlow 数据集
【发布时间】:2018-10-31 00:50:54
【问题描述】:

我的 npy 包含两个 ndarray,一个是 one-hot,另一个是标签。如何输入数据?

如何将这些数据转换成 x_train 作为张量?我只有 MNIST 数据集的经验,不需要处理输入数据。

【问题讨论】:

    标签: numpy tensorflow


    【解决方案1】:

    MNIST 数据集的加载方式与 .npy 数组类似。

    这行代码

    (X_train, y_train), (X_test, y_test) = 
    
     keras.datasets.mnist.load_data(path="C:/Users/476458/.keras/datasets/mnist.npz")
    

    依次使用以下代码。

    path = get_file(path,
                    origin='https://s3.amazonaws.com/img-datasets/mnist.npz',
                    file_hash='8a61469f7ea1b51cbae51d4f78837e45')
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)
    

    np.load 加载 .npz 文件,该文件是一个压缩存档,其中包含 4 个 .npy 文件(x_train.npy、y_train. npy, x_test.npy, y_test)。

    但是您应该能够直接使用 np.load 加载 .npy 文件。

    我希望这能给你一些想法。

    【讨论】:

      猜你喜欢
      • 2022-10-06
      • 2017-03-05
      • 1970-01-01
      • 2022-12-17
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2017-10-27
      相关资源
      最近更新 更多