Keras Tensorflow AlreadyExistsError?

时间:2019-02-28 12:22:13

标签: python tensorflow keras jupyter-notebook

我需要帮助,以解决此AlreadyExistsError问题 我是该技术的新手,因此不胜感激。 它在model.fit上抛出错误 ,在互联网上我找不到任何可以帮助我的东西 因此有关此代码的任何信息都是有用的

import tensorflow as tf
from tensorflow import keras
from langdetect import detect
from nltk.tokenize import sent_tokenize
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.utils import np_utils
from keras import optimizers

引发错误的代码

#This trains the model batching from the text file
#every epoch it prints out 300 characters at different "temperatures"
#temperature controls how random the characters sample: more temperature== more crazy (but often better) text
for iteration in range(1, 20):
    print()
    print('-' * 50)
    print('Iteration', iteration)
    with open("short_reviews_shuffle.txt",encoding="utf8") as f:
        for chunk in iter(lambda: f.read(90000), ""):
            X, y = getDataFromChunk(chunk)
            model.fit(X, y, batch_size=128, epochs=1, callbacks=None)

     # Select a text seed at random
    start_index = random.randint(0, len(text) - maxlen - 1)
    generated_text = text[start_index: start_index + maxlen]
    print('--- Generating with seed: "' + generated_text + '"')

    for temperature in [0.5, 0.8, 1.0]:
        print('------ temperature:', temperature)
        sys.stdout.write(generated_text)

        # We generate 300 characters
        for i in range(300):
            sampled = np.zeros((1, maxlen, len(chars)))
            for t, char in enumerate(generated_text):
                sampled[0, t, char_indices[char]] = 1.

            preds = model.predict(sampled, verbose=0)[0]
            next_index = sample(preds, temperature)
            next_char = chars[next_index]

            generated_text += next_char
            generated_text = generated_text[1:]

            sys.stdout.write(next_char)
            sys.stdout.flush()
        print()

这是我得到的错误以及遇到的问题。

AlreadyExistsError                        Traceback (most recent call last)
<ipython-input-27-80576e87ab39> in <module>
      9         for chunk in iter(lambda: f.read(90000), ""):
     10             X, y = getDataFromChunk(chunk)
---> 11             model.fit(X, y, batch_size=128, epochs=1, callbacks=None)
     12 
     13      # Select a text seed at random

c:\users\inias somers\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
   1637           initial_epoch=initial_epoch,
   1638           steps_per_epoch=steps_per_epoch,
-> 1639           validation_steps=validation_steps)
   1640 
   1641   def evaluate(self,

c:\users\inias somers\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in fit_loop(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps)
    213           ins_batch[i] = ins_batch[i].toarray()
    214 
--> 215         outs = f(ins_batch)
    216         if not isinstance(outs, list):
    217           outs = [outs]

c:\users\inias somers\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\keras\backend.py in __call__(self, inputs)
   2984 
   2985     fetched = self._callable_fn(*array_vals,
-> 2986                                 run_metadata=self.run_metadata)
   2987     self._call_fetch_callbacks(fetched[-len(self._fetches):])
   2988     return fetched[:len(self.outputs)]

c:\users\inias somers\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args, **kwargs)
   1437           ret = tf_session.TF_SessionRunCallable(
   1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
   1440         if run_metadata:
   1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

c:\users\inias somers\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

AlreadyExistsError: Resource __per_step_6/training/Adam/gradients/lstm/while/ReadVariableOp_8/Enter_grad/ArithmeticOptimizer/AddOpsRewrite_Add/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
     [[{{node training/Adam/gradients/lstm/while/ReadVariableOp_8/Enter_grad/ArithmeticOptimizer/AddOpsRewrite_Add/tmp_var}} = TemporaryVariable[dtype=DT_FLOAT, shape=[1024,4096], var_name="training/A...dd/tmp_var", _device="/job:localhost/replica:0/task:0/device:CPU:0"](^training/Adam/gradients/lstm/while/strided_slice_11_grad/StridedSliceGrad)]]

某人可以帮助我与keras融为一体,我现在不该做什么

1 个答案:

答案 0 :(得分:2)

我建议您将代码转换为更惯用的语言。

代替

for chunk in iter(lambda: f.read(90000), ""):
    X, y = getDataFromChunk(chunk)
    model.fit(X, y, batch_size=128, epochs=1, callbacks=None)

创建一个生成数据的生成器并使用fit_generator方法。

与其执行20次迭代的顶部循环,不如使用回调函数评估您的预测,并使用epochs = 20调用fit_generator。