def set_up_noise_examples(self):

        if self.prior_noise_type == 'Gaussian':
            mean = np.zeros(self.latent_dim)
            covariance = np.eye(N=self.latent_dim) * 1.0
            self.positive_examples_training = generate_positive_samples(
                len(self.train_X),
                mean,
                covariance,
                'Gaussian',
                seed=np.random.randint(0, 2000))
            self.positive_examples_valid = generate_positive_samples(
                len(self.valid_X),
                mean,
                covariance,
                'Gaussian',
                seed=np.random.randint(0, 2000))
            self.positive_examples_test = generate_positive_samples(
                len(self.test_X),
                mean,
                covariance,
                'Gaussian',
                seed=np.random.randint(0, 2000))

        else:
            raise NotImplementedError()
Пример #2
0
    def set_up_dataset(self, dataset_obj):
        # save datasource to meta_data.txt
        with open('meta_data.txt', 'a') as file:
            file.writelines(dataset_obj.file_source)

        if hasattr(dataset_obj, 'max_len'):
            self.max_len = dataset_obj.max_len
        else:
            raise ValueError(
                "Attribute 'max_len' doesn't exist in dataset obj ")

        if hasattr(dataset_obj, 'dof'):
            self.dof = dataset_obj.dof - 1
        else:
            raise ValueError("Attribute 'dof' doesn't exist in dataset obj ")

        self.train_X = dataset_obj.train_X[:, :, 1:]
        self.train_Y1 = dataset_obj.train_Y1
        self.train_Y2 = dataset_obj.train_Y2
        self.train_Y3 = dataset_obj.train_Y3
        #self.train_speed = dataset_obj.train_speed
        print "training set size: {}".format(self.train_X.shape)

        self.valid_X = dataset_obj.valid_X[:, :, 1:]
        self.valid_Y1 = dataset_obj.valid_Y1
        self.valid_Y2 = dataset_obj.valid_Y2
        self.valid_Y3 = dataset_obj.valid_Y3
        #self.valid_speed = dataset_obj.valid_speed

        self.test_X = dataset_obj.test_X[:, :, 1:]
        self.test_Y1 = dataset_obj.test_Y1
        self.test_Y2 = dataset_obj.test_Y2
        self.test_Y3 = dataset_obj.test_Y3
        #self.test_speed = dataset_obj.test_speed
        self.max_vector = dataset_obj.max_vector[1:]
        self.min_vector = dataset_obj.min_vector[1:]

        self.postprocess = dataset_obj.postprocess
        self.sampling_interval = dataset_obj.sampling_interval
        #set up noise vector to generate seqeunces

        if self.prior_noise_type == 'Gaussian':
            mean = np.zeros(self.latent_dim)
            covariance = np.eye(N=self.latent_dim) * 1.0
            self.noise_vectors = generate_positive_samples(self.nb_to_generate,
                                                           mean,
                                                           covariance,
                                                           'Gaussian',
                                                           seed=1234)

        else:
            raise NotImplementedError()
    encoder_seq2seq = encoder(max_len=200,dof=70,hidden_dim_enc_list=[100,100],activation_enc_list=['tanh','tanh'],
                              latent_BN=False,latent_dim=50,latent_activation='tanh')
    encoder_seq2seq.summary()
    encoder_seq2seq.load_weights(expr021_path+'encoder.h5')

    latent_codes_seq2seq = encoder_seq2seq.predict(x=train_X,batch_size=1000)
    mean_seq2seq = np.mean(latent_codes_seq2seq,axis=0)
    cov_seq2seq = np.cov(latent_codes_seq2seq,rowvar=False)
    print "shape of mean of seq2seq is {}".format(mean_seq2seq.shape)
    print "shape of covariance of seq2seq is {}".format(cov_seq2seq.shape)

    #sample z from prior distribution p(z)~N(0,1.0),and p(z)~N(mean,cov)
    mean_saae = np.zeros(50)
    cov_saae = np.eye(N=50) * 1.0

    z_saae= generate_positive_samples(N_g, mean_saae, cov_saae, 'Gaussian',
                                                                seed=np.random.randint(0, 2000))

    z_seq2seq = generate_positive_samples(N_g, mean_seq2seq, cov_seq2seq, 'Gaussian',
                                       seed=np.random.randint(0, 2000))


    #generate sequences from sampled z
    seq_saae = decoder_saae.predict(x=z_saae)
    seq_seq2seq = decoder_seq2seq.predict(x=z_seq2seq)
    print "shape of seq_saae is {}".format(seq_saae.shape)
    print "shape of seq_seq2seq is {}".format(seq_seq2seq.shape)
    #truncate sequences of 200 frames into ones of 10 frames and flatten them into a vector

    N = seq_saae.shape[0]
    samples_saae = np.zeros(shape=(N,70*200/tt),dtype=np.float32)
    samples_seq2seq = np.zeros(shape=(N, 70 * 200/tt), dtype=np.float32)
Пример #4
0
    args = parser.parse_args()

    ##generate noise vectors
    if args.seq2seq is True:
        mean_cov = np.load('mean_cov.npz', 'r')
        names = mean_cov.files
        mean = mean_cov[names[1]]
        covariance = mean_cov[names[0]]
    else:
        mean = np.zeros(50)
        covariance = np.eye(N=50) * 1.0
    print 'shape of mean {}'.format(mean.shape)
    print 'shape of cov {}'.format(covariance.shape)
    noise_vectors = generate_positive_samples(args.nb_generated,
                                              mean, covariance,
                                              'Gaussian', seed=2345)
    data_obj = Emilya_Dataset(window_width=200, shift_step=20, sampling_interval=None, with_velocity=False,
                              number=None, nb_valid=None, nb_test=None)
    print data_obj.test_X.shape
    X = data_obj.test_X[:, :, 1:]
    print "size of all real seqs is {}".format(X.shape)
    Y1 = data_obj.test_Y1[:]
    Y2 = data_obj.test_Y2[:]
    Y1 = convert_indices_2_onehot(Y1, 8)
    Y2 = convert_indices_2_onehot(Y2, 8)
    #if it's ERD then use 70 dimension
    X_test = X[:args.nb_test,::10,:]
    X_test=X_test.reshape(X_test.shape[0],X_test.shape[1]*X_test.shape[2])
    if args.load_data is False:
        # load model and randomly generated some seqs
    print "nb_total ={}".format(nb_total)
    print "nb_query ={}".format(nb_query)
    ##generate noise vectors
    if args.seq2seq is True:
        mean_cov = np.load('mean_cov.npz', 'r')
        names = mean_cov.files
        mean = mean_cov[names[1]]
        covariance = mean_cov[names[0]]
    else:
        mean = np.zeros(50)
        covariance = np.eye(N=50) * 1.0
    print 'shape of mean {}'.format(mean.shape)
    print 'shape of cov {}'.format(covariance.shape)
    noise_vectors = generate_positive_samples(nb_total,
                                              mean,
                                              covariance,
                                              'Gaussian',
                                              seed=2345)

    generated_seqs, covariance = decoder.predict(x=noise_vectors,
                                                 batch_size=1000)
    ##compute the (x_t-x_t-1)
    if args.query_difference is not True:
        generated_seqs = generated_seqs[:, ::10, :]
        generated_seqs = generated_seqs.reshape(
            generated_seqs.shape[0],
            generated_seqs.shape[1] * generated_seqs.shape[2])
        X = X[:, ::10, :]
        X = X.reshape(X.shape[0], X.shape[1] * X.shape[2])
        ##compute the mean max min distance from generated seqs to real sequences.
        print 'compute the first distance '
Пример #6
0
        f = 20.
        tmp_long_speed_seq = [
            0.5 * np.sin(2. * np.pi * f * (i / fs)) + 1.1
            for i in np.arange(fs)
        ]
        tmp_long_speed_seq = np.asarray(tmp_long_speed_seq).reshape(2000, 1)
        fig = plt.figure()
        plt.plot(tmp_long_speed_seq)
        plt.savefig('./fake_speed_seq.png')
    if args.latentsource == 'prior':
        latent_dim = 50
        mean = np.zeros(latent_dim)
        covariance = np.eye(N=latent_dim) * 1.0
        latent_vector = generate_positive_samples(10,
                                                  mean,
                                                  covariance,
                                                  'Gaussian',
                                                  seed=np.random.randint(
                                                      0, 2000))
        true_latent_vectors = latent_vector

    #duplicate the long_speed_seq to ten sequences
    long_speed_seq = []
    for i in range(10):
        long_speed_seq.append(tmp_long_speed_seq)

    long_speed_seq = np.asarray(long_speed_seq)
    print "shape of long_speed_seq is {}".format(long_speed_seq.shape)

    latent_dim = true_latent_vectors.shape[1]
    print "latent_dim is %d" % latent_dim
    decoder = decoder(latent_dim=latent_dim,
    latent_codes = encoder.predict(x=train_X, batch_size=1000)
    if expr_name == 'saae':
        mean = np.zeros(50)
        covariance = np.eye(N=50) * 1.0
    else:
        mean = np.mean(latent_codes, axis=0)
        covariance = np.cov(latent_codes, rowvar=False)
    print "shape of mean of {0} is {1}".format(expr_name, mean.shape)
    print "shape of covariance of {0} is {1}".format(expr_name,
                                                     covariance.shape)

    #sample z from prior distribution p(z)~N(0,1.0),and p(z)~N(mean,cov)

    z = generate_positive_samples(N_g,
                                  mean,
                                  covariance,
                                  'Gaussian',
                                  seed=np.random.randint(0, 2000))

    #generate sequences from sampled z
    sequences = decoder.predict(x=z)
    print "shape of sequences of {0} is {1}".format(expr_name, sequences.shape)

    #truncate sequences of 200 frames into ones of 10 frames and flatten them into a vector

    N = sequences.shape[0]
    samples_generated = np.zeros(shape=(N, 70 * 200 / tt), dtype=np.float32)
    samples_test_set = np.zeros(shape=(N_test, 70 * 200 / tt),
                                dtype=np.float32)
    count = 0
    for seq in sequences: