tum-pbs / PhiFlow

A differentiable PDE solving framework for machine learning
MIT License
1.47k stars 192 forks source link

Manual PyTorch Implementation #21

Closed oguzziya closed 4 years ago

oguzziya commented 4 years ago

Currently I am trying to adapt manual_numpy_or_tf.py demo to PyTorch, with some additional changes. However, I could not make it work with PyTorch at all. The simplified steps are:

1- Create Fluid object, which will consist of numpy.ndarray by default.

FLOW = Fluid(Domain([RES] * DIM, boundaries=OPEN), batch_size=BATCH_SIZE, buoyancy_factor=0.2)

2- Copy this object to PyTorch

FLOW_TORCH = torch_from_numpy(FLOW)

3- (After some unrelated operations) Sample velocity at points of density

x_rho = DENSITY.points.data
v_rho = VELOCITY.sample_at(x_rho)

Problems occur at this stage. First of all, type of x_rho is still numpy.ndarray, not torch.Tensor. Secondly, sample_at operation throws the error:

 File "/Users/oguzziya/miniconda3/envs/phiflow/lib/python3.6/site-packages/phi/torch/torch_backend.py", line 381, in cast
    dtype = {np.float16: torch.float16, np.float32: torch.float32, np.float64: torch.float64, np.bool: torch.bool, np.int8: torch.int8, np.int16: torch.int16, np.int32: torch.int32, np.int64: torch.int64}[dtype]
KeyError: dtype('float32')

I would be glad to hear any opinions, possible fixes, or steps that I overlook.

Thank you

holl- commented 4 years ago

Hi oguzziya, I'll look into it. Could you attach the full script you're running?

oguzziya commented 4 years ago

I attach the files. Apparently, github does not support .py format, therefore I send them with .txt postfix manual_torch.txt utils.txt

oguzziya commented 4 years ago

Or if it is easier, here is link to my fork and corresponding branch: oguzziya/PhiFlow

holl- commented 4 years ago

Thanks. I have pushed a fix to the 1.5 branch. You will still get TypeErrors from combining Numpy arrays with PyTorch tensors. I've added additional torch_from_numpy calls to fix it. This should work: manual_torch.py.txt