[英]Declaring input_shape of a converted Sequence in Keras?
I am trying to run a neural network on text inputs.我正在尝试在文本输入上运行神经网络。 This is a binary classification.
这是一个二元分类。 Here is my working code so far:
到目前为止,这是我的工作代码:
df = pd.read_csv(pathname, encoding = "ISO-8859-1")
df = df[['content_cleaned', 'meaningful']] #Content cleaned: text, meaningful: label
X = df['content_cleaned']
y = df['meaningful']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=21)
tokenizer = Tokenizer(num_words=100)
tokenizer.fit_on_texts(X_train)
X_train_encoded = tokenizer.texts_to_sequences(X_train)
X_test_encoded = tokenizer.texts_to_sequences(X_test)
max_len = 100
X_train = pad_sequences(X_train_encoded, maxlen=max_len)
X_test = pad_sequences(X_test_encoded, maxlen=max_len)
batch_size = 100
max_words = 100
input_dim = X_train.shape[1] # Number of features
model = Sequential()
model.add(layers.Dense(10, activation='relu', input_shape=X_train.shape[1:]))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
history = model.fit(X_train, X_test,
batch_size=batch_size,
epochs=5,
verbose=1,
validation_split=0.1)
My question is two parts.我的问题是两部分。 First is with the
input_shape
when creating the layers.首先是在创建图层时使用
input_shape
。 I am confused as to the syntax of declaring this.我对声明这一点的语法感到困惑。 When running this command:
运行此命令时:
print(X_train.shape)
I am getting this shape: (3609, 100)
.我得到这个形状:
(3609, 100)
。
From my understanding, this is telling me that there are 3609 instances.根据我的理解,这告诉我有 3609 个实例。 From viewing other examples, my naive assumption was to use the 100 as there are 100 types (may be understanding this incorrectly) corresponding to the
max_words
that I initialized.从查看其他示例来看,我天真的假设是使用 100,因为有 100 种类型(可能理解错误)对应于我初始化的
max_words
。 I believe that I may have done the syntax incorrectly when initializing the input_shape
.我相信在初始化
input_shape
时我可能没有正确地完成语法。
The second question is with an error message when running all of this (most likely with the incorrect input_shape
).第二个问题是运行所有这些时的错误消息(很可能是不正确的
input_shape
)。 The error message highlights this line of code:错误消息突出显示了这行代码:
validation_split=0.1)
The error message is:错误信息是:
ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (1547, 1
Am I going about this problem incorrectly?我是否错误地解决了这个问题? I am very new to Deep Learning.
我对深度学习很陌生。
The input_shape
argument specifies the shape of one training sample . input_shape
参数指定一个训练样本的形状。 Therefore, you need to set it to X_train.shape[1:]
(ie ignore samples or batch axis):因此,您需要将其设置为
X_train.shape[1:]
(即忽略样本或批处理轴):
model.add(layers.Dense(10, activation='relu', input_shape=X_train.shape[1:]))
Further, pass X_train
and y_train
to the fit_generator
(instead of X_train_encoded
and X_test_encoded
).此外,将
X_train
和y_train
传递给fit_generator
(而不是X_train_encoded
和X_test_encoded
)。
You missed two ending parenthesis )
at the line where you defined the input of your model.您在定义模型输入的行中错过了两个结束括号
)
。 Also make sure that you provide your activation function .还要确保您提供了激活函数。
Change your code as below:更改您的代码如下:
model.add(layers.Dense(10, activation='relu', input_shape=(X_train.shape[0],)))
EDIT:编辑:
For your last error just change your input_shape
to input_shape=(X_train.shape[0],)
.对于您的最后一个错误,只需将
input_shape
更改为input_shape=(X_train.shape[0],)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.