【问题标题】:How to reshape a tensor in TensorFlowSharp如何在 TensorFlowSharp 中重塑张量
【发布时间】:2017-08-18 09:25:44
【问题描述】:

TensorFlowSharpTensorFlow 在 c# 平台上的封装。 click it to jump to TensorFlowSharp github

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

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

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

【问题讨论】:

  • 对于shape 参数,你能用值[1, 2048] 构造一个元素类型为int64Constant 并传递它吗?

标签: c# tensorflow tensorflowsharp


【解决方案1】:

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

【讨论】:

  • 太棒了!非常感谢
猜你喜欢
  • 2018-08-27
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
  • 2016-10-18
  • 2018-10-27
  • 1970-01-01
相关资源
最近更新 更多