LoginSignup
0
1

More than 3 years have passed since last update.

【pytorch】numpyからtensor

Posted at

pytorchでnumpyからtensorに変える方法を書く。

numpyからtensor

torch.from_numpy(ndarray)をつかう。
ndarrayにはnumpyの行列が入る。

>>>import numpy
>>>import torch
>>> a = numpy.array([0, 1, 2])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 0, 1,  2])
>>> t[0] = 1
>>> a
array([ 1,  1,  2])

上の例から、arrayをtensorに変えることができていることが分かる。
また、aのarrayとtのtensorはメモリを共有していることが分かる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1