my code: %% import os import numpy as np import pandas as pd from…
- User
my code:
%%
import os import numpy as np import pandas as pd from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from tensorflow.keras.layers import Input, Dropout from tensorflow.keras import Model
%%
def data_generator(file_paths, batch_size, train=True, scaler=None): while True: # Loop forever so the generator never terminates # Shuffle file paths to ensure random distribution of data across files np.random.shuffle(file_paths)
for file_path in file_paths: data = pd.read_csv(file_path) # Split features and labels X = data.drop([ 'Date', 'Symbol', 'y_High_1d', 'y_Low_1d', 'y_Priority_1d', 'y_High_2d', 'y_Low_2d', 'y_Priority_2d', 'y_High_3d', 'y_Low_3d', 'y_Priority_3d', 'y_High_5d', 'y_Low_5d', 'y_Priority_5d'], axis=1).values y_high_1d = data['y_High_1d'].values y_low_1d = data['y_Low_1d'].values y_priority_1d = data['y_Priority_1d'].values y_high_2d = data['y_High_2d'].values y_low_2d = data['y_Low_2d'].values y_priority_2d = data['y_Priority_2d'].values y_high_3d = data['y_High_3d'].values y_low_3d = data['y_Low_3d'].values y_priority_3d = data['y_Priority_3d'].values y_high_5d = data['y_High_5d'].values y_low_5d = data['y_Low_5d'].values y_priority_5d = data['y_Priority_5d'].values # Optionally, apply preprocessing here (e.g., Scaler transform) if scaler is not None and train: X = scaler.fit_transform(X) elif scaler is not None: X = scaler.transform(X) # Splitting data into batches for i in range(0, len(data), batch_size): end = i + batch_size X_batch = X[i:end] y_high_batch_1d = y_high_1d[i:end] y_low_batch_1d = y_low_1d[i:end] y_priority_batch_1d = y_priority_1d[i:end] y_high_batch_2d = y_high_2d[i:end] y_low_batch_2d = y_low_2d[i:end] y_priority_batch_2d = y_priority_2d[i:end] y_high_batch_3d = y_high_3d[i:end] y_low_batch_3d = y_low_3d[i:end] y_priority_batch_3d = y_priority_3d[i:end] y_high_batch_5d = y_high_5d[i:end] y_low_batch_5d = y_low_5d[i:end] y_priority_batch_5d = y_priority_5d[i:end] yield X_batch, [y_high_batch_1d, y_low_batch_1d,y_priority_batch_1d, y_high_batch_2d, y_low_batch_2d,y_priority_batch_2d, y_high_batch_3d, y_low_batch_3d, y_priority_batch_3d, y_high_batch_5d, y_low_batch_5d, y_priority_batch_5d]%%
dataset_dir = r'C:\Users\arisa\Desktop\day_spot' file_paths = [os.path.join(dataset_dir, file_name) for file_name in os.listdir(dataset_dir)] train_files, val_files = train_test_split(file_paths, test_size=0.06, random_state=42)
- Assistant
- User
now im getting this error: { "name": "TypeError", "message": "'numpy.float64' object cannot be interpreted as an integer", "stack": "--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[20], line 12 3 validation_steps = np.ceil(number_of_validation_samples / batch_size) # Replace number_of_validation_samples 5 # model.fit(train_gen, 6 # steps_per_epoch=steps_per_epoch, 7 # validation_data=val_gen, (...) 10 11 # Then use these datasets in your model.fit call ---> 12 model.fit(train_dataset, 13 steps_per_epoch=steps_per_epoch, 14 validation_data=val_dataset, 15 validation_steps=validation_steps, 16 epochs=1000)
File c:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback..error_handler(*args, **kwargs) 119 filtered_tb = _process_traceback_frames(e.) 120 # To get the full stack trace, call: 121 # --> 122 raise e.with_traceback(filtered_tb) from None 123 finally: 124 del filtered_tb
- Assistant
The error message you are seeing,
TypeError: 'numpy.float64' object cannot be interpreted as an integer, is likely due to howsteps_per_epochandvalidation_stepsare calculated. Thenp.ceilfunction returns anumpy.float64value, butsteps_per_epochandvalidation_stepsparameters inmodel.fitexpect integer values to denote the number of steps per epoch during training and validation, respectively.To resolve this error, you can simply convert the
numpy.float64values ofsteps_per_epochandvalidation_stepsto integers. Here's how you can modify your code:steps_per_epoch = np.ceil(number_of_training_samples / batch_size).astype(int) # Convert to int validation_steps = np.ceil(number_of_validation_samples / batch_size).astype(int) # Convert to int # Then use these integer values in your model.fit call model.fit(train_dataset, steps_per_epoch=steps_per_epoch, validation_data=val_dataset, validation_steps=validation_steps, epochs=1000) - User
now im getting this error: { "name": "ValueError", "message": "Creating variables on a non-first call to a function decorated with tf.function.", "stack": "--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[26], line 12 3 validation_steps = np.ceil(number_of_validation_samples / batch_size).astype(int) # Replace number_of_validation_samples 5 # model.fit(train_gen, 6 # steps_per_epoch=steps_per_epoch, 7 # validation_data=val_gen, (...) 10 11 # Then use these datasets in your model.fit call ---> 12 model.fit(train_dataset, 13 steps_per_epoch=steps_per_epoch, 14 validation_data=val_dataset, 15 validation_steps=validation_steps, 16 epochs=1000)
- Assistant
- User
- Assistant