Created by: 123malin
add linear regression
62 shape=[1], 63 dtype='float32', 64 default_initializer=fluid.initializer.ConstantInitializer(value=0)) 65 66 self.predict = fluid.layers.relu(weight_sum + b_linear) 67 cost = fluid.layers.square_error_cost( 68 input=self.predict, label=self.label) 69 avg_cost = fluid.layers.reduce_sum(cost) 70 71 self._cost = avg_cost 72 73 self._metrics["COST"] = self._cost 74 self._metrics["Predict"] = self.predict 75 if is_infer: 76 self._infer_results["Predict"] = self.predict 77 self._infer_results["COST"] = self._cost 125 with open(path) as f: 126 contents = f.readlines() 127 lines_per_file = len(contents) / num 128 print("contents: ", str(len(contents))) 129 print("lines_per_file: ", str(lines_per_file)) 130 131 for i in range(1, num + 1): 132 with open(os.path.join(output_dir, "part_" + str(i)), 'w') as fout: 133 data = contents[(i - 1) * lines_per_file:min(i * lines_per_file, 134 len(contents))] 135 for line in data: 136 fout.write(line) 137 138 139 if __name__ == "__main__": 140 random.seed(1111111) 125 with open(path) as f: 126 contents = f.readlines() 127 lines_per_file = len(contents) / num 128 print("contents: ", str(len(contents))) 129 print("lines_per_file: ", str(lines_per_file)) 130 131 for i in range(1, num + 1): 132 with open(os.path.join(output_dir, "part_" + str(i)), 'w') as fout: 133 data = contents[(i - 1) * lines_per_file:min(i * lines_per_file, 134 len(contents))] 135 for line in data: 136 fout.write(line) 137 138 139 if __name__ == "__main__": 140 random.seed(1111111) 102 arr = in_str.split(":") 103 tmp_arr = arr[1].split(" ") 104 out_str = "" 105 for item in tmp_arr: 106 item = item.strip() 107 if item != "": 108 key = "%s:%s" % (arr[0], item) 109 out_str += "%s " % (to_hash(key)) 110 return out_str.strip() 111 112 113 def get_hash(path): 114 #0-34831 1-time:974673057 2-userid:2021 3-gender:M 4-age:25 5-occupation:0 6-movieid:1345 7-title:Carrie (1976) 8-genres:Horror 9-label:2 115 for line in open(path): 116 arr = line.strip().split("\t") 117 out_str = "logid:%s %s %s %s %s %s %s %s %s %s" % \ 30 test_user_path = "online_user" 31 32 33 def process(path, output_path): 34 user_dict = parse_data(data_path + "/users.dat", user_fea) 35 movie_dict = parse_movie_data(data_path + "/movies.dat", movie_fea) 36 37 res = [] 38 for line in open(path): 39 line = line.strip() 40 arr = line.split("::") 41 userid = arr[0] 42 movieid = arr[1] 43 out_str = "time:%s\t%s\t%s\tlabel:%s" % (arr[3], user_dict[userid], 44 movie_dict[movieid], arr[2]) 45 log_id = hash(out_str) % 1000000000 26 27 def _init_hyper_parameters(self): 28 self.sparse_feature_number = envs.get_global_env( 29 "hyper_parameters.sparse_feature_number", None) 30 self.reg = envs.get_global_env("hyper_parameters.reg", 1e-4) 31 32 def net(self, inputs, is_infer=False): 33 init_value_ = 0.1 34 is_distributed = True if envs.get_trainer() == "CtrTrainer" else False 35 36 # ------------------------- network input -------------------------- 37 38 sparse_var = self._sparse_data_var 39 self.label = self._dense_data_var[0] 40 41 def embedding_layer(input):