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)
def main(): X, Y, Nsample = getData() reward = np.reshape(pixelDBR.reward(Y, tarwave, wavelength, bandwidth), newshape=(-1, 1)) Pt = Tstatic(reward, Nsample) x = np.arange(10) xs = np.arange(0.1, 1.1, 0.1) Tvalues = [ '0,1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0' ] plt.bar(x, Pt) plt.xticks(x, Tvalues) plt.show() with open('D:/1D_DBR/Figure_plot/Data_distribution(04).csv', "a") as sf: np.savetxt(sf, np.reshape(xs, (1, xs.shape[0])), fmt='%.1f', delimiter=',') with open('D:/1D_DBR/Figure_plot/Data_distribution(04).csv', "a") as sf: np.savetxt(sf, np.reshape(Pt, (1, len(Pt))), fmt='%d', delimiter=',')
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()