Пример #1
0
def get_model(name):
    name = functools.partial('{}-{}'.format, name)

    self_pos = tf.placeholder(Config.dtype, Config.data_shape, name='self_pos')
    self_ability = tf.placeholder(Config.dtype,
                                  Config.data_shape,
                                  name='self_ability')
    enemy_pos = tf.placeholder(Config.dtype,
                               Config.data_shape,
                               name='enemy_pos')
    input_label = tf.placeholder(Config.dtype,
                                 Config.label_shape,
                                 name='input_label')

    x = tf.concat(3, [self_pos, self_ability, enemy_pos],
                  name=name('input_concat'))
    y = input_label

    nl = tf.nn.tanh

    def conv_pip(name, x):
        name = functools.partial('{}_{}'.format, name)

        x = conv2d(name('0'),
                   x,
                   Config.data_shape[3] * 2,
                   kernel=3,
                   stride=1,
                   nl=nl)
        x = conv2d(name('1'),
                   x,
                   Config.data_shape[3],
                   kernel=3,
                   stride=1,
                   nl=nl)
        return x

    pred = conv_pip(name('conv0'), x)
    for layer in range(5):
        pred_branch = tf.concat(3, [pred, x], name=name('concate%d' % layer))
        pred += conv_pip(name('conv%d' % (layer + 1)), pred_branch)

    x = tf.tanh(pred, name=name('control_tanh'))

    z = tf.mul(tf.exp(x), self_ability)
    z_sum = tf.reduce_sum(
        z, reduction_indices=[1, 2, 3],
        name=name('partition_function'))  # partition function

    # another formula of y*logy
    loss = -tf.reduce_sum(tf.mul(x, y), reduction_indices=[1, 2, 3
                                                           ]) + tf.log(z_sum)
    z_sum = tf.reshape(z_sum, [-1, 1, 1, 1])
    pred = tf.div(z, z_sum, name=name('predict'))
    return Model([self_pos, self_ability, enemy_pos],
                 input_label,
                 loss,
                 pred,
                 debug=z)
Пример #2
0
def fill_model(model: Model) -> str:
    """
    A model is trained based on the properties selected by the user.
    :param model: The model to be filled - trained and then saved.
    :return: String message about the status of the model that should be displayed as info.
    """
    # split_type = model.split_type_dd.value
    # split_feature = list(model.cross_columns_sm.value)
    model.model_type.algorithm = Algorithm[model.model_type_dd.value]
    model.split = Split(SplitTypes[model.split_type_dd.value], list(model.cross_columns_sm.value))

    model_pipeline, X_test, y_test = \
        train_model(model.model_type, model.split, model.X, model.y)

    model.model = model_pipeline
    model.X_test = X_test
    model.y_test = y_test

    msg = "Model {} trained successfully!".format(model.name)
    log.info(msg)
    return msg
Пример #3
0
def remove_model_features(model: Model) -> str:
    """
    Removes features selected by the user from a model.
    :param model: The model, for which features should be removed.
    :return: Message indicating that the features were successfully removed.
    """
    features = list(model.remove_features_sm.value)
    df_X_new = model.X.drop(columns=features, axis=1)
    model.X = df_X_new

    msg = 'Features: {} were removed successfully for model {}.\n{}'.format(features, model.name, df_X_new.head(5))
    log.info(msg)
    return msg
Пример #4
0
def get_model(name):
    name = functools.partial('{}-{}'.format, name)

    self_pos = tf.placeholder(Config.dtype, Config.data_shape, name='self_pos')
    self_ability = tf.placeholder(Config.dtype,
                                  Config.data_shape,
                                  name='self_ability')
    enemy_pos = tf.placeholder(Config.dtype,
                               Config.data_shape,
                               name='enemy_pos')
    input_label = tf.placeholder(Config.dtype,
                                 Config.label_shape,
                                 name='input_label')

    x = tf.concat(3, [self_pos, self_ability, enemy_pos],
                  name=name('input_concat'))
    y = input_label

    nl = tf.nn.tanh

    def conv_pip(name, x, nl):
        name = functools.partial('{}_{}'.format, name)

        x = conv2d(name('0'),
                   x,
                   Config.data_shape[3] * 2,
                   kernel=3,
                   stride=1,
                   nl=nl)
        x = conv2d(name('1'),
                   x,
                   Config.data_shape[3],
                   kernel=3,
                   stride=1,
                   nl=nl)
        return x

    for layer in range(5):
        x_branch = conv_pip(name('conv%d' % layer), x, nl)
        x = tf.concat(3, [x, x_branch], name=name('concate%d' % layer))

    x = conv_pip(name('conv5'), x, nl=None)
    pred = tf.sigmoid(x)

    # another formula of y*logy
    loss = -tf.log(tf.reduce_sum(tf.mul(x, y), reduction_indices=[1, 2, 3]))
    loss += -0.1 * tf.log(
        tf.reduce_sum(tf.mul(x, self_ability), reduction_indices=[1, 2, 3]))
    pred = tf.mul(pred, self_ability)

    return Model([self_pos, self_ability, enemy_pos], input_label, loss, pred)
Пример #5
0
def fill_empty_models(df_X: pd.DataFrame, df_y: pd.Series, number_of_models: int) -> (list, str):
    """
    A list of models will be created, where each model gets a name and the initial X and y of the dataset.
    :param df_X: Dataframe containing all columns of the dataset excluding the target.
    :param df_y: Series containing the target of the dataset.
    :param number_of_models: How many models should be trained.
    :return: (models, message) - Models is a list containing the all initial models to be trained, Message is a
    log message indicating that the operation was successful.
    """
    models = []
    for m in range(number_of_models):
        models.append(Model(m, "Model " + str(m+1), None, df_X, df_y, get_model_type(df_y)))

    msg = "Models to be trained: \'{}\'.".format(number_of_models)
    log.debug(msg)
    return models, msg
Пример #6
0
def get_model(name):
    with tf.name_scope(name) as scope:

        self_pos = tf.placeholder(config.dtype,
                                  config.data_shape,
                                  name='self_pos')
        enemy_pos = tf.placeholder(config.dtype,
                                   config.data_shape,
                                   name='enemy_pos')
        self_ability = tf.placeholder(config.dtype,
                                      config.data_shape,
                                      name='self_ability')
        enemy_ability = tf.placeholder(config.dtype,
                                       config.data_shape,
                                       name='enemy_ability')
        self_protect = tf.placeholder(config.dtype,
                                      config.data_shape,
                                      name='self_protect')
        enemy_protect = tf.placeholder(config.dtype,
                                       config.data_shape,
                                       name='enemy_protect')

        input_label = tf.placeholder(config.dtype,
                                     config.label_shape,
                                     name='input_label')

        x = tf.concat(3, [
            self_pos, enemy_pos, self_ability, enemy_ability, self_protect,
            enemy_protect
        ],
                      name='input_concat')
        y = input_label

        nl = tf.nn.tanh

        def conv_pip(name, x):
            with tf.name_scope(name) as scope:
                x = conv2d('0',
                           x,
                           config.data_shape[3] * 2,
                           kernel=3,
                           stride=1,
                           nl=nl)
                x = conv2d('1',
                           x,
                           config.data_shape[3],
                           kernel=3,
                           stride=1,
                           nl=nl)
            return x

        pred = conv_pip('conv0', x)
        for layer in range(5):
            pred_branch = tf.concat(3, [pred, x], name='concate%d' % layer)
            pred += conv_pip('conv%d' % (layer + 1), pred_branch)

        x = tf.tanh(pred, name='control_tanh')

        z = tf.mul(tf.exp(x), self_ability)
        z_sum = tf.reduce_sum(z,
                              reduction_indices=[1, 2, 3],
                              name='partition_function')  # partition function

        # another formula of y*logy
        loss = -tf.reduce_sum(tf.mul(x, y),
                              reduction_indices=[1, 2, 3]) + tf.log(z_sum)
        z_sum = tf.reshape(z_sum, [-1, 1, 1, 1])
        pred = tf.div(z, z_sum, name='predict')
        return Model([
            self_pos, enemy_pos, self_ability, enemy_ability, self_protect,
            enemy_protect
        ],
                     input_label,
                     loss,
                     pred,
                     debug=[z, z_sum])
import tensorflow as tf
import librosa
from util.config import Config
from util.model import Model

##### Fixed configuration #####
CHUNK_SIZE = 8192
AUDIO_FORMAT = pyaudio.paInt16
SAMPLE_RATE = 44100
N_MFCC = 13

##### User configuration #####
config = Config('../config.yaml')
BUFFER_HOURS = config.BUFFER_HOURS
WAVE_FILENAME = config.WAV_PATH
model = Model(config.MODEL_TYPE, config.MODEL_PATH)
# print(config.__dict__)

SERVER_ADDRESS = config.SERVER_ADDRESS


def process_audio(shared_mfcc, shared_time, shared_pos, lock):
    """
    Grab some audio from the mic, save it to the file and calculate mfcc 
    :param shared_mfcc:
    :param shared_time:
    :param shared_pos:
    :param lock:
    :return:
    """