简体   繁体   English

Keras Conv1D ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2

[英]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).每个输入(行)是一个长度为 684 的数组/向量(它不是时间序列)。 I want to use it in the Conv1D keras.我想在 Conv1D keras 中使用它。 But each time it says it needs more dimensions.但每次它都说它需要更多的维度。 Can you suggest a way to use it Conv1D.你能建议一种使用 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.所以我实现了一个解决方案,比如将数据条目的大小设置为 [342,2] 而不是 [684,1],但它不起作用。 I am open to your suggestions for alternative solutions and your help.我愿意接受您对替代解决方案的建议和您的帮助。

It works fine when i used dense layer instead of conv1d.当我使用密集层而不是 conv1d 时它工作正常。 Actually, I just need to adapt it to conv1d input in some way.实际上,我只需要以某种方式使其适应 conv1d 输入。

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.它给出错误:ValueError:层顺序的输入0与层不兼容::预期的min_ndim = 3,发现ndim = 2。 Full shape received: [None, 684]收到的完整形状:[无,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).每个输入(行)是一个长度为 684 的数组/向量(它不是时间序列)。 I want to use it in the Conv1D keras.我想在 Conv1D keras 中使用它。 But each time it says it needs more dimensions.但每次它都说它需要更多的维度。 Can you suggest a way to use it Conv1D.你能建议一种使用 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.所以我实现了一个解决方案,比如将数据条目的大小设置为 [342,2] 而不是 [684,1],但它不起作用。 I am open to your suggestions for alternative solutions and your help.我愿意接受您对替代解决方案的建议和您的帮助。

It works fine when i used dense layer instead of conv1d.当我使用密集层而不是 conv1d 时它工作正常。 Actually, I just need to adapt it to conv1d input in some way.实际上,我只需要以某种方式使其适应 conv1d 输入。

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.它给出错误:ValueError:层顺序的输入0与层不兼容::预期的min_ndim = 3,发现ndim = 2。 Full shape received: [None, 684]收到的完整形状:[无,684]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras Conv2D - ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3 - Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 ValueError: 层 conv1d 的输入 0 与层不兼容: : 预期 min_ndim=3, 发现 ndim=2 - ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras Conv1d输入形状问题,conv1d层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 Conv1D:ValueError:层序列_1 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到完整形状:(无,2) - Conv1D: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 2) 二进制信号数据:keras ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - binary signal data: keras ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 层 conv1d 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:(无,30) - Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30) ValueError:层顺序的输入0与层不兼容::预期的min_ndim = 4,发现ndim = 3 - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(2240、70、3) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (2240, 70, 3) ValueError:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(28、28、1) - ValueError: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (28, 28, 1) 在 Tensorflow 中创建 model 时出错。 ValueError: 层 conv2d_1 的输入 0 与层不兼容: : 预期 min_ndim=4, 发现 ndim=3 - Error with creating a model in Tensorflow. ValueError: Input 0 of layer conv2d_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM