Allocation of 179437568 exceeds 10% of free system memory.

简介: 本文讨论了在Python编程中遇到的"Allocation of XXXX exceeds 10% of free system memory"错误,并提供了几种解决方法,包括调整代码逻辑以减少内存分配和更改批量大小。

第一步:加载keras中的mnist集

from keras.datasets import mnist
(train_images, train_labels),(test_images, test_labels) = mnist.load_data()

print(train_images.shape)#查看效果的,这两步可以忽略
print(test_images.shape)

熟悉一下matplotlib.pyplot数据显示

import matplotlib.pyplot as plt
plt.figure()#新建一个图层
plt.imshow(train_images[0],cmap=“gray”)
plt.show()

第二步:建立网络架构,

from keras import models
from keras import layers

层layer就像数据处理的筛子

本例子含有两个Dense层,最后是一个10路的激活层softmax层,返回一个由10个概率值组成的数组,表示10个数字类别中某一个的概率

network=models.Sequential()
network.add(layers.Dense(512,activation=‘relu’,input_shape=(28*28,)))
network.add(layers.Dense(10,activation=‘softmax’))

第三步:编译

optimizer 优化器,loss损失函数,metrics代码级数据监控

network.compile(optimizer=‘rmsprop’,loss=‘categorical_crossentropy’,metrics=[‘accuracy’])

第四步:准备图像数据和标签

train_images = train_images.reshape((60000, 28 * 28))#图像处理
train_images = train_images.astype(‘float32’) / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype(‘float32’) / 255

通过二维关系矩阵的方式,生成一个对应关系

from keras.utils.all_utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

第五步:拟合(FIT)模型

network.fit(train_images,train_labels,batch_size=64,epochs=5)

第六步:评估(EVALUATE)模型

test_loss,test_acc=network.evaluate(test_images,test_labels)

第七步:查看测试集的精度

print(‘test_acc’,test_acc)

训练精度和测试精度之间的这种差距是过拟合(overfit)造成的

相关文章
|
29天前
|
JSON PHP 数据格式
PHP Allowed memory size of 134217728 bytes exhausted (tried to allocate 10489856 bytes)
PHP Allowed memory size of 134217728 bytes exhausted (tried to allocate 10489856 bytes)
35 2
|
1月前
|
Android开发
Out of memory on a 11111-byte allocation
Out of memory on a 11111-byte allocation
26 1
|
4月前
PGA memory operation
PGA memory operation
48 1
|
11月前
|
Java 应用服务中间件
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
133 0
|
4月前
|
安全 数据处理 C#
深入理解C#中的Span<T>和Memory<T>
【1月更文挑战第8天】本文旨在探讨C#中引入的两个重要类型:Span<T>和Memory<T>。它们为开发者提供了一种高效且安全的方式来处理内存中的数据。文章首先介绍这两个类型的基本概念和用途,接着深入分析它们的工作原理和适用场景,并通过代码示例展示如何在实际应用中使用它们。
|
Docker 容器
解决Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
解决Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
1102 0
|
Java 应用服务中间件
The field file exceeds its maximum permitted size of 1048576 bytes.
The field file exceeds its maximum permitted size of 1048576 bytes.