Beispiel #1
0
    def test_iteration(self):
        Constants.n = 10
        Constants.tau = 0.1
        t = 1.8

        print "----------------------------------------------"
        print "pradedama nuo: "
        print "----------------------------------------------"
        u_now = Functions.u_exact_range(t)
        Helpers.pretty_print_complex(u_now)

        u_new_expected = Functions.u_exact_range(t + Constants.tau)
        u_new_actual = Algorithm._iteration_block(u_now, t)

        print "----------------------------------------------"
        print "lauktas rezultatas: "
        print "----------------------------------------------"
        Helpers.pretty_print_complex(u_new_expected)
        print "----------------------------------------------"
        print "gautas rezultatas: "
        print "----------------------------------------------"
        Helpers.pretty_print_complex(u_new_actual)

        print "Max netiktis: ", Functions.u_error(u_new_actual, t)
        self.assertTrue(True)
 def plot():
     plt.figure(figsize=(20, 10))
     plt.subplot(221)
     loss = list(map(float, [i for i in Functions.read_points('loss').split('\n') if i]))
     all = plt.plot(loss, c='r')
     plt.legend(all, 'Loss')
     plt.title("Training Loss")
     plt.subplot(222)
     train_acc = list(map(float, [i for i in Functions.read_points('train_acc').split('\n') if i]))
     all1 = plt.plot(train_acc, c='g')
     plt.legend(all1, 'Training Accuracy')
     plt.title("Training Accuracy")
     plt.subplot(223)
     valid_acc = list(map(float, [i for i in Functions.read_points('valid_acc').split('\n') if i]))
     all2 = plt.plot(valid_acc, c='purple')
     plt.legend(all2, 'Validation accuracy')
     plt.title("Validation accuracy")
     plt.subplot(224)
     time = list(map(float, [i for i in Functions.read_points('time').split('\n') if i]))
     all3 = plt.plot(time, c='b')
     plt.legend(all3, 'time')
     plt.title("Time")
     plt.savefig(os.path.dirname(os.path.realpath(
         os.path.dirname(os.path.realpath(__file__)))) + '/graphs/plot.png')
     print('*** Image of plot saved successfully ***')
Beispiel #3
0
    def test_f_prime(self):
        print "-------------------------------------"
        print "-- F testas"
        print "-------------------------------------"
        x, t = 0.7, 1.7

        Constants.beta = 1
        Constants.n = 5             # pradinis intervalu skaicius
        Constants.tau = 0.2         # pradinis laiko zingsnis
        error = []                  # netiktys
        for i in range(0, 15):
            # tikrinamos reiksmes is tiksliu funkciju
            u_now0 = Functions.u_exact(x - Constants.h(), t)
            u_now1 = Functions.u_exact(x, t)
            u_now2 = Functions.u_exact(x + Constants.h(), t)
            u_next0 = Functions.u_exact(x - Constants.h(), t + Constants.tau)
            u_next1 = Functions.u_exact(x, t + Constants.tau)
            u_next2 = Functions.u_exact(x + Constants.h(), t + Constants.tau)
            f_now = Functions.f(x,t)
            f_next = Functions.f(x, t + Constants.tau)

            # print "F testo f_now, f_next:", f_now, f_next

            f_prime_val = Functions.f_prime(u_now0, u_now1, u_now2, u_next0, u_next1, u_next2, f_now, f_next)
            # print "F reiksme:", Constants.h(), f_prime_val
            result = u_now2 - (Constants.u_const() * u_now1) + u_now0 + f_prime_val
            error.append(abs(result))
            # print "h, tau = ", Constants.h(), ",", Constants.tau
            # print result
            # print "-------------------------------------"

            Constants.n *= 2
            Constants.tau /= 2.0
        Helpers.pretty_print_complex(error)
        self.assertTrue(True)
Beispiel #4
0
 def test_iteration_precision(self):
     t = 0.8
     Constants.n = 10
     Constants.tau = 1.0
     errors = []
     for i in range(0, 5):
         u_now = Functions.u_exact_range(t)                              # tikslios reiksmes dabartiniu laiko momentu
         u_next = Algorithm._iteration_block(u_now, t)                   # isskaiciuotos reikmes sekanciu laiko momentu
         errors.append(Functions.u_error(u_next, t + Constants.tau))     # netiktis
         Constants.n *= 3
         Constants.tau /= 3
     Helpers.pretty_print_complex(errors)
     self.assertTrue(True)
Beispiel #5
0
    def test_algorithm(self):
        print "-------------------------------------"
        print "-- Viso algoritmo testas"
        print "-------------------------------------"        
        Constants.n = 4
        Constants.tau = 0.1
        errors = []
        for i in range(0, 5):
            u_initial = Functions.u_exact_range(0.0)            # tikslios reiksmes pradiniu laiko momentu
            func_points, time_points = Algorithm.run(u_initial)
            max_error = Functions.u_error_total(func_points, time_points)
            errors.append(max_error)
            Constants.n *= 2
            # Constants.tau /= 2

        Helpers.pretty_print_complex(errors)
        self.assertTrue(True)
Beispiel #6
0
    def test_solve(self):
        t = 2.7                             # koks nors laiko momentas
        u = Functions.u_exact_range(t)      # laukiamos tikslios reiksmes

        # sufabrikuotos desines lygybes puses reiksmes (F)
        fake_f_prime = []
        for i in range(1, len(u) - 1):
            f = u[i+1] - (Constants.u_const() * u[i]) + u[i-1]
            # butinai reikia padauginti is -1, nes algoritmas tikisi F reiksmiu, bet desinese lygybiu pusese yra -F
            f *= -1
            fake_f_prime.append(f)

        # testas
        actual = ThomasAlgorithm.solve(Constants.u_const(), fake_f_prime, Constants.kappa, Constants.gamma)
        equal = Helpers.almost_equal_complex(u, actual)
        self.assertTrue(equal)
Beispiel #7
0
    def test_f(self):
        print "-------------------------------------"
        print "-- f(x,t) testas"
        print "-------------------------------------"        
        x, t = 0.42, 0.8

        Constants.beta = 3
        Constants.n = 10             # pradinis intervalu skaicius
        Constants.tau = 0.1         # pradinis laiko zingsnis
        errors = []

        for i in range(0, 7):
            print Constants.h(), Constants.tau
            # print x, t
            # tikrinamos reiksmes is tiksliu funkciju
            u_now0 = Functions.u_exact(x - Constants.h(), t)
            u_now1 = Functions.u_exact(x, t)
            u_now2 = Functions.u_exact(x + Constants.h(), t)
            u_next0 = Functions.u_exact(x - Constants.h(), t + Constants.tau)
            u_next1 = Functions.u_exact(x, t + Constants.tau)
            u_next2 = Functions.u_exact(x + Constants.h(), t + Constants.tau)
            f_now = Functions.f(x,t)
            f_next = Functions.f(x, t + Constants.tau)

            # kaire algoritmo lygybes puse
            left = (u_next1 - u_now1) / Constants.tau

            # desine algoritmo lygybes puse
            right11 = (u_next2 - 2.0 * u_next1 + u_next0) / pow(Constants.h(), 2)
            right12 = (u_now2 - 2.0 * u_now1 + u_now0) / pow(Constants.h(), 2)
            right1 = 1j * 0.5 * (right11 + right12)
            right21 = (pow(abs(u_next2), 2) * u_next2 - pow(abs(u_next0), 2) * u_next0) / (Constants.h() * 2.0)
            right22 = (pow(abs(u_now2), 2) * u_now2 - pow(abs(u_now0), 2) * u_now0) / (Constants.h() * 2.0)
            right2 = Constants.beta * 0.5 * (right21 + right22)
            right3 = (f_next + f_now) / 2.0
            right = right1 + right2 + right3
            
            errors.append(abs(left - right))
            Constants.n *= 10
            Constants.tau /= 10.0
        Helpers.pretty_print_complex(errors)
        # self.assertTrue(Helpers.sequence_descending(errors))
        self.assertTrue(True)
Beispiel #8
0
model_history_path = '../train_info/{}_{}_modelHis.dat'.format(
    data_name, epochs)
history_path = '../train_info/{}_{}_his.jpg'.format(data_name, epochs)
pred_path = '../train_info/{}_{}_pred.jpg'.format(data_name, epochs)
target_size = (32, 32)
classes = 10
num_pred = 30
batch_size = 32
gray = False
load_existent_model = False

# load train and test data
print('loading data ...')
# (x_train, y_train), (x_test, y_test) = Funcs.load_data(dirs, target_size, gray=gray, classes=classes)
(x_train, y_train), (x_test, y_test) = load_data()
(x_train, y_train) = Funcs.trans_data(x_train, y_train, classes)
(x_test, y_test) = Funcs.trans_data(x_test, y_test, classes)
print('loading data done')

# define callbacks for models to render model performance better
checkpoint = ModelCheckpoint(model_save_path,
                             monitor='val_acc',
                             save_best_only=True,
                             verbose=0)
lrschedual = LearningRateScheduler(
    lambda epoch: Funcs.lr_schedual(epoch, epochs=epochs), verbose=0)

# whether load existent model
if load_existent_model and os.path.exists(model_load_path):
    model = load_model(model_load_path)
else:
model_load_path = '../models/{}_{}_{}.h5'.format(data_name, epochs, 9947)
model_history_path = '../train_info/{}_{}_modelHis.dat'.format(
    data_name, epochs)
history_path = '../train_info/{}_{}_his.jpg'.format(data_name, epochs)
pred_path = '../train_info/{}_{}_pred.jpg'.format(data_name, epochs)
target_size = (28, 28)
classes = 10
num_pred = 30
batch_size = 32
gray = True
load_existent_model = False

# load train and test data
print('loading data ...')
(x_train, y_train), (x_test, y_test) = Funcs.load_data(dirs,
                                                       target_size,
                                                       gray=gray,
                                                       classes=classes)
print('loading data done')

# define callbacks for models to render model performance better
checkpoint = ModelCheckpoint(model_save_path,
                             monitor='val_acc',
                             save_best_only=True,
                             verbose=1)
lrschedual = LearningRateScheduler(
    lambda epoch: Funcs.lr_schedual(epoch, epochs=epochs), verbose=0)

# whether load existent model
if load_existent_model and os.path.exists(model_load_path):
    model = load_model(model_load_path)
else:
Beispiel #10
0
Created on 08.03.2016

@author: cypher9
'''
from src.info import Info
from src.functions import Functions
from src.xml_func import write_xml
from src.crypto import set_password,change_password
from os.path import isfile



if __name__ == '__main__':
    info = Info()
    info.start_text()
    function = Functions()
    if isfile('termine.enc'):
        function.xml_to_cal()
    else:
        set_password()
        write_xml("")
        
    
    
options = {1 : function.add_event,
           2 : info.submenu_search,
           3 : info.submenu_view,
           4 : info.submenu_delete,
           5 : function.print_calendar,
           6 : info.help,
           7 : change_password,
Beispiel #11
0
'''
Created on 08.03.2016

@author: cypher9
'''
from src.info import Info
from src.functions import Functions
from src.xml_func import write_xml
from src.crypto import set_password, change_password
from os.path import isfile

if __name__ == '__main__':
    info = Info()
    info.start_text()
    function = Functions()
    if isfile('termine.enc'):
        function.xml_to_cal()
    else:
        set_password()
        write_xml("")

options = {
    1: function.add_event,
    2: info.submenu_search,
    3: info.submenu_view,
    4: info.submenu_delete,
    5: function.print_calendar,
    6: info.help,
    7: change_password,
    0: function.quit
}
Beispiel #12
0
 def loader(self):
     _, (self.source_vocab_to_int, self.target_vocab_to_int), \
     (self.source_int_to_vocab, self.target_int_to_vocab) = Main().load_process()
     self.load_path = Functions().load_params()
Beispiel #13
0
#!/usr/bin/python
import sys
from src.algorithm import Algorithm
from src.constants import Constants
from src.functions import Functions

runs = int(sys.argv[1])

# skaiciavimai
Constants.n = 5
Constants.tau = 0.1

# penkis kartus mazinami zingsniai
for i in range(0, runs):
    u_initial = Functions.u_exact_range(0)
    func_points, time_points = Algorithm.run(u_initial)
    error_total = Functions.u_error_total(func_points, time_points)
    print "h = %.5f, tau = %.5f, netiktis: %.10f" % (Constants.h(), Constants.tau, error_total)
    Constants.n *= 2
    Constants.tau /= 2.0
Beispiel #14
0
#!/usr/bin/python

from src.functions import Functions
from src.constants import Constants
from src.algorithm import Algorithm
from src.plot import Plot

# testine animacija
Constants.n = 10
Constants.tau = 0.01
u_initial = Functions.u_exact_range(0)
func_points, time_points = Algorithm.run(u_initial)

p = Plot()
p.set_data(func_points, time_points)
p.animate(5.0)
 def train(self):
     epochs, batch_size, rnn_size, _, _, _, learning_rate, keep_probability = Params(
     ).get()
     train_source = self.source_int_text[batch_size:]
     train_target = self.target_int_text[batch_size:]
     valid_source = Functions().pad_sentence_batch(
         self.source_int_text[:batch_size])
     valid_target = Functions().pad_sentence_batch(
         self.target_int_text[:batch_size])
     with tf.Session(graph=self.train_graph) as sess:
         sess.run(tf.global_variables_initializer())
         for epoch_i in range(epochs):
             for batch_i, (source_batch, target_batch) \
                     in enumerate(Functions().batch_data(train_source, train_target, batch_size)):
                 start_time = time.time()
                 _, loss = sess.run(
                     [self.train_op, self.cost], {
                         self.input_data: source_batch,
                         self.targets: target_batch,
                         self.lr: learning_rate,
                         self.sequence_length: target_batch.shape[1],
                         self.keep_prob: keep_probability
                     })
                 batch_train_logits = sess.run(self.inference_logits, {
                     self.input_data: source_batch,
                     self.keep_prob: 1.0
                 })
                 batch_valid_logits = sess.run(self.inference_logits, {
                     self.input_data: valid_source,
                     self.keep_prob: 1.0
                 })
                 train_acc = self.get_accuracy(target_batch,
                                               batch_train_logits)
                 valid_acc = self.get_accuracy(np.array(valid_target),
                                               batch_valid_logits)
                 end_time = time.time()
                 time_ = end_time - start_time
                 sys.stdout.write(
                     '\r epoch: {:>3} batch: {:>4}/{} - train_acc: {:>6.3f}, validation_acc: {:>6.3f}, Loss: {:>6.3f} time: {:>6.3f}s'
                     .format(epoch_i, batch_i,
                             len(self.source_int_text) // batch_size,
                             train_acc, valid_acc, loss, time_))
                 Functions.save_points(loss, 'loss')
                 Functions.save_points(train_acc, 'train_acc')
                 Functions.save_points(valid_acc, 'valid_acc')
                 Functions.save_points(time_, 'time')
         saver = tf.train.Saver()
         saver.save(sess, self.save_path)
         sys.stdout.write('\r Training finished, model saved....')
         Functions().save_params(self.save_path)
Beispiel #16
0
dirs = '../data/{}'.format(data_name)
json_path = '{}/index.json'.format(dirs)
model_save_path = '../models/{}_{}.h5'.format(data_name, epochs)
model_load_path = '../models/{}_{}_{}.h5'.format(data_name, epochs, 10)
model_history_path = '../train_info/{}_{}_modelHis.dat'.format(data_name, epochs)
history_path = '../train_info/{}_{}_his.jpg'.format(data_name, epochs)
pred_path = '../train_info/{}_{}_pred.jpg'.format(data_name, epochs)
target_size = (224, 224)
num_pred = 30
batch_size = 32
gray = False
load_existent_model = False

# load train and test data
print('loading data ...')
(images, labels, counts) = Funcs.read_images_from_directories(dirs, target_size, get_counts=True, init_json=True,
                                                              shuffle=True)
classes = len(counts)
(x_train, y_train), (x_test, y_test) = Funcs.split_train_test(images, labels, counts)
(x_train, y_train) = Funcs.trans_data(x_train, y_train, classes)
(x_test, y_test) = Funcs.trans_data(x_test, y_test, classes)
print('loading data done')

# define callbacks for models to render model performance better
checkpoint = ModelCheckpoint(model_save_path, monitor='val_acc', save_best_only=True, verbose=0)
lrschedual = LearningRateScheduler(lambda epoch: Funcs.lr_schedual(epoch, epochs=epochs), verbose=0)

# whether load existent model
if load_existent_model and os.path.exists(model_load_path):
    model = load_model(model_load_path)
else:
    model = model_design(x_train.shape[1:], classes)