簡體   English   中英

keras 順序 model 的基數問題

[英]Cardinality problem with keras Sequential model

我對 keras 有一些問題。 我想預測一個序列中的下一個數字,我決定使用 keras.Sequential 來做到這一點(因為我們的教授談到了遞歸神經網絡)。 在 model.fit 運行結束時,代碼警告說測試集的基數存在問題。 我是 keras(和一般編碼)的新手,所以我看不出問題出在哪里,所以我需要你的幫助。 這是我的代碼:

# here there's some code to construct the Fibonacci sequence mod 15 in a dataset (my code has to be generic)


predicted_values=[1,2,3,4,5]
rows=df_fib.shape[0]

# Dictionary for storing generated models
models = {}

#I want to predict k values in the sequence
for k in predicted_values:
    
    n = int(rows*(2/3))
    
    X_train = df_fib.iloc[0:n, 0].values
    y_train = df_fib.iloc[n:n+k, 0].values
    X_test = df_fib.iloc[n+k:rows-k, 0].values
    y_test = df_fib.iloc[rows-k:, 0].values

    model = keras.Sequential()
    model.add(layers.Dense(1, activation = 'relu'))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(X_train.reshape([1,-1]), y_train, epochs=20)
    scores = model.evaluate(X_test.reshape([1,-1]), y_test)
    print('Accuracy Score - ',k,' values : ' % scores[1]*100)

警告是:

ValueError: Data cardinality is ambiguous:
  x sizes: 1
  y sizes: 2
Please provide data which shares the same first dimension.

您的數據形狀不匹配。 您的數據的第一個維度,通常是您的批量大小,特別是因為您沒有指定一個,是不同的,因此不清楚如何使用數據。 同樣在順序 model 的第一層上,您將要設置數據的 input_shape,例如: model.add(layers.Dense(16, activation='relu', input_shape=(6,))) 除外您的輸入顯然不會是 6。 玩轉 x_train 和 y_train 的 output 形狀的打印,這應該可以幫助您找出問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM