def build(self, optimizer='Adam'): # build encoder enc_input = Input(shape=self.input_shape) enc_nn = NN(self.encoder_network) enc_out = enc_nn.build(enc_input) code_mean = Dense(self.code_size)(enc_out) code_var = Dense(self.code_size)(enc_out) code = Add()([ code_mean, Multiply()( [Lambda(K.exp)(code_var), Lambda(random_normal)(code_var)]) ]) enc_obj = Subtract()([ Add()([Lambda(K.square)(code_mean), Lambda(K.exp)(code_var)]), code_var ]) self.encoder = Model(enc_input, [code, enc_obj], name='encoder') # build decoder dec_input = Input(shape=[self.code_size]) dec_nn = NN(self.decoder_network) dec_out = dec_nn.build(dec_input) self.decoder = Model(dec_input, dec_out, name='decoder') # compose VAE real_img = Input(shape=self.input_shape) z, z_obj = self.encoder(real_img) reconstruct_img = self.decoder(z) self.vae = Model(inputs=[real_img], outputs=[reconstruct_img, z_obj]) self.vae.compile(optimizer=optimizer, loss=[self.reconstruct_loss, self.encoding_loss], loss_weights=self.loss_weights)
for experiment in experiments: augment, batch_size, keep_prob, filters, ksize, config = experiment start_time = time.time() network = NN(epochs=5, batch_size=batch_size, learning_rate=0.001) features = np.concatenate([X_train, aug_X ]) if augment == 'True' else X_train labels = np.concatenate([y_train, aug_y]) if augment == 'True' else y_train network.add_train_data(features, labels) network.add_test_data(X_test, y_test) network.add_validation_data(X_valid, y_valid) network.add_configuration(create_network_config(config, filters, ksize), input_size=input_size) network.build(num_labels=num_labels) print( "Training model with hyperparameters: augmented: {}, batch_size: {}, keep_prob: {}, filters: {}, ksize: {}, config: {}" .format(augment, batch_size, keep_prob, filters, ksize, network.get_string())) validation_accuracy = network.train(keep_prob=keep_prob) end_time = time.time() stat_values = [ end_time - start_time, validation_accuracy, augment, batch_size, keep_prob, filters, ksize, network.get_string() ] stat_entry = pd.Series(stat_values, index=stat_labels) stats = stats.append(stat_entry, ignore_index=True)
import numpy from nn import NN import pandas as pd model = NN(10) data = pd.read_csv('xor.csv', header=None).to_numpy() xs = data[:, :2] ys = data[:, 2:] model.build(2) for i in range(len(data)): print(model.backward(xs[i], ys[i]))