adriangb / scikeras

Scikit-Learn API wrapper for Keras.
https://www.adriangb.com/scikeras/
MIT License
239 stars 47 forks source link

UNKNOWN: NewRandomAccessFile failed to Create/Open #291

Closed slimebob1975 closed 1 year ago

slimebob1975 commented 1 year ago

Introduction

I am trying to save and load a scikeras model in a pipeline with standard scaling and principal component analysis. I can save the pipeline, but when I try to load it I get some strange errors. I am running Jupyter-lab on Windows 10 Enterprise with Python 3.9.16.

The error look like this:

WARNING:absl:Found untraced functions such as _update_step_xla while saving (showing 1 of 1). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: C:\Users\...\AppData\Local\Temp\tmp1te5qppx\assets
INFO:tensorflow:Assets written to: C:\Users\...\AppData\Local\Temp\tmp1te5qppx\assets
---------------------------------------------------------------------------
OpError                                   Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12448\1012496037.py in <module>
     60 pickle.dump(clf, open(".\model.sav", 'wb'))
     61 
---> 62 clf2 = pickle.load(open(".\model.sav", 'rb'))
     63 
     64 clf2.predict(X[:5])

~\Anaconda3\lib\site-packages\scikeras\_saving_utils.py in unpack_keras_model(packed_keras_model)
     50                     f.write(archive.extractfile(fname).read())
     51         model: keras.Model = load_model(temp_dir)
---> 52         model.load_weights(temp_dir)
     53         model.optimizer.build(model.trainable_variables)
     54         return model

~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

~\Anaconda3\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in error_translator(e)
     43     raise errors_impl.InternalError(None, None, error_message)
     44   else:
---> 45     raise errors_impl.OpError(None, None, error_message, errors_impl.UNKNOWN)
     46 
     47 

OpError: 

Also in the Anaconda Prompt window I get something mysterious message:

2023-03-01 16:24:12.033629: W tensorflow/core/util/tensor_slice_reader.cc:96] Could not open C:\Users\rgt0227\AppData\Local\Temp\tmppzfngdbl: UNKNOWN: NewRandomAccessFile failed to Create/Open: C:\Users\rgt0227\AppData\Local\Temp\tmppzfngdbl : Access denied.
; Input/output error

Code for reproduction

import numpy as np
from scikeras.wrappers import KerasClassifier
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

from tensorflow import keras
from sklearn.datasets import make_classification
from typing import Dict, Iterable, Any
import pickle

X, y = make_classification(1000, 20, n_informative=10, random_state=0)
X.shape, y.shape, y.mean()

steps=[('std', StandardScaler()), ('pca', PCA()),  ('keras', MLPClassifier())]
clf = Pipeline(steps=steps)

print(clf.fit(X, y).score(X, y))

pickle.dump(clf, open(".\model.sav", 'wb'))

clf2 = pickle.load(open(".\model.sav", 'rb'))

clf2.predict(X[:5])

where MLPClassifier is taken from https://www.adriangb.com/scikeras/stable/notebooks/MLPClassifier_MLPRegressor.html

class MLPClassifier(KerasClassifier):

    def __init__(
        self,
        hidden_layer_sizes=(100, ),
        optimizer="adam",
        optimizer__learning_rate=0.001,
        epochs=200,
        verbose=0,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.hidden_layer_sizes = hidden_layer_sizes
        self.optimizer = optimizer
        self.epochs = epochs
        self.verbose = verbose

    def _keras_build_fn(self, compile_kwargs: Dict[str, Any]):
        model = keras.Sequential()
        inp = keras.layers.Input(shape=(self.n_features_in_))
        model.add(inp)
        for hidden_layer_size in self.hidden_layer_sizes:
            layer = keras.layers.Dense(hidden_layer_size, activation="relu")
            model.add(layer)
        if self.target_type_ == "binary":
            n_output_units = 1
            output_activation = "sigmoid"
            loss = "binary_crossentropy"
        elif self.target_type_ == "multiclass":
            n_output_units = self.n_classes_
            output_activation = "softmax"
            loss = "sparse_categorical_crossentropy"
        else:
            raise NotImplementedError(f"Unsupported task type: {self.target_type_}")
        out = keras.layers.Dense(n_output_units, activation=output_activation)
        model.add(out)
        model.compile(loss=loss, optimizer=compile_kwargs["optimizer"])
        return model
adriangb commented 1 year ago

I don't think this is a SciKeras error. We test something very similar in CI. Is it possible something strange is going on with the permissions on your system?

slimebob1975 commented 1 year ago

Thanks @adriangb for your reply. Yes, this is probably something related to permission on my system since I was recently unable to replicate it on another Windows machine. I have noticed something similar happens when I try use copy.deepcopy() on the SciKeras model.

bizso09 commented 10 months ago

I also get the same error. How did you fix it?


 E tensorflow/tsl/platform/windows/windows_file_system.cc:363] ERROR: GetSymbolicLinkTarget cannot open file for \\?\C:\Users\itsme\AppData\Local\Temp\tmpd4kdyj5q GetLastError: 5

 W tensorflow/core/util/tensor_slice_reader.cc:98] Could not open C:\Users\itsme\AppData\Local\Temp\tmpd4kdyj5q: UNKNOWN: NewRandomAccessFile failed to Create/Open: C:\Users\itsme\AppData\Local\Temp\tmpd4kdyj5q : Access is denied.
slimebob1975 commented 10 months ago

I have not managed to solve it. It has something to do with the permission settings on the particular machine, and our IT department have been looking into it. Without progress so far, I am afriad.

Från: bizso09 @.> Skickat: den 21 oktober 2023 03:58 Till: adriangb/scikeras @.> Kopia: Granat Robert @.>; State change @.> Ämne: Re: [adriangb/scikeras] UNKNOWN: NewRandomAccessFile failed to Create/Open (Issue #291)

I also get the same error. How did you fix it?

E tensorflow/tsl/platform/windows/windows_file_system.cc:363] ERROR: GetSymbolicLinkTarget cannot open file for \?\C:\Users\itsme\AppData\Local\Temp\tmpd4kdyj5qfile://%3f/C:/Users/itsme/AppData/Local/Temp/tmpd4kdyj5q GetLastError: 5

W tensorflow/core/util/tensor_slice_reader.cc:98] Could not open C:\Users\itsme\AppData\Local\Temp\tmpd4kdyj5q: UNKNOWN: NewRandomAccessFile failed to Create/Open: C:\Users\itsme\AppData\Local\Temp\tmpd4kdyj5q : Access is denied.

— Reply to this email directly, view it on GitHubhttps://github.com/adriangb/scikeras/issues/291#issuecomment-1773600852, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AWFJZWV2C2VP7LU4IEWY5BTYAMT4JAVCNFSM6AAAAAAVMG2VLGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZTGYYDAOBVGI. You are receiving this because you modified the open/close state.Message ID: @.**@.>>