Проблема обучения модели глубокого обученияPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Проблема обучения модели глубокого обучения

Сообщение Anonymous »

Я столкнулся с проблемой во время построения модели глубокого обучения и процесса обучения, когда работал с Jupyter Notebook в коде VS.
Я пытался обучить модель глубокого обучения, которую я построил с помощью Tensorflow, используя метод model.fit, но возникла проблема.
Шаг 1. Генерация данных

Код: Выделить всё

import pandas as pd
import numpy as np

#Generate dummy data
np.random.seed(0)
n_products = 1000

#Dummy categories
categories = ['electronics', 'clothing', 'home', 'sports']

#Generate random products
data = {
'name': [f'Product[i]' for i in range(n_products)],
'description': [' '.join(np.random.choice(['good', 'awesome', 'excellent', 'fantastic'], size=10)) for _ in range(n_products)],
'price': np.random.randint(10, 1000, size=n_products),
'category': np.random.choice(categories, size=n_products)
}

#Create DataFrame
df = pd.DataFrame(data)
print(df)
Шаг 2. Предварительная обработка данных

Код: Выделить всё

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

#Tokenize text attributes
max_words = 1000
max_len = 100

tokenizer = Tokenizer(num_words = max_words)
tokenizer.fit_on_texts(df['description'])

X_text = tokenizer.texts_to_sequences(df['description'])
X_text = pad_sequences(X_text, maxlen = max_len)

#Encode categorical attribute
label_encoder = LabelEncoder()
df['category_encoded'] = label_encoder.fit_transform(df['category'])

#Split data into train and test sets
X_train_text, X_test_text, X_train_cat, X_test_cat, y_train, y_test = train_test_split(X_text, df['category_encoded'], df['price'], test_size = 0.2, random_state=42)
print("Done")
print(len(X_train_text))
print(y_train)
Шаг 3. Построение и обучение модели с использованием Tensorflow

Код: Выделить всё

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Flatten, Dense

#Define the model
model = Sequential([
Embedding(input_dim = max_words, output_dim = 64),
Flatten(),
Dense(64, activation='relu'),
Dense(4, activation='softmax') #4 categories
])

#Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

#Train the model
history = model.fit(X_train_text, y_train,
epochs=10,
batch_size=32,
validation_data=(X_test_text, y_test))

На шаге 3 я получил ошибку, как показано ниже:

Код: Выделить всё

Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[13], line 18

13 model.compile(optimizer='adam',
14               loss='sparse_categorical_crossentropy',
15               metrics=['accuracy'])
17 #Train the model
---> 18 history = model.fit(X_train_text, y_train,
19                     epochs=10,
20                     batch_size=32,
21                     validation_data=(X_test_text, y_test))

File c:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback..error_handler(*args, **kwargs)

119     filtered_tb = _process_traceback_frames(e.__traceback__)
120     # To get the full stack trace, call:
121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
123 finally:
124     del filtered_tb

File c:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\eager\execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)

51 try:
52   ctx.ensure_initialized()
---> 53   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
54                                       inputs, attrs, num_outputs)

File "c:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\backend\tensorflow\nn.py", line 638, in sparse_categorical_crossentropy

Received a label value of 981 which is outside the valid range of [0, 4).   Label values: 799 555 895 885 300 152 513 436 397 525 907 616 981 939 147 724 851 857 465 953 710 764 630 521 77 234 744 169 963 916 472 237
[[{{node compile_loss/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_one_step_on_iterator_3817]
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
Я думал, что возникла проблема с предварительной обработкой данных, но понятия не имею, как ее решить.

Подробнее здесь: https://stackoverflow.com/questions/785 ... ning-issue
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»