Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在tensorflow张量中插入平均行_Arrays_Numpy_Insert_Tensorflow - Fatal编程技术网

Arrays 在tensorflow张量中插入平均行

Arrays 在tensorflow张量中插入平均行,arrays,numpy,insert,tensorflow,Arrays,Numpy,Insert,Tensorflow,我在numpy中具有以下功能: A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) def insert_row_averages(A, n=2): slice2 = A[n::n] v = (A[n-1::n][:slice2.shape[0]] + slice2)/2.0 np.insert(A.astype(float),n*np.

我在numpy中具有以下功能:

A = np.array([[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9],
          [10, 11, 12]])


def insert_row_averages(A, n=2):
    slice2 = A[n::n]
    v = (A[n-1::n][:slice2.shape[0]] + slice2)/2.0
    np.insert(A.astype(float),n*np.arange(1,v.shape[0]+1),v,axis=0)
它基本上取上面和下面行的平均值,并每隔n个间隔将其插入这两行之间


你知道我怎么用Tensorflow做这个吗?具体来说,我可以使用什么来代替np.insert,因为似乎没有等效的函数。

您可以使用基于初始化的方法来解决它,如下所示-

def insert_row_averages_iniitalization_based(A, n=1):

    # Slice and compute averages   
    slice2 = A[n::n]
    v = (A[n-1::n][:slice2.shape[0]] + slice2)/2.0

    # Compute number of rows for o/p array
    nv = v.shape[0]
    nrows = A.shape[0] + nv

    # Row indices where the v values are the inserted in o/p array
    v_rows = (n+1)*np.arange(nv)+n

    # Initialize o/p array
    out = np.zeros((nrows,A.shape[1]))

    # Insert/assign v in output array
    out[v_rows] = v  # Assign v

    # Create 1D mask of length equal to no. of rows in o/p array, such that its
    # TRUE at places where A is to be assigned and FALSE at places where values
    # from v were assigned in previous step.
    mask = np.ones(nrows,dtype=bool)
    mask[v_rows] = 0

    # Use the mask to assign A.
    out[mask] = A
    return out

这看起来不错,但它仍然使用numpy而不是tensorflow。我的问题是,我似乎真的找不到可以用来翻译这段代码的tensorflow等价物。@Qubix那么,tensorflow不支持数组的初始化?我是说像np.setdiff1d这样的函数可能没有等价物。我现在正在浏览文档。@Qubix被更简单的函数替换。查看编辑!谢谢,我将尝试在tesorflow中实现,如果可行的话,在这里写下答案。