C++读取txt文本数据到vector / 读取文本文件的列并按顺序存到map里
//读取txt文本中的float数据(按行读取)
void read_txt_input(std::string path,std::vector<float> &pydata){
std::ifstream infile;
infile.open(path);
float value;
if(!infile){
std::cout<<"file not found"<<endl;
exit(1);
}
while(infile>>value){
pydata.push_back(value);
}
infile.close();
}
//工作中修改tensor数据方式
std::vector<float> py_y_output;
std::string path="";
read_txt_input(path,py_y_output);
int y_output_n = y_output->shape()[0];
int y_output_h = y_output->shape()[1];
int y_output_w = y_output->shape()[2];
int y_output_c = y_output->shape()[3];
for (int i=0;i<(y_output_n*y_output_h*y_output_w*y_output_c;i++)
{
y_output->mudata<float>()[i]=
}
//工作中给tensor赋值
copy(std::unique_ptr<const venus::Tensor>& src,std::unique_ptr<venus::Tensor> &dst){
auto shape=src->shape();
dst->reshape(shape);
int size = 1;
for(auto s:shape){
size*=s;
}
float* src_data=src->mudata<float>();
float* dst_data=dst->mudata<float>();
memcpy(dst_data,src_data,sizeof(float)*size;
}
copy(float* src,std::unique_ptr<venus::Tensor> &dst,std::vector<int> shape){
int size = 1;
for(auto s:shape){
size*=s;
}
std->reshape(shape);
float* dst_data=src->mudata<float>();
memcpy(dst_data,src,sizeof(float)*size;
}
// 读取文本文件的前两列,顺序保存在map里(使用了sstream)
int readline_model_txt(std::string txt_file,std::map<int,std::pair<std::string,std::string>>& dataMap){
std::ifstream file(txt_file);
if (file.is_open()) {
std::string line;
int index = 0;
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string layer_op;
std::string layer_name;
// 用stringstream分割每一行,获取第二列的值
ss >> layer_op >> layer_name;
dataMap[index] = std::make_pair(layer_op,layer_name);
index++;
}
file.close();
} else {
std::cout << "无法打开文件" << std::endl;
}
for (const auto& pair : dataMap) {
std::cout << "[ " << pair.first << ", ( " << pair.second.first << " , " << pair.second.second << " ) ]" << std::endl;
}
return 0;
}
//
int main(){
//std::unique_prt<venus::Tensor> pqmf_x =pqmf_net->get_input_by_name(n1);获取tensor
std::vector<float> py_data;
std::string path_py_data = "./test/py_data_"+std::to_string(k)+".txt";
read_txt_input(path_py_data,py_data);
//copy(py_data.data(),py_tensor,shape_size);
std::map<int, std::pair<std::string,std::string>> txtMap;
readline_model_txt(std::string(net_param_path),txtMap);
return 0;
}
生命依靠吸收负熵,避免了趋向平衡的衰退

浙公网安备 33010602011771号