2017-08-14 141 views
4

我正在学习使用张量流机器学习食谱的张量流(https://github.com/nfmcclure/tensorflow_cookbook)。我目前在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) 

,并在TF-IDF例如他们使用:

# 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) 

怎样才能当上在使用无对1决定占位符形状?谢谢!

+0

看一看[this answer](https://stackoverflow.com/ question/37096225/how-to-understand-static-shape-and-dynamic-shape-tensorflow)来解释如何在Tensorflow中处理形状 – GPhilo

回答

4

使用None作为形状的一部分,意味着它将在您运行session时确定。 这对培训所谓的batch培训非常有用,您可以在培训过程的每次迭代中提供数据的固定大小子集。 所以,如果你保持在None你可以切换批量大小没有问题。 (虽然你不会在同一会话中这样做,但每次会话都可以尝试不同的批量大小)

当你声明一个特定的形状时,这就是它的样子,那是唯一的形状会话(使用feed_dict PARAM)

在特定示例中,代码的第一部分期间被馈送给它,的y_target形状总是会[1, 1]其中的代码的第二部分,y_target可以[10, 1]/[200, 1]/[<whatever>, 1]

0

当应用中的元素数量应该使用“无”呃事先未知。但是,例如在x_data占位符中,如果数据元素的计数是1,即它是预先知道的,那么您可以用1替换'None'。