Я использую приведенный ниже код, и он сообщает об ошибке. Это очень простой пример, и он не должен сообщать об ошибке, как ожидалось.
import numpy as np
import torch as th
# Assuming tt is some data (example as list)
tt = [1, 2, 3, 4, 5] # Example data
# Check if tt is a NumPy array, and convert if necessary
if not isinstance(tt, np.ndarray):
tt = np.array(tt)
# Now, convert tt to a PyTorch tensor
tensor_tt = th.from_numpy(tt)
print(tensor_tt)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[26], line 12
9 tt = np.array(tt)
11 # Now, convert tt to a PyTorch tensor
---> 12 tensor_tt = th.from_numpy(tt)
13 print(tensor_tt)
TypeError: e x p e c t e d n p . n d a r r a y ( g o t n u m p y . n d a r r a y ) < b r / > < / к о д > < b r / > Я и с п о л ь з у ю с л е д у ю щ у ю с р е д у c o n d a : < / p > < b r / > < c o d e > c o n d a l i s t < b r / > # p a c k a g e s i n e n v i r o n m e n t a t / o p t / m i n i c o n d a 3 / e n v s / e t h o s : < b r / > # < b r / > # N a m e V e r s i o n B u i l d C h a n n e l < b r / > a p p n o p e 0 . 1 . 4 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > a s t t o k e n s 2 . 4 . 1 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > b l a s 1 . 0 o p e n b l a s < b r / > b o t t l e n e c k 1 . 3 . 7 p y 3 1 2 h a 8 6 b 8 6 1 _ 0 < b r / > b r o t l i 1 . 0 . 9 h 8 0 9 8 7 f 9 _ 8 < b r / > b r o t l i - b i n 1 . 0 . 9 h 8 0 9 8 7 f 9 _ 8 < b r / > b z i p 2 1 . 0 . 8 h 8 0 9 8 7 f 9 _ 6 < b r / > c a - c e r t i f i c a t e s 2 0 2 4 . 9 . 2 4 h c a 0 3 d a 5 _ 0 < b r / > c l i c k 8 . 1 . 7 p y p i _ 0 p y p i < b r / > c o l o r l o g 6 . 8 . 2 p y p i _ 0 p y p i < b r / > c o m m 0 . 2 . 2 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > c o n t o u r p y 1 . 2 . 0 p y 3 1 2 h 4 8 c a 7 d 4 _ 0 < b r / > c y c l e r 0 . 1 1 . 0 p y h d 3 e b 1 b 0 _ 0 < b r / > d e b u g p y 1 . 6 . 7 p y 3 1 2 h 3 1 3 b e b 8 _ 0 < b r / > d e c o r a t o r 5 . 1 . 1 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > e t h o s 0 . 1 . 0 p y p i _ 0 p y p i < b r / > e x c e p t i o n g r o u p 1 . 2 . 2 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > e x e c u t i n g 2 . 1 . 0 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > e x p a t 2 . 6 . 3 h 3 1 3 b e b 8 _ 0 < b r / > f i l e l o c k 3 . 1 6 . 1 p y p i _ 0 p y p i < b r / > f o n t t o o l s 4 . 5 1 . 0 p y 3 1 2 h 8 0 9 8 7 f 9 _ 0 < b r / > f r e e t y p e 2 . 1 2 . 1 h 1 1 9 2 e 4 5 _ 0 < b r / > f s s p e c 2 0 2 4 . 9 . 0 p y p i _ 0 p y p i < b r / > h 5 p y 3 . 1 2 . 1 p y p i _ 0 p y p i < b r / > i m p o r t l i b - m e t a d a t a 8 . 5 . 0 p y h a 7 7 0 c 7 2 _ 0 c o n d a - f o r g e < b r / > i p y k e r n e l 6 . 2 9 . 5 p y h 5 7 c e 5 2 8 _ 0 c o n d a - f o r g e < b r / > i p y t h o n 8 . 2 8 . 0 p y h 7 0 7 e 7 2 5 _ 0 c o n d a - f o r g e < b r / > j e d i 0 . 1 9 . 1 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > j i n j a 2 3 . 1 . 4 p y p i _ 0 p y p i < b r / > j o b l i b 1 . 4 . 2 p y p i _ 0 p y p i < b r / > j p e g 9 e h 8 0 9 8 7 f 9 _ 3 < b r / > j u p y t e r _ c l i e n t 8 . 6 . 3 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > j u p y t e r _ c o r e 5 . 7 . 2 p y h 3 1 0 1 1 f e _ 1 c o n d a - f o r g e < b r / > k i w i s o l v e r 1 . 4 . 4 p y 3 1 2 h 3 1 3 b e b 8 _ 0 < b r / > l c m s 2 2.12 hba8e193_0
lerc 3.0 hc377ac9_0
libbrotlicommon 1.0.9 h80987f9_8
libbrotlidec 1.0.9 h80987f9_8
libbrotlienc 1.0.9 h80987f9_8
libcxx 14.0.6 h848a8c0_0
libdeflate 1.17 h80987f9_1
libffi 3.4.4 hca03da5_1
libgfortran 5.0.0 11_3_0_hca03da5_28
libgfortran5 11.3.0 h009349e_28
libopenblas 0.3.21 h269037a_0
libpng 1.6.39 h80987f9_0
libsodium 1.0.18 h27ca646_1 conda-forge
libtiff 4.5.1 h313beb8_0
libwebp-base 1.3.2 h80987f9_0
llvm-openmp 14.0.6 hc6e5704_0
lz4-c 1.9.4 h313beb8_1
markupsafe 2.1.5 pypi_0 pypi
matplotlib-base 3.9.2 py312h2df2da3_0
matplotlib-inline 0.1.7 pyhd8ed1ab_0 conda-forge
mpmath 1.3.0 pypi_0 pypi
ncurses 6.4 h313beb8_0
nest-asyncio 1.6.0 pyhd8ed1ab_0 conda-forge
networkx 3.3 pypi_0 pypi
numexpr 2.8.7 py312h0f3ea24_0
numpy 2.1.2 pypi_0 pypi
numpy-base 1.26.4 py312he047099_0
openjpeg 2.5.2 h54b8e55_0
openssl 3.3.2 h8359307_0 conda-forge
packaging 24.1 pyhd8ed1ab_0 conda-forge
pandas 2.2.3 pypi_0 pypi
parso 0.8.4 pyhd8ed1ab_0 conda-forge
pexpect 4.9.0 pyhd8ed1ab_0 conda-forge
pickleshare 0.7.5 py_1003 conda-forge
pillow 10.4.0 py312h80987f9_0
pip 24.2 py312hca03da5_0
platformdirs 4.3.6 pyhd8ed1ab_0 conda-forge
prompt-toolkit 3.0.48 pyha770c72_0 conda-forge
psutil 5.9.0 py312h80987f9_0
ptyprocess 0.7.0 pyhd3deb0d_0 conda-forge
pure_eval 0.2.3 pyhd8ed1ab_0 conda-forge
pyarrow 17.0.0 pypi_0 pypi
pygments 2.18.0 pyhd8ed1ab_0 conda-forge
pyparsing 3.1.2 py312hca03da5_0
python 3.12.7 h99e199e_0
python-dateutil 2.9.0.post0 pypi_0 pypi
python-tzdata 2023.3 pyhd3eb1b0_0
pytz 2024.2 pypi_0 pypi
pyzmq 25.1.2 py312h313beb8_0
readline 8.2 h1a28f6b_0
seaborn 0.13.2 pypi_0 pypi
setuptools 75.1.0 py312hca03da5_0
six 1.16.0 pyh6c4a22f_0 conda-forge
sqlite 3.45.3 h80987f9_0
stack_data 0.6.2 pyhd8ed1ab_0 conda-forge
sympy 1.13.3 pypi_0 pypi
tk 8.6.14 h6ba3021_0
torch 2.4.1 pypi_0 pypi
tornado 6.4.1 py312h80987f9_0
tqdm 4.66.5 pypi_0 pypi
traitlets 5.14.3 pyhd8ed1ab_0 conda-forge
typing_extensions 4.12.2 pyha770c72_0 conda-forge
tzdata 2024.2 pypi_0 pypi
unicodedata2 15.1.0 py312h80987f9_0
wcwidth 0.2.13 pyhd8ed1ab_0 conda-forge
wheel 0.44.0 py312hca03da5_0
xz 5.4.6 h80987f9_1
zeromq 4.3.5 h313beb8_0
zipp 3.20.2 pyhd8ed1ab_0 conda-forge
zlib 1.2.13 h18a0788_1
zstd 1.5.5 hd90d995_2
Я пытаюсь преобразовать данные np.array в тензор в torch; Ошибка отчета сбивает с толку; Я не знаю, есть ли конфликты в версиях пакетов.
Когда я понижаю версию numpy, он все равно сообщает о той же ошибке:
import numpy as np
import torch
print(torch.__version__)
print(np.__version__)
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.float32)
y = torch.from_numpy(x)
2.4.1
1.26.0
{
"name": "TypeError",
"message": "expected np.ndarray (got numpy.ndarray)",
"stack": "---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[1], line 6
4 print(np.__version__)
5 x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.float32)
----> 6 y = torch.from_numpy(x)
TypeError: expected np.ndarray (got numpy.ndarray)"
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... py-ndarray
Ошибка типа: ожидался np.ndarray (получил numpy.ndarray) ⇐ Python
Программы на Python
-
Anonymous
1737455477
Anonymous
Я использую приведенный ниже код, и он сообщает об ошибке. Это очень простой пример, и он не должен сообщать об ошибке, как ожидалось.
import numpy as np
import torch as th
# Assuming tt is some data (example as list)
tt = [1, 2, 3, 4, 5] # Example data
# Check if tt is a NumPy array, and convert if necessary
if not isinstance(tt, np.ndarray):
tt = np.array(tt)
# Now, convert tt to a PyTorch tensor
tensor_tt = th.from_numpy(tt)
print(tensor_tt)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[26], line 12
9 tt = np.array(tt)
11 # Now, convert tt to a PyTorch tensor
---> 12 tensor_tt = th.from_numpy(tt)
13 print(tensor_tt)
TypeError: e x p e c t e d n p . n d a r r a y ( g o t n u m p y . n d a r r a y ) < b r / > < / к о д > < b r / > Я и с п о л ь з у ю с л е д у ю щ у ю с р е д у c o n d a : < / p > < b r / > < c o d e > c o n d a l i s t < b r / > # p a c k a g e s i n e n v i r o n m e n t a t / o p t / m i n i c o n d a 3 / e n v s / e t h o s : < b r / > # < b r / > # N a m e V e r s i o n B u i l d C h a n n e l < b r / > a p p n o p e 0 . 1 . 4 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > a s t t o k e n s 2 . 4 . 1 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > b l a s 1 . 0 o p e n b l a s < b r / > b o t t l e n e c k 1 . 3 . 7 p y 3 1 2 h a 8 6 b 8 6 1 _ 0 < b r / > b r o t l i 1 . 0 . 9 h 8 0 9 8 7 f 9 _ 8 < b r / > b r o t l i - b i n 1 . 0 . 9 h 8 0 9 8 7 f 9 _ 8 < b r / > b z i p 2 1 . 0 . 8 h 8 0 9 8 7 f 9 _ 6 < b r / > c a - c e r t i f i c a t e s 2 0 2 4 . 9 . 2 4 h c a 0 3 d a 5 _ 0 < b r / > c l i c k 8 . 1 . 7 p y p i _ 0 p y p i < b r / > c o l o r l o g 6 . 8 . 2 p y p i _ 0 p y p i < b r / > c o m m 0 . 2 . 2 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > c o n t o u r p y 1 . 2 . 0 p y 3 1 2 h 4 8 c a 7 d 4 _ 0 < b r / > c y c l e r 0 . 1 1 . 0 p y h d 3 e b 1 b 0 _ 0 < b r / > d e b u g p y 1 . 6 . 7 p y 3 1 2 h 3 1 3 b e b 8 _ 0 < b r / > d e c o r a t o r 5 . 1 . 1 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > e t h o s 0 . 1 . 0 p y p i _ 0 p y p i < b r / > e x c e p t i o n g r o u p 1 . 2 . 2 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > e x e c u t i n g 2 . 1 . 0 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > e x p a t 2 . 6 . 3 h 3 1 3 b e b 8 _ 0 < b r / > f i l e l o c k 3 . 1 6 . 1 p y p i _ 0 p y p i < b r / > f o n t t o o l s 4 . 5 1 . 0 p y 3 1 2 h 8 0 9 8 7 f 9 _ 0 < b r / > f r e e t y p e 2 . 1 2 . 1 h 1 1 9 2 e 4 5 _ 0 < b r / > f s s p e c 2 0 2 4 . 9 . 0 p y p i _ 0 p y p i < b r / > h 5 p y 3 . 1 2 . 1 p y p i _ 0 p y p i < b r / > i m p o r t l i b - m e t a d a t a 8 . 5 . 0 p y h a 7 7 0 c 7 2 _ 0 c o n d a - f o r g e < b r / > i p y k e r n e l 6 . 2 9 . 5 p y h 5 7 c e 5 2 8 _ 0 c o n d a - f o r g e < b r / > i p y t h o n 8 . 2 8 . 0 p y h 7 0 7 e 7 2 5 _ 0 c o n d a - f o r g e < b r / > j e d i 0 . 1 9 . 1 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > j i n j a 2 3 . 1 . 4 p y p i _ 0 p y p i < b r / > j o b l i b 1 . 4 . 2 p y p i _ 0 p y p i < b r / > j p e g 9 e h 8 0 9 8 7 f 9 _ 3 < b r / > j u p y t e r _ c l i e n t 8 . 6 . 3 p y h d 8 e d 1 a b _ 0 c o n d a - f o r g e < b r / > j u p y t e r _ c o r e 5 . 7 . 2 p y h 3 1 0 1 1 f e _ 1 c o n d a - f o r g e < b r / > k i w i s o l v e r 1 . 4 . 4 p y 3 1 2 h 3 1 3 b e b 8 _ 0 < b r / > l c m s 2 2.12 hba8e193_0
lerc 3.0 hc377ac9_0
libbrotlicommon 1.0.9 h80987f9_8
libbrotlidec 1.0.9 h80987f9_8
libbrotlienc 1.0.9 h80987f9_8
libcxx 14.0.6 h848a8c0_0
libdeflate 1.17 h80987f9_1
libffi 3.4.4 hca03da5_1
libgfortran 5.0.0 11_3_0_hca03da5_28
libgfortran5 11.3.0 h009349e_28
libopenblas 0.3.21 h269037a_0
libpng 1.6.39 h80987f9_0
libsodium 1.0.18 h27ca646_1 conda-forge
libtiff 4.5.1 h313beb8_0
libwebp-base 1.3.2 h80987f9_0
llvm-openmp 14.0.6 hc6e5704_0
lz4-c 1.9.4 h313beb8_1
markupsafe 2.1.5 pypi_0 pypi
matplotlib-base 3.9.2 py312h2df2da3_0
matplotlib-inline 0.1.7 pyhd8ed1ab_0 conda-forge
mpmath 1.3.0 pypi_0 pypi
ncurses 6.4 h313beb8_0
nest-asyncio 1.6.0 pyhd8ed1ab_0 conda-forge
networkx 3.3 pypi_0 pypi
numexpr 2.8.7 py312h0f3ea24_0
numpy 2.1.2 pypi_0 pypi
numpy-base 1.26.4 py312he047099_0
openjpeg 2.5.2 h54b8e55_0
openssl 3.3.2 h8359307_0 conda-forge
packaging 24.1 pyhd8ed1ab_0 conda-forge
pandas 2.2.3 pypi_0 pypi
parso 0.8.4 pyhd8ed1ab_0 conda-forge
pexpect 4.9.0 pyhd8ed1ab_0 conda-forge
pickleshare 0.7.5 py_1003 conda-forge
pillow 10.4.0 py312h80987f9_0
pip 24.2 py312hca03da5_0
platformdirs 4.3.6 pyhd8ed1ab_0 conda-forge
prompt-toolkit 3.0.48 pyha770c72_0 conda-forge
psutil 5.9.0 py312h80987f9_0
ptyprocess 0.7.0 pyhd3deb0d_0 conda-forge
pure_eval 0.2.3 pyhd8ed1ab_0 conda-forge
pyarrow 17.0.0 pypi_0 pypi
pygments 2.18.0 pyhd8ed1ab_0 conda-forge
pyparsing 3.1.2 py312hca03da5_0
python 3.12.7 h99e199e_0
python-dateutil 2.9.0.post0 pypi_0 pypi
python-tzdata 2023.3 pyhd3eb1b0_0
pytz 2024.2 pypi_0 pypi
pyzmq 25.1.2 py312h313beb8_0
readline 8.2 h1a28f6b_0
seaborn 0.13.2 pypi_0 pypi
setuptools 75.1.0 py312hca03da5_0
six 1.16.0 pyh6c4a22f_0 conda-forge
sqlite 3.45.3 h80987f9_0
stack_data 0.6.2 pyhd8ed1ab_0 conda-forge
sympy 1.13.3 pypi_0 pypi
tk 8.6.14 h6ba3021_0
torch 2.4.1 pypi_0 pypi
tornado 6.4.1 py312h80987f9_0
tqdm 4.66.5 pypi_0 pypi
traitlets 5.14.3 pyhd8ed1ab_0 conda-forge
typing_extensions 4.12.2 pyha770c72_0 conda-forge
tzdata 2024.2 pypi_0 pypi
unicodedata2 15.1.0 py312h80987f9_0
wcwidth 0.2.13 pyhd8ed1ab_0 conda-forge
wheel 0.44.0 py312hca03da5_0
xz 5.4.6 h80987f9_1
zeromq 4.3.5 h313beb8_0
zipp 3.20.2 pyhd8ed1ab_0 conda-forge
zlib 1.2.13 h18a0788_1
zstd 1.5.5 hd90d995_2
Я пытаюсь преобразовать данные np.array в тензор в torch; Ошибка отчета сбивает с толку; Я не знаю, есть ли конфликты в версиях пакетов.
Когда я понижаю версию numpy, он все равно сообщает о той же ошибке:
import numpy as np
import torch
print(torch.__version__)
print(np.__version__)
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.float32)
y = torch.from_numpy(x)
2.4.1
1.26.0
{
"name": "TypeError",
"message": "expected np.ndarray (got numpy.ndarray)",
"stack": "---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[1], line 6
4 print(np.__version__)
5 x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.float32)
----> 6 y = torch.from_numpy(x)
TypeError: expected np.ndarray (got numpy.ndarray)"
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79062292/typeerror-expected-np-ndarray-got-numpy-ndarray[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия