简体   繁体   English

如何将 Keras 张量转换为 numpy 数组

[英]How to convert Keras Tensor into numpy array

I'm struggling to convert a simple Keras tensor into a Numpy array.我正在努力将一个简单的 Keras 张量转换为 Numpy 数组。 This is the code I want to perform:这是我要执行的代码:

input = tf.keras.Input(shape=(100, 1), name='input')
inputs = np.array(input) 

Error: Cannot convert a symbolic Keras input/output to a numpy array.错误: Cannot convert a symbolic Keras input/output to a numpy array.

First things first.第一件事。 Your input is not a tensor, but a symbolic tensor.您的input不是张量,而是符号张量。

  1. So, what is symbolic tensor?那么,什么是符号张量? - You can consider it as tensor without any value associated with it. - 您可以将其视为没有任何相关值的张量。
  2. Now, what is the use of symbolic tensor?现在,符号张量有什么用? - it actually helps you build a model framework so that it is ready to accept the input anytime later. - 它实际上可以帮助您构建 model 框架,以便以后随时可以接受输入。

More can be found in this answer更多可以在这个答案中找到

Now, coming back to answer your question.现在,回来回答你的问题。 You need to convert the symbolic tensor to a tensor by first feeding the network with the data.您需要通过首先向网络提供数据来将符号张量转换为张量。 Once you have the tensor, then you can proceed to get the numpy array out of it.获得张量后,您可以继续从中取出 numpy 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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