Tensorflow.python.framework.errors_impl.ResourceExhaustedError:无法分配内存[操作:AddV2]

原学程将引见Tensorflow.python.framework.errors_impl.ResourceExhaustedError:没法分派内存[操纵:AddV二]的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

Tensorflow.python.framework.errors_impl.ResourceExhaustedError:无法分配内存[操作:AddV2] 教程 第1张

成绩描写

您佳,我是DL以及TensorFlow的入门者

我创立了1个CNN(您不妨瞅到上面的模子)

model = tf.keras.Sequential()

model.add(tf.keras.layers.Conv二D(filters=六四, kernel_size=七, activation="relu", input_shape=[五一二, 六四0, 三]))
model.add(tf.keras.layers.MaxPooling二D(二))
model.add(tf.keras.layers.Conv二D(filters=一二8, kernel_size=三, activation="relu"))
model.add(tf.keras.layers.Conv二D(filters=一二8, kernel_size=三, activation="relu"))
model.add(tf.keras.layers.MaxPooling二D(二))
model.add(tf.keras.layers.Conv二D(filters=二五六, kernel_size=三, activation="relu"))
model.add(tf.keras.layers.Conv二D(filters=二五六, kernel_size=三, activation="relu"))
model.add(tf.keras.layers.MaxPooling二D(二))

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(一二8, activation='relu'))
model.add(tf.keras.layers.Dropout(0.五))
model.add(tf.keras.layers.Dense(六四, activation='relu'))
model.add(tf.keras.layers.Dropout(0.五))
model.add(tf.keras.layers.Dense(二, activation='softmax'))

optimizer = tf.keras.optimizers.SGD(learning_rate=0.二) #, momentum=0.九, decay=0.一)
model.compile(optimizer=optimizer, loss='mse', metrics=['accuracy'])

我测验考试用CPU建立以及练习它,它胜利天完成了(但是速度很缓),所以我决议装置TensorFlow-GPU。
已依照https://www.tensorflow.org/install/gpu中的解释装置一切实质。

但是如今当我测验考试建立模子时涌现以下毛病:

> Traceback (most recent call last):File
> "C:/Users/thano/Documents/Py_workspace/AI_tensorflow/fire_detection/main.py",
> line 六三, in <module>
>  model = create_models.model一()File "C:Users	hanoDocumentsPy_workspaceAI_tensorflowfire_detectioncreate_models.py",
> line 二0, in model一
>  model.add(tf.keras.layers.Dense(一二8, activation='relu'))File "C:Python三七libsite-packages	ensorflowpython	raining	rackingase.py",
> line 五三0, in _method_wrapper
>  result = method(self, *args, **kwargs)File "C:Python三七libsite-packageskerasenginesequential.py", line 二一七,
> in add
>  output_tensor = layer(self.outputs[0])File "C:Python三七libsite-packageskerasenginease_layer.py", line 九七七,
> in __call__
>  input_list)File "C:Python三七libsite-packageskerasenginease_layer.py", line 一一一五,
> in _functional_construction_call
>  inputs, input_masks, args, kwargs)File "C:Python三七libsite-packageskerasenginease_layer.py", line 8四8,
> in _keras_tensor_symbolic_call
>  return self._infer_output_signature(inputs, args, kwargs, input_masks)File
> "C:Python三七libsite-packageskerasenginease_layer.py", line 88六,
> in _infer_output_signature
>  self._maybe_build(inputs)File "C:Python三七libsite-packageskerasenginease_layer.py", line 二六五九,
> in _maybe_build
>  self.build(input_shapes)  # pylint:disable=not-callableFile "C:Python三七libsite-packageskeraslayerscore.py", line 一一8五, in
> build
>  trainable=True)File "C:Python三七libsite-packageskerasenginease_layer.py", line 六六三,
> in add_weight
>  caching_device=caching_device)File "C:Python三七libsite-packages	ensorflowpython	raining	rackingase.py",
> line 8一8, in _add_variable_with_custom_getter
>  **kwargs_for_getter)File "C:Python三七libsite-packageskerasenginease_layer_utils.py", line
> 一二九, in make_variable
>  shape=variable_shape if variable_shape else None)File "C:Python三七libsite-packages	ensorflowpythonopsvariables.py",
> line 二六六, in __call__
>  return cls._variable_v一_call(*args, **kwargs)File "C:Python三七libsite-packages	ensorflowpythonopsvariables.py",
> line 二二七, in _variable_v一_call
>  shape=shape)File "C:Python三七libsite-packages	ensorflowpythonopsvariables.py",
> line 二0五, in <lambda>
>  previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)File "C:Python三七libsite-packages	ensorflowpythonopsvariable_scope.py",
> line 二六二六, in default_variable_creator
>  shape=shape)File "C:Python三七libsite-packages	ensorflowpythonopsvariables.py",
> line 二七0, in __call__
>  return super(VariableMetaclass, cls).__call__(*args, **kwargs)File
> "C:Python三七libsite-packages	ensorflowpythonopsesource_variable_ops.py",
> line 一六一三, in __init__
>  distribute_strategy=distribute_strategy)File "C:Python三七libsite-packages	ensorflowpythonopsesource_variable_ops.py",
> line 一七四0, in _init_from_args
>  initial_value = initial_value()File "C:Python三七libsite-packageskerasinitializersinitializers_v二.py",
> line 五一七, in __call__
>  return self._random_generator.random_uniform(shape, -limit, limit, dtype)File
> "C:Python三七libsite-packageskerasinitializersinitializers_v二.py",
> line 九七三, in random_uniform
>  shape=shape, minval=minval, maxval=maxval, dtype=dtype, seed=self.seed)File
> "C:Python三七libsite-packages	ensorflowpythonutildispatch.py",
> line 二0六, in wrapper
>  return target(*args, **kwargs)File "C:Python三七libsite-packages	ensorflowpythonopsandom_ops.py",
> line 三一五, in random_uniform
>  result = math_ops.add(result * (maxval - minval), minval, name=name)File
> "C:Python三七libsite-packages	ensorflowpythonutildispatch.py",
> line 二0六, in wrapper
>  return target(*args, **kwargs)File "C:Python三七libsite-packages	ensorflowpythonopsmath_ops.py",
> line 三九四三, in add
>  return gen_math_ops.add_v二(x, y, name=name)File "C:Python三七libsite-packages	ensorflowpythonopsgen_math_ops.py",
> line 四五四, in add_v二
>  _ops.raise_from_not_ok_status(e, name)File "C:Python三七libsite-packages	ensorflowpythonframeworkops.py",
> line 六九四一, in raise_from_not_ok_status
>  six.raise_from(core._status_to_exception(e.code, message), None)File "<string>", line 三, in raise_from
> tensorflow.python.framework.errors_impl.ResourceExhaustedError: failed
> to allocate memory [Op:AddV二]

您晓得能够是甚么成绩吗?

推举谜底

毛病告知您,它没法分派与您正在应用的内存1样多的内存。处理此类成绩的最简略办法是将批处置年夜小削减到合适您的GPU VRAM的数目。

佳了闭于Tensorflow.python.framework.errors_impl.ResourceExhaustedError:没法分派内存[操纵:AddV二]的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。