Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d1b2fb8b",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import torch "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c3dcca79",
"metadata": {},
"outputs": [],
"source": [
"my_list = [1, 2, 3, 4, 5]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6c84c67a",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate list (not \"int\") to list",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-c39b2b5ca5a3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_list\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list"
]
}
],
"source": [
"my_list + 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1a06d1e6",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array(my_list)\n",
"tns = torch.tensor(my_list)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "aa812044",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([1, 2, 3, 4, 5]), tensor([1, 2, 3, 4, 5]))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr , tns"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a73e337a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 3, 4, 5, 6])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr + 1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "64d5b6c2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([2, 3, 4, 5, 6])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tns + 1"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment