首页 > 解决方案 > 简单模型上的奇怪张量流行为

问题描述

在解决另一个问题时,我编写了这些代码行,得到了一个奇怪的输出。

我已经导入了 tensorflow 和以下层:

  import tensorflow as tf
    from keras import Sequential
    from keras.layers import Embedding
    from keras.layers import Dense
    from keras.layers import Dropout
    from keras.layers import LSTM

# create and fit the model

rnn_units = 1024
output_dim = 256
batch_size = 32
vocab_size = unique_columns.shape[1]
batch_input_dims = [batch_size, None]
input_shape_LSTM = (X.shape[1], 1)
# X has shape (200, 200000) and it is a numpy.ndarray

然后,我建立了两个模型。第一个带有导入层的:

def model_1(vocab_size, output_dim, batch_input_dims, rnn_units, input_shape_LSTM, name='LSTM_1'):
    
    model = Sequential(name=name)
    
    model.add(Embedding(input_dim=vocab_size+1, output_dim=output_dim, batch_input_shape=batch_input_dims))
    
    return model

第二个是 tf.keras.layers

def build_model(vocab_size, embedding_dim, rnn_units, batch_size, batch_input_dims, name='LSTM_2'):
    
    model = tf.keras.Sequential(name=name)
   
    model.add(tf.keras.layers.Embedding(vocab_size+1, embedding_dim, batch_input_shape=batch_input_dims))
        
    return model

然后我建立了两个模型:

      model = build_model(vocab_size, embedding_dim=output_dim, rnn_units=rnn_units,batch_size=batch_size, batch_input_dims=batch_input_dims)
model.summary()

Model: "LSTM_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (32, None, 256)           6522112   
=================================================================
Total params: 6,522,112
Trainable params: 6,522,112
Non-trainable params: 0

model_LSTM = model_1(vocab_size, output_dim, batch_input_dims, rnn_units, input_shape_LSTM)
model_LSTM.summary()
Model: "LSTM_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (32, None, 256)           6522112   
=================================================================
Total params: 6,522,112
Trainable params: 6,522,112
Non-trainable params: 0


model_LSTM = model_1(vocab_size, output_dim, batch_input_dims, rnn_units, input_shape_LSTM)

最后,如果我尝试输入像 X[:batch_size,:] 这样的输入

model(X[:32,:])
tf.Tensor: id=28, shape=(32, 200, 256), dtype=float32, numpy=
array([[[-0.02251144, -0.00920795, -0.01335046, ..., -0.00379463,
          0.00821525, -0.0356279 ],
        [-0.02251144, -0.00920795, -0.01335046, ..., -0.00379463,
          0.00821525, -0.0356279 ],
        [-0.02251144, -0.00920795, -0.01335046, ..., -0.00379463,
          0.00821525, -0.0356279 ],
        ...,
        [-0.02251144, -0.00920795, -0.01335046, ..., -0.00379463,
          0.00821525, -0.0356279 ],
        [-0.02251144, -0.00920795, -0.01335046, ..., -0.00379463,
          0.00821525, -0.0356279 ],
        [-0.02251144, -0.00920795, -0.01335046, ..., -0.00379463,
          0.00821525, -0.0356279 ]],...]]]

另一方面,如果我打电话,model_LSTM(X[:batch_size,:]我会得到原来的错误:

ValueError: Layer LSTM_1 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        3.92742126e-05, 3.92742126e-05, 3.92742126e-05],
       [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        3.92742126e-05, 3.92742126e-05, 3.92742126e-05],
       [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        5.30201869e-03, 2.12080748e-03, 3.92742126e-05],
       ...,
       [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        3.92742126e-05, 3.92742126e-05, 3.92742126e-05],
       [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        5.33383081e-01, 5.33383081e-01, 3.92742126e-05],
       [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        1.99395177e-01, 1.99395177e-01, 1.99395177e-01]])]. All inputs to the layer should be tensors.

有人可以解释这种行为吗?

标签: pythontensorflow

解决方案


tf.convert_to_tensor在传递之前在 X 上使用时错误会消失model_LSTM(X[:batch_size,:]吗?如果是这样,则该错误与您之前的问题相同。

如果你传入一个张量,通常一切都会正常工作。但是如果你传入张量以外的任何东西,比如 python 数组或 numpy 数组,它必须在某处转换为张量。Tensorflow 只能在内部使用它tf.Tensor的 s.

所以,如果你在某个地方传递一个非张量值,API 只希望看到张量,它会尝试对你的 numpy 数组进行张量操作,你会得到一个错误。

所以,对于任何一种All inputs to the layer should be tensorsvariable has no attribute foobar你应该做的第一件事就是尝试传入一个格式良好的张量。通常,这将消除异常。

另请注意,您应该始终像这样导入 keras 及其层:

from tensorflow.keras import someModule

不是

from keras import someModule

这是因为如果您还安装了单独的keras(不是 tf.keras),它将无法使用,tf.Tensor并且之后可能会导致混乱。准确说明您从哪里导入 keras API。kerasAPI有很多实现。例如,请参阅这个keras API 的实现。

这个答案有帮助吗?


推荐阅读