【问题标题】:Creating a tensor where each entry is a function of its index创建一个张量,其中每个条目都是其索引的函数
【发布时间】:2021-07-12 10:44:04
【问题描述】:

我想创建一个由D[i,j]=d(i-j) 定义的矩阵D,其中d 是我可以选择的任意函数。

使用循环可以轻松完成,但速度很慢。有没有用torch或numpy创建这个矩阵的有效方法?

【问题讨论】:

  • 肯定有办法,但是如果没有更多的标准,就很难提供帮助。您是否正在考虑特定的跳跃功能?
  • 以 i-j 为例

标签: python indexing pytorch tensor processing-efficiency


【解决方案1】:

您可以将函数(如果矢量化)应用于numpy.indices:

import numpy as np

i, j = np.indices((n, m))
D = d(i - j)

【讨论】:

    【解决方案2】:

    以下代码向您展示了如何为您的问题配置基于张量的计算过程:

    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras import layers
    import numpy as np
    import time
    
    #let's define heigth and width of D:
    height=45
    width=77
    
    #Let's configure inputs for neural network having input shape similar with D but also extra dimension of size 2
    syote=keras.Input(shape=(height,width,2))
    
    #Let's make next layer for the network...
    valikerros=layers.Dense(1)
    
    #And attach input to this layer...
    x=valikerros(syote)
    x=layers.Dense(1)(x)
    x=layers.Dense(1)(x)
    
    #...and select so many layers you need...according to complexity of the function d, more layers can easily be added...
    
    #Let's make the neural network...
    matriisimalli=keras.Model(inputs=syote,outputs=x,name="Special neural network model presenting D including function d")
    
    #And show its strutuce
    matriisimalli.summary()
    
    #next let's create ONCE the i,j -matrix index basis for the input, where there is in each i,j coordinate the index values of those coordinates...this need to be done once only, and can also be saved as a variable and be lodaded, if it is essential to avoid usage of for-loops
    pohjasyote=np.ones((1,height,width,2))
    
    for korkeus in range(height):
        for leveys in range(width):
            pohjasyote[0,korkeus,leveys,0]=korkeus
            pohjasyote[0,korkeus,leveys,1]=leveys
    
    #Now let's see how long time it takes to calculate the result for D:
    
    alkuaika=time.time()
    result_including_information_of_D=matriisimalli.predict(pohjasyote)
    loppuaika=time.time()
    print("It took ",loppuaika-alkuaika, " seconds to calculate D")
    
    #...and to use the created (rapid tensor-based) structure for calculation let's next train the network...
    #using the standard protocol ... where you train the network first to predict d accurately... then verify it works OK ...
    #after that simply use it...
    
    #alternative for the training is you arithmetically deduce the correct values for the weight tensors of the model (accurate results..)
    

    ...当然请注意,这是一种利用 keras 中张量优势的“技巧”,但是通过遵循代码中的想法,我认为您可以找到一种直接的方法来 为您的问题找到解决方案。

    如果您在计算中发现难以遵循这个想法(抱歉评论不佳),那么首先在计算中使用您的 D 大小来测试代码,并比较这个速度是否比您当前基于 for 循环的解决方案更好.如果“matriisimalli”更好,那么值得仔细阅读代码并利用其思想来达到更好的性能。

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多