如何在 TensorFlowSharp 中重塑张量

How to reshape a tensor in TensorFlowSharp

TensorFlowSharp 是 c# 平台中 TensorFlow 的包装器。 click it to jump to TensorFlowSharp github

现在我需要将形状为 [32,64,1] 的张量重塑为形状为 [1, 2048] 的新张量。但正如我参考官方 API 文档,用法似乎像那样:

TFOutput Reshape (TensorFlow.TFOutput tensor, TensorFlow.TFOutput shape);

问题是我不知道如何用TFOutput的方式表达我需要的形状 任何建议将不胜感激:)!

在标准 TensorFlowSharp 中,可以通过以下方式给出有关如何执行此操作的示例:

tf.Reshape(x, tf.Const(shape));

其中 tf 是您的 TFSession 中的当前默认 TFGraph。

或者,如果您使用 Keras Sharp,您可以使用

执行此操作
using (var K = new TensorFlowBackend())
{
    double[,] input_array = new double[,] { { 1, 2 }, { 3, 4 } };
    Tensor variable = K.variable(array: input_array);
    Tensor variable_new_shape = K.reshape(variable, new int[] { 1, 4 });
    double[,] output = (double[,])variable_new_shape.eval();

    Assert.AreEqual(new double[,] { { 1, 2, 3, 4 } }, output);
}

https://github.com/cesarsouza/keras-sharp/blob/efac7e34457ffb7cf6712793d5298b565549a1c2/Tests/TensorFlowBackendTest.cs#L45

所示