我在两个一维数组 (a,b) 上使用了 TensorFlow.js 框架中的 outerProduct 函数,但我发现很难以常规 javascript 格式获取结果张量的值。
即使在使用 .dataSync 和 Array.from() 之后,我仍然无法获得预期的输出格式。两个一维数组之间的结果外积应该给出一个二维数组,但我得到的是一维数组。
const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
const tensor = tf.outerProduct(b, a);
const values = tensor.dataSync();
const array1 = Array.from(values);
Run Code Online (Sandbox Code Playgroud)
控制台日志(数组1);
预期结果是 array1 = [ [ 3, 6 ] , [ 4, 8 ] ],但我得到 array1 = [ 3, 6, 4, 8 ]
在数组广播方面相当于 x1 * x2。
是np.multiply(x1, x2)
不同的,以x1 * x2
在任何情况下?
我在哪里可以找到每个的实现?
注意:除法存在一个类似的问题,但它没有提到乘法,也没有暗示乘法情况下的答案是相同的。
这个问题还要求提供特定于乘法的实现细节。
python arrays numpy matrix-multiplication vector-multiplication
v1 <- c(1,2)
v2 <- c(3,4,5,6)
Run Code Online (Sandbox Code Playgroud)
有没有办法将这两个向量相乘,使结果是向量暗(1,3)
,如(11,14,17)
这类似于所有可能的暗淡(1,2)乘法组合,例如
(1,2) %x% t(3,4)
,(1,2) %x% t(4,5)
,(1,2) %x% t(5,6)
看起来很简单,看起来没有运气.
我需要编写矩阵向量和矩阵乘法函数,但我无法绕过SSE命令.
矩阵和向量的维数总是4的倍数.
我设法编写了矢量矢量乘法函数,如下所示:
void vector_multiplication_SSE(float* m, float* n, float* result, unsigned const int size)
{
int i;
__declspec(align(16))__m128 *p_m = (__m128*)m;
__declspec(align(16))__m128 *p_n = (__m128*)n;
__declspec(align(16))__m128 *p_result = (__m128*)result;
for (i = 0; i < size / 4; ++i)
p_result[i] = _mm_mul_ps(p_m[i], p_n[i]);
// print the result
for (int i = 0; i < size; ++i)
{
if (i % 4 == 0) cout << endl;
cout << result[i] << '\t';
}
}
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试实现矩阵向量乘法.
这是我到目前为止所拥有的:
void multiply_matrix_by_vector_SSE(float* m, float* v, …
Run Code Online (Sandbox Code Playgroud) c++ sse intrinsics matrix-multiplication vector-multiplication
我决定潜入朱莉娅,撞墙。快速。
我正在尝试复制一个简单的操作,该操作在python numpy中如下
a = numpy.array([1,2,3])
b = numpy.array([1,2,3])
a*b
[output]: [1,4,9]
Run Code Online (Sandbox Code Playgroud)
换句话说,“ [1,4,9]”是我期望的输出。
我在Julia中尝试了以下方法:
a = [1,2,3]
b = [1,2,3]
a*b
[output]: MethodError: no method matching *(::Array{Int64,1}, ::Array{Int64,1})
Run Code Online (Sandbox Code Playgroud)
或尝试明智之后:
a = [1,2,3]
b = [1,2,3]'
a*b
[output]: 3×3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
Run Code Online (Sandbox Code Playgroud)
我知道这似乎是一个基本问题,但我的Google搜寻似乎不是我今天最好的,和/或stackoverflow可以使用此问题和答案;)
感谢您的帮助和指点!
最好
对于以下两个n
元素整数向量乘法的替代方法,可以更快(在代码所需的时间方面):
{
// code for obtaining two n element int vectors, a and b
}
int temp = 0; // a temporary variable
for (int ii = 0; ii < n; ++ii)
temp += a[ii]*b[ii];
Run Code Online (Sandbox Code Playgroud)
编辑:收到几个不错的想法.我必须检查每一个,看看哪一个是最好的.当然,每个回答都告诉我一些新的东西.
arrays ×3
c++ ×2
intrinsics ×1
javascript ×1
julia ×1
numpy ×1
python ×1
r ×1
sse ×1
vector ×1