Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow keras Sequential.add与内联定义不同?_Tensorflow_Keras_Deep Learning - Fatal编程技术网

Tensorflow keras Sequential.add与内联定义不同?

Tensorflow keras Sequential.add与内联定义不同?,tensorflow,keras,deep-learning,Tensorflow,Keras,Deep Learning,当我通过声明性方法而不是函数性方法定义模型时,Keras给出了不同的结果。这两个模型似乎是等价的,但使用“.add()”语法是有效的,而使用声明性语法会产生错误——每次都是不同的错误,但通常类似于: shape(10,1)的目标数组被传递为shape(None,16)的输出,同时用作loss`mean_squared_error`。此损失期望目标与输出具有相同的形状。 输入形状的自动转换似乎有些问题,但我不知道是什么。有人知道我做错了什么吗?为什么这两个模型不完全相同 import tensor

当我通过声明性方法而不是函数性方法定义模型时,Keras给出了不同的结果。这两个模型似乎是等价的,但使用“.add()”语法是有效的,而使用声明性语法会产生错误——每次都是不同的错误,但通常类似于:
shape(10,1)的目标数组被传递为shape(None,16)的输出,同时用作loss`mean_squared_error`。此损失期望目标与输出具有相同的形状。
输入形状的自动转换似乎有些问题,但我不知道是什么。有人知道我做错了什么吗?为什么这两个模型不完全相同

import tensorflow as tf
import tensorflow.keras
import numpy as np

x = np.arange(10).reshape((-1,1,1))
y = np.arange(10)

#This model works fine
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(32, input_shape=(1, 1), return_sequences = True))
model.add(tf.keras.layers.LSTM(16))
model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.layers.Activation('linear'))

#This model fails. But shouldn't this be equivalent to the above?
model2 = tf.keras.Sequential(
{
    tf.keras.layers.LSTM(32, input_shape=(1, 1), return_sequences = True),
    tf.keras.layers.LSTM(16),
    tf.keras.layers.Dense(1),
    tf.keras.layers.Activation('linear')
})

#This works
model.compile(loss='mean_squared_error', optimizer='adagrad')
model.fit(x, y, epochs=1, batch_size=1, verbose=2)

#But this doesn't! Why not? The error is different each time, but usually
#something about the input size being wrong
model2.compile(loss='mean_squared_error', optimizer='adagrad') 
model2.fit(x, y, epochs=1, batch_size=1, verbose=2)
为什么这两个模型不相等?为什么一个能正确处理输入大小,而另一个不能?第二个模型每次都会出现不同的错误(有时甚至会出现错误),所以我想可能与第一个模型有一些交互作用?但是我试着对第一个模型进行了注释,但没有任何帮助。那么为什么第二个不起作用呢

更新:这是第一个和第二个模型的“model.summary()。它们看起来确实不同,但我不明白为什么

对于model.summary():

对于model2.summary():


使用内联声明创建模型时,将层放在大括号
{}
中,这使其成为一个集合,本质上是无序的。将大括号更改为方括号
[]
将它们放在有序列表中。这将确保层在模型中的顺序正确。

使用内联声明创建模型时,将层放在大括号
{}
中,这使其成为一个集合,本质上是无序的。将大括号更改为方括号
[]
将它们放入一个有序列表中。这将确保层在模型中的顺序正确。

可以为每个模型包含
model.summary()
吗?好的,我在上面添加了它。但不确定它们为什么不同?可以包含
model.summary()吗
对于每种型号?好的,我在上面添加了。但不确定它们为什么不同?
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 1, 32)             4352      
_________________________________________________________________
lstm_1 (LSTM)                (None, 16)                3136      
_________________________________________________________________
dense (Dense)                (None, 1)                 17        
_________________________________________________________________
activation (Activation)      (None, 1)                 0         
=================================================================
Total params: 7,505
Trainable params: 7,505
Non-trainable params: 0
model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_2 (LSTM)                (None, 1, 32)             4352      
_________________________________________________________________
activation_1 (Activation)    (None, 1, 32)             0         
_________________________________________________________________
lstm_3 (LSTM)                (None, 16)                3136      
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 17        
=================================================================
Total params: 7,505
Trainable params: 7,505
Non-trainable params: 0```