tensorflow icon indicating copy to clipboard operation
tensorflow copied to clipboard

whenever I try fitting a sequential model it doesn't work

Open yousifj129 opened this issue 1 year ago • 5 comments

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

2.16.1

Custom code

Yes

OS platform and distribution

Windows 11

Mobile device

No response

Python version

3.12

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

release 12.4, V12.4.131

GPU model and memory

No response

Current behavior?

I can't use the fit function, I tried it everytime, even when I am using the code examples in the tensorflow website and documentation, it just doesn't seem to work

Standalone code to reproduce the issue

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from imblearn.over_sampling import RandomOverSampler
import tensorflow as tf


cols = ["flength", "fwidth", "fsize", "fconc", "fconc1", "fasym", "fm3long", "fm3trans", "falpha", "fdist", "class"]

df = pd.read_csv("./ML/gamma ray stuff/magic04.data", names=cols)
df.head()

df["class"] = (df["class"] == "g").astype(int)
print(df)

# for label in cols[:-1]:  # Exclude the last element ("class")
#     plt.hist(df[df["class"] == 1][label], color='blue', label="gamma", alpha=0.7, density=True)
#     plt.hist(df[df["class"] == 0][label], color='red', label="hadron", alpha=0.7, density=True)
#     plt.title(label)
#     plt.ylabel("probability")
#     plt.xlabel(label)
#     plt.legend()
#     plt.show()

train, valid, test = np.split(df.sample(frac=1), [int(0.6*len(df)), int(0.8*len(df))])

def scale_dataset(dataframe, oversample =False):
    X = dataframe[dataframe.columns[:-1]].values
    y = dataframe[dataframe.columns[-1]].values

    scalar = StandardScaler()

    X = scalar.fit_transform(X)

    if oversample:
        ros = RandomOverSampler()
        X,y = ros.fit_resample(X,y)
    
    data = np.hstack((X,np.reshape(y, (-1,1))))

    return data, X, y


train, x_train, y_train = scale_dataset(train,oversample=True)
valid, x_valid, y_valid = scale_dataset(valid,oversample=False)
test, x_test, y_test = scale_dataset(test,oversample=False)

nn_model = tf.keras.Sequential(
    [
        tf.keras.layers.Dense(32, activation='relu', input_shape= (10,)),
        tf.keras.layers.Dense(32, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ]
)

nn_model.compile(
    optimizer=tf.keras.optimizers.Adam(0.001),
      loss='binary_crossentropy',
        metrics=['accuracy']
)

nn_model.fit(x_train,y_train, epochs=100, batch_size=32, validation_split=0.2)

Relevant log output

C:\Users\PCMR\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\keras\src\layers\core\dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(activity_regularizer=activity_regularizer, **kwargs)
2024-06-23 11:43:50.343214: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Epoch 1/100
Traceback (most recent call last):
  File "d:\Developer\Python\testing territory\ML\gamma ray stuff\main.py", line 100, in <module>
    nn_model.fit(x_train,y_train, epochs=100, batch_size=32, validation_split=0.2)
  File "C:\Users\PCMR\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 23-42: character maps to <undefined>

yousifj129 avatar Jun 23 '24 08:06 yousifj129

@yousifj129 Could you please share the dependencies here to run the shared script? I tried to revise the code below, please let us know if it helps?


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from imblearn.over_sampling import RandomOverSampler
from sklearn.model_selection import train_test_split
import tensorflow as tf

# Load dataset
cols = ["flength", "fwidth", "fsize", "fconc", "fconc1", "fasym", "fm3long", "fm3trans", "falpha", "fdist", "class"]
df = pd.read_csv("./ML/gamma ray stuff/magic04.data", names=cols)

# Convert class to binary integer
df["class"] = (df["class"] == "g").astype(int)

# Split dataset into train, validation, test sets
train, test = train_test_split(df, test_size=0.2, random_state=42)
train, valid = train_test_split(train, test_size=0.25, random_state=42)  # 60% train, 20% valid

# Function to scale dataset and optionally oversample
def scale_dataset(dataframe, oversample=False):
    X = dataframe[dataframe.columns[:-1]].values
    y = dataframe[dataframe.columns[-1]].values

    scalar = StandardScaler()
    X = scalar.fit_transform(X)

    if oversample:
        ros = RandomOverSampler()
        X, y = ros.fit_resample(X, y)
    
    return X, y

# Preprocess datasets
x_train, y_train = scale_dataset(train, oversample=True)
x_valid, y_valid = scale_dataset(valid, oversample=False)
x_test, y_test = scale_dataset(test, oversample=False)

# Define neural network model
nn_model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='relu', input_shape=(x_train.shape[1],)),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

# Compile model
nn_model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
                 loss='binary_crossentropy',
                 metrics=['accuracy'])

# Train model with callbacks for early stopping and model checkpoint
callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True),
    tf.keras.callbacks.ModelCheckpoint('model_best.h5', save_best_only=True)
]

history = nn_model.fit(x_train, y_train, epochs=100, batch_size=32, validation_split=0.2, callbacks=callbacks)

# Evaluate model on train, validation, and test sets
eval_train = nn_model.evaluate(x_train, y_train)
eval_valid = nn_model.evaluate(x_valid, y_valid)
eval_test = nn_model.evaluate(x_test, y_test)

print("Train Accuracy:", eval_train[1])
print("Validation Accuracy:", eval_valid[1])
print("Test Accuracy:", eval_test[1])

Thank you!

sushreebarsa avatar Jun 27 '24 10:06 sushreebarsa

I tried the code but I dont have the file model_best.h5: ValueError: The filepath provided must end in .keras (Keras model format). Received: filepath=model_best.h5

absl-py==2.1.0 asgiref==3.8.1 asttokens==2.4.1 astunparse==1.6.3 cachetools==5.3.3 certifi==2024.6.2 chardet==5.2.0 charset-normalizer==2.1.1 colorama==0.4.6 comm==0.2.2 contourpy==1.2.1 cycler==0.12.1 debugpy==1.8.2 decorator==5.1.1 Django==5.0.6 executing==2.0.1 flatbuffers==24.3.25 fonttools==4.53.0 gast==0.5.4 gensim==4.3.2 google-auth==2.30.0 google-auth-oauthlib==1.2.0 google-pasta==0.2.0 grpcio==1.64.1 gspread==5.6.0 h5py==3.11.0 idna==3.7 imbalanced-learn==0.12.3 imblearn==0.0 ipykernel==6.29.4 ipython==8.25.0 jedi==0.19.1 joblib==1.4.2 jupyter_client==8.6.2 jupyter_core==5.7.2 keras==3.4.0 keras-nightly==3.3.3.dev2024062303 kiwisolver==1.4.5 libclang==18.1.1 Markdown==3.6 markdown-it-py==3.0.0 MarkupSafe==2.1.5 matplotlib==3.9.0 matplotlib-inline==0.1.7 mdurl==0.1.2 ml-dtypes==0.3.2 MouseInfo==0.1.3 namex==0.0.8 nest-asyncio==1.6.0 numpy==1.26.4 oauthlib==3.2.2 opt-einsum==3.3.0 optree==0.11.0 packaging==24.1 pandas==2.2.2 parso==0.8.4 pillow==10.3.0 platformdirs==4.2.2 prompt_toolkit==3.0.47 protobuf==4.25.3 psutil==6.0.0 pure-eval==0.2.2 pyasn1==0.6.0 pyasn1_modules==0.4.0 PyAutoGUI==0.9.54 pygame==2.6.0 PyGetWindow==0.0.9 Pygments==2.18.0 PyMsgBox==1.0.9 pyparsing==3.1.2 pyperclip==1.9.0 PyRect==0.2.0 PyScreeze==0.1.30 PySide6==6.7.2 PySide6_Addons==6.7.2 PySide6_Essentials==6.7.2 python-dateutil==2.9.0.post0 python-dotenv==0.21.0 pytweening==1.2.0 pytz==2024.1 pywin32==306 pyzmq==26.0.3 reportlab==4.2.2 requests==2.32.3 requests-oauthlib==1.3.1 rich==13.7.1 rsa==4.9 schedule==1.1.0 scikit-learn==1.5.0 scipy==1.12.0 setuptools==70.1.0 shiboken6==6.7.2 six==1.16.0 smart-open==7.0.4 sqlparse==0.5.0 stack-data==0.6.3 tb-nightly==2.18.0a20240616 tensorboard==2.16.2 tensorboard-data-server==0.7.2 tensorflow==2.16.1 tensorflow-intel==2.16.1 termcolor==2.4.0 tf-nightly==2.18.0.dev20240620 tf_nightly_intel==2.18.0.dev20240620 threadpoolctl==3.5.0 tornado==6.4.1 traitlets==5.14.3 tweepy==4.10.1 typing_extensions==4.12.2 tzdata==2024.1 urllib3==1.26.19 wcwidth==0.2.13 Werkzeug==3.0.3 wheel==0.43.0 wrapt==1.16.0

yousifj129 avatar Jun 27 '24 10:06 yousifj129

@yousifj129 I tried to replicate the issue reported here, could you please share the dependencies? Thank you!

sushreebarsa avatar Jun 28 '24 07:06 sushreebarsa

i am not sure what do you mean by "dependencies" cause i sent you all the packages, but just in case you meant the data: magic04.csv

yousifj129 avatar Jun 28 '24 07:06 yousifj129

Check if your environment is set to UTF-8 coding or not. in your terminal write these commands for checking environment coding:

import locale locale.getpreferredencoding()

and then and explicitly set encoding run this in your terminal: set PYTHONIOENCODING=utf-8

or you can try to redirect the output to a file to avoid any issues with terminal encoding

import sys sys.stdout = open ('output.txt', 'w', encoding='utf-8')

nn_model.fit(x_train, y_train, epochs=100, batch_size=32, validation_split=0.2)

If the issue is related to the verbose output during the training process, you can set the verbosity level to 0 or 1

if this also doesn't work, check latest version of tensor flow is installed.

harsh-playz31882 avatar Jun 30 '24 06:06 harsh-playz31882

@yousifj129 Could you refer to the above comment and let us know? Thank you!

sushreebarsa avatar Jul 01 '24 06:07 sushreebarsa

I tried to do it, but it doesn't change it set PYTHONIOENCODING=utf-8 and export PYTHONIOENCODING=utf-8 in bash, tried different methods but it kept giving me:

import locale locale.getpreferredencoding() 'cp1252'

it doesn't change to UTF-8

yousifj129 avatar Jul 01 '24 13:07 yousifj129

it didnt change it after I did set PYTHONIOENCODING=utf-8

but then searched around and found a solution that worked: Locale can be set in windows globally to UTF-8, if you so desire, as follows:

Control panel -> Clock and Region -> Region -> Administrative -> Change system locale -> Check Beta: Use Unicode UTF-8 ...

yousifj129 avatar Jul 01 '24 14:07 yousifj129

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Jul 01 '24 14:07 google-ml-butler[bot]

it didnt change it after I did set PYTHONIOENCODING=utf-8

but then searched around and found a solution that worked: Locale can be set in windows globally to UTF-8, if you so desire, as follows:

Control panel -> Clock and Region -> Region -> Administrative -> Change system locale -> Check Beta: Use Unicode UTF-8 ...

so problem was with utf-8 only, I 1st time contributed to a problem in github

harsh-playz31882 avatar Jul 01 '24 20:07 harsh-playz31882