我正在尝试使用 Python 中的 Theano 库对 Deep Belief Networks 进行一些实验。我使用这个地址中的代码:DBN full code。此代码使用MNIST 手写数据库。这个文件已经是pickle格式了。它未选中:
- 动车组
- 有效集
- 测试集
在以下内容中进一步未腌制:
- train_set_x, train_set_y = train_set
- 有效集合 x,有效集合 y = 有效集合
- test_set_x, test_set_y = test_set
请问有人可以给我构造这个数据集的代码以便创建我自己的吗?我使用的 DBN 示例需要这种格式的数据,我不知道该怎么做。如果有人对如何解决此问题有任何想法,请告诉我。
这是我的代码:
from datetime import datetime
import time
import os
from pprint import pprint
import numpy as np
import gzip, cPickle
import theano.tensor as T
from theano import function
os.system("cls")
filename = "completeData.txt"
f = open(filename,"r")
X = []
Y = []
for line in f:
line = line.strip('\n')
b = line.split(';')
b[0] = float(b[0])
b[1] = float(b[1])
b[2] = float(b[2])
b[3] = float(b[3])
b[4] = float(b[4])
b[5] = float(b[5])
b[6] = float(b[6])
b[7] = float(b[7])
b[8] = float(b[8])
b[9] = float(b[9])
b[10] = float(b[10])
b[11] = float(b[11])
b[12] = float(b[12])
b[13] = float(b[13])
b[14] = float(b[14])
b[15] = float(b[15])
b[17] = int(b[17])
X.append(b[:16])
Y.append(b[17])
Len = len(X);
X = np.asmatrix(X)
Y = np.asarray(Y)
sizes = [0.8, 0.1, 0.1]
arr_index = int(sizes[0]*Len)
arr_index2_start = arr_index + 1
arr_index2_end = arr_index + int(sizes[1]*Len)
arr_index3_start = arr_index2_start + 1
"""
train_set_x = np.array(X[:arr_index])
train_set_y = np.array(Y[:arr_index])
val_set_x = np.array(X[arr_index2_start:arr_index2_end])
val_set_y = np.array(Y[arr_index2_start:arr_index2_end])
test_set_x = np.array(X[arr_index3_start:])
test_set_y = np.array(X[arr_index3_start:])
train_set = train_set_x, train_set_y
val_set = val_set_x, val_set_y
test_set = test_set_x, test_set_y
"""
x = T.dmatrix('x')
z = x
t_mat = function([x],z)
y = T.dvector('y')
k = y
t_vec = function([y],k)
train_set_x = t_mat(X[:arr_index].T)
train_set_y = t_vec(Y[:arr_index])
val_set_x = t_mat(X[arr_index2_start:arr_index2_end].T)
val_set_y = t_vec(Y[arr_index2_start:arr_index2_end])
test_set_x = t_mat(X[arr_index3_start:].T)
test_set_y = t_vec(Y[arr_index3_start:])
train_set = train_set_x, train_set_y
val_set = val_set_x, val_set_y
test_set = test_set_x, test_set_y
dataset = [train_set, val_set, test_set]
f = gzip.open('..\..\..\data\dex.pkl.gz','wb')
cPickle.dump(dataset, f, protocol=-1)
f.close()
pprint(train_set_x.shape)
print('Finished\n')