【问题标题】:Tensor flow shuffle a tensor for batch gradientTensorFlow shuffle 用于批量梯度的张量
【发布时间】:2017-11-24 21:33:20
【问题描述】:

它可能与谁有关,

我对 tensorflow 很陌生。我正在尝试解决著名的 CNN MNIST 问题。但是当我不得不重新调整 x_training 数据(这是一个 [40000, 28, 28, 1] 形状数据)时,我遇到了困难。

我的代码如下:

x_train_final = tf.reshape(x_train_final, [-1, image_width, image_width, 1])

x_train_final = tf.cast(x_train_final, dtype=tf.float32)

perm = np.arange(num_training_example).astype(np.int32)
np.random.shuffle(perm)

x_train_final = x_train_final[perm]

发生以下错误:

ValueError:形状必须为 1 级,但对于 'strided_slice_1371'(操作:'StridedSlice')为 2 级,输入形状为:[40000,28,28,1]、[1,40000]、[1,40000] , [1].

任何人都可以建议我如何解决这个问题?谢谢。

【问题讨论】:

    标签: python-2.7 tensorflow shuffle


    【解决方案1】:

    我建议你使用 scikit 的 shuffle 函数。

    from sklearn.utils import shuffle
    x_train_final = shuffle(x_train_final)
    

    此外,您可以传入多个数组,shuffle 函数将重新组织(混洗)这些多个数组中的数据,并在所有这些数组中保持相同的混洗顺序。因此,您甚至可以传入您的标签数据集。 例如:

    X_train, y_train = shuffle(X_train, y_train)
    

    【讨论】:

    • 感谢您的回复。我试过这个方法,出现以下错误。 TypeError: 不支持的操作数类型 /: 'Dimension' 和 'int'
    • 我建议您不要将数据转换为 tensorflow 计算图的一部分。在 tensorflow 中定义层、操作并以 numpy 格式提供图像数据。
    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 2016-08-20
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2020-09-01
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多