简体   繁体   中英

Cardinality problem with keras Sequential model

I have some problem with keras. I want to predict next numbers in a sequence, and I decided to use keras.Sequential to do it (since our professor talked about recursive neural networks). At the end of the model.fit run, the code warns that there is a problem with the cardinality of the test sets. I'm new to keras (and coding in general), so I cannot see where the problem is, so I need your help. Here is my code:

# 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)

The warning is:

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

The shape of your data doesn't match up. The first dimension of your data, which is usually going to be your batch size, especially since you don't specify one, is different, so its unclear how to use the data. Also on that first layer of your sequential model you are going to want to set the input_shape of your data, something like: model.add(layers.Dense(16, activation='relu', input_shape=(6,))) Except your input is not going to be 6 obviously. Play around the print the output shape of your x_train and y_train, that should help you to figure out what the issue is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM