MrBor
March 16, 2023, 9:45am
#1
import taichi as ti
import numpy as np
ti.init(arch=ti.cpu)
all_model = np.random.random((100000,50)).astype(np.float32)
all_t = ti.Matrix.field(n=10,m=5,dtype =ti.f32, shape=())
all_t.from_numpy(all_model)
id = np.random.random(50).astype(np.float32)
id_t = ti.Vector.field(5,ti.f32,shape=())
id_t.from_numpy(id)
mesh2 = ti.field(dtype =ti.f32, shape=())
@ti.kernel
def compute():
mesh2= all_t[None] @ id_t[None]
compute()
print('mesh2:',mesh2)
#output: mesh2: 0.0
按理来说mesh2不应该为0把?请问为什么呢?
YuPeng
March 17, 2023, 2:15am
#3
mesh2是一个scalar field,所以赋值的时候可以用 mesh2[None] = all_t[Nonoe] @ id_t[None]
。然后输出的话:print(mesh2[None])
试一下?
MrBor
March 17, 2023, 2:35am
#4
环境:[Taichi] version 1.4.1, llvm 15.0.1, commit e67c674e, win, python 3.8.12
尝试更改赋值代码:
@ti.kernel
def compute():
mesh2[None] = all_t[None] @ id_t[None] #更改
报错:Field with dim 2 accessed with indices of dim 0
如果将mesh2换种方式定义:
mesh2 = ti.Matrix.field(n=10,m=5,dtype =ti.f32, shape=())#方式1
mesh2 = ti.Vector.field(5,dtype =ti.f32, shape=())#方式2
# mesh2 = ti.field(dtype =ti.f32, shape=(1000,50))
@ti.kernel
def compute(): #a:ti.types.ndarray(),b:ti.types.ndarray()
mesh2[None] = all_t[None] @ id_t[None]
都会红色报错:
[E 03/17/23 10:30:43.104 118484] [scalarize.cpp:taichi::lang::Scalarize::scalarize_store_stmt@51] Assertion failure: dest_tensor_type->get_shape() == val_tensor_type->get_shape()
[E 03/17/23 10:30:43.104 118484] [ir.cpp:taichi::lang::DelayedIRModifier::~DelayedIRModifier@408] Assertion failure: to_insert_before_.empty()
如果单独输出mesh2
mesh2 = ti.field(dtype =ti.f32, shape=(1000,50))
print('mesh2:',mesh2[None])
报错:AssertionError: Slicing is not supported on ti.field
1 Like
YuPeng
March 17, 2023, 2:41am
#5
Hi @MrBor , 我看了下你的代码。发现你kernel内计算的 field之间的dimension是不匹配的。
import taichi as ti
import numpy as np
ti.init(arch=ti.cpu)
all_model = np.random.random((10, 5)).astype(np.float32)
all_t = ti.Matrix.field(n=10,m=5,dtype =ti.f32, shape=())
all_t.from_numpy(all_model)
id = np.random.random((5, 10)).astype(np.float32)
id_t = ti.Vector.field(5,ti.f32,shape=(10))
id_t.from_numpy(id)
mesh2 = ti.Vector.field(10, dtype =ti.f32, shape=())
@ti.kernel
def compute():
# all_t[None] is a 10x5 matrix, id_t[0] is a 5 x 1 vector , mesh2 should be a 10x1 vector
mesh2[None]= all_t[None] @ id_t[0]
compute()
print('mesh2:',mesh2[None])
第三个问题中如果你定义 mesh2 = ti.field(dtype =ti.f32, shape=(1000,50))
, 那么mesh2就不是一个scalar field, 就不能用 mesh2[None]
, 而是应该用 mesh2[i, j]
。
MrBor
March 17, 2023, 2:45am
#6
感谢回复,
我复制了你的代码,但是无法正确运行
Traceback (most recent call last):
File "test/test_mul.py", line 30, in <module>
id_t.from_numpy(id)
File "F:\anaconda\envs\opencvandpytorch\lib\site-packages\taichi\lang\util.py", line 310, in wrapped
return func(*args, **kwargs)
File "F:\anaconda\envs\opencvandpytorch\lib\site-packages\taichi\lang\matrix.py", line 1376, in from_numpy
self._from_external_arr(arr)
File "F:\anaconda\envs\opencvandpytorch\lib\site-packages\taichi\lang\util.py", line 310, in wrapped
return func(*args, **kwargs)
File "F:\anaconda\envs\opencvandpytorch\lib\site-packages\taichi\lang\matrix.py", line 1356, in _from_external_arr
assert len(arr.shape) == len(self.shape) + 2
AssertionError
当我尝试将id_t = ti.Vector.field(5,ti.f32,shape=(10))的10去掉后
代码将会运行到mesh2[None]= all_t[None] @ id_t[0]
并报错:
Traceback (most recent call last):
File "test/test_mul.py", line 39, in <module>
compute()
File "F:\anaconda\envs\opencvandpytorch\lib\site-packages\taichi\lang\kernel_impl.py", line 976, in wrapped
raise type(e)('\n' + str(e)) from None
taichi.lang.exception.TaichiIndexError:
File "test/test_mul.py", line 37, in compute:
mesh2[None]= all_t[None] @ id_t[0]
^^^^^^^
Field with dim 0 accessed with indices of dim 1
MrBor
March 17, 2023, 3:04am
#8
感谢指点
我再次修改了代码,将其与numpy对齐:
import taichi as ti
import numpy as np
ti.init(arch=ti.cpu)
all_model = np.random.random((10, 5)).astype(np.float32)
all_t = ti.Matrix.field(n=10,m=5,dtype =ti.f32, shape=())
all_t.from_numpy(all_model)
id = np.random.random((5, 1)).astype(np.float32)
id_t = ti.Vector.field(5,ti.f32,shape=())#这里的shape为空才能运行
id_t.from_numpy(id)
mesh2 = ti.Vector.field(10, dtype =ti.f32, shape=())
@ti.kernel
def compute():
# all_t[None] is a 10x5 matrix, id_t[0] is a 5 x 1 vector , mesh2 should be a 10x1 vector
mesh2[None]= all_t[None] @ id_t[None]
compute()
print('mesh1:',all_model.dot(id))
print('mesh2:',mesh2)
再次感谢,我还需要多研究一下