【问题标题】:Dimensions must be equal, but are 2 and 3 for node binary_crossentropy/mul维度必须相等,但节点 binary_crossentropy/mul 为 2 和 3
【发布时间】:2021-09-07 18:19:20
【问题描述】:

我正在检查我找到的代码here,例如Multivariate Multi-Step LSTM Models - > Multiple Input Multi-Step Output

我修改了代码并在最后一层使用了binary_crossentropysigmoid 激活。

from numpy import array
from numpy import hstack
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
 
# split a multivariate sequence into samples
def split_sequences(sequences, n_steps_in, n_steps_out):
    X, y = list(), list()
    for i in range(len(sequences)):
        # find the end of this pattern
        end_ix = i + n_steps_in
        out_end_ix = end_ix + n_steps_out-1
        # check if we are beyond the dataset
        if out_end_ix > len(sequences):
            break
        # gather input and output parts of the pattern
        seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1:out_end_ix, -1]
        X.append(seq_x)
        y.append(seq_y)
    return array(X), array(y)
 
# define input sequence
in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90])
in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95])
out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])
# convert to [rows, columns] structure
in_seq1 = in_seq1.reshape((len(in_seq1), 1))
in_seq2 = in_seq2.reshape((len(in_seq2), 1))
out_seq = out_seq.reshape((len(out_seq), 1))
# horizontally stack columns
dataset = hstack((in_seq1, in_seq2, out_seq))

# choose a number of time steps
n_steps_in, n_steps_out = 3, 3
# convert into input/output
X, y = split_sequences(dataset, n_steps_in, n_steps_out)

n_features = X.shape[2]

# define model
model = Sequential()
model.add((LSTM(5, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features))))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# fit model
model.fit(X, y, epochs=20, verbose=0, batch_size=1)

上面的代码运行良好。但是,当我尝试更改 n_steps_in, n_steps_out 并使用例如:n_steps_in, n_steps_out = 3, 2 时,它会给出:

 ValueError: Dimensions must be equal, but are 2 and 3 for '{{node binary_crossentropy/mul}} = Mul[T=DT_FLOAT](binary_crossentropy/Cast, binary_crossentropy/Log)' with input shapes: [1,2], [1,3].

为什么会出现这个错误,我该如何克服?

【问题讨论】:

    标签: tensorflow keras deep-learning


    【解决方案1】:

    这是因为您的网络构建为输出形状为 (None, 3, 1) 的 3D 序列,而您的目标的形状为 (None, 2, 1)

    正确处理这种情况的最佳和自动化方法是构建编码器-解码器结构...在示例下方:

    model = Sequential()
    model.add(LSTM(5, activation='relu', return_sequences=False, 
                   input_shape=(n_steps_in, n_features)))  # ENCODER
    model.add(RepeatVector(n_steps_out))
    model.add(LSTM(5, activation='relu', return_sequences=True))  # DECODER
    model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    model.fit(X, y, epochs=20, batch_size=1)
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2018-06-12
      • 2022-01-19
      • 2018-02-26
      • 1970-01-01
      相关资源
      最近更新 更多