本文整理匯總了Python中tensorflow.foldr方法的典型用法代碼示例。如果您正苦於以下問題:Python tensorflow.foldr方法的具體用法?Python tensorflow.foldr怎麽用?Python tensorflow.foldr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow
的用法示例。
在下文中一共展示了tensorflow.foldr方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testFoldr_Scoped
# 需要導入模塊: import tensorflow [as 別名]
# 或者: from tensorflow import foldr [as 別名]
def testFoldr_Scoped(self):
with self.test_session() as sess:
with tf.variable_scope("root") as varscope:
elems = tf.constant([1, 2, 3, 4, 5, 6], name="data")
r = tf.foldr(simple_scoped_fn, elems)
# Check that we have the one variable we asked for here.
self.assertEqual(len(tf.trainable_variables()), 1)
self.assertEqual(tf.trainable_variables()[0].name, "root/body/two:0")
sess.run([tf.global_variables_initializer()])
self.assertAllEqual(450, r.eval())
# Now let's reuse our single variable.
varscope.reuse_variables()
r = tf.foldr(simple_scoped_fn, elems, initializer=10)
self.assertEqual(len(tf.trainable_variables()), 1)
self.assertAllEqual(1282, r.eval())
示例2: foldr
# 需要導入模塊: import tensorflow [as 別名]
# 或者: from tensorflow import foldr [as 別名]
def foldr(fn, elems, initializer=None, name=None):
"""Reduce elems using fn to combine them from right to left.
# Arguments
fn: Callable that will be called upon each element in elems and an
accumulator, for instance `lambda acc, x: acc + x`
elems: tensor
initializer: The first value used (`elems[-1]` in case of None)
name: A string name for the foldr node in the graph
# Returns
Tensor with same type and shape as `initializer`.
"""
return tf.foldr(fn, elems, initializer=initializer, name=name)
示例3: testFoldr_Simple
# 需要導入模塊: import tensorflow [as 別名]
# 或者: from tensorflow import foldr [as 別名]
def testFoldr_Simple(self):
with self.test_session():
elems = tf.constant([1, 2, 3, 4, 5, 6], name="data")
r = tf.foldr(lambda a, x: tf.mul(tf.add(a, x), 2), elems)
self.assertAllEqual(450, r.eval())
r = tf.foldr(
lambda a, x: tf.mul(tf.add(a, x), 2), elems, initializer=10)
self.assertAllEqual(1282, r.eval())
示例4: testFold_Grad
# 需要導入模塊: import tensorflow [as 別名]
# 或者: from tensorflow import foldr [as 別名]
def testFold_Grad(self):
with self.test_session():
elems = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name="data")
v = tf.constant(2.0, name="v")
r = tf.foldl(
lambda a, x: tf.mul(a, x), elems, initializer=v)
r = tf.gradients(r, v)[0]
self.assertAllEqual(720.0, r.eval())
r = tf.foldr(
lambda a, x: tf.mul(a, x), elems, initializer=v)
r = tf.gradients(r, v)[0]
self.assertAllEqual(720.0, r.eval())