简体   繁体   中英

Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2

I have an data set and it is not a time series. Each inputs (row) is an array/vector which has the length 684 (It is not a time series). I want to use it in the Conv1D keras. But each time it says it needs more dimensions. Can you suggest a way to use it Conv1D. I want to apply input without being dependent on previous data such as time series. So I implemented a solution like making the size of the data entry [342,2] instead of [684,1], but it didn't work. I am open to your suggestions for alternative solutions and your help.

It works fine when i used dense layer instead of conv1d. Actually, I just need to adapt it to conv1d input in some way.

As an example:

from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import to_categorical
import time
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf

verbose, epochs, batch_size = 0, 10, 100


df = pd.DataFrame(np.random.randint(0,3,(1000,685)))
x=df[df.columns[1:]] 
y=df[df.columns[1]] 


train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.15, random_state=17)

train_y = to_categorical(train_y)
test_y = to_categorical(test_y)

n_features, n_outputs = train_x.shape[1], train_y.shape[1]
    
                
model = Sequential()
#model.add(Dense(n_features, activation='relu'))
#model.add(Dropout(0.25))
model.add(tf.keras.layers.Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(684,1)))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Dense(50, activation= 'relu'))
model.add(Dropout(0.2))
model.add(Dense(20, activation= 'relu'))
model.add(Dropout(0.2))
model.add(Dense(n_outputs, activation='softmax'))

t=time.time()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history=model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(test_x, test_y, batch_size=batch_size, verbose=verbose)


print(accuracy)

it give error: ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 684]

I have an data set and it is not a time series. Each inputs (row) is an array/vector which has the length 684 (It is not a time series). I want to use it in the Conv1D keras. But each time it says it needs more dimensions. Can you suggest a way to use it Conv1D. I want to apply input without being dependent on previous data such as time series. So I implemented a solution like making the size of the data entry [342,2] instead of [684,1], but it didn't work. I am open to your suggestions for alternative solutions and your help.

It works fine when i used dense layer instead of conv1d. Actually, I just need to adapt it to conv1d input in some way.

As an example:

from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import to_categorical
import time
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf

verbose, epochs, batch_size = 0, 10, 100


df = pd.DataFrame(np.random.randint(0,3,(1000,685)))
x=df[df.columns[1:]] 
y=df[df.columns[1]] 


train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.15, random_state=17)

train_y = to_categorical(train_y)
test_y = to_categorical(test_y)

n_features, n_outputs = train_x.shape[1], train_y.shape[1]
    
                
model = Sequential()
#model.add(Dense(n_features, activation='relu'))
#model.add(Dropout(0.25))
model.add(tf.keras.layers.Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(684,1)))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Dense(50, activation= 'relu'))
model.add(Dropout(0.2))
model.add(Dense(20, activation= 'relu'))
model.add(Dropout(0.2))
model.add(Dense(n_outputs, activation='softmax'))

t=time.time()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history=model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(test_x, test_y, batch_size=batch_size, verbose=verbose)


print(accuracy)

it give error: ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 684]

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