def main(output_folder, weight_name_save, n_batch, lr_rate, num_layers, n_hidden): # Load training data sX, sY = getData() ## Define NN ## X = tf.placeholder(tf.float32, [None, INPUT_SIZE], name="input_x") Y = tf.placeholder(tf.float32, [None, OUTPUT_SIZE], name="output_y") weights = [] biases = [] for i in range(0,num_layers): if i ==0: weights.append(init_weights((INPUT_SIZE, n_hidden))) else: weights.append(init_weights((n_hidden, n_hidden))) biases.append(init_bias(n_hidden)) weights.append(init_weights((n_hidden, OUTPUT_SIZE))) biases.append(init_bias(OUTPUT_SIZE)) # Forward propagation Yhat = forwardprop(X, weights, biases, num_layers) loss = tf.reduce_mean(tf.square(Y-Yhat)) train = tf.train.AdamOptimizer(learning_rate=lr_rate).minimize(loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # Training for n in range(int(Nsample*Nfile/n_batch)): input_X = np.reshape(sX[n*n_batch:(n+1)*n_batch], [n_batch, INPUT_SIZE]) output_Y = np.reshape(sY[n*n_batch:(n+1)*n_batch], [n_batch, OUTPUT_SIZE]) feed = {X: input_X, Y: output_Y} sess.run(train, feed_dict=feed) # Save save_weights(weights, biases, output_folder, weight_name_save, num_layers) # Test Tstate = np.random.randint(2, size=N_pixel) TR = pixelDBR.calR(Tstate, dx, N_pixel, wavelength, nh, nl) tX = np.reshape(Tstate, [-1, INPUT_SIZE]) tY = np.reshape(TR, [-1, OUTPUT_SIZE]) NR = sess.run(Yhat, feed_dict={X: tX}) Tloss = sess.run(loss, feed_dict={X: tX, Y: tY}) # sess.close() print("LOSS: ", Tloss) x = np.reshape(wavelength, wavelength.shape[1]) TR = np.reshape(TR, wavelength.shape[1]) NR = np.reshape(NR, wavelength.shape[1]) plt.figure(1) plt.subplot(2, 1, 1) plt.plot(x, TR) plt.subplot(2, 1, 2) plt.plot(x, NR) plt.show()
def main(): X, Y = getData() rX = X * np.reshape(pixelDBR.reward(Y, tarwave, wavelength, bandwidth), newshape=(-1, 1)) rX = np.sum(rX, axis=0) minX = np.min(rX) rX = rX - minX avgX = np.mean(rX) result_state = (rX >= avgX).astype(int) # result_state = np.reshape(result_state, newshape=N_pixel) result_R = pixelDBR.calR(result_state, dx, N_pixel, wavelength, nh, nl) result_R = np.reshape(result_R, newshape=(1, wavelength.shape[1])) result_reward = pixelDBR.reward(result_R, tarwave, wavelength, bandwidth) # result_fwhml, result_fwhmf = pixelDBR.calBand(result_R, wavelength, tarwave, minwave, wavestep, 0.5) # result_99l, result_99f = pixelDBR.calBand(result_R, wavelength, tarwave, minwave, wavestep, 0.99) print("======== Result ========") print('result reward: ', result_reward) # print('resulr fwhm: {} um, {:.3f} THz'.format(result_fwhml, result_fwhmf*10**-12)) # print('resulr 99% width: {} um, {:.3f} THz'.format(result_99l, result_99f*10**-12)) thickness = getThickness(result_state, dx, N_pixel) print(thickness) for idx, x in enumerate(result_state): if idx == 0: print("[{}, ".format(x), end='') elif idx == N_pixel - 1: print("{}]".format(x), end='') else: print("{}, ".format(x), end='') x = np.reshape(wavelength, wavelength.shape[1]) result_R = np.reshape(result_R, wavelength.shape[1]) plt.figure(1) plt.plot(x, result_R) plt.figure(2) x = (c * (1. / wavelength)) x = np.reshape(x * (10**-12), wavelength.shape[1]) plt.plot(x, result_R) plt.figure(3) lx = np.arange(N_pixel) plt.bar(lx, result_state, width=1, color='blue') plt.show()
def Ratio_Optimization(output_folder, weight_name_save, n_batch, lr_rate, num_layers, n_hidden): init_list_rand = tf.constant(np.random.randint(2, size=(1, N_pixel)), dtype=tf.float32) X = tf.get_variable(name='b', initializer=init_list_rand) Xint = binaryRound(X) Xint = tf.clip_by_value(Xint, clip_value_min=0, clip_value_max=1) Y = tf.placeholder(tf.float32, shape=[None, OUTPUT_SIZE], name="output_y") weights, biases = load_weights(output_folder, weight_name_save, num_layers) Yhat = forwardprop(Xint, weights, biases, num_layers) Inval = tf.matmul(Y, tf.transpose(Yhat)) Outval = tf.matmul((1-Y), tf.transpose(Yhat)) # cost = Inval / Outval cost = Outval / Inval optimizer = tf.train.AdamOptimizer(learning_rate=1E-3).minimize(cost, var_list=[X]) design_y = pd.read_csv(PATH + "/SpectFile(200,800)_500.csv", header=None) design_y = design_y.values with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for n in range(10000): sess.run(optimizer, feed_dict={Y: design_y}) if (n % 100) == 0: temp_R = np.reshape(sess.run(Yhat), newshape=(1, wavelength.shape[1])) temp_cost = sess.run(cost, feed_dict={Y: design_y})[0][0] temp_reward = pixelDBR.reward(temp_R, tarwave, wavelength, bandwidth) print("{}th epoch, reward: {:.4f}, cost: {:.4f}".format(n, temp_reward[0], temp_cost)) optimized_x = np.reshape(Xint.eval().astype(int), newshape=N_pixel) # optimized_R = np.reshape(sess.run(Yhat), newshape=(1, wavelength.shape[1])) optimized_R = np.reshape(pixelDBR.calR(optimized_x, dx, N_pixel, wavelength, nh, nl), newshape=(1, wavelength.shape[1])) optimized_reward = pixelDBR.reward(optimized_R, tarwave, wavelength, bandwidth) print("Optimized result: {:.4f}".format(optimized_reward[0])) print(optimized_x) wavelength_x = np.reshape(wavelength, wavelength.shape[1]) optimized_R = np.reshape(optimized_R, wavelength.shape[1]) plt.figure(1) plt.subplot(2, 1, 1) plt.plot(wavelength_x, optimized_R) pixel_x = np.arange(N_pixel) plt.subplot(2, 1, 2) plt.bar(pixel_x, optimized_x, width=1, color="black") plt.show()
def main(): X, Y = getData() rX = X * np.reshape(pixelDBR.reward(Y, tarwave, wavelength, bandwidth), newshape=(-1, 1)) rX = np.sum(rX, axis=0) minX = np.min(rX) rX = rX - minX avgX = np.mean(rX) result_state = (rX >= avgX).astype(int) # result_state = np.reshape(result_state, newshape=N_pixel) result_R = pixelDBR.calR(result_state, dx, N_pixel, wavelength, nh, nl) result_R = np.reshape(result_R, newshape=(1, wavelength.shape[1])) result_reward = pixelDBR.reward(result_R, tarwave, wavelength, bandwidth) result_fwhm = pixelDBR.calFWHM(result_R, wavelength, tarwave) print('result reward: ', result_reward) print('resulr fwhm: ', result_fwhm) x = np.reshape(wavelength, wavelength.shape[1]) result_R = np.reshape(result_R, wavelength.shape[1]) plt.figure(2) plt.subplot(2, 1, 1) plt.plot(x, result_R) lx = np.arange(N_pixel) plt.subplot(2, 1, 2) plt.bar(lx, result_state, width=1, color='blue') # plt.show() plt.figure(1) plt.bar(lx, rX, width=1, color='blue') plt.show() thickness = getThickness(result_state, dx, N_pixel) print(thickness)
import numpy as np import pixelDBR # import tensorflow as tf from dbr_arla_20190517 import N_pixel, dx, nh, nl, wavelength, tarwave, Nsample PATH = 'D:/NBTP_Lab/DBR/DBR_ARLA(python)' TRAIN_PATH = PATH + '/trainset' + '/01' print('N_pixel: {}, nh: {:.3f}, nl: {:.3f}'.format(N_pixel, nh, nl)) for i in range(10): sname = TRAIN_PATH + '/state_' + str(i) + '.csv' Rname = TRAIN_PATH + '/R_' + str(i) + '.csv' n = 0 for n in range(Nsample): state = np.random.randint(2, size=N_pixel) R = pixelDBR.calR(state, dx, N_pixel, wavelength, nh, nl) state = np.reshape(state, (1, N_pixel)) R = np.reshape(R, (1, wavelength.shape[1])) with open(sname, "a") as sf: np.savetxt(sf, state, fmt='%d', delimiter=',') with open(Rname, "a") as Rf: np.savetxt(Rf, R, fmt='%.5f', delimiter=',') if (n + 1) % 1000 == 0: print('{}th {}step'.format(i + 1, n + 1)) print('*****Train Set Prepared*****')
def Ratio_Optimization(output_folder, weight_name_save, n_batch, lr_rate, num_layers, n_hidden): OUTPUT_SIZE = wavelength.shape[1] idx_1 = np.where(wavelength == int(tarwave - bandwidth / 2))[1][0] idx_2 = np.where(wavelength == int(tarwave + bandwidth / 2))[1][0] design_y = np.zeros((1, OUTPUT_SIZE)) design_y[0, idx_1:idx_2 + 1] = 1 design_y.tolist() init_list_rand = tf.constant(np.random.randint(2, size=(1, N_pixel)), dtype=tf.float32) X = tf.get_variable(name='b', initializer=init_list_rand) Xint = binaryRound(X) Xint = tf.clip_by_value(Xint, clip_value_min=0, clip_value_max=1) Y = tf.placeholder(tf.float32, shape=[None, OUTPUT_SIZE], name="output_y") weights, biases = load_weights(output_folder, weight_name_save, num_layers) Yhat = forwardprop(Xint, weights, biases, num_layers) idxwidth = idx_2 - idx_1 Inval = tf.reduce_mean(tf.matmul(Y, tf.transpose(Yhat))) / idxwidth Outval = tf.reduce_mean(tf.matmul( (1 - Y), tf.transpose(Yhat))) / (OUTPUT_SIZE - idxwidth) # cost = Outval / Inval # cost = Outval * (10 - Inval) cost = Outval * (1 - Inval) optimizer = tf.train.AdamOptimizer(learning_rate=1E-4).minimize( cost, var_list=[X]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for n in range(20000): sess.run(optimizer, feed_dict={Y: design_y}) if (n % 100) == 0: temp_R = np.reshape(sess.run(Yhat), newshape=(1, wavelength.shape[1])) temp_cost = sess.run(cost, feed_dict={Y: design_y}) temp_Inval = sess.run(Inval, feed_dict={Y: design_y}) temp_reward = pixelDBR.reward(temp_R, tarwave, wavelength, bandwidth) print( "{}th epoch, reward: {:.4f}, cost: {:.4f}, Inval: {:.4f}". format(n, temp_reward[0], temp_cost, temp_Inval)) op_x = np.reshape(Xint.eval().astype(int), newshape=N_pixel) # op_R = np.reshape(sess.run(Yhat), newshape=(1, wavelength.shape[1])) op_R = np.reshape(pixelDBR.calR(op_x, dx, N_pixel, wavelength, nh, nl), newshape=(1, wavelength.shape[1])) op_reward = pixelDBR.reward(op_R, tarwave, wavelength, bandwidth) op_fwhml, op_fwhmf = pixelDBR.calBand(op_R, wavelength, tarwave, minwave, wavestep, 0.5) op_99l, op_99f = pixelDBR.calBand(op_R, wavelength, tarwave, minwave, wavestep, 0.99) print("======== Result ========") print('result fwhm: {} um, {:.3f} THz'.format(op_fwhml, op_fwhmf * 10**-12)) print('result 99% width: {} um, {:.3f} THz'.format(op_99l, op_99f * 10**-12)) print("Optimized result: {:.4f}".format(op_reward[0])) for idx, x in enumerate(op_x): if idx == 0: print("[{}, ".format(x), end='') elif idx == N_pixel - 1: print("{}]".format(x), end='') else: print("{}, ".format(x), end='') wavelength_x = np.reshape(wavelength, wavelength.shape[1]) op_R = np.reshape(op_R, wavelength.shape[1]) plt.figure(1) plt.plot(wavelength_x, op_R) plt.figure(2) wavelength_x = (c * (1. / wavelength)) * 10**(-12) wavelength_x = np.reshape(wavelength_x, wavelength.shape[1]) plt.plot(wavelength_x, op_R) plt.figure(3) pixel_x = np.arange(N_pixel) plt.bar(pixel_x, op_x, width=1, color="black") plt.show()
def main(output_folder, weight_name_save, n_batch, lr_rate, num_layers, n_hidden): # Load training data sX, sY = getData() Nsample = sY.shape[0] INPUT_SIZE = sX.shape[1] OUTPUT_SIZE = sY.shape[1] Nr = 0.8 Nlearning = int(Nr * Nsample) Ntest = Nsample - Nlearning testX = sX[Nlearning:, :] testY = sY[Nlearning:, :] trainX = sX[0:Nlearning, :] trainY = sY[0:Nlearning, :] trainX_total = trainX trainY_total = trainY n_copy = 10 for i in range(n_copy): trainX, trainY = shuffle_data(trainX, trainY) trainX_total = np.concatenate((trainX_total, trainX), axis=0) trainY_total = np.concatenate((trainY_total, trainY), axis=0) ## Define NN ## X = tf.placeholder(tf.float32, [None, INPUT_SIZE], name="input_x") Y = tf.placeholder(tf.float32, [None, OUTPUT_SIZE], name="output_y") weights = [] biases = [] for i in range(0, num_layers): if i == 0: weights.append(init_weights((INPUT_SIZE, n_hidden))) else: weights.append(init_weights((n_hidden, n_hidden))) biases.append(init_bias(n_hidden)) weights.append(init_weights((n_hidden, OUTPUT_SIZE))) biases.append(init_bias(OUTPUT_SIZE)) # Forward propagation Yhat = forwardprop(X, weights, biases, num_layers) loss = tf.reduce_mean(tf.square(Y - Yhat)) train = tf.train.AdamOptimizer(learning_rate=lr_rate).minimize(loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # Training Training_loss = [] for n in range(int(Nlearning * n_copy / n_batch)): input_X = np.reshape(trainX_total[n * n_batch:(n + 1) * n_batch], [n_batch, INPUT_SIZE]) output_Y = np.reshape(trainY_total[n * n_batch:(n + 1) * n_batch], [n_batch, OUTPUT_SIZE]) feed = {X: input_X, Y: output_Y} _, temp = sess.run([train, loss], feed_dict=feed) Training_loss.append(temp) # Save save_weights(weights, biases, output_folder, weight_name_save, num_layers) # Test Test_loss = [] test_batch = int(n_batch / 20) for n in range(int(Ntest / test_batch)): input_X = np.reshape(testX[n * test_batch:(n + 1) * test_batch], [test_batch, INPUT_SIZE]) output_Y = np.reshape(testY[n * test_batch:(n + 1) * test_batch], [test_batch, OUTPUT_SIZE]) feed = {X: input_X, Y: output_Y} temp = sess.run(loss, feed_dict=feed) Test_loss.append(temp) # Example test Tstate = np.random.randint(2, size=N_pixel) TR = pixelDBR.calR(Tstate, dx, N_pixel, wavelength, nh, nl) tX = np.reshape(Tstate, [-1, INPUT_SIZE]) tY = np.reshape(TR, [-1, OUTPUT_SIZE]) NR = sess.run(Yhat, feed_dict={X: tX}) Tloss = sess.run(loss, feed_dict={X: tX, Y: tY}) # sess.close() print("LOSS: ", Tloss) x = np.reshape(wavelength, wavelength.shape[1]) TR = np.reshape(TR, wavelength.shape[1]) NR = np.reshape(NR, wavelength.shape[1]) plt.figure(1) plt.subplot(2, 1, 1) plt.plot(x, TR) plt.subplot(2, 1, 2) plt.plot(x, NR) plt.figure(3) plt.plot(Training_loss) with open('D:/1D_DBR/FCNN_1THz/Training_loss.csv', 'w') as lossfile: np.savetxt(lossfile, Training_loss, delimiter=',', fmt='%.5f') plt.figure(4) plt.plot(Test_loss) with open('D:/1D_DBR/FCNN_1THz/Test_loss.csv', 'w') as lossfile: np.savetxt(lossfile, Test_loss, delimiter=',', fmt='%.5f') plt.show()