首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keras-tuner搜索函数抛出无法创建NewWriteableFile错误

Keras-tuner搜索函数抛出无法创建NewWriteableFile错误
EN

Stack Overflow用户
提问于 2019-12-22 03:10:51
回答 3查看 3.4K关注 0票数 5

tensorflow-2相对较新的keras-tuner模块导致了错误“无法创建NewWriteableFile”。tuner.search函数正在工作,只有在试验完成后才会抛出错误。这是来自sentdex Youtube频道的教程。

代码如下:

代码语言:javascript
复制
from tensorflow import keras
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Activation, Flatten
from kerastuner.tuners import RandomSearch
from kerastuner.engine.hyperparameters import HyperParameters
import matplotlib.pyplot as plt
import time

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

x_train = x_train[:1000].reshape(-1, 28, 28, 1)
x_test = x_test[:100].reshape(-1, 28, 28, 1)
y_train = y_train[:1000]
y_test = y_test[:100]
# x_train = x_train.reshape(-1, 28, 28, 1)
# x_test = x_test.reshape(-1, 28, 28, 1)

LOG_DIR = f"{int(time.time())}"


def build_model(hp):  
    model = keras.models.Sequential()
    model.add(Conv2D(hp.Int("layer1_channels", min_value=32,
            max_value=256, step=32), (3,3), input_shape=x_train.shape[1:]))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2)))
    for i in range(hp.Int("n_layers", 1, 4)):
        model.add(Conv2D(hp.Int(f"conv_{i}_channels", min_value=32,
            max_value=256, step=32), (3,3)))
    model.add(Flatten())
    model.add(Dense(10))
    model.add(Activation('softmax'))
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    return model

tuner = RandomSearch(build_model, 
                    objective = "val_accuracy", 
                    max_trials = 1, 
                    executions_per_trial = 1, 
                    directory = LOG_DIR,
                    project_name = 'junk')

tuner.search(x_train,
            y_train,
            epochs=1,
            batch_size=64,
            validation_data=(x_test, y_test))

这是回溯打印输出:

代码语言:javascript
复制
(tf_2.0) C:\Users\redex\OneDrive\Documents\Education\Sentdex Tutorials\Keras-Tuner>C:/Users/redex/Anaconda3/envs/tf_2.0/python.exe "c:/Users/redex/OneDrive/Documents/Education/Sentdex Tutorials/Keras-Tuner/keras-tuner.py"

2019-12-21 10:07:47.556531: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  AVX AVX2
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags. 
2019-12-21 10:07:47.574699: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 8. Tune using inter_op_parallelism_threads for best performance.

Train on 1000 samples, validate on 100 samples
 960/1000 [===========================>..] - ETA: 0s - loss: 64.0616 - accuracy: 0.2844

2019-12-21 10:07:55.080024: W tensorflow/core/framework/op_kernel.cc:1622] OP_REQUIRES failed at save_restore_v2_ops.cc:109 : Not found: Failed to create a NewWriteableFile: 1576951667\junk\trial_c5a5436b1d28a85446ce55c8d13f9657\checkpoints\epoch_0\checkpoint_temp_8a230a5ae2d046098456d1fdfc696690/part-00000-of-00001.data-00000-of-00001.tempstate15377864750281844169 : The system cannot find the path specified.
; No such process
Traceback (most recent call last):
  File "c:/Users/redex/OneDrive/Documents/Education/Sentdex Tutorials/Keras-Tuner/keras-tuner.py", line 65, in <module>
    validation_data=(x_test, y_test))
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\kerastuner\engine\base_tuner.py", line 122, in search
    self.run_trial(trial, *fit_args, **fit_kwargs)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\kerastuner\engine\multi_execution_tuner.py", line 95, in run_trial
    history = model.fit(*fit_args, **fit_kwargs, callbacks=callbacks)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 372, in fit
    prefix='val_')
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\contextlib.py", line 119, in __exit__
    next(self.gen)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 685, in on_epoch
    self.callbacks.on_epoch_end(epoch, epoch_logs)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 298, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 965, in on_epoch_end
    self._save_model(epoch=epoch, logs=logs)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 999, in _save_model
    self.model.save_weights(filepath, overwrite=True)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1090, in save_weights
    self._trackable_saver.save(filepath, session=session)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\training\tracking\util.py", line 1155, in save
    file_prefix=file_prefix_tensor, object_graph_tensor=object_graph_tensor)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\training\tracking\util.py", line 1103, in _save_cached_when_graph_building
    save_op = saver.save(file_prefix)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\training\saving\functional_saver.py", line 230, in save
    sharded_saves.append(saver.save(shard_prefix))
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\training\saving\functional_saver.py", line 72, in save
    return io_ops.save_v2(file_prefix, tensor_names, tensor_slices, tensors)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\ops\gen_io_ops.py", line 1932, in save_v2
    ctx=_ctx)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\ops\gen_io_ops.py", line 1969, in save_v2_eager_fallback
    ctx=_ctx, name=name)
  File "C:\Users\redex\Anaconda3\envs\tf_2.0\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a NewWriteableFile: 1576951667\junk\trial_c5a5436b1d28a85446ce55c8d13f9657\checkpoints\epoch_0\checkpoint_temp_8a230a5ae2d046098456d1fdfc696690/part-00000-of-00001.data-00000-of-00001.tempstate15377864750281844169 : The system cannot find the path specified.
; No such process [Op:SaveV2]

我的机器是Windows10,keras-tuner文档指定TensorFlow2.0和Python3.6,但我使用的是3.7.4。我想最近的就可以了。我不是软件专家,所以这就是我所知道的一切,任何帮助都是感激的。

EN

回答 3

Stack Overflow用户

发布于 2020-02-28 17:10:01

我在Windows中使用kerastuner时遇到了类似的问题,我已经解决了它:

  1. 第一个问题是日志目录的路径可能太长。我不得不减少它。
  2. 第二个问题是python (或tf)不能在视窗系统中使用混合斜杠。但是kerastuner使用反斜杠形成路径。因此,我应该为路径提供反斜杠。我已经用os.path.normpath()方法做到了这一点:

代码语言:javascript
复制
tuner=RandomSearch(build_model,objective='val_accuracy',max_trials=10,directory=os.path.normpath('C:/'))
tuner.search(x_train,y_train,batch_size=256,epochs=30,validation_split=0.2,verbose=1)    

现在我没有收到这个错误。

票数 5
EN

Stack Overflow用户

发布于 2020-01-14 16:49:08

在我的例子中,路径超过了windows中路径的最大长度,因为Keras Turner生成的路径长度约为170。在我把文件夹变短之后,它就能正常工作了。

票数 4
EN

Stack Overflow用户

发布于 2019-12-25 14:29:51

它将出现的问题是Windows问题。在Linux环境中运行相同的代码在这方面没有问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59439124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档