GVKun编程网logo

以行方式使用 numpy matmul 和广播

1

本文将带您了解关于以行方式使用numpymatmul和广播的新内容,另外,我们还将为您提供关于einsum和matmul、InvalidArgumentError:无法计算MatMul,因为输入#0(

本文将带您了解关于以行方式使用 numpy matmul 和广播的新内容,另外,我们还将为您提供关于einsum 和 matmul、InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]、matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配、matmul:输入操作数 1 不匹配的实用信息。

本文目录一览:

以行方式使用 numpy matmul 和广播

以行方式使用 numpy matmul 和广播

如何解决以行方式使用 numpy matmul 和广播

我有一个 3D 点 (n,3) 数组,它们将使用以 nx3x3 数组形式存储的 3x3rotation 矩阵绕原点旋转。

目前我只是在使用 matmul 的 for 循环中执行此操作,但我认为这是毫无意义的,因为必须有更快的广播方式来执行此操作。

当前代码

n = 10
points = np.random.random([10,3])
rotation_matrices = np.tile(np.random.random([3,3]),(n,1,1))

result = []

for point in range(len(points)):
    rotated_point = np.matmul(rotation_matrices[point],points[point])

    result.append(rotated_point)

result = np.asarray(result)

注意:在这个例子中,我只是平铺了相同的旋转矩阵,但在我的真实情况下,每个 3x3 旋转矩阵都是不同的。

我想做什么

我猜一定有某种广播方式,因为当点云变得非常大时,for 循环会变得非常缓慢。我想这样做:

np.matmul(rotation_matrices,points)

其中 row 中的每个 points 乘以其相应的旋转矩阵。可能有一种方法可以使用 np.einsum 执行此操作,但我无法弄清楚签名。

解决方法

如果您看到 https://ghostbin.co/paste/s2qdw,则 np.einsum(''ij,jk'',a,b)matmul 的签名。

所以你可以试试带有签名的np.einsum

np.einsum(''kij,kj->ki'',rotation_matrices,points)

测试

einsum = np.einsum(''kij,points)
manual = np.array([np.matmul(x,y) for x,y in zip (rotation_matrices,points)])
np.allclose(einsum,manual)
# True

einsum 和 matmul

einsum 和 matmul

如何解决einsum 和 matmul

相关问题BLAS with symmetry in higher order tensor in Fortran

我尝试使用 python 代码来利用张量收缩中的对称性, A[a,b] B[b,c,d] = C[a,d] B[b,d] = B[b,d,c] 因此 C[a,c]。 (假设爱因斯坦求和约定,即重复的 b 表示对其求和)

通过以下代码

  1. import numpy as np
  2. import time
  3. # A[a,b] * B[b,d]
  4. na = nb = nc = nd = 100
  5. A = np.random.random((na,nb))
  6. B = np.random.random((nb,nc,nd))
  7. C = np.zeros((na,nd))
  8. C2= np.zeros((na,nd))
  9. C3= np.zeros((na,nd))
  10. # symmetrize B
  11. for c in range(nc):
  12. for d in range(c):
  13. B[:,d] = B[:,c]
  14. start_time = time.time()
  15. C2 = np.einsum(''ab,bcd->acd'',A,B)
  16. finish_time = time.time()
  17. print(''time einsum'',finish_time - start_time )
  18. start_time = time.time()
  19. for c in range(nc):
  20. # c+1 is needed,since range(0) will be skipped
  21. for d in range(c+1):
  22. #C3[:,d] = np.einsum(''ab,b->a'',A[:,:],B[:,d] )
  23. C3[:,d] = np.matmul(A[:,d] )
  24. for c in range(nc):
  25. for d in range(c+1,nd):
  26. C3[:,d] = C3[:,c]
  27. finish_time = time.time()
  28. print( ''time partial einsum'',finish_time - start_time )
  29. for a in range(int(na/10)):
  30. for c in range(int(nc/10)):
  31. for d in range(int(nd/10)):
  32. if abs((C3-C2)[a,d])> 1.0e-12:
  33. print(''warning'',a,(C3-C2)[a,d])

在我看来 np.matmulnp.einsum 快,例如,通过使用 np.matmul,我得到了

  1. time einsum 0.07406115531921387
  2. time partial einsum 0.0553278923034668

通过使用 np.einsum,我得到了

  1. time einsum 0.0751657485961914
  2. time partial einsum 0.11624622344970703

上面的性能差异是不是一般?我经常认为 einsum 是理所当然的。

解决方法

作为一般规则,我希望 matmul 更快,但在更简单的情况下,einsum 似乎实际上使用了 matmul

但这里是我的时间

  1. In [20]: C2 = np.einsum(''ab,bcd->acd'',A,B)
  2. In [21]: timeit C2 = np.einsum(''ab,B)
  3. 126 ms ± 1.3 ms per loop (mean ± std. dev. of 7 runs,10 loops each)

你的对称性尝试einsum

  1. In [22]: %%timeit
  2. ...: for c in range(nc):
  3. ...: # c+1 is needed,since range(0) will be skipped
  4. ...: for d in range(c+1):
  5. ...: C3[:,c,d] = np.einsum(''ab,b->a'',A[:,:],B[:,d] )
  6. ...: #C3[:,d] = np.matmul(A[:,d] )
  7. ...:
  8. ...: for c in range(nc):
  9. ...: for d in range(c+1,nd):
  10. ...: C3[:,d] = C3[:,d,c]
  11. ...:
  12. 128 ms ± 3.39 ms per loop (mean ± std. dev. of 7 runs,10 loops each)

matmul 相同:

  1. In [23]: %%timeit
  2. ...: for c in range(nc):
  3. ...: # c+1 is needed,since range(0) will be skipped
  4. ...: for d in range(c+1):
  5. ...: #C3[:,d] )
  6. ...: C3[:,c]
  7. ...:
  8. 81.3 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs,10 loops each)

直接matmul

  1. In [24]: C4 = np.matmul(A,B.reshape(100,-1)).reshape(100,100,100)
  2. In [25]: np.allclose(C2,C4)
  3. Out[25]: True
  4. In [26]: timeit C4 = np.matmul(A,100)
  5. 14.9 ms ± 167 µs per loop (mean ± std. dev. of 7 runs,100 loops each)

einsum 也有一个 optimize 标志。我认为只有 3 个或更多参数才重要,但它似乎在这里有帮助:

  1. In [27]: timeit C2 = np.einsum(''ab,B,optimize=True)
  2. 20.3 ms ± 688 µs per loop (mean ± std. dev. of 7 runs,10 loops each)

有时当数组非常大时,某些迭代会更快,因为它降低了内存管理的复杂性。但是我认为在尝试利用对称性时不值得。其他 SO 表明,在某些情况下 matmul 可以检测对称性,并使用自定义 BLAS 调用,但我认为这里不是这种情况(它无法检测 {{1}没有昂贵的比较。)

InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]

InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]

有人可以解释一下,TensorFlow的急切模式如何工作?我正在尝试建立一个简单的回归,如下所示:

import tensorflow as tftfe = tf.contrib.eagertf.enable_eager_execution()import numpy as npdef make_model():    net = tf.keras.Sequential()    net.add(tf.keras.layers.Dense(4, activation=''relu''))    net.add(tf.keras.layers.Dense(1))    return netdef compute_loss(pred, actual):    return tf.reduce_mean(tf.square(tf.subtract(pred, actual)))def compute_gradient(model, pred, actual):    """compute gradients with given noise and input"""    with tf.GradientTape() as tape:        loss = compute_loss(pred, actual)    grads = tape.gradient(loss, model.variables)    return grads, lossdef apply_gradients(optimizer, grads, model_vars):    optimizer.apply_gradients(zip(grads, model_vars))model = make_model()optimizer = tf.train.AdamOptimizer(1e-4)x = np.linspace(0,1,1000)y = x+np.random.normal(0,0.3,1000)y = y.astype(''float32'')train_dataset = tf.data.Dataset.from_tensor_slices((y.reshape(-1,1)))epochs = 2# 10batch_size = 25itr = y.shape[0] // batch_sizefor epoch in range(epochs):    for data in tf.contrib.eager.Iterator(train_dataset.batch(25)):        preds = model(data)        grads, loss = compute_gradient(model, preds, data)        print(grads)        apply_gradients(optimizer, grads, model.variables)#         with tf.GradientTape() as tape:#             loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(preds, data))))#         grads = tape.gradient(loss, model.variables)#         print(grads)#         optimizer.apply_gradients(zip(grads, model.variables),global_step=None)

Gradient output: [None, None, None, None, None, None] 错误如下:

----------------------------------------------------------------------ValueError                           Traceback (most recent call last)<ipython-input-3-a589b9123c80> in <module>     35         grads, loss = compute_gradient(model, preds, data)     36         print(grads)---> 37         apply_gradients(optimizer, grads, model.variables)     38 #         with tf.GradientTape() as tape:     39 #             loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(preds, data))))<ipython-input-3-a589b9123c80> in apply_gradients(optimizer, grads, model_vars)     17      18 def apply_gradients(optimizer, grads, model_vars):---> 19     optimizer.apply_gradients(zip(grads, model_vars))     20      21 model = make_model()~/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/optimizer.py in apply_gradients(self, grads_and_vars, global_step, name)    589     if not var_list:    590       raise ValueError("No gradients provided for any variable: %s." %--> 591                        ([str(v) for _, v, _ in converted_grads_and_vars],))    592     with ops.init_scope():    593       self._create_slots(var_list)ValueError: No gradients provided for any variable:

编辑

我更新了我的代码。现在,问题出在梯度计算上,它返回零。我已经检查了非零的损失值。

答案1

小编典典

第1部分:
问题确实是您输入的数据类型。默认情况下,您的keras模型期望float32,但您传递的是float64。您可以更改模型的dtype或将输入更改为float32。

更改模型:

def make_model():    net = tf.keras.Sequential()    net.add(tf.keras.layers.Dense(4, activation=''relu'', dtype=''float32''))    net.add(tf.keras.layers.Dense(4, activation=''relu''))    net.add(tf.keras.layers.Dense(1))    return net

更改输入: y = y.astype(''float32'')

第2部分:
您需要model(data)在tf.GradientTape()下调用用于计算模型的函数(即)。例如,您可以compute_loss使用以下方法替换您的方法:

def compute_loss(model, x, y):    pred = model(x)    return tf.reduce_mean(tf.square(tf.subtract(pred, y)))

matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配

matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配

如何解决matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配

我是 AI 和 tensorflow.js 的完全初学者。目前正在学习 Stephen Grider 的机器学习课程。我应该在下面的代码之后得到一个输出,但我得到了错误。请帮忙:

代码: 线性回归.js:

  1. const tf = require(''@tensorflow/tfjs'');
  2. class LinearRegression {
  3. constructor(features,labels,options) {
  4. this.features = tf.tensor(features);
  5. this.labels = tf.tensor(labels);
  6. this.features = tf.ones([this.features.shape[0],1]).concat(this.features) //generates the column of one for the horse power
  7. this.options = Object.assign(
  8. { learningRate: 0.1,iterations: 1000 },options
  9. ); //default value is 0.1,if the learning rate is provided,the value is overrided... iteration no. of times gradient decent runs
  10. this.weights = tf.zeros([2,1]); // intial tensor of both m and b are zeros
  11. }
  12. gradientDescent() {
  13. const currentGuesses = this.features.matMul(this.weights); //matMul is matrix multiplication which is features * weights
  14. const differences = currentGuesses.sub(this.labels); //(features * weights) - labels
  15. const slopes = this.features
  16. .transpose()
  17. .matMul(differences)
  18. .div(features.shape[0]); // slope of MSE with respect to both m and b. features * ((features * weights) - labels) / total no. of features.
  19. this.weights = this.weights.sub(slopes.mul(this.options.learningRate));
  20. }
  21. train() {
  22. for (let i=0; i < this.options.iterations; i++) {
  23. this.gradientDescent();
  24. }
  25. /*test(testFeatures,testLabels) {
  26. testFeatures = tf.tensor(testFeatures);
  27. testLabels = tf.tensor(testLabels);
  28. } */
  29. }
  30. }
  31. module.exports = LinearRegression;

index.js:

  1. require(''@tensorflow/tfjs-node'');
  2. const tf = require(''@tensorflow/tfjs'');
  3. const loadCSV = require(''./load-csv'');
  4. const LinearRegression = require(''./linear-regression'');
  5. let { features,testFeatures,testLabels } =loadCSV(''./cars.csv'',{
  6. shuffle: true,splitTest: 50,dataColumns: [''horsepower''],labelColumns: [''mpg'']
  7. });
  8. const regression = new LinearRegression(features,{
  9. learningRate: 0.002,iterations: 100
  10. });
  11. regression.train();
  12. console.log(
  13. ''Updated M is:'',regression.weights.get(1,0),''Updated B is:'',regression.weights.get(0,0)
  14. );

错误:

  1. D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\ops\\operation.js:32
  2. throw ex;
  3. ^
  4. Error: Error in matMul: inner shapes (1) and (2) of Tensors with shapes 684,1 and 2,1 and transposeA=false and transposeB=false must match.
  5. at Object.assert (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\util.js:36:15)
  6. at matMul_ (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\ops\\matmul.js:25:10)
  7. at Object.matMul (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\ops\\operation.js:23:29)
  8. at Tensor.matMul (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\tensor.js:315:26)
  9. at LinearRegression.gradientDescent (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\linear-regression.js:19:46)
  10. at LinearRegression.train (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\linear-regression.js:34:18)
  11. at Object.<anonymous> (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\index.js:18:12)
  12. at Module._compile (internal/modules/cjs/loader.js:1063:30)
  13. at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
  14. at Module.load (internal/modules/cjs/loader.js:928:32)

解决方法

错误是由

抛出的

this.features.matMul(this.weights)

形状this.features[684,1] 和形状this.weights[2,1] 之间存在矩阵乘法。为了能够将矩阵 A(形状 [a,b])与 B(形状 [c,d])相乘,bc 应该匹配,但此处并非如此。

要解决这里的问题,this.weights 应该转置

  1. this.features.matMul(this.weights,false,true)

matmul:输入操作数 1 不匹配

matmul:输入操作数 1 不匹配

如何解决matmul:输入操作数 1 不匹配

显示:

matmul:输入操作数 1 在其核心维度 0 中存在不匹配,其中 gufunc 签名 (n?,k),(k,m?)->(n?,m?)(大小 5 不同于 1

当我跑步时:

  1. pre = lm.predict(y_test)

请建议做什么

今天的关于以行方式使用 numpy matmul 和广播的分享已经结束,谢谢您的关注,如果想了解更多关于einsum 和 matmul、InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]、matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配、matmul:输入操作数 1 不匹配的相关知识,请在本站进行查询。

本文标签: