如何在TensorFlowSharp中重塑张量

时间:2017-08-18 09:25:44

标签: c# tensorflow 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的方式表达我需要的形状 任何建议将不胜感激:)!

1 个答案:

答案 0 :(得分:2)

在标准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

所示