def t6():
"""
success
Returns:
"""
sess = tf.Session()
a = tf.get_variable(dtype=tf.int32, shape=[10, 3], name='item_table')
print(a)
a = a[5,:].assign([100,100,100])
print(a)
sess.run(tf.global_variables_initializer())
print(sess.run(a))
记住tf.assign之后,这个对象就不再是variable了,所以下面两段代码(t7,t8)是错误的
def t7():
"""
error
Returns:
"""
sess = tf.Session()
a = tf.get_variable(dtype=tf.int32, shape=[10, 3], name='item_table')
a[5,:].assign([100,100,100])
sess.run(tf.global_variables_initializer())
print(sess.run(a))
a[6,:].assign([200, 200, 200])
print(sess.run(a))
def t8():
"""
error
Returns:
"""
sess = tf.Session()
a = tf.get_variable(dtype=tf.int32, shape=[10, 3], name='item_table')
a = a[5,:].assign([100,100,100])
a = a[6,:].assign([200, 200, 200])
sess.run(tf.global_variables_initializer())
print(sess.run(a))
def t9():
"""
success
Returns:
"""
sess = tf.Session()
a = tf.get_variable(dtype=tf.int32, shape=[4, 3], name='item_table')
b = tf.placeholder(tf.int32, shape=(None, ))
c = tf.placeholder(tf.int32, shape=(None, 3))
d = tf.scatter_update(a, b, c)
sess.run(tf.global_variables_initializer())
i = 0
while i < 4:
k = sess.run(d, feed_dict={b: [i,i+1], c:[[i, 2, 3], [i+1, 5, 6]]})
i = i + 2
print(k)
print("---------------")
print(sess.run(a))
结果
a.weight.data *= 0
a.weight.data = torch.ones([5])