对tensorflow变量形状感到困惑

对tensorflow变量形状感到困惑,tensorflow,Tensorflow,我正在使用tensorflow机器学习烹饪书()学习tensorflow。我目前在NLP第(07)章。我对如何决定张量流变量的维数感到非常困惑。例如,在他们使用的单词包示例中: # Create variables for logistic regression A = tf.Variable(tf.random_normal(shape=[embedding_size,1])) b = tf.Variable(tf.random_normal(shape=[1,1])) # Initiali

我正在使用tensorflow机器学习烹饪书()学习tensorflow。我目前在NLP第(07)章。我对如何决定张量流变量的维数感到非常困惑。例如,在他们使用的单词包示例中:

# Create variables for logistic regression
A = tf.Variable(tf.random_normal(shape=[embedding_size,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

# Initialize placeholders
x_data = tf.placeholder(shape=[sentence_size], dtype=tf.int32)
y_target = tf.placeholder(shape=[1, 1], dtype=tf.float32)
# Create variables for logistic regression
A = tf.Variable(tf.random_normal(shape=[max_features,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

x_data = tf.placeholder(shape=[None, max_features], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
在tf idf示例中,他们使用:

# Create variables for logistic regression
A = tf.Variable(tf.random_normal(shape=[embedding_size,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

# Initialize placeholders
x_data = tf.placeholder(shape=[sentence_size], dtype=tf.int32)
y_target = tf.placeholder(shape=[1, 1], dtype=tf.float32)
# Create variables for logistic regression
A = tf.Variable(tf.random_normal(shape=[max_features,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

x_data = tf.placeholder(shape=[None, max_features], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

如何决定在占位符形状中何时使用None和1?谢谢大家!

None
用作形状的一部分,意味着它将在运行时确定。 这对于所谓的培训非常有用,在这种培训中,您为培训过程的每个迭代提供一个固定大小的数据子集。 因此,如果将其设置为
None
,则可以在批大小之间切换,而不会出现问题。(虽然您不会在同一个会话中执行此操作,但每个会话都可以尝试不同的批处理大小)

当您陈述一个特定的形状时,它将是什么,这是会话期间唯一可以馈送给它的形状(使用
feed_dict
param)


在您的特定示例中,
y_目标的第一部分的形状将始终是
[1,1]
,其中在代码的第二部分,
y_目标可以是
[10,1]
/
[200,1]
/
[1]

当占位符中的元素计数事先未知时,应使用“无”。但例如,在x_数据占位符中,如果数据元素的计数为1,即事先已知,则可以将“无”替换为1

请参阅,了解如何在Tensorflow中处理形状的说明