cwenzi/neuroflow-cpp
1
1#include <iostream>2#include "../include/neuroflow/tensor.hpp"3 4using namespace neuroflow;5 6int main() {7 std::cout << "Reshape test..." << std::endl;8 9 Tensor input({1, 64});10 std::cout << "input shape: [" << input.shape_[0] << ", " << input.shape_[1] << "]" << std::endl;11 std::cout << "input strides: [" << input.strides[0] << ", " << input.strides[1] << "]" << std::endl;12 std::cout << "input numel: " << input.numel() << std::endl;13 std::cout << "input data_size: " << input.data_size_ << std::endl;14 15 // 添加数据16 float* d = input.as_fp32();17 std::cout << "Writing data..." << std::endl;18 for (size_t i = 0; i < input.numel(); ++i) d[i] = 0.1f * i;19 std::cout << "Data written" << std::endl;20 21 // reshape22 std::cout << "Calling reshape..." << std::endl;23 Tensor reshaped = input.reshape({1, 64});24 25 std::cout << "reshaped shape: [" << reshaped.shape_[0] << ", " << reshaped.shape_[1] << "]" << std::endl;26 std::cout << "reshaped strides: [" << reshaped.strides[0] << ", " << reshaped.strides[1] << "]" << std::endl;27 std::cout << "reshaped numel: " << reshaped.numel() << std::endl;28 std::cout << "reshaped owns_data: " << reshaped.owns_data_ << std::endl;29 30 // 访问 reshaped 数据31 std::cout << "Accessing reshaped data..." << std::endl;32 float* rd = reshaped.as_fp32();33 std::cout << "First 5 values: ";34 for (size_t i = 0; i < 5; ++i) std::cout << rd[i] << " ";35 std::cout << std::endl;36 37 std::cout << "Success!" << std::endl;38 return 0;39}40 