# Path: utils.py
# def zero_mean_unit_variance(Data):
#     """ Scales all values in the ndarray ndar to be between 0 and 1 """
#     Mean = numpy.mean(Data, axis=0)
#     Data  -=  Mean
# 
#     Std = numpy.std(Data, axis = 0)
#     index = (numpy.abs(Std<10**-5))
#     Std[index] = 1
#     Data /= Std
#     return [Data, Mean, Std]
# 
# Path: utils.py
# def normalize(Data, Mean, Std):
#     Data -= Mean
#     Data /= Std
#     return Data


import cPickle
import gzip
import os
import sys
import time
import numpy
import sys
import theano
import theano.tensor as T
    import cPickle as pickle
                import warnings
from theano.tensor.shared_randomstreams import RandomStreams
from logistic_sgd import LogisticRegression
from mlp import HiddenLayer
from rbm import RBM
from grbm import GBRBM
from utils import zero_mean_unit_variance
from utils import normalize
from GRBM_DBN import GRBM_DBN
from sklearn import preprocessing
    from time import gmtime, strftime

    # we separate the dataset into training: 80%, validation: 10%, testing: 10%
    train_end = int(rand_num.shape[0]*ratio_train)
    valid_end = int(rand_num.shape[0]*(ratio_train+ration_valid))

    # Wudi made it a small set:
    train_set_feature = Feature_all[0:train_end,:]#ÑµÁ·¼¯ºÏ£¬Ö»ÓÃÁËËùÓÐÌØÕ÷µÄ80%
    train_set_new_target = Target_all_numeric[0:train_end]#ÕâÀï·µ»ØÊÇ¶¯×÷idµÄÐòºÅ

    # Wudi added normalized data for GRBM
    [train_set_feature_normalized, Mean1, Std1]  = preprocessing.scale(train_set_feature)
    #numpyµÄ·½·¨£¬¼ÆËã¾ùÖµºÍ·½²î£¬ ¹éÒ»»¯µÄ½á¹û

    #±£´æ¹éÒ»»¯µÄ½á¹û
    f = open('SK_normalization.pkl','wb')
    pickle.dump( {"Mean1": Mean1, "Std1": Std1 },f)
    f.close()

    train_set_x, train_set_y = shared_dataset( (train_set_feature_normalized, train_set_new_target))
    #ÕâÀïÖ±½ÓÒ»¸ö£¨£© ¾Í½«Êý¾Ý´ò°üÎªÒ»¸ö tuple ÁË

    #½«ÑéÖ¤Êý¾ÝÒ²¹éÒ»»¯
    valid_set_feature = Feature_all[train_end:valid_end,:]
    valid_set_new_target = Target_all_numeric[train_end:valid_end]#ÕâÊÇ´Ó000100ÐòÁÐ Êý×ÖÐòºÅ»°µÄ½á¹û
    valid_set_feature = normalize(valid_set_feature, Mean1, Std1)
    valid_set_x, valid_set_y = shared_dataset((valid_set_feature,valid_set_new_target))

    # test feature set
    test_set_feature = Feature_all[valid_end:,:]
    test_set_new_target = Target_all_numeric[valid_end:]

# Path: utils.py
# def zero_mean_unit_variance(Data):
#     """ Scales all values in the ndarray ndar to be between 0 and 1 """
#     Mean = numpy.mean(Data, axis=0)
#     Data  -=  Mean
# 
#     Std = numpy.std(Data, axis = 0)
#     index = (numpy.abs(Std<10**-5))
#     Std[index] = 1
#     Data /= Std
#     return [Data, Mean, Std]
# 
# Path: utils.py
# def normalize(Data, Mean, Std):
#     Data -= Mean
#     Data /= Std
#     return Data


# Path: Step1_DBN_Structure2.py
import cPickle
import gzip
import os
import sys
import time
import numpy
import sys
import theano
import theano.tensor as T
    import cPickle as pickle
                import warnings
from theano.tensor.shared_randomstreams import RandomStreams
from logistic_sgd import LogisticRegression
from mlp import HiddenLayer
from rbm import RBM
from grbm import GBRBM
from utils import zero_mean_unit_variance
from utils import normalize
from GRBM_DBN import GRBM_DBN
from sklearn import preprocessing
    from time import gmtime, strftime


    # we separate the dataset into training: 80%, validation: 10%, testing: 10%
    train_end = int(rand_num.shape[0]*ratio_train)
    valid_end = int(rand_num.shape[0]*(ratio_train+ration_valid))

    # Wudi made it a small set:
    train_set_feature = Feature_all[0:train_end,:]#ÑµÁ·¼¯ºÏ£¬Ö»ÓÃÁËËùÓÐÌØÕ÷µÄ80%
    train_set_new_target = Target_all_numeric[0:train_end]#ÕâÀï·µ»ØÊÇ¶¯×÷idµÄÐòºÅ

    # Wudi added normalized data for GRBM
    [train_set_feature_normalized, Mean1, Std1]  = preprocessing.scale(train_set_feature)
    #numpyµÄ·½·¨£¬¼ÆËã¾ùÖµºÍ·½²î£¬ ¹éÒ»»¯µÄ½á¹û

    #±£´æ¹éÒ»»¯µÄ½á¹û
    f = open('SK_normalization.pkl','wb')
    pickle.dump( {"Mean1": Mean1, "Std1": Std1 },f)
    f.close()

    train_set_x, train_set_y = shared_dataset( (train_set_feature_normalized, train_set_new_target))
    #ÕâÀïÖ±½ÓÒ»¸ö£¨£© ¾Í½«Êý¾Ý´ò°üÎªÒ»¸ö tuple ÁË

    #½«ÑéÖ¤Êý¾ÝÒ²¹éÒ»»¯
    valid_set_feature = Feature_all[train_end:valid_end,:]
    valid_set_new_target = Target_all_numeric[train_end:valid_end]#ÕâÊÇ´Ó000100ÐòÁÐ Êý×ÖÐòºÅ»°µÄ½á¹û
    valid_set_feature = normalize(valid_set_feature, Mean1, Std1)
    valid_set_x, valid_set_y = shared_dataset((valid_set_feature,valid_set_new_target))

    # test feature set
    test_set_feature = Feature_all[valid_end:,:]
    test_set_new_target = Target_all_numeric[valid_end:]

    test_set_feature = normalize(test_set_feature, Mean1, Std1)