700 lines
21 KiB
Text
700 lines
21 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'0123456789\""
|
|
]
|
|
},
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import unicodedata\n",
|
|
"import string\n",
|
|
"import pandas as pd\n",
|
|
"import re\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"import sklearn\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import seaborn as sns\n",
|
|
"\n",
|
|
"all_letters = string.ascii_letters + \" .,;'0123456789\"\n",
|
|
"n_letters = len(all_letters)\n",
|
|
"\n",
|
|
"all_letters"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Build Category dictionary, category and list of product name, shor description"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from elasticsearch import Elasticsearch\n",
|
|
"client = Elasticsearch(\"http://172.17.212.35:9200\")\n",
|
|
"resp = client.search(index=\"retromotion-indexer_development_products\",\n",
|
|
" body={\"_source\":[\"descriptions\",\"descriptionsSource\",\"nameSource\",\"shortDescriptionSource\",\"categoriesSource\"],\n",
|
|
" \"query\": {\"match_all\": {}}})"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"df_eng = pd.DataFrame(columns=['name','sdesc','category','catlevel0','catlevel1','catlevel2','catlevel3','catlevel4','catlevel5'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def cleanStr(text):\n",
|
|
" \n",
|
|
" # remove numbers\n",
|
|
" # no_number_string = re.sub(r'\\d+','',lower_string)\n",
|
|
"\n",
|
|
" # remove all punctuation except words and space\n",
|
|
" # text = re.sub(r'[^\\w\\s]','', lower_string)\n",
|
|
"\n",
|
|
" # clean = re.compile('<.*?>')\n",
|
|
"\n",
|
|
" # text = re.sub(clean,'',text)\n",
|
|
" text = re.sub('<[^>]*>', '', text)\n",
|
|
" emoticons = re.findall('(?::|;|=)(?:-)?(?:\\)|\\(|D|P)', text)\n",
|
|
" text = (re.sub('[\\W]+', ' ', text.lower()) + ' ' .join(emoticons).replace('-', ''))\n",
|
|
"\n",
|
|
" return text\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"name_en=[]\n",
|
|
"name_de=[]\n",
|
|
"\n",
|
|
"sdesc_en=[]\n",
|
|
"sdesc_de=[]\n",
|
|
"\n",
|
|
"desc_en=[]\n",
|
|
"desc_de=[]\n",
|
|
"\n",
|
|
"cat_en=[]\n",
|
|
"cat_de=[]\n",
|
|
"\n",
|
|
"catpath_en=[]\n",
|
|
"catpath_de=[]\n",
|
|
"\n",
|
|
"for hit in resp['hits']['hits']:\n",
|
|
" list_row =[]\n",
|
|
" for name in hit['_source']['nameSource']:\n",
|
|
" if (name[\"language\"]==\"en\"):\n",
|
|
" list_row.append(cleanStr(name[\"value\"]))\n",
|
|
" else:\n",
|
|
" name_de.append(name[\"value\"])\n",
|
|
" \n",
|
|
" for sdesc in hit['_source']['shortDescriptionSource']:\n",
|
|
" if (sdesc[\"language\"]==\"en\"):\n",
|
|
" list_row.append(cleanStr(sdesc[\"value\"]))\n",
|
|
" else:\n",
|
|
" sdesc_de.append(sdesc[\"value\"])\n",
|
|
"\n",
|
|
" # for desc in hit['_source']['descriptionsSource']:\n",
|
|
" # if (desc[\"language\"]==\"en\"):\n",
|
|
" # desc_en.append(readHtml(desc[\"value\"]))\n",
|
|
" # else:\n",
|
|
" # desc_de.append(desc[\"value\"])\n",
|
|
" \n",
|
|
" for cats in hit['_source']['categoriesSource']:\n",
|
|
" if (cats[\"language\"]==\"en\"):\n",
|
|
" list_row.append(cleanStr(cats[\"label\"]))\n",
|
|
" else:\n",
|
|
" desc_de.append(cats[\"label\"])\n",
|
|
"\n",
|
|
" if (cats[\"language\"]==\"en\"):\n",
|
|
" categories =cats[\"path\"].split('/')\n",
|
|
" i =0\n",
|
|
" catls=[]\n",
|
|
" for category in categories:\n",
|
|
"\n",
|
|
" list_row.append(cleanStr(category))\n",
|
|
" i= i+1\n",
|
|
" \n",
|
|
" else:\n",
|
|
" catpath_de.append(cats[\"path\"])\n",
|
|
" \n",
|
|
" # new_row = pd.DataFrame({'name':_name, 'sdesc':_sdesc, 'category':_cat_en }, index=[0])\n",
|
|
"\n",
|
|
" # pd.concat([new_row,df_eng.loc[:]]).reset_index(drop=True)\n",
|
|
"\n",
|
|
" for i in range (0,9-len(list_row)):\n",
|
|
" list_row.append(np.nan)\n",
|
|
"\n",
|
|
" df_eng.loc[len(df_eng)] = list_row\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<div>\n",
|
|
"<style scoped>\n",
|
|
" .dataframe tbody tr th:only-of-type {\n",
|
|
" vertical-align: middle;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe tbody tr th {\n",
|
|
" vertical-align: top;\n",
|
|
" }\n",
|
|
"\n",
|
|
" .dataframe thead th {\n",
|
|
" text-align: right;\n",
|
|
" }\n",
|
|
"</style>\n",
|
|
"<table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: right;\">\n",
|
|
" <th></th>\n",
|
|
" <th>name</th>\n",
|
|
" <th>sdesc</th>\n",
|
|
" <th>category</th>\n",
|
|
" <th>catlevel0</th>\n",
|
|
" <th>catlevel1</th>\n",
|
|
" <th>catlevel2</th>\n",
|
|
" <th>catlevel3</th>\n",
|
|
" <th>catlevel4</th>\n",
|
|
" <th>catlevel5</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>denso dtm82363 thermostat coolant</td>\n",
|
|
" <td>thermostat coolant</td>\n",
|
|
" <td>thermostat</td>\n",
|
|
" <td>sparepart</td>\n",
|
|
" <td>cooling system</td>\n",
|
|
" <td>thermostat</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>1</th>\n",
|
|
" <td>zf 1043 010 297 automatic transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>sparepart</td>\n",
|
|
" <td>transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>2</th>\n",
|
|
" <td>zf 1060 040 003 automatic transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>sparepart</td>\n",
|
|
" <td>transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>automatic transmission</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>3</th>\n",
|
|
" <td>vaico v10 0131 camshaft</td>\n",
|
|
" <td>camshaft</td>\n",
|
|
" <td>camshaft</td>\n",
|
|
" <td>sparepart</td>\n",
|
|
" <td>engine</td>\n",
|
|
" <td>engine timing</td>\n",
|
|
" <td>camshaft</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>4</th>\n",
|
|
" <td>vaico v26 0302 track control arm</td>\n",
|
|
" <td>track control arm</td>\n",
|
|
" <td>track control arm</td>\n",
|
|
" <td>sparepart</td>\n",
|
|
" <td>powertrain suspension</td>\n",
|
|
" <td>wheel suspension</td>\n",
|
|
" <td>track control arm</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" <td>NaN</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" name sdesc \\\n",
|
|
"0 denso dtm82363 thermostat coolant thermostat coolant \n",
|
|
"1 zf 1043 010 297 automatic transmission automatic transmission \n",
|
|
"2 zf 1060 040 003 automatic transmission automatic transmission \n",
|
|
"3 vaico v10 0131 camshaft camshaft \n",
|
|
"4 vaico v26 0302 track control arm track control arm \n",
|
|
"\n",
|
|
" category catlevel0 catlevel1 \\\n",
|
|
"0 thermostat sparepart cooling system \n",
|
|
"1 automatic transmission sparepart transmission \n",
|
|
"2 automatic transmission sparepart transmission \n",
|
|
"3 camshaft sparepart engine \n",
|
|
"4 track control arm sparepart powertrain suspension \n",
|
|
"\n",
|
|
" catlevel2 catlevel3 catlevel4 catlevel5 \n",
|
|
"0 thermostat NaN NaN NaN \n",
|
|
"1 automatic transmission automatic transmission NaN NaN \n",
|
|
"2 automatic transmission automatic transmission NaN NaN \n",
|
|
"3 engine timing camshaft NaN NaN \n",
|
|
"4 wheel suspension track control arm NaN NaN "
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"df_eng.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 54,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"df_category = df_eng.groupby('category')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 46,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"1 zf 1043 010 297 automatic transmission\n",
|
|
"2 zf 1060 040 003 automatic transmission\n",
|
|
"Name: name, dtype: object"
|
|
]
|
|
},
|
|
"execution_count": 46,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"category.get_group('automatic transmission')['name']\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 55,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Result:\n",
|
|
" dict_keys(['automatic transmission', 'camshaft', 'mounting', 'shock absorber', 'suspension kit', 'tensioner pulley', 'thermostat', 'track control arm', 'wheel hub'])\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# extract keys from groups\n",
|
|
"keys = df_category.groups.keys()\n",
|
|
"# for i in keys:\n",
|
|
"# print(category.get_group(i))\n",
|
|
"# print('\\n')\n",
|
|
"\n",
|
|
" # Display result\n",
|
|
"\n",
|
|
"all_categories=list(keys)"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Turning product Names into Tensors\n",
|
|
" \t \n",
|
|
"Represent each name in one-hot encoded form (1 for the position of the letter, 0 elsewhere)\n",
|
|
"\n",
|
|
"To make a word we join a bunch of those into a 2D matrix <line_length x 1 x n_letters>.\n",
|
|
"That extra 1 dimension is because PyTorch assumes everything is in batches - we're just using a batch size of 1 here."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import torch"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def letterToTensor(letter):\n",
|
|
" \n",
|
|
" tensor = torch.zeros(1, n_letters)\n",
|
|
" tensor[0][all_letters.find(letter)] = 1\n",
|
|
" \n",
|
|
" return tensor"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
|
|
" 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
|
|
" 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
|
|
" 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(letterToTensor('a'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def nameToTensor(name):\n",
|
|
" tensor = torch.zeros(len(name), 1, n_letters)\n",
|
|
" \n",
|
|
" for li, letter in enumerate(name):\n",
|
|
" tensor[li][0][all_letters.find(letter)] = 1\n",
|
|
" \n",
|
|
" return tensor"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"torch.Size([33, 1, 67])"
|
|
]
|
|
},
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"p_tensor = nameToTensor('denso dtm82363 thermostat coolant')\n",
|
|
"\n",
|
|
"p_tensor.size()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"tensor([[[0., 0., 0., ..., 0., 0., 0.]],\n",
|
|
"\n",
|
|
" [[0., 0., 0., ..., 0., 0., 0.]],\n",
|
|
"\n",
|
|
" [[0., 0., 0., ..., 0., 0., 0.]],\n",
|
|
"\n",
|
|
" ...,\n",
|
|
"\n",
|
|
" [[1., 0., 0., ..., 0., 0., 0.]],\n",
|
|
"\n",
|
|
" [[0., 0., 0., ..., 0., 0., 0.]],\n",
|
|
"\n",
|
|
" [[0., 0., 0., ..., 0., 0., 0.]]])"
|
|
]
|
|
},
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"p_tensor"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import torch.nn as nn\n",
|
|
"\n",
|
|
"class RNN(nn.Module):\n",
|
|
" \n",
|
|
" def __init__(self, input_size, hidden_size, output_size):\n",
|
|
" \n",
|
|
" super(RNN, self).__init__()\n",
|
|
"\n",
|
|
" self.hidden_size = hidden_size\n",
|
|
"\n",
|
|
" self.i2h = nn.Linear(input_size + hidden_size, hidden_size)\n",
|
|
" \n",
|
|
" self.i2o = nn.Linear(input_size + hidden_size, output_size)\n",
|
|
" \n",
|
|
" self.softmax = nn.LogSoftmax(dim=1)\n",
|
|
"\n",
|
|
" def forward(self, input, hidden):\n",
|
|
" combined = torch.cat((input, hidden), 1)\n",
|
|
" \n",
|
|
" hidden = self.i2h(combined)\n",
|
|
" \n",
|
|
" output = self.i2o(combined)\n",
|
|
" output = self.softmax(output)\n",
|
|
" \n",
|
|
" return output, hidden\n",
|
|
"\n",
|
|
" def initHidden(self):\n",
|
|
" return torch.zeros(1, self.hidden_size)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"n_hidden = 256\n",
|
|
"n_category = len(category)\n",
|
|
"\n",
|
|
"rnn = RNN(n_letters, n_hidden, n_category)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"output size = torch.Size([1, 9])\n",
|
|
"next_hidden size = torch.Size([1, 256])\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"inp = letterToTensor('C')\n",
|
|
"\n",
|
|
"hidden = torch.zeros(1, n_hidden)\n",
|
|
"\n",
|
|
"output, next_hidden = rnn(inp, hidden)\n",
|
|
"\n",
|
|
"print('output size =', output.size())\n",
|
|
"print('next_hidden size =', next_hidden.size())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"inp = nameToTensor('denso dtm82363 thermostat coolant')\n",
|
|
"\n",
|
|
"hidden = torch.zeros(1, n_hidden)\n",
|
|
"\n",
|
|
"output, next_hidden = rnn(inp[0], hidden)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 31,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"tensor([[-2.2812, -2.2042, -2.2270, -2.1712, -2.1616, -2.1168, -2.2543, -2.1659,\n",
|
|
" -2.2031]], grad_fn=<LogSoftmaxBackward0>)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(output)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 44,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def categoryFromOutput(output):\n",
|
|
" \n",
|
|
" _, top_i = output.topk(1)\n",
|
|
" \n",
|
|
" cat_i = top_i[0].item()\n",
|
|
" \n",
|
|
" return all_categories[cat_i], cat_i"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 45,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"('tensioner pulley', 5)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(categoryFromOutput(output))"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### get training samples category and names"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 51,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import random\n",
|
|
"\n",
|
|
"def randomTrainingExample():\n",
|
|
" \n",
|
|
" random_cat_index = random.randint(0, n_category - 1)\n",
|
|
" randcategory = all_categories[random_cat_index]\n",
|
|
" \n",
|
|
" # get feature name from the category\n",
|
|
" random_feature_names = category.get_group(randcategory)['name']\n",
|
|
" \n",
|
|
" name = random_feature_names[random.randint(0, len(random_feature_names) - 1)]\n",
|
|
" \n",
|
|
" category_tensor = torch.tensor([all_categories.index(randcategory)], dtype=torch.long)\n",
|
|
" name_tensor = nameToTensor(name)\n",
|
|
" \n",
|
|
" return randcategory, name, category_tensor, name_tensor"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 53,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "AttributeError",
|
|
"evalue": "'str' object has no attribute 'get_group'",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
|
"Cell \u001b[0;32mIn[53], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39m10\u001b[39m):\n\u001b[0;32m----> 2\u001b[0m category, name, category_tensor, name_tensor \u001b[39m=\u001b[39m randomTrainingExample()\n\u001b[1;32m 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m'\u001b[39m\u001b[39mcategory =\u001b[39m\u001b[39m'\u001b[39m, category, \u001b[39m'\u001b[39m\u001b[39m, name =\u001b[39m\u001b[39m'\u001b[39m, name)\n",
|
|
"Cell \u001b[0;32mIn[51], line 9\u001b[0m, in \u001b[0;36mrandomTrainingExample\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m randcategory \u001b[39m=\u001b[39m all_categories[random_cat_index]\n\u001b[1;32m 8\u001b[0m \u001b[39m# get feature name from the category\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m random_feature_names \u001b[39m=\u001b[39m category\u001b[39m.\u001b[39;49mget_group(randcategory)[\u001b[39m'\u001b[39m\u001b[39mname\u001b[39m\u001b[39m'\u001b[39m]\n\u001b[1;32m 11\u001b[0m name \u001b[39m=\u001b[39m random_feature_names[random\u001b[39m.\u001b[39mrandint(\u001b[39m0\u001b[39m, \u001b[39mlen\u001b[39m(random_feature_names) \u001b[39m-\u001b[39m \u001b[39m1\u001b[39m)]\n\u001b[1;32m 13\u001b[0m category_tensor \u001b[39m=\u001b[39m torch\u001b[39m.\u001b[39mtensor([all_categories\u001b[39m.\u001b[39mindex(randcategory)], dtype\u001b[39m=\u001b[39mtorch\u001b[39m.\u001b[39mlong)\n",
|
|
"\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'get_group'"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for i in range(10):\n",
|
|
" category, name, category_tensor, name_tensor = randomTrainingExample()\n",
|
|
" \n",
|
|
" print('category =', category, ', name =', name)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv",
|
|
"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.10.12"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|