def dream(): c = constants() p = params() # connect to matlab eng = mo.matlab_connection(c.MATLAB_SESSION) x = tf.placeholder(tf.float32, shape=(1, 100, 2)) logits = model(x, c) logits = tf.reshape(logits, [-1, c.MAX_TIME, f.OUTPUT_DIM]) init_op = tf.global_variables_initializer() rand_state = np.random.uniform(-.005, .005, (1, 100, 2)) t = np.linspace(0, 1, 100) x1 = .65 + .25 * np.cos(4.5 * 2.0 * np.pi * t) x2 = .45 + .25 * np.cos(6.2 * 2.0 * np.pi * t) istate = np.asarray([x1, x2]) istate = [np.transpose(istate)] istate = istate + rand_state with tf.Session() as sess: # initialize global variables sess.run(init_op) # enable batch fetchers coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) #setup saver saver = u.saver_ops(sess=sess, max_to_keep=3, global_step=p.global_step) xs = [] logi = [] # run the training operation for indx in range(1200): feed_dict = {x: istate} ex, ostate = sess.run([x, logits], feed_dict=feed_dict) logi.append(ostate[0]) xs.append(ex[0]) istate = np.roll(istate, -1, axis=1) istate[-1, -1, :] = ostate[-1, -1, :] u.display_update(indx, 20) eng.put_var('logits', logi) eng.put_var('x', xs) # matlab command => # >>plot( reshape(logits(1,:,:), [100,2]) ) coord.request_stop() coord.join(threads)
def train(): c = constants() p = params() x, y = f.inputs([f.TRAIN_TF_RECORDS_FILE], shuffle=True, name='inputs') u.shape_log(x) u.shape_log(y) logits = model(x, c) logits = tf.reshape(logits, [-1, c.MAX_TIME, f.OUTPUT_DIM]) labels = tf.reshape(y, [-1, c.MAX_TIME, f.OUTPUT_DIM]) loss = tf.reduce_mean(tf.square(logits - labels)) train_op = tf.train.AdamOptimizer(learning_rate=0.05).minimize( loss, global_step=p.global_step) init_op = tf.global_variables_initializer() with tf.Session() as sess: # initialize global variables sess.run(init_op) # enable batch fetchers coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) #setup saver saver = u.saver_ops(sess=sess, max_to_keep=3, global_step=p.global_step) eng = mo.matlab_connection(c.MATLAB_SESSION) # run the training operation for indx in range(20000): _, log = sess.run([train_op, logits]) # logi.append(log) u.display_update(indx, 25) if ((indx + 1) % 2000 == 0): saver.save() if ((indx) % 5 == 0): # CHECK EMERGENCY STOP if eng.should_stop(): print('stopping') break # connect to matlab # eng = mo.matlab_connection(c.MATLAB_SESSION) # eng.put_var('logits', logi) saver.save() coord.request_stop() coord.join(threads)
def train(): c = constants() p = params() # connect to matlab eng = mo.matlab_connection(c.MATLAB_SESSION) # matlab initialization... # [b,a] = butter(7, .2); # x = [zeros(1,10), ones(1,90)]; # y = filter(b,a,x); mx = eng.get_var('x') my = eng.get_var('y') x = tf.constant(mx, dtype=tf.float32) logits = model(x, c) labels = tf.constant(my, dtype=tf.float32) labels = tf.reshape(labels, [c.MAX_TIME]) loss = tf.reduce_mean(tf.square(logits - labels)) train_op = tf.train.AdamOptimizer(learning_rate=0.05).minimize( loss, global_step=p.global_step) init_op = tf.global_variables_initializer() with tf.Session() as sess: # initialize global variables sess.run(init_op) #setup saver saver = u.saver_ops(sess=sess, max_to_keep=3, global_step=p.global_step) logi = [] # run the training operation for indx in range(750): _, log = sess.run([train_op, logits]) logi.append(log) u.display_update(indx, 250) eng.put_var('logits', logi) saver.save()
def train(): c = constants() some_example_random_number = -0.31415 x = tf.constant(some_example_random_number, dtype=tf.float32) x = tf.reshape(x, [1, 1]) d = tf.layers.dense(x, units=2, name='dense_1') e = tf.layers.dense(d, units=1) weights, biases = get_dense_vars('dense_1') print(d) print(weights) print(biases) # https://stackoverflow.com/questions/45372291/how-to-get-weights-in-tf-layers-dense labels = tf.constant(1.0, dtype=tf.float32) loss = tf.reduce_sum(tf.square(e - labels)) train_op = tf.train.AdamOptimizer(learning_rate=0.5).minimize(loss) init_op = tf.global_variables_initializer() # connect to matlab eng = m.matlab_connection(c.MATLAB_SESSION) with tf.Session() as sess: # initialize global variables sess.run(init_op) xs = [] ws = [] bs = [] # run the training operation for indx in range(1000): _, x_eval, w, b = sess.run([train_op, x, weights, biases]) xs.append(x_eval) ws.append(w) bs.append(b) # print("x = {}".format(x_eval)) eng.put_var('xs', xs) eng.put_var('ws', ws) eng.put_var('bs', bs)
def test(): c = constants() p = params() # connect to matlab eng = mo.matlab_connection(c.MATLAB_SESSION) # matlab initialization... # [b,a] = butter(7, .2); # x = [zeros(1,10), ones(1,90)]; # y = filter(b,a,x); mx = eng.get_var('s') my = eng.get_var('t') x = tf.constant(mx, dtype=tf.float32) logits = model(x, c) labels = tf.constant(my, dtype=tf.float32) labels = tf.reshape(labels, [c.MAX_TIME]) init_op = tf.global_variables_initializer() with tf.Session() as sess: # initialize global variables sess.run(init_op) #setup saver saver = u.saver_ops(sess=sess, max_to_keep=3, global_step=p.global_step) logi = [] labs = [] # run the training operation for indx in range(1): log, lab = sess.run([logits, labels]) logi.append(log) labs.append(lab) u.display_update(indx, 250) eng.put_var('logits', logi) eng.put_var('labels', labs) saver.save()
def train(): c = constants() p = params() x, y = f.inputs([f.TRAIN_TF_RECORDS_FILE], name='inputs') u.shape_log(x) u.shape_log(y) logits = model(x, c) labels = tf.reshape(y, [c.MAX_TIME]) loss = tf.reduce_mean(tf.square(logits - labels)) train_op = tf.train.AdamOptimizer(learning_rate=0.05).minimize( loss, global_step=p.global_step) init_op = tf.global_variables_initializer() with tf.Session() as sess: # initialize global variables sess.run(init_op) # enable batch fetchers coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) #setup saver saver = u.saver_ops(sess=sess, max_to_keep=3, global_step=p.global_step) logi = [] # run the training operation for indx in range(10000): _, log = sess.run([train_op, logits]) logi.append(log) u.display_update(indx, 50) # connect to matlab eng = mo.matlab_connection(c.MATLAB_SESSION) eng.put_var('logits', logi) saver.save() coord.request_stop() coord.join(threads)
def test(): c = constants() p = params() # connect to matlab eng = mo.matlab_connection(c.MATLAB_SESSION) x, y = f.inputs([f.TEST_TF_RECORDS_FILE], shuffle=True, name='inputs') u.shape_log(x) u.shape_log(y) logits = model(x, c) logits = tf.reshape(logits, [-1, c.MAX_TIME, f.OUTPUT_DIM]) labels = tf.reshape(y, [-1, c.MAX_TIME, f.OUTPUT_DIM]) init_op = tf.global_variables_initializer() with tf.Session() as sess: # initialize global variables sess.run(init_op) # enable batch fetchers coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) #setup saver saver = u.saver_ops(sess=sess, max_to_keep=3, global_step=p.global_step) logi = [] labs = [] # run the training operation for indx in range(6): log, lab = sess.run([logits, labels]) logi.append(log) labs.append(lab) u.display_update(indx, 250) eng.put_var('logits', logi) eng.put_var('labels', labs) coord.request_stop() coord.join(threads)
def train(): c = constants() # connect to matlab eng = mo.matlab_connection(c.MATLAB_SESSION) # matlab initialization... # [b,a] = butter(7, .2); # x = [zeros(1,10), ones(1,90)]; # y = filter(b,a,x); mx = eng.get_var('x') my = eng.get_var('y') x = tf.constant(mx, dtype=tf.float32) logits = model(x, c) labels = tf.constant(my, dtype=tf.float32) labels = tf.reshape(labels, [100]) loss = tf.reduce_mean(tf.square(logits - labels)) train_op = tf.train.AdamOptimizer(learning_rate=0.05).minimize(loss) init_op = tf.global_variables_initializer() with tf.Session() as sess: # initialize global variables sess.run(init_op) logi = [] # run the training operation for indx in range(750): _, log = sess.run([train_op, logits]) logi.append(log) # post an update if ((indx + 1) % 250 == 0): print("[step: {}]".format(indx)) eng.put_var('logits', logi)