我如何将 .pkl.gz 文件从我的计算机导入到 python 程序中

How would I import .pkl.gz file from my computer into a python program

我有自己的数据集,我想用它来训练我的模型。我已成功创建 .pk.gz 文件,但我不知道如何将它们导入我的模型。

我正在使用 windows 10、python 3.5.2 和 tensor-flow 以及 tflearn 和 sublime text 3 来编写代码。

我用来创建 pickle 文件的代码:

from numpy import genfromtxt

import gzip
import _pickle as cPickle

#data = sio.loadmat('C:/DeepLearning_lib/Theano/Data/test_x.mat')

train_set_x = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/Kdd_Train_41.csv', delimiter=',')

train_set_y = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/NSL_TrainLabels_mat4.csv', delimiter=',')

valid_set_x = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/Kdd_Valid_41.csv', delimiter=',')

valid_set_y = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/NSL_ValidLabels_int2.csv', delimiter=',')

test_set_x = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/Kdd_Test_41.csv', delimiter=',')

test_set_y = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/NSL_TestLabels_mat5.csv', delimiter=',')



train_set = test_set_x
train_set_labels= test_set_y

valid_set = valid_set_x
valid_set_labels= valid_set_y

test_set = train_set_x
test_set_labels= train_set_y


f = gzip.open('C:/Users/Jay/Desktop/Data/train_set.pkl.gz','wb')
cPickle.dump(train_set, f, protocol=2)

f.close()

f = gzip.open('C:/Users/Jay/Desktop/Data/train_set_labels.pkl.gz','wb')
cPickle.dump(train_set_labels, f, protocol=2)

f.close()

f = gzip.open('C:/Users/Jay/Desktop/Data/valid_set_labels.pkl.gz','wb')
cPickle.dump(valid_set_labels, f, protocol=2)

f.close()
f = gzip.open('C:/Users/Jay/Desktop/Data/test_set_labels.pkl.gz','wb')
cPickle.dump(test_set_labels, f, protocol=2)

f.close()
f = gzip.open('C:/Users/Jay/Desktop/Data/valid_set.pkl.gz','wb')
cPickle.dump(valid_set, f, protocol=2)

f.close()
f = gzip.open('C:/Users/Jay/Desktop/Data/test_set.pkl.gz','wb')
cPickle.dump(test_set, f, protocol=2)

f.close()

错误:使用时 'rb'

'OSError: [Errno 9] peek() on write-only GzipFile object'

下面的代码应该重建你的 train_set:

with gzip.open('C:/Users/Jay/Desktop/Data/train_set.pkl.gz', 'rb') as f:
  train_set = cPickle.load(f)