简体   繁体   中英

Tensorflow C API: How to modify the value in tensor

How to change values ​​in a tensor without creating a new tensor?

For example, I have a tensor :

const size_t input_dims_len = 3;
int64_t input_dims[input_dims_len] = {1, 5, 12};
float input[5 * 12] = {...};
TF_Tensor* input_tensor = TF_NewTensor(TF_FLOAT,
                                       input_dims,
                                       input_dims_len,
                                       input,
                                       5 * 12 * sizeof(float),
                                       deallocate_tensor,
                                       nullptr);

When I change the values in input , how do make it so that the values in input_tensor are also changed?

// Allocate tensor
const int input_dims_len = 3;
int64_t input_dims[input_dims_len] = {1, 5, 12};
const int data_size = 5 * 12 * sizeof(float));
TF_Tensor* tensor = TF_AllocateTensor(TF_FLOAT,
                                      input_dims, input_dims_len,
                                      data_size);
// Change values ​​in a tensor
float input[5 * 12] = {...};
std::memcpy(TF_TensorData(tensor), input, std::min(data_size, TF_TensorByteSize(tensor)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM