python - 如何正确保存.npz格式的字典

标签 python numpy

我正在编写一个程序,该程序加载 npz 文件,对值进行一些更改并再次存储它。 .npz 文件的形状(尺寸)各不相同。

我已经到了可以做所有事情的地步,除了个别 numpy 数组乱序之外。

代码流程:

data = dict(np.load(my_npz_file))

#Do some changes to dictionary

data_to_store = [data[layer] for layer in data]
np.savez(output_npz_file, *data_to_store)

但是问题是这样的,顺序丢失了。

打印代码:

keys=data.keys()
    for i,h in enumerate(keys):
        print "i="+str(i)+"h="+str(h)+"shape="+str(np.shape(data[h]))

读取的数据格式:

i=0h=arr_24shape=(128, 32, 3, 3)
i=1h=arr_25shape=(128,)
i=2h=arr_26shape=(48, 256, 1, 1)
i=3h=arr_27shape=(48,)
i=4h=arr_20shape=(32, 256, 1, 1)
i=5h=arr_21shape=(32,)
i=6h=arr_22shape=(128, 32, 1, 1)
i=7h=arr_23shape=(128,)
i=8h=arr_28shape=(192, 48, 1, 1)
i=9h=arr_29shape=(192,)
i=10h=arr_46shape=(256, 64, 1, 1)
i=11h=arr_47shape=(256,)
i=12h=arr_44shape=(64, 512, 1, 1)
i=13h=arr_45shape=(64,)
i=14h=arr_42shape=(256, 64, 3, 3)
i=15h=arr_43shape=(256,)
i=16h=arr_40shape=(256, 64, 1, 1)
i=17h=arr_41shape=(256,)
i=18h=arr_48shape=(256, 64, 3, 3)
i=19h=arr_49shape=(256,)
i=20h=arr_33shape=(48,)
i=21h=arr_32shape=(48, 384, 1, 1)
i=22h=arr_31shape=(192,)
i=23h=arr_30shape=(192, 48, 3, 3)
i=24h=arr_37shape=(192,)
i=25h=arr_36shape=(192, 48, 3, 3)
i=26h=arr_35shape=(192,)
i=27h=arr_34shape=(192, 48, 1, 1)
i=28h=arr_39shape=(64,)
i=29h=arr_38shape=(64, 384, 1, 1)
i=30h=arr_19shape=(128,)
i=31h=arr_18shape=(128, 32, 3, 3)
i=32h=arr_51shape=(1000,)
i=33h=arr_50shape=(1000, 512, 1, 1)
i=34h=arr_11shape=(64,)
i=35h=arr_10shape=(64, 16, 1, 1)
i=36h=arr_13shape=(64,)
i=37h=arr_12shape=(64, 16, 3, 3)
i=38h=arr_15shape=(32,)
i=39h=arr_14shape=(32, 128, 1, 1)
i=40h=arr_17shape=(128,)
i=41h=arr_16shape=(128, 32, 1, 1)
i=42h=arr_1shape=(96,)
i=43h=arr_0shape=(96, 3, 7, 7)
i=44h=arr_3shape=(16,)
i=45h=arr_2shape=(16, 96, 1, 1)
i=46h=arr_5shape=(64,)
i=47h=arr_4shape=(64, 16, 1, 1)
i=48h=arr_7shape=(64,)
i=49h=arr_6shape=(64, 16, 3, 3)
i=50h=arr_9shape=(16,)
i=51h=arr_8shape=(16, 128, 1, 1)

数据输出格式:

i=0h=arr_24shape=(192,)
i=1h=arr_25shape=(192, 48, 3, 3)
i=2h=arr_26shape=(192,)
i=3h=arr_27shape=(192, 48, 1, 1)
i=4h=arr_20shape=(48,)
i=5h=arr_21shape=(48, 384, 1, 1)
i=6h=arr_22shape=(192,)
i=7h=arr_23shape=(192, 48, 3, 3)
i=8h=arr_28shape=(64,)
i=9h=arr_29shape=(64, 384, 1, 1)
i=10h=arr_46shape=(64,)
i=11h=arr_47shape=(64, 16, 1, 1)
i=12h=arr_44shape=(16,)
i=13h=arr_45shape=(16, 96, 1, 1)
i=14h=arr_42shape=(96,)
i=15h=arr_43shape=(96, 3, 7, 7)
i=16h=arr_40shape=(128,)
i=17h=arr_41shape=(128, 32, 1, 1)
i=18h=arr_48shape=(64,)
i=19h=arr_49shape=(64, 16, 3, 3)
i=20h=arr_33shape=(1000, 512, 1, 1)
i=21h=arr_32shape=(1000,)
i=22h=arr_31shape=(128, 32, 3, 3)
i=23h=arr_30shape=(128,)
i=24h=arr_37shape=(64, 16, 3, 3)
i=25h=arr_36shape=(64,)
i=26h=arr_35shape=(64, 16, 1, 1)
i=27h=arr_34shape=(64,)
i=28h=arr_39shape=(32, 128, 1, 1)
i=29h=arr_38shape=(32,)
i=30h=arr_19shape=(256,)
i=31h=arr_18shape=(256, 64, 3, 3)
i=32h=arr_51shape=(16, 128, 1, 1)
i=33h=arr_50shape=(16,)
i=34h=arr_11shape=(256,)
i=35h=arr_10shape=(256, 64, 1, 1)
i=36h=arr_13shape=(64,)
i=37h=arr_12shape=(64, 512, 1, 1)
i=38h=arr_15shape=(256,)
i=39h=arr_14shape=(256, 64, 3, 3)
i=40h=arr_17shape=(256,)
i=41h=arr_16shape=(256, 64, 1, 1)
i=42h=arr_1shape=(128,)
i=43h=arr_0shape=(128, 32, 3, 3)
i=44h=arr_3shape=(48,)
i=45h=arr_2shape=(48, 256, 1, 1)
i=46h=arr_5shape=(32,)
i=47h=arr_4shape=(32, 256, 1, 1)
i=48h=arr_7shape=(128,)
i=49h=arr_6shape=(128, 32, 1, 1)
i=50h=arr_9shape=(192,)
i=51h=arr_8shape=(192, 48, 1, 1)

如您所见,没有数据丢失,但数据出现故障。

最佳答案

那是因为一开始你将数据转换为字典:

data = dict(np.load(my_npz_file))

Python 中的字典不保留顺序 ( at least in your Python version ),但您可以使用 OrderedDict .

<小时/>

更新:确切的问题在这里......

data_to_store = [data[layer] for layer in data]
np.savez(output_npz_file, *data_to_store)

您创建数据中所有层的列表,然后以随机顺序迭代它们并将它们写入文件中。因此,之前称为 arr_0 的内容现在将成为 arr_23,因为这就是它最终会随机遍历数据的方式,而 np.savez > 只会分配新的连续名称。

但是您也可以向 np.savez 提供您自己的名称,这将大大简化您的代码:

np.savez(output_npz_file, **data)  # data is a dict here

这将以与原来相同的名称保存每个图层。

关于python - 如何正确保存.npz格式的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058500/

相关文章:

python - Python numpy 数组(和 Pandas 索引)中不区分大小写的逻辑

Python 尝试/排除

python - 在 python 中将数据分类到 bin 的有效方法

python - 元素不可点击,因为另一个元素遮挡了它

python - 如何 numpy.nan*0=0

arrays - 如何将数组a转换为下面的数组b?

python - 不使用 numpy.meshgrid 方法创建等高线图?

python - 行和列的 ndarray 字段名称?

python - cx_Oracle : How do I connect to Oracle when you use a Wallet?

python - numpy:用不同的起点分割数组