Я хочу реализовать функцию, которая принимает переменную как входную, изменяет некоторые ее строки или столбцы и заменяет их обратно в исходной переменной. Я могу реализовать его для срезов строк, используя tf.gather и tf.scatter_update, но не смог сделать это для срезов столбца, поскольку, видимо, tf.scatter_update обновляет только срезы строк и не имеет функции оси. Я не эксперт в тензорном потоке, поэтому я могу что-то упустить. Может кто-нибудь помочь?
def matrix_reg(t, percent_t, beta):
''' Takes a variable tensor t as input and regularizes some of its rows.
The number of rows to be regularized are specified by the percent_t. Reuturns the original tensor by updating its rows indexed by row_ind.
Arguements:
t -- input tensor
percent_t -- percentage of the total rows
beta -- the regularization factor
Output:
the regularized tensor
'''
row_ind = np.random.choice(int(t.shape[0]), int(percent_t*int(t.shape[0])), replace = False)
t_ = tf.gather(t,row_ind)
t_reg = (1+beta)*t_-beta*(tf.matmul(tf.matmul(t_,tf.transpose(t_)),t_))
return tf.scatter_update(t, row_ind, t_reg)
Ниже приведена небольшая демонстрация того, как обновлять строки или столбцы. Идея состоит в том, что вы указываете индексы строк и столбцов переменных, в которых вы хотите, чтобы каждый элемент в обновлении заканчивался. Это легко сделать с tf.meshgrid
.
import tensorflow as tf
var = tf.get_variable('var', [4, 3], tf.float32, initializer=tf.zeros_initializer())
updates = tf.placeholder(tf.float32, [None, None])
indices = tf.placeholder(tf.int32, [None])
# Update rows
var_update_rows = tf.scatter_update(var, indices, updates)
# Update columns
col_indices_nd = tf.stack(tf.meshgrid(tf.range(tf.shape(var)[0]), indices, indexing='ij'), axis=-1)
var_update_cols = tf.scatter_nd_update(var, col_indices_nd, updates)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print('Rows updated:')
print(sess.run(var_update_rows, feed_dict={updates: [[1, 2, 3], [4, 5, 6]], indices: [3, 1]}))
print('Columns updated:')
print(sess.run(var_update_cols, feed_dict={updates: [[1, 5], [2, 6], [3, 7], [4, 8]], indices: [0, 2]}))
Выход:
Rows updated:
[[0. 0. 0.]
[4. 5. 6.]
[0. 0. 0.]
[1. 2. 3.]]
Columns updated:
[[1. 0. 5.]
[2. 5. 6.]
[3. 0. 7.]
[4. 2. 8.]]