예제 #1
0
    def make_model(self):
        inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
        reshaped = K_layer.Reshape(
            (self.unit_n, self.unit_t, self.input_dim))(inputs)
        encode_reshape = K_layer.Reshape(
            (self.unit_n, self.partial_latent_dim))
        encode_1 = abs_model.RNN_UNIT(self.partial_latent_dim)
        encode_2 = abs_model.RNN_UNIT(self.latent_dim)

        def encode_partials(seq):
            encoded = [None] * self.unit_n
            for i in range(self.unit_n):
                rs = K_layer.Lambda(lambda x: x[:, i],
                                    output_shape=(self.unit_t,
                                                  self.input_dim))(seq)
                encoded[i] = encode_1(rs)
            return encode_reshape(K_layer.concatenate(encoded, axis=1))

        encoded = encode_partials(reshaped)
        encoded = encode_2(encoded)

        z = K_layer.Input(shape=(self.latent_dim, ))
        decode_repeat_units = K_layer.RepeatVector(self.unit_n)
        decode_units = abs_model.RNN_UNIT(self.partial_latent_dim,
                                          return_sequences=True,
                                          activation=self.activation)

        decode_euler_1 = K_layer.Dense(self.output_dim * 4,
                                       activation=self.activation)
        decode_euler_2 = K_layer.Dense(self.output_dim,
                                       activation=self.activation)
        decode_repete_angles = K_layer.Lambda(
            lambda x: K_backend.repeat_elements(x, self.unit_t, 1),
            output_shape=(self.timesteps, self.output_dim))

        decode_repete = K_layer.RepeatVector(self.timesteps)
        decode_residual_1 = abs_model.RNN_UNIT(self.output_dim * 4,
                                               return_sequences=True,
                                               activation=self.activation)
        decode_residual_2 = abs_model.RNN_UNIT(self.output_dim,
                                               return_sequences=True,
                                               activation=self.activation)

        def decode_angle(e):
            angle = decode_units(decode_repeat_units(e))
            angle = K_layer.TimeDistributed(decode_euler_1)(angle)
            angle = K_layer.TimeDistributed(decode_euler_2)(angle)
            angle = decode_repete_angles(angle)
            residual = decode_repete(e)
            residual = decode_residual_2(decode_residual_1(residual))
            angle = K_layer.Activation(self.activation)(K_layer.add(
                [angle, residual]))
            return angle

        decoded = decode_angle(encoded)
        decoded_ = decode_angle(z)

        self.encoder = Model(inputs, encoded)
        self.decoder = Model(z, decoded_)
        self.model = Model(inputs, decoded)
예제 #2
0
    def make_model(self):
        self.input_dim = self.input_dim - self.name_dim
        self.output_dim = self.name_dim

        inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
        reshaped = K_layer.Reshape(
            (self.unit_n, self.unit_t, self.input_dim))(inputs)
        encode_reshape = K_layer.Reshape((self.unit_n, self.latent_dim / 2))
        encode_1 = abs_model.RNN_UNIT(self.latent_dim / 2)
        encode_2 = abs_model.RNN_UNIT(self.latent_dim)

        def encode_partials(seq):
            encoded = [None] * self.unit_n
            for i in range(self.unit_n):
                rs = K_layer.Lambda(lambda x: x[:, i],
                                    output_shape=(self.unit_t,
                                                  self.input_dim))(seq)
                encoded[i] = encode_1(rs)
            return encode_reshape(K_layer.concatenate(encoded, axis=1))

        encoded = encode_partials(reshaped)
        encoded = encode_2(encoded)

        decoded = K_layer.Dense(self.output_dim, activation='sigmoid')(encoded)
        decoded = K_layer.Lambda(lambda x: K.tf.nn.softmax(x))(decoded)
        output = K_layer.RepeatVector(self.timesteps)(decoded)
        self.model = Model(inputs, output)
        self.encoder = self.model
        self.decoder = self.model
예제 #3
0
	def make_model(self):
		self.partial_latent_dim = 256 # self.latent_dim/2

		# Similar to HH_RNN
		inputs = K_layer.Input(shape=(self.timesteps_in, self.input_dim))
		reshaped = K_layer.Reshape((self.unit_n, self.unit_t, self.input_dim))(inputs)
		encode_reshape = K_layer.Reshape((self.unit_n, self.partial_latent_dim))
		encode_1 = abs_model.RNN_UNIT(self.partial_latent_dim)
		encode_2 = abs_model.RNN_UNIT(self.latent_dim)

		def encode_partials(seq):
			encoded = [None]*self.unit_n
			for i in range(self.unit_n):
				rs = K_layer.Lambda(lambda x: x[:,i], output_shape=(self.unit_t, self.input_dim))(seq)
				encoded[i] = encode_1(rs)
			return encode_reshape(K_layer.concatenate(encoded, axis=1))

		encoded = encode_partials(reshaped)
		encoded = encode_2(encoded)

		z = K_layer.Input(shape=(self.latent_dim,))
		decode_euler_1 = K_layer.Dense(self.partial_latent_dim, activation=self.activation)
		decode_euler_2 = K_layer.Dense(self.output_dim, activation=self.activation)

		decode_repete = K_layer.RepeatVector(self.timesteps_out)
		decode_residual_1 = abs_model.RNN_UNIT(self.partial_latent_dim, return_sequences=True, activation=self.activation)
		decode_residual_2 = abs_model.RNN_UNIT(self.output_dim, return_sequences=True, activation=self.activation)

		decoded =  decode_residual_2(decode_residual_1(decode_repete(encoded)))
		decoded_ = decode_residual_2(decode_residual_1(decode_repete(z)))

		self.encoder = Model(inputs, encoded)
		self.decoder = Model(z, decoded_)
		self.model = Model(inputs, decoded)
예제 #4
0
    def make_model(self):
        inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
        encoded = abs_model.RNN_UNIT(self.latent_dim)(inputs)

        z = K_layer.Input(shape=(self.latent_dim, ))
        decode_repeat = RepeatVector(self.timesteps)
        decode_rnn = abs_model.RNN_UNIT(self.input_dim, return_sequences=True)

        decoded = decode_rnn(decode_repeat(encoded))
        decoded_ = decode_rnn(decode_repeat(z))

        self.encoder = Model(inputs, encoded)
        self.decoder = Model(z, decoded_)
        self.model = Model(inputs, decoded)
예제 #5
0
    def back_make_model(self):
        # Same as seq2seq.py
        inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
        encoded = abs_model.RNN_UNIT(self.latent_dim)(inputs)

        z = K_layer.Input(shape=(self.latent_dim, ))
        decode_repete = K_layer.RepeatVector(self.timesteps)
        decode_rnn = abs_model.RNN_UNIT(self.output_dim,
                                        return_sequences=True,
                                        activation=self.activation)

        decoded = decode_rnn(decode_repete(encoded))
        decoded_ = decode_rnn(decode_repete(z))

        self.encoder = Model(inputs, encoded)
        self.decoder = Model(z, decoded_)
        self.model = Model(inputs, decoded)
예제 #6
0
	def make_model(self):
		inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
		reshaped = K_layer.Reshape((self.unit_n, self.unit_t, self.input_dim))(inputs)
		encode_reshape = K_layer.Reshape((self.unit_n, self.latent_dim/2))
		encode_1 = abs_model.RNN_UNIT(self.latent_dim/2)
		encode_2 = abs_model.RNN_UNIT(self.latent_dim, return_sequences=True)

		def encode_partials(seq):
			encoded = [None]*self.unit_n
			for i in range(self.unit_n):
				rs = K_layer.Lambda(lambda x: x[:,i], output_shape=(self.unit_t, self.input_dim))(seq)
				encoded[i] = encode_1(rs)
			return encode_reshape(K_layer.concatenate(encoded, axis=1))

		encoded = encode_partials(reshaped)
		encoded = encode_2(encoded)

		z = K_layer.Input(shape=(self.latent_dim,))
		decode_euler_1 = K_layer.Dense(self.latent_dim/2, activation=self.activation)
		decode_euler_2 = K_layer.Dense(self.output_dim, activation=self.activation)

		decode_repete = K_layer.RepeatVector(self.timesteps)
		decode_residual_1 = abs_model.RNN_UNIT(self.latent_dim/2, return_sequences=True, activation=self.activation)
		decode_residual_2 = abs_model.RNN_UNIT(self.output_dim, return_sequences=True, activation=self.activation)

		def decode_angle(e):
			angle = decode_euler_2(decode_euler_1(e))
			residual = decode_repete(e)
			residual = decode_residual_2(decode_residual_1(residual))
			angle = K_layer.Activation(self.activation)(K_layer.add([decode_repete(angle), residual]))
			return angle

		angles = [None]*len(self.sup_hierarchies)
		for i,k in enumerate(self.sup_hierarchies):
			e = K_layer.Lambda(lambda x: x[:,k], output_shape=(self.latent_dim,))(encoded)
			angles[i] = decode_angle(e)

		decoded =  K_layer.concatenate(angles, axis=1)
		decoded_ = decode_angle(z)

		self.encoder = Model(inputs, encoded)
		self.decoder = Model(z, decoded_)
		self.model = Model(inputs, decoded)
예제 #7
0
	def make_model(self):
		inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
		encoded = abs_model.RNN_UNIT(self.latent_dim, return_sequences=True)(inputs)

		z = K_layer.Input(shape=(self.latent_dim,))
		decode_repete = K_layer.RepeatVector(self.timesteps)
		decode_rnn = abs_model.RNN_UNIT(self.output_dim, return_sequences=True, activation=self.activation)

		partials = [None]*len(self.hierarchies)
		for i,h in enumerate(self.hierarchies):
			e = K_layer.Lambda(lambda x: x[:,h], output_shape=(self.latent_dim,))(encoded)
			partials[i] = decode_rnn(decode_repete(e))

		decoded =  K_layer.concatenate(partials, axis=1)
		decoded_ = decode_rnn(decode_repete(z))

		self.encoder = Model(inputs, encoded)
		self.decoder = Model(z, decoded_)
		self.model = Model(inputs, decoded)
예제 #8
0
    def make_model(self):
        assert self.sub_unit**2 == self.unit_t

        # ENCODER
        inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
        reshaped = K_layer.Reshape(
            (self.unit_n, self.unit_t, self.input_dim))(inputs)

        reshaped_to_sub_units = K_layer.Reshape(
            (self.sub_unit, self.sub_unit, self.input_dim))
        encode_reshape_1 = K_layer.Reshape(
            (self.sub_unit, self.sub_partial_latent_dim))
        encode_reshape_2 = K_layer.Reshape(
            (self.unit_n, self.partial_latent_dim))
        encode_1 = abs_model.RNN_UNIT(self.sub_partial_latent_dim)
        encode_2 = abs_model.RNN_UNIT(self.partial_latent_dim)
        encode_3 = abs_model.RNN_UNIT(self.latent_dim, return_sequences=True)

        def encode_partials(seq,
                            encoder_layer,
                            reshape_layer_1,
                            shape_n,
                            n,
                            reshape_layer_2=None):
            encoded = [None] * n
            for i in range(n):
                rs = K_layer.Lambda(lambda x: x[:, i],
                                    output_shape=(shape_n,
                                                  self.input_dim))(seq)
                if reshape_layer_2 is not None:
                    rs = reshape_layer_2(rs)
                    rs = encode_partials(rs, encode_1, encode_reshape_1,
                                         self.sub_unit, self.sub_unit)
                encoded[i] = encoder_layer(rs)
            return reshape_layer_1(K_layer.concatenate(encoded, axis=1))

        encoded = encode_partials(reshaped, encode_2, encode_reshape_2,
                                  self.unit_t, self.unit_n,
                                  reshaped_to_sub_units)
        encoded = encode_3(encoded)

        # DECODER
        z = K_layer.Input(shape=(self.latent_dim, ))
        decode_repeat_units = K_layer.RepeatVector(self.unit_n)
        decode_repeat_elements = K_layer.Lambda(
            lambda x: K_backend.repeat_elements(x, rep=self.sub_unit, axis=1),
            output_shape=(self.unit_n * self.sub_unit,
                          self.partial_latent_dim))
        decode_units_1 = abs_model.RNN_UNIT(self.partial_latent_dim,
                                            return_sequences=True,
                                            activation=self.activation)
        decode_units_2 = abs_model.RNN_UNIT(self.sub_partial_latent_dim,
                                            return_sequences=True,
                                            activation=self.activation)

        decode_euler_1 = K_layer.Dense(self.output_dim * 4,
                                       activation=self.activation)
        decode_euler_2 = K_layer.Dense(self.output_dim,
                                       activation=self.activation)
        decode_repete_dense = K_layer.Lambda(
            lambda x: K_backend.repeat_elements(x, self.sub_unit, 1),
            output_shape=(self.timesteps, self.output_dim))

        decode_repete = K_layer.Lambda(
            lambda x: K_backend.repeat_elements(x, self.sub_unit, 1),
            output_shape=(self.timesteps, self.sub_partial_latent_dim))
        # K_layer.RepeatVector(self.timesteps)
        decode_residual_1 = abs_model.RNN_UNIT(self.output_dim * 4,
                                               return_sequences=True,
                                               activation=self.activation)
        decode_residual_2 = abs_model.RNN_UNIT(self.output_dim,
                                               return_sequences=True,
                                               activation=self.activation)

        def decode_angle(e):
            res = decode_units_1(decode_repeat_units(e))
            res = decode_repeat_elements(res)
            res = decode_units_2(res)
            seq = K_layer.TimeDistributed(decode_euler_1)(res)
            seq = K_layer.TimeDistributed(decode_euler_2)(seq)
            seq = decode_repete_dense(seq)
            residual = decode_repete(res)
            residual = decode_residual_2(decode_residual_1(residual))
            res = K_layer.Activation(self.activation)(K_layer.add(
                [seq, residual]))
            return res

        angles = [None] * len(self.sup_hierarchies)
        for i, k in enumerate(self.sup_hierarchies):
            e = K_layer.Lambda(lambda x: x[:, k],
                               output_shape=(self.latent_dim, ))(encoded)
            angles[i] = decode_angle(e)

        decoded = K_layer.concatenate(angles, axis=1)
        decoded_ = decode_angle(z)

        self.encoder = Model(inputs, encoded)
        self.decoder = Model(z, decoded_)
        self.model = Model(inputs, decoded)
예제 #9
0
    def make_model(self):
        inputs = K_layer.Input(shape=(self.timesteps_in, self.input_dim))
        encoded = abs_model.RNN_UNIT(self.latent_dim)(inputs)

        z = K_layer.Input(shape=(self.latent_dim, ))
        #decode_repete = K_layer.RepeatVector(1)
        #decode_rnn = abs_model.RNN_UNIT(self.latent_dim, return_state=True, return_sequences=True, activation=self.activation)
        decode_repete = K_layer.RepeatVector(self.timesteps_out)
        decode_rnn = abs_model.RNN_UNIT(self.output_dim,
                                        return_sequences=True,
                                        activation=self.activation)

        dense = K_layer.TimeDistributed(K_layer.Dense(self.output_dim))
        last_frame = K_layer.Lambda(lambda x: x[:, -1],
                                    output_shape=(self.output_dim, ))(inputs)

        # no teacher forcing
        def decode(inputs):
            all_outputs = [None] * self.timesteps_out
            states = None
            for i in range(self.timesteps_out):
                if states is None:
                    inputs = decode_repete(inputs)
                    outputs, states = decode_rnn(inputs)
                else:
                    outputs, states = decode_rnn(inputs, initial_state=states)
                all_outputs[i] = outputs
                inputs = outputs
            outputs = K_layer.Lambda(lambda x: K_layer.concatenate(x, axis=1))(
                all_outputs)
            return dense(outputs)

        #decoded = decode(encoded)
        #decoded_ = decode(z)

        # residual
        #decode_rnn_2 = abs_model.RNN_UNIT(self.output_dim*4, return_sequences=True, activation=self.activation)

        decoded = decode_rnn(decode_repete(encoded))
        decoded_ = decode_rnn(decode_repete(z))

        #	last_frame = K_layer.Dense(self.output_dim*4)(encoded)
        #last_frame = K_layer.Dense(self.output_dim)(encoded)
        #last_frame_ = K_layer.Dense(self.output_dim)(z)

        #decoded = K_layer.add([decode_repete(last_frame), decoded])
        #decoded_ = K_layer.add([decode_repete(last_frame_), decoded_])

        outputs = [None] * self.timesteps_out
        first_decoded = K_layer.Lambda(
            lambda x: x[:, 0], output_shape=(self.output_dim, ))(decoded)
        outputs[0] = K_layer.add([first_decoded, last_frame])
        for i in range(1, self.timesteps_out):
            dec_ = K_layer.Lambda(lambda x: x[:, i],
                                  output_shape=(self.output_dim, ))(decoded)
            outputs[i] = K_layer.add([dec_, outputs[i - 1]])
        decoded = K_layer.concatenate(outputs, axis=1)
        decoded = K_layer.Reshape(
            (self.timesteps_out, self.output_dim))(decoded)

        self.encoder = Model(inputs, encoded)
        self.decoder = Model(z, decoded_)
        self.model = Model(inputs, decoded)
예제 #10
0
    def make_model(self):
        self.timesteps_out = 10
        self.partial_latent_dim = 514  #self.latent_dim/2

        inputs = K_layer.Input(shape=(self.timesteps, self.input_dim))
        reshaped = K_layer.Reshape(
            (self.unit_n, self.unit_t, self.input_dim))(inputs)
        encode_reshape = K_layer.Reshape(
            (self.unit_n, self.partial_latent_dim))
        encode_1 = abs_model.RNN_UNIT(self.partial_latent_dim)
        encode_2 = abs_model.RNN_UNIT(self.latent_dim, return_sequences=True)

        def encode_partials(seq):
            encoded = [None] * self.unit_n
            for i in range(self.unit_n):
                rs = K_layer.Lambda(lambda x: x[:, i],
                                    output_shape=(self.unit_t,
                                                  self.input_dim))(seq)
                encoded[i] = encode_1(rs)
            return encode_reshape(K_layer.concatenate(encoded, axis=1))

        encoded = encode_partials(reshaped)
        encoded = encode_2(encoded)

        z = K_layer.Input(shape=(self.latent_dim, ))
        decode_repeat = K_layer.RepeatVector(1)
        decode_units = abs_model.RNN_UNIT(
            self.latent_dim, return_sequences=True,
            return_state=True)  #, activation=self.activation)

        decode_dense_1 = K_layer.Dense(self.partial_latent_dim,
                                       activation=self.activation)
        decode_dense_2 = K_layer.Dense(self.output_dim,
                                       activation=self.activation)
        #decode_repete_angles = K_layer.Lambda(lambda x:K_backend.repeat_elements(x, self.unit_t, 1), output_shape=(self.timesteps, self.output_dim))

        #decode_repete = K_layer.RepeatVector(self.timesteps)
        #decode_residual_1 = abs_model.RNN_UNIT(self.output_dim*4, return_sequences=True, activation=self.activation)
        decode_residual_2 = abs_model.RNN_UNIT(self.partial_latent_dim,
                                               return_sequences=True,
                                               return_state=True,
                                               activation=self.activation)

        def sampling_base(inputs, t, n, dec_fn, states=None):
            all_outputs = [None] * t
            for i in range(t):
                if i == 0:
                    inputs = decode_repeat(inputs)
                if states is None:
                    outputs, states = dec_fn(inputs)
                else:
                    outputs, states = dec_fn(inputs, initial_state=states)
                all_outputs[i] = outputs
                inputs = outputs
            outputs = K_layer.Lambda(lambda x: K_layer.concatenate(x, axis=1))(
                all_outputs)
            return outputs, states

        def decode_angle(e):
            angle, _ = sampling_base(e, self.unit_n, self.partial_latent_dim,
                                     decode_units)
            angle = K_layer.TimeDistributed(decode_dense_1)(angle)
            residual = [None] * self.unit_n
            states = None
            for i in range(self.unit_n):
                inputs = K_layer.Lambda(lambda x: x[:, i])(angle)
                residual[i], states = sampling_base(inputs, self.unit_t,
                                                    self.output_dim,
                                                    decode_residual_2, states)
            angle = K_layer.Lambda(lambda x: K_layer.concatenate(x, axis=1))(
                residual)
            angle = K_layer.TimeDistributed(decode_dense_2)(angle)
            angle = K_layer.Activation(self.activation)(angle)
            return angle

        angles = [None] * len(self.sup_hierarchies)
        for i, k in enumerate(self.sup_hierarchies):
            e = K_layer.Lambda(lambda x: x[:, k],
                               output_shape=(self.latent_dim, ))(encoded)
            angles[i] = decode_angle(e)

        decoded = K_layer.concatenate(angles, axis=1)
        decoded_ = decode_angle(z)

        self.encoder = Model(inputs, encoded)
        self.decoder = Model(z, decoded_)
        self.model = Model(inputs, decoded)