第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Keras LSTM 輸入和輸出維度問(wèn)題

Keras LSTM 輸入和輸出維度問(wèn)題

牛魔王的故事 2023-05-09 15:36:01
我正在嘗試為多步預(yù)測(cè)創(chuàng)建 LSTM 模型。現(xiàn)在我正在測(cè)試模型網(wǎng)絡(luò)設(shè)置,但發(fā)現(xiàn)它在設(shè)置上存在尺寸問(wèn)題。這是我的測(cè)試數(shù)據(jù)集:length = 100df = pd.DataFrame()df['x1'] = [i/float(length) for i in range(length)]df['x2'] = [i**2 for i in range(length)]df['y'] = df['x1'] + df['x2'] x_value = df.drop(columns = 'y').valuesy_value = df['y'].values.reshape(-1,1)這是我的 t 窗口數(shù)據(jù)構(gòu)建函數(shù):def build_data(x_value, y_value ,n_input, n_output):    X, Y = list(), list()    in_start = 0    data_len = len(x_value)    # step over the entire history one time step at a time    for _ in range(data_len):        # define the end of the input sequence        in_end = in_start + n_input        out_end = in_end + n_output        if out_end <= data_len:            x_input = x_value[in_start:in_end] # e.g. t0-t3            X.append(x_input)            y_output = y_value[in_end:out_end] # e.g. t4-t5            Y.append(y_output)        # move along one time step        in_start += 1    return np.array(X), np.array(Y)            X, Y = build_data(x_value, y_value, 1, 2)X 和 Y 的形狀X.shape### (98, 1, 2)Y.shape### (98, 2, 1)對(duì)于模型部分,verbose, epochs, batch_size = 1, 20, 16n_neurons =  100n_inputs, n_features  = X.shape[1], X.shape[2]n_outputs = Y.shape[1]model = Sequential()model.add(LSTM(n_neurons, input_shape = (n_inputs, n_features), return_sequences=True))model.add(TimeDistributed(Dense(1)))model.compile(loss='mean_squared_error', optimizer='adam')model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=verbose)它發(fā)生了錯(cuò)誤: ValueError: Error when checking target: expected time_distributed_41 to have shape (1, 1) but got array with shape (2, 1)如果使用那X, Y = build_data(x_value, y_value, 2, 2) i.e. input window == output window將是有效的。但我認(rèn)為它不應(yīng)該包含這個(gè)約束。我怎樣才能克服這個(gè)問(wèn)題?即設(shè)置為input window != output window或我應(yīng)該設(shè)置的任何圖層或設(shè)置?
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

您在處理時(shí)間維度時(shí)遇到形狀不匹配...當(dāng)您嘗試預(yù)測(cè)時(shí)間維度為 2 的內(nèi)容時(shí),時(shí)間輸入 dim 為 1。因此您的網(wǎng)絡(luò)中需要一些能夠從 1 增加到 2 時(shí)間的東西方面。我使用了Upsampling1D圖層,下面是一個(gè)完整的例子


# create fake data

X = np.random.uniform(0,1, (98,1,2))

Y = np.random.uniform(0,1, (98,2,1))


verbose, epochs, batch_size = 1, 20, 16

n_neurons =  100

n_inputs, n_features  = X.shape[1], X.shape[2]

n_outputs = Y.shape[1]


model = Sequential()

model.add(LSTM(n_neurons, input_shape = (n_inputs, n_features), return_sequences=True))

model.add(UpSampling1D(n_outputs))

model.add(TimeDistributed(Dense(1)))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=verbose)

輸入時(shí)間暗淡 > 輸出時(shí)間暗淡,您可以使用 Lambda 或 Pooling 操作(如果維度匹配)。下面是一個(gè)使用 Lambda 的例子


X = np.random.uniform(0,1, (98,3,2))

Y = np.random.uniform(0,1, (98,2,1))


verbose, epochs, batch_size = 1, 20, 16

n_neurons =  100

n_inputs, n_features  = X.shape[1], X.shape[2]

n_outputs = Y.shape[1]


model = Sequential()

model.add(LSTM(n_neurons, input_shape = (n_inputs, n_features), return_sequences=True))

model.add(Lambda(lambda x: x[:,-n_outputs:,:]))

model.add(TimeDistributed(Dense(1)))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=verbose)


查看完整回答
反對(duì) 回復(fù) 2023-05-09
  • 1 回答
  • 0 關(guān)注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)