Как сделать комбинацию матриц в tf.Variable? - PullRequest
0 голосов
/ 09 марта 2020

У меня есть эти 2 матрицы, и мне нужно объединить их так:

enter image description here

это они в коде:

matrix_a = tf.Variable(np.zeros(big_shape, dtype=np.float32))
matrix_b = tf.Variable(np.zeros(small_shape, dtype=np.float32))  

#here I need to combine them

Как я могу это сделать?

############################################# ############### EDITED

Благодаря @jdehesa я написал этот код:

    shape = (batch_size, window_size, window_size, num_channels)

    # the variable we're going to optimize over
    modifier = tf.Variable(np.zeros(shape, dtype=np.float32))

    mask = tf.zeros((batch_size, image_size, image_size, num_channels), tf.float32)
    # Get input shapes
    modifier_shape = tf.shape(modifier)
    mask_shape = tf.shape(mask)
    # Make indices grid
    oo, ii, jj, kk = tf.meshgrid(tf.range(modifier_shape[0]), tf.range(modifier_shape[1]), tf.range(modifier_shape[2], modifier_shape[3]), indexing='ij')
    # Shift indices
    ii += y_window
    jj += x_window
    # Scatter update
    mask_to_apply = tf.tensor_scatter_nd_update(mask, tf.stack([oo, ii, jj, kk], axis=-1), modifier)

бот теперь у меня есть эта ошибка:

ValueError: Requires start <= limit when delta > 0: 28/1 for 'range_2' (op: 'Range') with input shapes: [], [], [] and with computed input tensors: input[0] = <28>, input[1] = <1>, input[2] = <1>.

Почему?

1 Ответ

1 голос
/ 09 марта 2020

Это способ сделать это:

import tensorflow as tf

# Input data (assumes two 2D tensors, `a` at least as big as `b`)
a = tf.zeros((4, 4), tf.int32)
b = tf.ones((2, 2), tf.int32)
# Get input shapes
a_shape = tf.shape(a)
b_shape = tf.shape(b)
# Make indices grid
ii, jj = tf.meshgrid(tf.range(b_shape[0]), tf.range(b_shape[1]), indexing='ij')
# Shift indices
ii += a_shape[0] - b_shape[0]
jj += a_shape[1] - b_shape[1]
# Scatter update
c = tf.tensor_scatter_nd_update(a, tf.stack([ii, jj], axis=-1), b)
tf.print(c)
# [[0 0 0 0]
#  [0 0 0 0]
#  [0 0 1 1]
#  [0 0 1 1]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...