<jupyter_start><jupyter_text>Data preparation  Hyperparameters<jupyter_code>LEARNING_RATE = 0.001
BATCH_SIZE = 32<jupyter_output><empty_output><jupyter_text>data loading<jupyter_code>l = np.load('./env_set/dataset.npz')
train_input = l['train_input']
train_label = l['train_label']
test_input = l['test_input']
test_label = l['test_label']
MAXS = l['MAXS']
MINS = l['MINS']

TIME_STEPS = l['TIME_STEPS']
OUTPUT_SIZE = l['OUTPUT_SIZE']
NUM_FEATURES = train_input.shape[-1]
print(train_input.shape)
print(train_label.shape)
print()
print(test_input.shape)
print(test_label.shape)
trainset = np.concatenate([train_input, train_label], axis=0).astype(np.float32)
testset = np.concatenate([test_input, test_label], axis=0).astype(np.float32)
train_dataset = tf.data.Dataset.from_tensor_slices((trainset, trainset))
train_dataset = train_dataset.cache().shuffle(BATCH_SIZE*100).batch(BATCH_SIZE).repeat()
test_dataset = tf.data.Dataset.from_tensor_slices((testset, testset))
test_dataset = test_dataset.batch(BATCH_SIZE)
class RetrainLayer(tf.keras.layers.Layer):
    def __init__(self, num_hidden, activation=tf.nn.relu):
        super(RetrainLayer, self).__init__()
        self.num_hidden = num_hidden
        
        self.dense = tf.keras.layers.Dense(self.num_hidden, activation=activation, kernel_initializer='he_uniform')
        
    def call(self, inp):
        return self.dense(inp)
class Encoder(tf.keras.layers.Layer):
    def __init__(self, num_hiddens, encoding_size):
        super(Encoder, self).__init__()
        
        self.num_hiddens = num_hiddens
        self.encoding_size = encoding_size
        
        self.denses = [tf.keras.layers.Dense(self.num_hiddens[_], activation=tf.nn.relu, kernel_initializer='he_uniform')
                       for _ in range(len(self.num_hiddens))]
        
        self.output_layer = tf.keras.layers.Dense(self.encoding_size, activation=tf.nn.sigmoid)
        
    def call(self, inp):
        for _ in range(len(self.num_hiddens)):
            inp = self.denses[_](inp)
        
        return self.output_layer(inp)        
class Decoder(tf.keras.layers.Layer):
    def __init__(self, num_hiddens, original_size):
        super(Decoder, self).__init__()
        
        self.num_hiddens = num_hiddens[::-1]
        self.original_size = original_size
        
        self.denses = [tf.keras.layers.Dense(self.num_hiddens[_], activation=tf.nn.relu, kernel_initializer='he_uniform')
                       for _ in range(len(self.num_hiddens))]
        
    def call(self, inp):
        for _ in range(len(self.num_hiddens)):
            inp = self.denses[_](inp)
        
        return inp
class Autoencoder(tf.keras.Model):
    def __init__(self, num_hiddens, encoding_size, original_size):
        super(Autoencoder, self).__init__()
        self.num_hiddens = num_hiddens
        self.encoding_size = encoding_size
        self.original_size = original_size
        
        self.in_retrain_layer = RetrainLayer(self.num_hiddens[0])
        self.encoder = Encoder(self.num_hiddens, self.encoding_size)
        self.decoder = Decoder(self.num_hiddens, self.original_size)
        self.out_retrain_layer = RetrainLayer(self.original_size, activation = tf.nn.sigmoid)
        
    def call(self, inp, need_code=False, decoding=None):
        inp = self.in_retrain_layer(inp)
        encoded_values = self.encoder(inp)
        if decoding is not None:
            decoding = self.decoder(decoding)
            return self.out_retrain_layer(decoding)
        if not need_code:
            encoded_values = self.decoder(encoded_values)
            return self.out_retrain_layer(encoded_values)
        else:
            return encoded_values
def loss(model, original):
    reconstruction_error = tf.reduce_mean(tf.square(tf.subtract(model(original), original)))
    return reconstruction_error
@tf.function
def train(loss, model, opt, original):
    with tf.GradientTape() as tape:
        gradients = tape.gradient(loss(model, original), model.trainable_variables)
        gradient_variables = zip(gradients, model.trainable_variables)
        opt.apply_gradients(gradient_variables)
num_hiddens = [32, 16]
encoding_size = 8
original_size = 5
autoencoder = Autoencoder(num_hiddens, encoding_size, original_size)
opt = tf.optimizers.Adam(learning_rate=0.001)
checkpoint_path = "./checkpoints/trained_AE"
ckpt = tf.train.Checkpoint(autoencoder=autoencoder,
                           opt=opt)
ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=10)
writer = tf.summary.create_file_writer('tmp')
EPOCHS = 50
prev_test_loss = 100.0
with writer.as_default():
    with tf.summary.record_if(True):
        for epoch in range(EPOCHS):
            for step, (inp, tar) in enumerate(train_dataset):
                train(loss, autoencoder, opt, inp)
                loss_values = loss(autoencoder, inp)
                tf.summary.scalar('loss', loss_values, step=step)
                
                if step % 500 == 0:
                    test_loss = 0
                    for step_, (inp_, tar_) in enumerate(test_dataset):
                        test_loss += loss(autoencoder, inp_)
                        
                        if step_ > 500:
                            test_loss /= 100
                            break
                    if test_loss.numpy() < prev_test_loss:
                        ckpt_save_path = ckpt_manager.save()
                        prev_test_loss = test_loss.numpy()
                        print('Saving checkpoint at {}'.format(ckpt_save_path))
                    print('Epoch {} batch {} train loss: {:.4f} test loss: {:.4f}'
                          .format(epoch, step, loss_values.numpy(), test_loss.numpy()))<jupyter_output>Saving checkpoint at ./checkpoints/trained_AE/ckpt-59
Epoch 0 batch 0 train loss: 0.0000 test loss: 0.0001
Saving checkpoint at ./checkpoints/trained_AE/ckpt-60
Epoch 0 batch 500 train loss: 0.0000 test loss: 0.0001
Saving checkpoint at ./checkpoints/trained_AE/ckpt-61
Epoch 0 batch 1000 train loss: 0.0000 test loss: 0.0001
Saving checkpoint at ./checkpoints/trained_AE/ckpt-62
Epoch 0 batch 1500 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 2000 train loss: 0.0000 test loss: 0.0001
Saving checkpoint at ./checkpoints/trained_AE/ckpt-63
Epoch 0 batch 2500 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 3000 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 3500 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 4000 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 4500 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 5000 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 5500 train loss: 0.0000 test loss: 0.0001
Epoch 0 batch 6000 train loss: 0.0000 test loss: 0.0001
Epoch [...]<jupyter_text>Model saving<jupyter_code>i = -1
if ckpt_manager.checkpoints:
    ckpt.restore(ckpt_manager.checkpoints[i])
    print ('Checkpoint ' + ckpt_manager.checkpoints[i][-6:] +' restored!!')
test_loss = 0
for step_, (inp_, tar_) in enumerate(test_dataset):
    test_loss += loss(autoencoder, inp_)
autoencoder.save_weights('./checkpoints/trained_AE')<jupyter_output><empty_output>