def main():
    backend.set_floatx("float16")
    backend.set_epsilon(1e-4)

    data = get_dataset(
        block_interval=8,
        block_size=INPUT_COUNT,
        file_count=40,
        output_size=OUTPUT_COUNT,
    )
    train_data = data.train_data.reshape(len(data.train_data), INPUT_COUNT, 1)
    test_data = data.test_data.reshape(len(data.test_data), INPUT_COUNT, 1)

    model = ExperimentalModel()
    model.load()
    # model.train(train_data, data.train_out, test_data, data.test_out)

    for i in range(min(len(data.files), min(len(data.files), 10))):
        result = data.files[i][40]
        inp = result.reshape(INPUT_COUNT, 1)
        result = list(result)

        generate_steps = 4000
        iterations = int(generate_steps / OUTPUT_COUNT)
        for j in range(iterations):
            if j % 1000 == 0:
                print(f"Progress: {j} / {iterations}")
            out = model.predict_output(inp.reshape(1, *inp.shape))
            result.extend(out[0])
            # inp = out.reshape(32, 1)
            inp = np.concatenate([inp, out.reshape(OUTPUT_COUNT,
                                                   1)])[-INPUT_COUNT:]

        data.write_wav(f"output-lstm-{MODEL_ID}-{i}.wav", np.array(result))
    def _enable_float32(self):
        dtype = 'float32'

        K.set_floatx(dtype)
        K.set_epsilon(1e-7)

        return dtype
Exemple #3
0
def main():
    backend.set_floatx("float16")
    backend.set_epsilon(1e-4)

    data = get_dataset(block_interval=int(INPUT_COUNT / 4), block_size=INPUT_COUNT, file_count=10)
    # train_data = data.train_data.reshape(len(data.train_data), INPUT_COUNT, 1)
    # test_data = data.test_data.reshape(len(data.test_data), INPUT_COUNT, 1)
    train_data = data.train_data
    test_data = data.test_data

    # plt.plot(train_data.flatten())
    # plt.show()

    # plot_data = np.bincount((train_data.flatten() * 255).astype(int))

    # plt.plot(plot_data)
    # plt.show()

    # print(plot_data.shape)

    model = AutoEncoder()
    model.load()
    model.train(train_data, test_data)
    for i in range(min(len(data.files), 10)):
        output = model.predict_output(data.files[i])
        data.write_wav(f"output-conv-{CONV_ID}-{i}.wav", output)
    def _enable_float16(self):
        dtype = 'float16'

        K.set_floatx(dtype)
        K.set_epsilon(1e-4)

        return dtype
Exemple #5
0
def F1(y_true, y_pred):
    K.set_epsilon(1e-12)
    predicted_positives = pre_t(y_true, y_pred) # 预测为正的个数
    possible_positives = real_t(y_true, y_pred) # 实际为正的个数
    intersect = inter(y_true, y_pred)  # 为1的交集个数
    P = intersect / (predicted_positives + K.epsilon())
    R = intersect / (possible_positives + K.epsilon())
    return 2*P*R/(P+R+K.epsilon())
def main():
    backend.set_floatx("float16")
    backend.set_epsilon(1e-4)

    data = get_dataset(block_interval=1000, block_size=2000, file_count=10)
    # train_data = data.train_data.reshape(len(data.train_data), 2000, 1)
    # test_data = data.test_data.reshape(len(data.test_data), 2000, 1)

    model = AutoEncoder()
    model.load()
    # model.train(train_data, test_data)
    for i in range(10):
        inp = data.files[i].reshape(len(data.files[i]), 2000, 1)
        output = model.predict_output(inp).reshape(len(data.files[i]), 2000)
        data.write_wav(f"output-conv{i}.wav", output)
Exemple #7
0
    def pre_encoder(self, X_train, param_reg):
        """
		First part of the stacked AE.
		Train the AE on the ROI input images.
		:param X_train: ROI input image
		:param get_history: boolean to return the loss history
		:param loss: if "customized_loss" -> customized_loss
		:return: encoded ROI image
		"""
        K.set_epsilon(0.1)
        rm = run_model.run_model('pre')
        #define callbacks
        callbacks = rm.callbacks_define(self.monitor, self.weight_name)
        autoencoder_0 = Sequential()
        dim = self.pretrain_window * self.pretrain_window
        #print(dim)
        encoder_0 = Dense(input_dim=dim,
                          units=self.units_ed,
                          kernel_regularizer=regularizers.l2(param_reg))
        decoder_0 = Dense(input_dim=self.units_ed,
                          units=dim,
                          kernel_regularizer=regularizers.l2(param_reg))
        autoencoder_0.add(encoder_0)
        autoencoder_0.add(decoder_0)
        rm = run_model.run_model('roi')
        loss = rm.load_loss(self.loss_pre)
        autoencoder_0.compile(loss=loss,
                              optimizer=self.roi_optimizer,
                              metrics=['accuracy'])
        h = autoencoder_0.fit(X_train,
                              X_train,
                              epochs=self.epoch_pre,
                              verbose=self.verbose,
                              callbacks=callbacks)
        autoencoder_0.load_weights(self.store_model + "/weights_%s_%s.hdf5" %
                                   ('pre', self.weight_name))
        weights = autoencoder_0.layers[0].get_weights()
        temp_0 = Sequential()
        temp_0.add(encoder_0)
        temp_0.compile(loss=loss,
                       optimizer=self.roi_optimizer,
                       metrics=['accuracy'])
        encoded_X = temp_0.predict(X_train, verbose=self.verbose)

        if self.get_history:
            return h.history['loss'], encoded_X, encoder_0, weights
        else:
            return encoded_X, encoder_0, weights
Exemple #8
0
def main():
    backend.set_floatx("float16")
    backend.set_epsilon(1e-4)

    data = get_dataset(block_interval=1, block_size=INPUT_COUNT, file_count=1)
    train_data = data.train_data.reshape(len(data.train_data), INPUT_COUNT, 1)
    test_data = data.test_data.reshape(len(data.test_data), INPUT_COUNT, 1)

    model = ExperimentalModel()
    model.load()
    # model.train(train_data, test_data)
    for i in range(min(len(data.files), min(len(data.files), 10))):
        inp = data.files[i].reshape(len(data.files[i]), INPUT_COUNT, 1)
        output = model.predict_output(inp).reshape(len(data.files[i]),
                                                   INPUT_COUNT)
        data.write_wav(f"output-lstm-{MODEL_ID}-{i}.wav", output)
Exemple #9
0
def build_model(model, learning_rate):
    # 1e-4
    # 1e-5  cnn 6-1-6  mape-5.86
    # 1e-6  cnn-6-1-6  mape-6.49
    K.set_epsilon(1e-7)

    def rmse(y_true, y_pred):
        return losses.mean_squared_error(y_true, y_pred) ** 0.5

    def mape(y_true, y_pred):
        return losses.mae(y_true, y_pred)

    adam = Adam(lr=learning_rate)
    model.compile(loss='mse', optimizer=adam, metrics=[rmse, mape])
    # model.summary()

    return model
Exemple #10
0
def init_tensorflow(seed, use_float16=False):
    if use_float16:
        import keras.backend as K

        dtype = 'float16'
        K.set_floatx(dtype)
        K.set_epsilon(1e-4)

    # Solves an issue with regard to the use of newest CUDA versions
    from tensorflow.compat.v1 import ConfigProto
    from tensorflow.compat.v1 import InteractiveSession

    config = ConfigProto()
    config.gpu_options.allow_growth = True
    _ = InteractiveSession(config=config)

    tf.random.set_seed(seed)
Exemple #11
0
def fm_model(x, y, p):
    model = Sequential()

    model.add(
        Dense(p['l1'],
              input_dim=3,
              kernel_initializer=p['kernel_init'],
              activation=p['ac1']))
    model.add(Dropout(p['d1']))

    for i in range(4):
        layerstr = "l" + str(i + 2)
        dropoutstr = "d" + str(i + 2)
        activationstr = "ac" + str(i + 2)
        model.add(Dense(p[layerstr], activation=p[activationstr]))
        model.add(Dropout(p[dropoutstr]))

    model.add(Dense(3, activation='linear'))

    K.set_epsilon(1)

    model.compile(optimizer=tf.train.AdamOptimizer(p['lr']),
                  loss=[p['loss_func']],
                  metrics=[p['metrics']])

    history = model.fit(x=x,
                        y=y,
                        validation_split=0.2,
                        epochs=p['epoch_size'],
                        batch_size=p['batch_size'],
                        shuffle=True,
                        verbose=0,
                        callbacks=[
                            EarlyStopping(monitor='val_mean_absolute_error',
                                          min_delta=0.1,
                                          patience=30,
                                          verbose=0,
                                          mode='auto')
                        ])

    return history, model
    def __init__(self, debug=False):
        # Limit GPU memory(VRAM) usage in TensorFlow 2.0
        # https://github.com/tensorflow/tensorflow/issues/34355
        # https://medium.com/@starriet87/tensorflow-2-0-wanna-limit-gpu-memory-10ad474e2528
        gpus = tf.config.experimental.list_physical_devices('GPU')
        if gpus:
            try:
                for gpu in gpus:
                    tf.config.experimental.set_memory_growth(gpu, True)
                    print("Limit GPU memory ...")
            except RuntimeError as e:
                print(e)

        # lower float precision when RTX optimizer is switched on
        if self.rtx_optimizer == True:
            print("RTX optimizer is on ...")
            K.set_epsilon(1e-4)

        self.debug = debug
        if self.debug:
            print("ada_conv initiated...")
Exemple #13
0
 def worker(self,
            path,
            data,
            task_sequence,
            render_que,
            new_weights,
            use_gpu=True):
     import os
     if not use_gpu:
         os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
     import sys
     from keras import backend as K
     K.set_epsilon(1e-9)
     gpu_options = tf.GPUOptions(allow_growth=True)
     K.set_session(
         tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)))
     policy_net = create_policy_net('policy_model.h5')
     value_net = create_policy_net('value_model.h5')
     while True:
         item = task_sequence.get()
         render = False
         if render_que.qsize() > 0:
             render = render_que.get()
         if new_weights.qsize() > 0:
             if new_weights.get():
                 try:
                     policy_net.load_weights('policy_model.h5')
                     value_net.load_weights('value_model.h5')
                     print('new weights are loaded by process', os.getpid())
                 except:
                     print('new weights are not loaded by process',
                           os.getpid())
         #if item == 0:
         #    render = True
         state_memory, reward_memory, action_memory, old_memory, value_memory = self.run_episode(
             item, policy_net, value_net, render)
         advantage_memory, target_values = self.compute_advantages(
             reward_memory, action_memory, value_memory)
         data.put((state_memory[:-1], advantage_memory, target_values,
                   old_memory, sum(reward_memory)))
Exemple #14
0
    def __init__(self,
                 summary_writer=tf.summary.FileWriter('./train'),
                 start_point=0,
                 seed=123456,
                 decay=1e-12,
                 value_loss_coeff=0.1,
                 entropy_coeff=1e-1,
                 batch_size=16,
                 learning_rate=1e-4,
                 data_format='channels_first'):
        self.model = None
        self.target_model = None
        self.value_loss_coeff = value_loss_coeff
        self.batch_size = batch_size
        self.entropy_coeff = entropy_coeff
        self.learning_rate = learning_rate
        self.iterations = start_point
        self.seed = seed
        self.data_format = data_format
        self.channel_axis = -1 if self.data_format == 'channels_last' else -3
        self.decay = decay

        # summary
        self.writer = summary_writer

        ###################################
        # TensorFlow wizardry
        config = tf.ConfigProto()

        # Don't pre-allocate memory; allocate as-needed
        config.gpu_options.allow_growth = True

        # Only allow a total of half the GPU memory to be allocated
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5

        # Create a session with the above options specified.
        k.clear_session()
        k.set_session(tf.Session(config=config))
        k.set_image_data_format(self.data_format)
        k.set_epsilon(1e-12)
Exemple #15
0
def main():
    backend.set_floatx("float16")
    backend.set_epsilon(1e-4)

    data = get_dataset(
        block_interval=1,
        block_size=INPUT_COUNT,
        file_count=1,
        output_size=OUTPUT_COUNT,
        shuffle=True,
    )
    train_data = data.train_data.reshape(len(data.train_data), INPUT_COUNT, 1)
    test_data = data.test_data.reshape(len(data.test_data), INPUT_COUNT, 1)

    model = ExperimentalModel()
    model.load()
    # model.train(train_data, train_data, test_data, test_data)

    for i in range(1):
        inp = data.files[i].reshape(len(data.files[i]), INPUT_COUNT, 1)
        output = model.predict_output(inp).reshape(len(data.files[i]),
                                                   INPUT_COUNT)
        data.write_wav(f"output-{NAME}-{MODEL_ID}-{i}.wav", output)
        print(f"output-{NAME}-{MODEL_ID}-{i}.wav created")
Exemple #16
0
from keras.models import Model
from keras.optimizers import RMSprop
'''**************************************Parameters******************************************************************'''

# data import options
class_list_filename = "siamese_trainingSet_classes.txt"

# specify root directory where training / validation folders are located
root_dir = "data/product"
n_classes = 26

# training hyperparameters
base_id = 1  # 0=Inception-v3, 1=MobileNet, 2=InceptionResNet-v2, 3=ResNet50
n_epochs = 100
batch_size = 32
K.set_epsilon(1e-07)
epsilon = K.epsilon()

# save parameters
base_name = ' '  # base network name corresponding to base_ids 0-3
date_dir = ' '  # specify save directory for results using YYYY-MM-DD format
similarity_type = 'cosine'  # l1_sum, l2_sum, cosine
enable_model_save = True
'''******************************************************************************************************************'''
'''**************************************Functions*******************************************************************'''


# create a list of training image directory locations,class labels and image file formats
def create_class_list(list_filename):

    with open(list_filename, 'r') as f:
Exemple #17
0
import keras
from keras.callbacks import EarlyStopping
from keras.models import Input, Model, Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, BatchNormalization
from keras.layers import *
from keras.models import load_model
from keras import initializers
from keras import optimizers
from keras.utils.vis_utils import plot_model

import keras.backend as K

dtype = 'float32'
K.set_floatx(dtype)
# default is 1e-7 which is too small for float16.  Without adjusting the epsilon, we will get NaN predictions because of divide by zero problems
K.set_epsilon(1e-4)

from gym_drones.classes.map import WorldMap
from gym_drones.envs.drones_env import DronesDiscreteEnv


def testing_network(
        world_size: [int], target_num: int, drone_goal: str, drone_num: int,
        extra_drone_num: int, world_gain_peak_range: [float],
        world_gain_var_range: [float], world_evolution_speed: [float],
        drone_comm: float, drone_view: float, drone_memory: int,
        drone_battery: float, action_step: int, max_age: int,
        lookahead_step: int, malus: float, final_bonus: float, malus_sm: float,
        random_episode: bool, alpha: float, alpha_dec: float, epsilon: float,
        epsilon_dec: float, temperature: float, temperature_dec: float,
        state_MR: int, limit_MR: bool, three_MR: bool, prioritized: bool,
theano.config.nvcc.fastmath = True
theano.config.nvcc.flags = '-D_FORCE_INLINES'
theano.config.cxx = os.environ['CONDA_PREFIX'] + '/bin/g++'

# import and check Keras version
import keras
import keras.backend as K
from keras import __version__ as keras_version
from pkg_resources import parse_version
if (parse_version(keras_version) >= parse_version('2.0')):
    raise RuntimeError('DeepCell requires Keras 1 to run')

# configure Keras, to avoid using file ~/.keras/keras.json
K.set_image_dim_ordering('th')  # theano's image format (required by DeepCell)
K.set_floatx('float32')
K.set_epsilon('1e-07')

##
###############################################################################

import cytometer.deepcell as deepcell
import cytometer.deepcell_models as deepcell_models

###############################################################################
## Check response changes with step size changes
###############################################################################

# create test images with a corner step
im = np.ones(shape=(1, 2, 200, 200))
im[:, :, 0:50, 0:50] = 2
Exemple #19
0
from keras.callbacks import Callback, TerminateOnNaN, TensorBoard, ModelCheckpoint, CSVLogger, EarlyStopping

import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops, control_flow_ops
from tensorflow.python.training import optimizer

from heat.utils import hyperboloid_to_poincare_ball, load_data, load_embedding
from heat.utils import perform_walks, determine_positive_and_negative_samples
from heat.losses import  hyperbolic_softmax_loss, hyperbolic_sigmoid_loss, hyperbolic_hypersphere_loss
from heat.generators import TrainingDataGenerator
from heat.visualise import draw_graph, plot_degree_dist
from heat.callbacks import Checkpointer

K.set_floatx("float64")
K.set_epsilon(1e-15)

np.set_printoptions(suppress=True)

# TensorFlow wizardry
config = tf.ConfigProto()

# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True
 
# Only allow a total of half the GPU memory to be allocated
config.gpu_options.per_process_gpu_memory_fraction = 0.5

config.log_device_placement=False
config.allow_soft_placement=True
Exemple #20
0
# MULTI_CPU = True
EXPERIMENTS_DIR = "D:\\!DA-20092018\\AVCexperimentsData"
MULTI_CPU = False
# SET OUTPUT_SAVES OUTSIDE THE DOCKER CONTAINER
OUTPUT_SAVES = "./"
# EXTEND_ACTIONS = True
USE_SLIDINGWINDOW = True
# USE_SCALER = False
CNN_TRAINABLE = True
FRAMES_THRESHOLD = 13
COEFF_SLIDINGWINDOW = 0.8
COEFF_DROPOUT = 0.6
COEFF_REGULARIZATION_L2 = 0.015
CNN_BATCH_SIZE = 50
RNN_BATCH_SIZE = 16
k.set_epsilon(1e-06)

ITERATIONS = 1
NUM_EPOCHS = 1
AUGMENTATIONS = [
    'none',
    # "scale_shift",
    # 'scale',
    # 'shift_gauss_xy',
    # 'noise',
    # 'subsample',
    # 'interpol',
    # 'translate',
    # 'scale_translate'
]
OPTIMIZER = [
Exemple #21
0
from keras.layers import Lambda
from keras.layers.wrappers import TimeDistributed
from keras.layers.normalization import BatchNormalization

from keras import regularizers
from keras import callbacks
from keras.optimizers import RMSprop,adam
from keras.models import load_model

import wtte.weibull as weibull
import wtte.wtte as wtte
from wtte.wtte import WeightWatcher

# from phased_lstm_keras.PhasedLSTM import PhasedLSTM as PLSTM
np.random.seed(1)
K.set_epsilon(1e-8)

## Callbacks
checkpointer = callbacks.ModelCheckpoint('./tensorboard_log/wtte-rnn/model_checkpoint.h5', 
                          monitor='loss', 
                          verbose=1, 
                          save_best_only=True, 
                          save_weights_only=True, 
                          mode='auto', period=5)

reduce_lr = callbacks.ReduceLROnPlateau(monitor='loss', 
                                        factor  =0.5, 
                                        patience=15, 
                                        verbose=1, 
                                        mode='auto', 
                                        epsilon=0.0001, 
Exemple #22
0
def dsc_crossentropy(y_true, y_pred, smooth=0.00000001):
    K.set_epsilon(1e-8)
    num = K.binary_crossentropy(y_true, y_pred)
    den = ( K.sum( K.abs(y_true), axis=(1,2,3)) + smooth ) * K.ones_like(y_true)
    return 0.5 * 256.0*256.0 * num/den
Exemple #23
0
from keras import backend as K
from keras.callbacks import ModelCheckpoint, TerminateOnNaN, EarlyStopping

import tensorflow as tf

from hednet.utils import hyperboloid_to_poincare_ball, load_data
from hednet.utils import determine_positive_and_negative_samples
from hednet.generators import TrainingDataGenerator
from hednet.visualise import draw_graph, plot_degree_dist
from hednet.callbacks import Checkpointer, elu
from hednet.models import build_hyperboloid_asym_model
from hednet.optimizers import ExponentialMappingOptimizer
from hednet.losses import asym_hyperbolic_loss

K.set_floatx("float64")
K.set_epsilon(np.float64(1e-15))

np.set_printoptions(suppress=True)

# TensorFlow wizardry
config = tf.ConfigProto()

# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True

# Only allow a total of half the GPU memory to be allocated
config.gpu_options.per_process_gpu_memory_fraction = 0.5

config.log_device_placement = False
config.allow_soft_placement = True
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun  8 11:57:25 2017

@author: shubham
"""
from default import *
from keras import backend as K
from keras import metrics
K.set_epsilon(1e-7)
np.set_printoptions(threshold=np.inf)
#%% Handle dim ordering
assert (BACKEND == K.backend())
if BACKEND == 'theano':
    K.theano.config.__setattr__('exception_verbosity', 'high')
    K.set_image_data_format('channels_first')
elif BACKEND == 'tensorflow':
    K.set_image_data_format('channels_last')

print("Image_ordering:", K.image_dim_ordering())
print("Backend:", K.backend())

#%%LOSS FUNCTIONS

#%%1.DICE LOSS
smooth = 1.0
w_dice = 0.5


def old_dice_coef(y_true, y_pred):
Exemple #25
0
import numpy as np
np.random.seed(123)  # for reproducibility

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
from keras import backend as K

K.set_epsilon(1e-6)

# 4. Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 5. Preprocess input data
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# 6. Preprocess class labels
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

# 7. Define model architecture
model = Sequential()

model.add(Convolution2D(32, (3, 3), activation='relu',
Exemple #26
0
                       callbacks=[schedule, checkpoint1, checkpoint2])

    pd.DataFrame(result.history).to_csv('models\\GRU_fit_history.csv')
    #get the highest validation accuracy of the training epochs
    validation_loss = np.amin(result.history['val_loss'])
    print('validation loss of epoch:', validation_loss)
    return model


if __name__ == '__main__':
    X_train, y_train, X_test, y_test, look_back, num_features = data()
    """
    GTX 960m and GTX 970 support FP32
    """

    from keras import backend as K

    float_type = 'float32'  # Change this to float16 to use FP16
    K.set_floatx(float_type)
    K.set_epsilon(1e-4)  #default is 1e-7

    X_train = X_train.astype(float_type)
    y_train = y_train.astype(float_type)
    X_test = X_test.astype(float_type)
    y_test = y_test.astype(float_type)

    model = create_model(X_train, y_train, X_test, y_test, look_back,
                         num_features)

    #print("Evalutation of best performing model:")
    #print(model.evaluate(X_test, y_test))
Exemple #27
0
def train_two_phrase(train_file_names, test_file_names, params):
    K.set_epsilon(1e-4)
    # form vocabs and data
    numpy.set_printoptions(precision=3)
    print 'loading data.'
    old_std = sys.stdout
    f_print = open('debug_print_phr1.txt','w')
    f_test = open('debug_print_test.txt','w')
    
    sys.stdout = f_print
    lines = []
    for f_name in train_file_names:
        f = open(f_name, 'r')
        lines.extend(f.readlines())
        f.close()
    
    lines_test = []
    for f_name in test_file_names:
        f = open(f_name, 'r')
        lines_test.extend(f.readlines())
        f.close()
        
    vocab_text = {}
    inv_vocab_text = {}    
    data = parse_stories(lines, vocab_text, inv_vocab_text, 1)
    data_test = parse_stories(lines_test, vocab_text, inv_vocab_text, 0)
    
    l_train = len(data)
    l_test = len(data_test)
    data = data[:min(params['train_set_size'], l_train)]
    data_test = data_test[:min(params['test_set_size'], l_test)]
    
    story_int, story_mask, q_int, q_mask, ans_int, ans_mask, sizes = int_stories(data, vocab_text)
    story_mask = repeatTensor(story_mask, params['dim_emb_story'])
    q_mask = repeatTensor(q_mask, params['dim_emb_story'])
    
    story_int_test, story_mask_test, q_int_test, q_mask_test, ans_int_test, ans_mask_test, sizes_test = int_stories(data_test, vocab_text)
    story_mask_test = repeatTensor(story_mask_test, params['dim_emb_story'])
    q_mask_test = repeatTensor(q_mask_test, params['dim_emb_story'])

    inv_vocab_text['0'] = 'dududu'
    
    params['max_story_len'] = sizes[0]
    params['max_sent_len'] = max(sizes[1], sizes[3])
    params['max_q_num'] = sizes[2]
    
    params['max_story_len_test'] = sizes_test[0]
    params['max_q_num_test'] = sizes_test[2]
    
    params['vocab_size'] = len(vocab_text)
    params['vocab_size_ans'] = len(vocab_text)
    n_samples = len(story_int)
    
    params['ent_range'] = 1 # normal order and inverse order.


    print 'params:'
    print params
    print 'n_samples:'
    print n_samples
    print 'vocab_text:'
    print vocab_text
    sys.stdout = old_std
    
    
    # initialize the env embeddings, actions taken, rewards for the whole train data of an epoch
    story_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_story_len'], params['max_sent_len']))
    story_mask_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_story_len'], params['max_sent_len'], params['dim_emb_story']))
    q_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_q_num'], params['max_sent_len']))
    q_mask_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_q_num'], params['max_sent_len'], params['dim_emb_story']))
    
    act_selected_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_story_len'], (params['ent_range']) * 1 * params['relation_num'])) # one hot vector
    reward_train = numpy.ones((n_samples * params['story_sims_per_epoch'], params['max_story_len'], (params['ent_range']) * 1 * params['relation_num'])) # real number reward signal
    
    reward_immediate_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_story_len']))
    
    ans_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_q_num'], params['vocab_size']))
    ans_mask_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_q_num']))# maybe used as mul input at final answer position
    fin_one_hot_train = numpy.zeros((n_samples * params['story_sims_per_epoch'], params['max_q_num'], (params['ent_range']) * 1 * params['relation_num'])) # one hot arg1 arg2 r_pred
    reward_fin_train = numpy.ones((n_samples * params['story_sims_per_epoch'], params['max_q_num'], (params['ent_range']) * 1 * params['relation_num']))    
    
    print 'building model.'
    # simulate and generate final train data, do batch train and policy gd
    # build model
    reasoner, simulator = DRL_Reasoner(params)
    test_simulator = DRL_Reasoner_Test(params)
    
    working_memory = working_environment(params['enti_num'], params['relation_num_expand'])
    work_space_table = varTable()
    # for every single story-question group, take simulations, compute the env embeddings, actions taken, and total rewards.
    # take a certain number of simulations for each group, and use the pool as total train data, do sgd and batch training.
    # loop that progress for many epoches.
    mask_sim = numpy.zeros((1, params['max_story_len'], params['dim_emb_story'] * params['max_sent_len']))
    
    all_sim_number = 0
    
    print 'two phrase training started.'
    
    for epoch_id in range(params['epoch_num']):
        train_trace_id = 0
        avg_reward_q_epoch = 0.
        avg_reward_action_epoch = numpy.zeros(params['max_story_len'])
        print 'epoch %d phrase 1' %epoch_id
        # phrase 1: simulate and train data generating
        epoch_precision_rate = 0.
        sample_rate = numpy.zeros(n_samples)
        for story_id in range(n_samples):
            story_precision = 0.
            for sim_round in range(params['story_sims_per_epoch']):
                # write story, q, ans, data
                # print story_train[train_trace_id,:,:],  story_int[story_id]                
                sys.stdout = f_print
                story_train[train_trace_id,:len(story_int[0]),:len(story_int[0][0])] = story_int[story_id]
                story_mask_train[train_trace_id,:len(story_int[0]),:len(story_int[0][0]),:] = story_mask[story_id]
                q_train[train_trace_id,:len(q_int[0]),:len(q_int[0][0])] = q_int[story_id]
                q_mask_train[train_trace_id,:len(q_int[0]),:len(q_int[0][0]),:] = q_mask[story_id]
                ans_train[train_trace_id,:len(ans_int[0]),:len(ans_int[0][0])] = ans_int[story_id]
                
                arg_cached = []
                
                for time_slice in range(params['max_story_len']):
                    mask_sim[0][time_slice][:] = numpy.ones(params['dim_emb_story'] * params['max_sent_len'])
                    
                action_probs, retrieve_probs = simulator.predict([story_train[numpy.newaxis,train_trace_id], 
                                                                      story_mask_train[numpy.newaxis,train_trace_id],
                                                                      q_train[numpy.newaxis,train_trace_id],
                                                                      q_mask_train[numpy.newaxis,train_trace_id],
                                                                      mask_sim[:], 
                                                                      reward_train[numpy.newaxis,train_trace_id],
                                                                      reward_fin_train[numpy.newaxis,train_trace_id]])
                for time_slice in range(params['max_story_len']):
                        
                    action_selected, action_one_hot = select_action(action_probs[:,time_slice,:], params['epsilon'])

                    act_selected_train[train_trace_id, time_slice,:] = action_one_hot
                    arg_1_ptr = action_selected // ((1) * params['relation_num']) # start from 0 (empty number)
                    arg_2_ptr = (action_selected - arg_1_ptr * (1) * params['relation_num']) // params['relation_num']
                    arg_r_ptr = (action_selected - arg_1_ptr * (1) * params['relation_num']) % params['relation_num'] + 1
                    arg_r = arg_r_ptr
                    ents_in_sent = []
                    if time_slice < len(story_int[story_id]):
                        for w_id in story_int[story_id][time_slice]:
                            if inv_vocab_text[str(int(w_id))] in ent_list.values():
                                ents_in_sent.append(w_id)
                        if len(ents_in_sent) > 0:
                            arg_1 = int(ents_in_sent[0])
                            if arg_1 == vocab_text['they']:
                                arg_1 = arg_cached[0]
                                arg_2 = arg_cached[1]
                                arg_3 = int(ents_in_sent[1])
                            else:
                                arg_2 = int(ents_in_sent[1])
                                arg_cached = [arg_1, arg_2]
                                arg_3 = int(ents_in_sent[2])
                        # if len(ents_in_sent) == 3:
                        
                    
                    # slice_reward = 0
                    # for tt in range(time_slice+1):    
                        # reward_immediate_train[train_trace_id][tt] += slice_reward * (params['reward_decay'] ** (time_slice - tt))

                    if arg_1 > 0 and arg_2 > 0 and story_mask_train[train_trace_id, time_slice, 0, 0] > 0:
                        arg_1_int = work_space_table.retr_insert(arg_1, inv_vocab_text)
                        arg_2_int = work_space_table.retr_insert(arg_2, inv_vocab_text)
                        arg_3_int = work_space_table.retr_insert(arg_3, inv_vocab_text)
                        if arg_r <= 2:
                            working_memory.modifyEnv((arg_1_int, arg_r, arg_3_int))
                            working_memory.modifyEnv((arg_2_int, arg_r, arg_3_int))
                            
                
                # compute fin_train and fin_train_pred
                retrieved_relation_list = []
                reward_temp_list = [] # reward for every question based on arg1/2 in q or not.
                for q_idx in range(len(retrieve_probs[0])):                    
                    retr_idx, action_one_hot_retr = select_action(retrieve_probs[0,q_idx,:], params['epsilon'])
                    arg1_retr_ptr = retr_idx // ((1) * params['relation_num'])
                    arg2_retr_ptr = (retr_idx - arg1_retr_ptr * (1) * params['relation_num']) // params['relation_num']
                    relation_pred = (retr_idx - arg1_retr_ptr * (1) * params['relation_num']) % params['relation_num'] + 1
                    # convert from ptr to id:
                    flag = 0
                    for q_w in q_int[story_id, 0]:
                        if inv_vocab_text[str(int(q_w))] in ent_list.values() and flag == 0:
                            arg1_retr = int(q_w)
                            flag = 1
                        elif inv_vocab_text[str(int(q_w))] in ent_list.values() and flag == 1:
                            arg2_retr = int(q_w)
                    

                    retrieve_reward_pre = 0
                    reward_temp_list.append(retrieve_reward_pre)
                    
                    arg1_retr_int = work_space_table.retr(arg1_retr, inv_vocab_text)
                    # arg2_retr_int = work_space_table.retr(arg2_retr, inv_vocab_text)
                    search_result = working_memory.retrieveArg(arg1_retr_int)
                    arg_retr_str = work_space_table.inv_retr(search_result)                    
                    
                    retrieved_relation_list.append(arg_retr_str)
                    one_hot_single = numpy.zeros(((params['ent_range']) * 1 * params['relation_num']))
                    one_hot_single[retr_idx] = 1
                    fin_one_hot_train[train_trace_id, q_idx, :] = one_hot_single

                reward_q_total = 0.
                for q_idx in range(len(retrieve_probs[0])):
                    ans_word_int = numpy.argmax(ans_train[train_trace_id][0]) + 1
                    ans_word = inv_vocab_text[str(ans_word_int)]
                    reward_scalar_q, ans_q, label_q = compute_single_reward(retrieved_relation_list[q_idx],ans_word)
                    
                    if reward_scalar_q > 0 and q_idx == 0:
                        epoch_precision_rate += 1.
                        story_precision += 1.
                        
                    reward_scalar_q *= q_mask_train[train_trace_id, q_idx, 0, 0]   
                    reward_q_total += reward_scalar_q
                reward_q_total += reward_temp_list[0]
                
                for time_slice in range(params['max_story_len']):
                    reward_immediate_train[train_trace_id, time_slice] += reward_q_total * (params['reward_decay'] ** (params['max_story_len'] - time_slice))
                for q_idx in range(len(retrieve_probs[0])):
                    # pass
                    if q_idx == 0:
                        mask_reward = 1.
                    else:
                        mask_reward = 0.
                    reward_fin_train[train_trace_id, q_idx, :] *= (reward_q_total * mask_reward) # used as input at the last softmax
                
                for time_slice in range(params['max_story_len']):
                    # pass
                    reward_train[train_trace_id, time_slice, :] *= (reward_immediate_train[train_trace_id, time_slice] * story_mask_train[train_trace_id, time_slice, 0, 0]) # used as input at the last softmax

                
                train_trace_id += 1
                all_sim_number += 1
                mask_sim *= 0
                working_memory.resetEnv()
                work_space_table.reset()

            sample_rate[story_id] = story_precision / params['story_sims_per_epoch']
        
                
        epoch_precision_rate = epoch_precision_rate / (n_samples * params['story_sims_per_epoch'])
        print 'the total answer precision of this epoch:'
        print epoch_precision_rate
        
        sys.stdout = old_std
        print 'precision of this epoch: %f' %epoch_precision_rate
        print 'epoch %d phrase 2' %(epoch_id)
        print 'sample_rate:'
        print sample_rate
        # phrase2: go batch train on the trace pool.
        mask_sim_2 = numpy.ones((n_samples * params['story_sims_per_epoch'], params['max_story_len'], params['dim_emb_story'] * params['max_sent_len']))

        reasoner.fit([story_train, story_mask_train, q_train, q_mask_train, mask_sim_2, reward_train, reward_fin_train],
                      {'action_probs_re': act_selected_train, 'retrieve_probs_re': fin_one_hot_train},
                      batch_size=params['batch_size_phrase2'], nb_epoch=params['nb_epoch_phrase2'], verbose=2)
        
        sys.stdout = old_std
                
        # test the model
        weights_train =  [i for i in simulator.layers if len(i.get_weights()) != 0]
        weights_test =  [i for i in test_simulator.layers if len(i.get_weights()) != 0]
        for (l1,l2) in zip(weights_train,weights_test):
            l2.set_weights(l1.get_weights())

        if (epoch_id + 1) % params['test_epoch_period'] == 0:
            test_model(test_simulator, story_int_test, story_mask_test, q_int_test, q_mask_test, ans_int_test, ans_mask_test, params, old_std, f_test, vocab_text, inv_vocab_text)
            
        sys.stdout = old_std
        story_train *= 0
        story_mask_train *= 0
        q_train *= 0
        q_mask_train *= 0    
        act_selected_train *= 0
        reward_train = numpy.ones((n_samples * params['story_sims_per_epoch'], params['max_story_len'], (params['ent_range']) * 1 * params['relation_num'])) # real number reward signal
        ans_train *= 0
        ans_mask_train *= 0
        reward_fin_train = numpy.ones((n_samples * params['story_sims_per_epoch'], params['max_q_num'], params['ent_range'] * 1 * params['relation_num']))
        fin_one_hot_train *= 0
        reward_immediate_train *= 0
    
    f_print.close()
    f_test.close()
Exemple #28
0
                                         comp_count] = G_temp_component
                G_temp_count += comp_count
    return Gfunc_data


##################################
###### BPNN models ######
##################################
import os
#os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=cuda,floatX=float64,dnn.enabled=True"
os.environ["KERAS_BACKEND"] = "tensorflow"
from keras.models import load_model, model_from_json
#===== setting for float64 for keras ======
from keras import backend as K
K.set_floatx('float64')
K.set_epsilon(1e-14)


def bpnn_2b_model(base_path, at_idx_map, Gfunc_data, s):
    """calculate sum of all atomic energies from subnetworks for dimers 
    # Arguments
        base_path: path to bpnn-2b model
        at_idx_map: a mapping between atom types and atom indexes; dictionary
        Gfunc_data: symmetry function values; 
                    dictionary with 1st layer keys = atom types,
                        2nd layer keys = atom indexes,
                        values = 2D arrays with shape=(nb_samples, nb_sym_functions)
        s: switching function
    # Returns
        bpnn 2B-energy
    """
def main():
    backend.set_floatx("float16")
    backend.set_epsilon(1e-4)

    just_files = False
    data = get_dataset(
        block_interval=50,
        block_size=INPUT_COUNT,
        file_count=1,
        output_size=0,
        shuffle=True,
        just_files=just_files,
    )
    if not just_files:
        train_data = data.train_data.reshape(len(data.train_data), INPUT_COUNT, 1)
        test_data = data.test_data.reshape(len(data.test_data), INPUT_COUNT, 1)

    level1 = Level1Autoencoder()
    level1.train(train_data, train_data, test_data, test_data)

    # Prepare data by running it through our first level autoencoder
    data = level1.encode(data.files[0].reshape(len(data.files[0]), INPUT_COUNT, 1))

    plotdata = data.reshape(len(data), 128)[:1000]
    plt.subplot(2, 1, 1)
    plt.plot(plotdata)

    data = data[:int(len(data) / LEVEL_2_INPUT_COUNT) * LEVEL_2_INPUT_COUNT]
    data = np.array(np.split(data, len(data) / LEVEL_2_INPUT_COUNT))
    data = data.reshape(len(data), LEVEL_2_INPUT_COUNT, 128)

    # Unload level 1 model
    del level1
    backend.clear_session()

    level2 = Level2Autoencoder()
    level2.train(data, data, data, data)

    output = level2.predict_output(data)
    print(output.shape)

    plotdata = output.reshape(output.shape[0] * output.shape[1], 128)[:1000]
    plt.subplot(2, 1, 2)
    plt.plot(plotdata)
    plt.show()

    print(output.shape)
    output = output.reshape(output.shape[0] * output.shape[1], 1, 128)
    print(output.shape)

    del level2
    backend.clear_session()

    level1 = Level1Autoencoder()
    output = level1.decode(output).flatten()

    data = Dataset()
    data.write_wav(f"output-{NAME}-{MODEL_ID}-level2.wav", output)

    for i in range(min(len(data.files), 2)):
        inp = data.files[i].reshape(len(data.files[i]), INPUT_COUNT, 1)
        output = level1.decode(level1.encode(inp)).reshape(len(data.files[i]), INPUT_COUNT)
        # output = level1.predict_output(inp).reshape(len(data.files[i]), INPUT_COUNT)
        data.write_wav(f"output-{NAME}-{MODEL_ID}-level1-{i}.wav", output)
        print(f"output-{NAME}-{MODEL_ID}-{i}.wav created")
        plt.subplot(2, 2)
        plt.plot(inp.flatten()[2000:8000])
        plt.subplot(2, 2)
        plt.plot(output.flatten()[2000:8000])
        plt.show()
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 21 19:44:05 2017

@author: hh
"""

from keras import backend as K
import numpy as np

#def Solve():

K.set_epsilon(1e-11)
x = K.placeholder(shape=(None, 1))
y = K.placeholder(shape=(None, 1))
label = K.placeholder(shape=(None, 1))

#x= K.variable(value = X_train)
#y = K.variable(value = Y_train)
#label = K.variable(value = L_train)

W = K.variable(value=0.5)  #np.random.rand(1))
b = K.variable(value=1.0)  #np.random.rand(1))
U = K.square(W * x + b)

lamda = K.variable(value=1.0)  #np.random.rand(1))
U2 = y - lamda * U

#U2 = K.concatenate([U, 1-U], axis = -1)