This repository has been archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create brain/selectivity; create new test file for augmented_nn.
- Loading branch information
Showing
14 changed files
with
583 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#===----------------------------------------------------------------------===# | ||
# | ||
# Peloton | ||
# | ||
# AugmentedNN.py | ||
# | ||
# Identification: src/brain/modelgen/AugmentedNN.py | ||
# | ||
# Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
# | ||
#===----------------------------------------------------------------------===# | ||
|
||
import tensorflow as tf | ||
import functools | ||
import os | ||
import argparse | ||
|
||
def lazy_property(function): | ||
attribute = '_cache_' + function.__name__ | ||
|
||
@property | ||
@functools.wraps(function) | ||
def decorator(self): | ||
if not hasattr(self, attribute): | ||
setattr(self, attribute, function(self)) | ||
return getattr(self, attribute) | ||
|
||
return decorator | ||
|
||
class AugmentedNN: | ||
|
||
def __init__(self, column_num, order=1, neuron_num=16, lr=0.1, **kwargs): | ||
tf.reset_default_graph() | ||
self.data = tf.placeholder(tf.float32, [None, column_num*2], name="data_") | ||
self.target = tf.placeholder(tf.float32, [None, 1], name="target_") | ||
self._column_num = column_num | ||
self._order = order | ||
self._neuron_num = neuron_num | ||
self._lr = tf.placeholder_with_default(lr, shape=None, | ||
name="learn_rate_") | ||
self.tf_init = tf.global_variables_initializer | ||
self.prediction | ||
self.loss | ||
self.optimize | ||
|
||
@staticmethod | ||
def jump_activation(k): | ||
""" | ||
This is an activation function used to learn discontinuous functions. | ||
Reference: https://dl.acm.org/citation.cfm?id=2326898 | ||
""" | ||
def jump_activation_k(x): | ||
return tf.pow(tf.maximum(0.0, 1-tf.exp(-x)), k) | ||
return jump_activation_k | ||
|
||
@lazy_property | ||
def prediction(self): | ||
net = self.data | ||
kernel_init = tf.random_normal_initializer(mean=0.0001, stddev=0.0001) | ||
with tf.name_scope("hidden_layer"): | ||
net_shape = tf.shape(net) | ||
bsz = net_shape[0] | ||
|
||
h1_layers = [] | ||
for i in range(1, self._order+1): | ||
h1 = tf.layers.dense(net, self._neuron_num, | ||
activation=self.jump_activation(i), | ||
kernel_initializer=kernel_init) | ||
h1_layers.append(h1) | ||
h1_layers = tf.concat(h1_layers, 1) | ||
with tf.name_scope("output_layer"): | ||
net = tf.layers.dense(h1_layers, 1, | ||
activation=self.jump_activation(1), | ||
kernel_initializer=kernel_init) | ||
net = tf.reshape(net, [bsz, -1], name="pred_") | ||
return net | ||
|
||
@lazy_property | ||
def loss(self): | ||
loss = tf.reduce_mean(tf.squared_difference(self.target, self.prediction), name='lossOp_') | ||
return loss | ||
|
||
@lazy_property | ||
def optimize(self): | ||
params = tf.trainable_variables() | ||
gradients = tf.gradients(self.loss, params) | ||
optimizer = tf.train.AdagradOptimizer(learning_rate=self._lr) | ||
return optimizer.apply_gradients(zip(gradients, | ||
params), name="optimizeOp_") | ||
|
||
def write_graph(self, dir): | ||
fname = "{}.pb".format(self.__repr__()) | ||
abs_path = os.path.join(dir, fname) | ||
if not os.path.exists(abs_path): | ||
tf.train.write_graph(tf.get_default_graph(), | ||
dir, fname, False) | ||
|
||
def __repr__(self): | ||
return "augmented_nn" | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='AugmentedNN Model Generator') | ||
|
||
parser.add_argument('--column_num', type=int, default=1, help='Number of augmentedNN Hidden units') | ||
parser.add_argument('--order', type=int, default=3, help='Max order of activation function') | ||
parser.add_argument('--neuron_num', type=int, default=20, help='Number of neurons in hidden layer') | ||
parser.add_argument('--lr', type=float, default=0.001, help='Learning rate') | ||
parser.add_argument('graph_out_path', type=str, help='Path to write graph output', nargs='+') | ||
args = parser.parse_args() | ||
model = AugmentedNN(args.column_num, args.order, args.neuron_num, args.lr) | ||
model.tf_init() | ||
model.write_graph(' '.join(args.graph_out_path)) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// selectivity_defaults.cpp | ||
// | ||
// Identification: src/brain/workload/selectivity_defaults.cpp | ||
// | ||
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "brain/selectivity/selectivity_defaults.h" | ||
|
||
namespace peloton { | ||
namespace brain { | ||
|
||
const int AugmentedNNDefaults::COLUMN_NUM = 1; | ||
const int AugmentedNNDefaults::ORDER = 1; | ||
const int AugmentedNNDefaults::NEURON_NUM = 16; | ||
const float AugmentedNNDefaults::LR = 0.1f; | ||
const int AugmentedNNDefaults::BATCH_SIZE = 256; | ||
const int AugmentedNNDefaults::EPOCHS = 600; | ||
|
||
|
||
} // namespace brain | ||
} // namespace peloton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// selectivity_defaults.h | ||
// | ||
// Identification: src/include/brain/workload/selectivity_defaults.h | ||
// | ||
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
/** | ||
* This header file contains default attributes | ||
* associated with the selectivity prediction task | ||
**/ | ||
|
||
namespace peloton { | ||
namespace brain { | ||
|
||
struct AugmentedNNDefaults { | ||
static const int COLUMN_NUM; | ||
static const int ORDER; | ||
static const int NEURON_NUM; | ||
static const float LR; | ||
static const int BATCH_SIZE; | ||
static const int EPOCHS; | ||
}; | ||
|
||
} // namespace brain | ||
} // namespace peloton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// augmented_nn_test.cpp | ||
// | ||
// Identification: test/brain/augmented_nn_test.cpp | ||
// | ||
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <algorithm> | ||
#include "brain/selectivity/augmented_nn.h" | ||
#include "brain/selectivity/selectivity_defaults.h" | ||
#include "brain/testing_augmented_nn_util.h" | ||
#include "brain/util/model_util.h" | ||
#include "brain/workload/workload_defaults.h" | ||
#include "common/harness.h" | ||
#include "util/file_util.h" | ||
|
||
namespace peloton { | ||
namespace test { | ||
class AugmentedNNTests : public PelotonTest {}; | ||
|
||
TEST_F(AugmentedNNTests, DISABLED_AugmentedNNUniformTest) { | ||
auto model = std::unique_ptr<brain::AugmentedNN>(new brain::AugmentedNN( | ||
brain::AugmentedNNDefaults::COLUMN_NUM, | ||
brain::AugmentedNNDefaults::ORDER, | ||
brain::AugmentedNNDefaults::NEURON_NUM, | ||
brain::AugmentedNNDefaults::LR, | ||
brain::AugmentedNNDefaults::BATCH_SIZE, | ||
brain::AugmentedNNDefaults::EPOCHS)); | ||
EXPECT_TRUE(model->IsTFModel()); | ||
size_t LOG_INTERVAL = 20; | ||
size_t NUM_SAMPLES = 10000; | ||
float VAL_SPLIT = 0.5; | ||
bool NORMALIZE = false; | ||
float VAL_THESH = 0.05; | ||
|
||
TestingAugmentedNNUtil::Test(*model, DistributionType::UniformDistribution, | ||
LOG_INTERVAL, NUM_SAMPLES, | ||
VAL_SPLIT, NORMALIZE, VAL_THESH, | ||
brain::CommonWorkloadDefaults::ESTOP_PATIENCE, | ||
brain::CommonWorkloadDefaults::ESTOP_DELTA); | ||
} | ||
|
||
TEST_F(AugmentedNNTests, DISABLED_AugmentedNNSkewedTest) { | ||
auto model = std::unique_ptr<brain::AugmentedNN>(new brain::AugmentedNN( | ||
brain::AugmentedNNDefaults::COLUMN_NUM, | ||
brain::AugmentedNNDefaults::ORDER, | ||
brain::AugmentedNNDefaults::NEURON_NUM, | ||
brain::AugmentedNNDefaults::LR, | ||
brain::AugmentedNNDefaults::BATCH_SIZE, | ||
brain::AugmentedNNDefaults::EPOCHS)); | ||
EXPECT_TRUE(model->IsTFModel()); | ||
size_t LOG_INTERVAL = 20; | ||
size_t NUM_SAMPLES = 10000; | ||
float VAL_SPLIT = 0.5; | ||
bool NORMALIZE = false; | ||
float VAL_THESH = 0.05; | ||
|
||
TestingAugmentedNNUtil::Test(*model, DistributionType::SkewedDistribution, | ||
LOG_INTERVAL, NUM_SAMPLES, | ||
VAL_SPLIT, NORMALIZE, VAL_THESH, | ||
brain::CommonWorkloadDefaults::ESTOP_PATIENCE, | ||
brain::CommonWorkloadDefaults::ESTOP_DELTA); | ||
} | ||
|
||
} // namespace test | ||
} // namespace peloton |
Oops, something went wrong.