def __constants_compute(self): const_circular, const_hyperbolic = 1.0, 1.0 for i in range(len(self.__circular_data)): const_circular = const_circular * np.sqrt(1 + (1 / (2 ** (2 * i)))) for i in range(len(self.__hyperbolic_data)): const_hyperbolic = const_hyperbolic * np.sqrt(1 - (1 / (2 ** (2 * (i + 1))))) self.__const_circular = coding(1 / const_circular, self.__resolution) self.__const_hyperbolic = coding(1 / const_hyperbolic, self.__resolution)
def __select_f(self, coord, d, i): if coord == 'circular': f = np.arctan(d / (2 ** i)) elif coord == 'hyperbolic': f = np.arctanh(d / (2 ** i)) else: f = d / (2 ** i) return coding(f, self.__resolution)
def get_batch_from_file_ctc(self, data_file, batch_size): count = 0 labels_temp = [] inputs_temp = [] file = open(data_file) #读取训练数据 while 1: line = file.readline() #if count%10000 == 0: # print count,line if not line: break if "NONE" in line: continue linelist = line.strip().split() topwords = linelist[0] #上屏词作为label pinyin = linelist[1] #输入串作为Input if len(topwords) % 2 != 0: #print "Bad case",line continue label_len = len(topwords) / 2 label_temp = [] #if int(label_len)+2 > len(linelist): # continue for i in range(int(label_len)): #label_char = linelist[2+i] #读取上屏词在词表中的位置作为label label_char = linelist[3 + int(label_len) + i] #读取上屏词在词表中的位置作为label label_temp.append(int(label_char)) if len(label_temp) == 0: continue count += 1 inputs_temp.append(coding(pinyin)) labels_temp.append(np.array(label_temp).astype(np.int64)) #inputs = np.asarray(coding(pinyin)) #labels = np.asarray(np.array(label_temp).astype(np.int64)) num_examples = count num_batches_per_epoch = num_examples // batch_size inputs = np.asarray(inputs_temp) labels = np.asarray(labels_temp) for batch in range(num_batches_per_epoch): #Getting the index indexes = [ i % num_examples for i in range(batch * batch_size, (batch + 1) * batch_size) ] yield inputs[indexes], labels[indexes]
if not line: break linelist = line.strip().split() topwords = linelist[0] #上屏词作为label pinyin = linelist[1] #输入串作为Input if len(topwords) % 2 != 0: print "Bad case", line label_len = len(topwords) / 2 label_temp = [] if int(label_len) + 4 > len(linelist): continue for i in range(int(label_len)): label_char = linelist[4 + i] #读取上屏词在词表中的位置作为label label_temp.append(int(label_char)) labels_temp.append(np.array(label_temp).astype(np.int64)) inputs_temp.append(coding(pinyin)) num_examples = count splits_num = int(num_examples * 0.9) #切分训练数据 num_epochs = int(sys.argv[2]) #迭代次数 test_num = int(sys.argv[3]) #测试样本数 num_batches_per_epoch = int(0.9 * num_examples / batch_size) num_batches_per_epoch_for_test = int(0.1 * num_examples / batch_size) #inputs, labels = fake_data(num_examples, num_features, num_classes - 1) inputs = np.asarray(inputs_temp) labels = np.asarray(labels_temp) # preprocess the input data train_inputs = inputs[:splits_num] #0.9 for train test_inputs = inputs[splits_num:] #0.1 for test
def create_files_to_simulate(resolution=14): angles = np.arange(-89, 89, 1) path_input = os.path.abspath(__file__) path_dir, _ = os.path.split(path_input) path_input_dir = os.path.join(path_dir, 'input') inputs_files = [ 'input_x.txt', 'input_y.txt', 'input_z.txt', 'input_mode.txt', 'input_coor.txt', 'input_enable.txt' ] path_inputs = [] for file in inputs_files: path_inputs.append(os.path.join(path_input_dir, file)) axes_circular, axes_hyperbolic, axes_arctanh = [], [], [] x, y, z = [], [], [] # Values to simulate the module enable, mode, coord = [], [], [] # Values to simulate the module sin, cos, arctan = [], [], [] # Values to compare sinh, cosh, arctanh = [], [], [] # Values to compare for angle in angles: angle_rad = deg_to_rad(angle) angle_fixed = coding(angle_rad, resolution) axes_circular.append(angle) # Enable enable.append(1) # --- Circular coordinate system # Adding data for vectoring mode x.append(coding(np.cos(angle_rad), resolution)) y.append(coding(np.sin(angle_rad), resolution)) z.append(0) mode.append(1) coord.append(0) # Adding data for rotation mode x.append(0) y.append(0) z.append(angle_fixed) mode.append(0) coord.append(0) # Save real data to compare with the simulation arctan.append( rad_to_deg(np.arctan2(np.sin(angle_rad), np.cos(angle_rad)))) sin.append(np.sin(angle_rad)) cos.append(np.cos(angle_rad)) # --- Hyoperbolic coordinate system if (abs(angle)) < 61: if (abs( coding(np.sin(angle_rad), resolution) / coding(np.cos(angle_rad), resolution))) < 0.6: # Adding data for vectoring mode x.append(coding(np.cos(angle_rad), resolution)) y.append(coding(np.sin(angle_rad), resolution)) z.append(0) mode.append(1) coord.append(1) # Save real data to compare with the simulation arctanh.append( rad_to_deg( np.arctanh(np.sin(angle_rad) / np.cos(angle_rad)))) axes_arctanh.append(np.sin(angle_rad) / np.cos(angle_rad)) # Adding data for rotation mode x.append(0) y.append(0) z.append(angle_fixed) mode.append(0) coord.append(1) # Save real data to compare with the simulation sinh.append(np.sinh(angle_rad)) cosh.append(np.cosh(angle_rad)) axes_hyperbolic.append(angle_rad) write_file(path_inputs[0], x) write_file(path_inputs[1], y) write_file(path_inputs[2], z) write_file(path_inputs[3], mode) write_file(path_inputs[4], coord) write_file(path_inputs[5], enable) return sin, cos, arctan, sinh, cosh, arctanh, axes_circular, axes_hyperbolic, axes_arctanh
def division(self, x, y): x_current = coding(x, self.__resolution) y_current, z_current = coding(y, self.__resolution), 0.0 _, _, z = self.__iterations_compute(x_current, y_current, z_current, mode='vectoring', coord='linear') return z
def product(self, x, z): x_current = coding(x, self.__resolution) y_current, z_current = 0.0, coding(z, self.__resolution) _, y, _ = self.__iterations_compute(x_current, y_current, z_current, mode='rotation', coord='linear') return y
def arctanh(self, x, y): x_current = coding(x, self.__resolution) y_current, z_current = coding(y, self.__resolution), 0.0 _, _, z = self.__iterations_compute(x_current, y_current, z_current, mode='vectoring', coord='hyperbolic') return rad_to_deg(z)
def cosh_sinh(self, angle_deg): angle_rad = coding(deg_to_rad(angle_deg), self.__resolution) x_current = self.__const_hyperbolic y_current, z_current = 0.0, angle_rad x, y, _ = self.__iterations_compute(x_current, y_current, z_current, mode='rotation', coord='hyperbolic') return x, y
def cos_sin(self, angle_deg): angle_rad = coding(deg_to_rad(angle_deg), self.__resolution) x_current = self.__const_circular y_current, z_current = 0.0, angle_rad x, y, _ = self.__iterations_compute(x_current, y_current, z_current, mode='rotation', coord='circular') return x, y