博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
examples of scatter_nd_update
阅读量:4031 次
发布时间:2019-05-24

本文共 2103 字,大约阅读时间需要 7 分钟。

Several simple examples showing the usage of scatter_nd_update is provided in the tensor flow official document( accessible via ). However, this example only shows its usage on 1 dimensional tensor. It cost me quite a time to use it on multi dimensional tensor. Meanwhile, few examples about its usage on multi dimensional tensor can be found on the web. Following shows three examples I have successfully finished. Before that, first shows the example from the tensor flow official document.

Example 1:( from tensorflow documentation)

For example, say we want to update 4 scattered elements to a rank-1 tensor to 8 elements. In Python, that update would look like this:

ref = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])indices = tf.constant([[4], [3], [1] ,[7]])updates = tf.constant([9, 10, 11, 12])update = tf.scatter_nd_update(ref, indices, updates)with tf.Session() as sess:  print sess.run(update)

The resulting update to ref would look like this:

[1, 11, 3, 10, 9, 6, 7, 12]

Next are two examples written by me.

Example 2:

>>> ref = tf.Variable(tf.ones([2,3],tf.int32)) >>> updates = tf.constant([[0,0,0]])>>> update = tf.scatter_nd_update(ref,[[0]],updates) >>> init = tf.global_variables_initializer()>>> sess.run(init)>>> sess.run(update)array([[0, 0, 0],   [1, 1, 1]], dtype=int32)

Example 3:

>>> ref = tf.Variable(tf.ones([2,3,3],tf.int32))>>> indices = tf.constant([[0,1]])#>>> updates = tf.constant([0,0,0]) #wrong>>> updates = tf.constant([[0,0,0]])#correct>>> update = tf.scatter_nd_update(ref,indices,updates) >>> init = tf.global_variables_initializer()>>> sess.run(init)>>> print(ref.eval())[[[1 1 1]  [1 1 1]  [1 1 1]] [[1 1 1]  [1 1 1]  [1 1 1]]]>>> sess.run(update)array([[[1, 1, 1],    [0, 0, 0],    [1, 1, 1]],   [[1, 1, 1],    [1, 1, 1],    [1, 1, 1]]], dtype=int32)

Example 4:

>>> updates = tf.constant([0])>>> indices = tf.constant([[1,0,1]])>>> init = tf.global_variables_initializer()>>> sess.run(init)>>> update = tf.scatter_nd_update(ref,indices,updates)>>> sess.run(update)array([[[1, 1, 1],    [1, 1, 1],    [0, 0, 0]],   [[1, 0, 1],    [1, 1, 1],    [1, 1, 1]]], dtype=int32)

转载地址:http://moqbi.baihongyu.com/

你可能感兴趣的文章
一个简单的TabLayout的使用
查看>>
关于let{a}=B出现的解构赋值
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming course_2_assessment_1
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming Tuple Assignment with Unpacking
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
visca接口转RS-232C接口线序
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>