大佬教程收集整理的这篇文章主要介绍了理解 tf.keras.Input 中的形状?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我只学习了 tensorflow 和 keras。这是一个代码示例:
# Create a symbolic input
input = tf.keras.input(shape=(2,),dtype=tf.float32)
# Do a calculation using is
result = 2*input + 1
# the result doesn't have a value
result
calc = tf.keras.Model(inputs=input,outputs=result)
print(calc(np.array([1,2,3,4])).numpy())
print(calc(2).numpy())
文档说 shape:一个形状元组(整数),不包括批量大小。
例如,shape=(32,)
表示预期输入将是 32 维向量的批次。这个元组的元素可以是None
; None
元素表示形状未知的维度。
但是在上面的代码中,两条打印线都有效。但对我来说,它们是 1D
维和 1 个标量。那么如何理解形状呢?
事情是 tf. keras.Input
产生符号张量或占位符。它可以与 TF
操作一起使用。请参阅source code:
Note that even if eager execution is enabled,`Input` produces a symbolic tensor (i.e. a placeholder).
This symbolic tensor can be used with other
TensorFlow ops,as such:
'''python
x = Input(shape=(32,))
y = tf.square(X)
'''
这就是为什么这些打印行都有效。
现在,这里有一些场景。在您的代码中,您可以设置 shape = [n]
where n > = 0
,用于等级 0
和 1
,它们分别是缩放器和向量。但是,如果 2
不等于 n
,您将收到秩 x.shape[1]
或矩阵错误。例如:
import tensorflow as tf
import numpy as np
# Create a symbolic input
input = tf.keras.Input(shape=[0],dtype=tf.float32)
result = 2*input + 1
calc = tf.keras.Model(inputs=input,outputs=result)
print(calc(1).numpy()) # scaler rank 0
print(calc(np.array([1,2,3,4])).numpy()) # vector rank 1
print(calc(np.array([[1,4]])).numpy()) # matrix rank 2
3.0
[3. 5. 7. 9.]
ValueError: Input 0 is incompatible with layer model_2:
expected shape=(None,0),found shape=(1,4)
为了解决这个问题,我们需要为形状参数设置精确数量的特征维度,这里应该是 4
。
# Create a symbolic input
input = tf.keras.Input(shape=[4],4]])).numpy()) # matrix rank 2
3.0
[3. 5. 7. 9.]
[[3. 5. 7. 9.]]
这里有一个有趣的事实,如果我们用带有标量或向量的 calc
构建这个 shape = [1]
模型,然后后跟 2D
矩阵,它不会对 2D
输入提出任何投诉,因为只有在模型没有首先构建时才会引发错误。通过一些输入调用模型,模型的形状被设置。
# Create a symbolic input
input = tf.keras.Input(shape=[1],4]])).numpy()) # matrix rank 2
3.0
[[3.]
[5.]
[7.]
[9.]]
[[3. 5. 7. 9.]]
但是 AFAIK,如果您构建具有可训练层的模型,就不可能像这样玩。在这种情况下,您需要确保 shape
和输入数据之间的形状匹配问题。例如:
x = tf.keras.Input(shape=[4])
y = tf.keras.layers.Dense(10)(X)
model = tf.keras.Model(x,y)
print(model(np.array([[1,4]])).numpy()) # (1,4)
print(model(np.array([1,4])).numpy()) # (4,)
[[ 1.4779348 -1.8168153 -0.93788755 -1.4927139 -0.23618054 2.4305463
-1.6176091 0.6640817 -1.648994 3.5819988 ]]
ValueError: Input 0 of layer dense_1 is incompatible with the layer:
: expected min_ndim=2,found ndim=1. Full shape received: (4,)
,
如果您要编译并尝试 fit
,我很确定您的“模型”会失败,因为没有什么可“学习”的。
然而:它认为对于 2*input + 1
这样的简单操作,形状是不相关的,因为模型不包含任何神经网络节点,所以它没有建立权重和偏置矩阵,这需要一个形状众所周知。
因此,您不应该期望输入形状为 (2,)
的模型既不接受形状 (4,)
的标量也不接受矢量,这是正确的 - 一旦您有了添加了一些神经网络层,例如 keras.layers.Dense()
或可比较的。
以上是大佬教程为你收集整理的理解 tf.keras.Input 中的形状?全部内容,希望文章能够帮你解决理解 tf.keras.Input 中的形状?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。