python - Keras tensorflow 已经存在错误?

标签 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 的人,但我现在不知道该怎么办

最佳答案

我建议您将代码转换为更惯用的内容。

而不是

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。

关于python - Keras tensorflow 已经存在错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925653/

相关文章:

python - 如何修复三重引号 ""上的 python 无效语法“

python - AttributeError:模块 'tensorboard' 没有属性 'lazy'

python - 当自定义命名空间为对象时,未设置默认 argparse 参数值

python - 带有 Amazon S3 的 django-storages - 防止覆盖

python - 雪茄 - 10/Unpickle

tensorflow - 如何从内存地址加载 tensorflow 图

tensorflow - 将字数向量逆变换为原始文档

tensorflow - 在预测期间从 Keras/Tensorflow 获得中间输出

tensorflow - 将层的输出作为特征向量(KERAS)

python - 错误(theano.gof.opt): Optimization failure due to: constant_folding