【问题标题】:Can you Transpose/Reverse the shape in Tensorflow's placeholder?你能在 Tensorflow 占位符中转置/反转形状吗?
【发布时间】:2019-05-03 19:49:48
【问题描述】:

假设我的输入数据 x 的形状为 (2000, 2),其中 2000 是样本数,2 是特征数。

所以对于这个输入数据,我可以像这样设置一个占位符: x = tf.placeholder(tf.float32, shape=[None, 2], name='features')

我的问题是,如果我转置输入数据 x,使形状现在为 (2, 2000),其中 2000 仍然是样本数,我将如何更改 tf.placeholder 中的“形状”参数?

我尝试设置 shape=[2, None],但我得到了一个错误。 “形状”参数中的第一个元素是否总是必须是“无”? 这里我得到的错误是:“ValueError:应该定义Dense的输入的最后一个维度。找到None。”

import tensorflow as tf
# Binary Classifier Implementation

# Training data
x_train = np.transpose(X) #shape=(2, 2000)
y_train = np.hstack((np.zeros((1, 1000)),np.zeros((1, 1000)) + 1)) #shape=(1, 2000)


# Variables
x = tf.placeholder(tf.float32, shape=[2, None], name='features')
y_ = tf.placeholder(tf.int64, shape=[1, None], name='labels')
h1 = tf.layers.dense(inputs=x, units=50, activation=tf.nn.relu) #one hidden layer with 50 neurons
y = tf.layers.dense(inputs=h1, units=1, activation=tf.nn.sigmoid) #one output layer with 1 neuron


# Functions
#loss
cross_entropy = tf.losses.sigmoid_cross_entropy(multi_class_labels=y_, logits=y)
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)


# Initializer
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for i in range(1000):
        sess.run([cross_entropy], feed_dict={x: x_train, y_: y_train})

【问题讨论】:

    标签: python tensorflow neural-network


    【解决方案1】:

    我认为您可能会遇到形状不一致的问题,而不是转置或在第一个暗淡之外的其他地方具有未定义的尺寸大小。
    以下事情对我来说很好:

    import tensorflow as tf
    
    x = tf.placeholder(tf.float32, shape=[None, 2])
    x_t = tf.transpose(x)
    y = tf.placeholder(tf.float32, shape=[2, None])
    
    print(x_t.shape)
    >> TensorShape([Dimension(2), Dimension(None)])
    
    print(y.shape)
    >> TensorShape([Dimension(2), Dimension(None)])
    

    我假设第一个维度可能是您的批量大小?您是否可能与此不一致或提供了错误形状的数据?
    或者您可能正在尝试手动更改张量的形状? (如果是这样,那么您就不需要。tf 会在我的示例中处理这一点)。
    如果您不知道您遇到的错误、代码 sn-p 或任何比模糊和笼统的描述更多的东西,这是最有帮助的,但我希望它仍然有帮助。
    祝你好运!


    由于更新的问题而更新:
    你得到的错误是告诉你到底是什么问题。你可以转置任何你想要的东西,但是dense 层特别不能接受 last 维度为None 的张量。这是有道理的;例如,如果不知道权重矩阵的大小,您将无法创建完全连接的层。
    如果您要转置x,那么您是在说您有 2000 个特征(和 2 个数据点)并且 NN 需要知道这一点才能创建您要使用的参数火车。
    如果您仍然认为自己有 2 个特征和许多示例,那么您首先不应该使用 (2, 2000) 的形状!
    请尝试以下操作:

    # Training data
    x_train = X #shape=(2000, 2)
    y_train = np.hstack((np.zeros((1000, 1)),np.zeros((1000, 1)) + 1)) #shape=(2000, 1)
    
    
    # Variables
    x = tf.placeholder(tf.float32, shape=[None, 2], name='features')
    y_ = tf.placeholder(tf.int64, shape=[None, 1], name='labels')
    h1 = tf.layers.dense(inputs=x, units=50, activation=tf.nn.relu) #one hidden layer with 50 neurons
    y = tf.layers.dense(inputs=h1, units=1, activation=tf.nn.sigmoid) #one output layer with 1 neuron
    
    
    # Functions
    #loss
    cross_entropy = tf.losses.sigmoid_cross_entropy(multi_class_labels=y_, logits=y)
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
    
    
    # Initializer
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init)
        for i in range(1000):
            sess.run([cross_entropy], feed_dict={x: x_train, y_: y_train})
    

    希望这会有所帮助。


    在一个完全不同且不相关的注释中:我希望您知道以这种方式将 2 个特征嵌入到 50 维空间中时您在做什么。

    【讨论】:

    • 感谢您的提示。我已经用代码更新了我的问题。我仍然不太确定如何解决它。如果我只使用 (2000, 2) 格式和 tf.placeholder shape=[None, 2] 而不是 (2, 2000) 格式和 tf.placeholder shape=[2, None],它就可以正常工作。
    • 感谢您的帮助!我尝试使用 (2, 2000) 形状作为输入数据的原因是因为这是我通过其他没有提到 Tensorflow 的神经网络来源了解到的惯例。我想我可以尝试通过样本计数保持相同的特征计数矩阵约定,但我猜你已经确认这个约定与 Tensorflow 不兼容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    相关资源
    最近更新 更多