Tensorflow C API:如何修改张量中的值

时间:2018-05-16 12:28:12

标签: c tensorflow

如何在不创建新张量的情况下更改张量中的值?

例如,我有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);

当我更改input中的值时,如何使input_tensor中的值也发生变化?

1 个答案:

答案 0 :(得分:0)

// 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)));