我正在神经网络中加载句子向量嵌入(Facebook的InferSent)以及其他元数据。以下是我的数据集的一瞥:
端点矢量:句子嵌入ndarray。形状:(4096,1) 描述Vector:句子嵌入ndarray。形状:(4096,1) HTTP路径:分类变量(0或1) 是描述:二进制因变量(待预测) EndpointDesc Count:数值变量(int) 余弦相似度:端点与极端之间的相似性描述载体。
我的数据集包含5621行和这6个功能。我是神经网络的新手,想要使用端点向量(以后使用所有其他功能)来预测“Is Description”变量。 Keras中LSTM的输入形状非常棘手。我不得不做同样的研究,我仍然不确定我输入LSTM的数据是否正常。
X = trainDf.iloc[:, [0]].values
y = trainDf.iloc[:, [3]].values
X = X.reshape(len(X), 1, 1)
y = y.reshape(len(y), 1, 1)
model = Sequential()
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=1, batch_size=1, validation_data=(X, y))
predict = model.predict(X)
代码抛出以下错误:
Train on 5621 samples, validate on 5621 samples
Epoch 1/1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-217-c46383616bf3> in <module>()
8 model.add(LSTM(1, input_shape=(1,1), return_sequences=True, activation="sigmoid"))
9 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
---> 10 model.fit(X, y, epochs=1, batch_size=1, validation_data=(X, y))
11
12 predict = model.predict(X)
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
963 initial_epoch=initial_epoch,
964 steps_per_epoch=steps_per_epoch,
--> 965 validation_steps=validation_steps)
966
967 def evaluate(self, x=None, y=None,
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1667 initial_epoch=initial_epoch,
1668 steps_per_epoch=steps_per_epoch,
-> 1669 validation_steps=validation_steps)
1670
1671 def evaluate(self, x=None, y=None,
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
1204 ins_batch[i] = ins_batch[i].toarray()
1205
-> 1206 outs = f(ins_batch)
1207 if not isinstance(outs, list):
1208 outs = [outs]
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2473 session = get_session()
2474 updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2475 **self.session_kwargs)
2476 return updated[:len(self.outputs)]
2477
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
952 np_val = subfeed_val.to_numpy_array()
953 else:
--> 954 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
955
956 if (not is_tensor_handle_feed and
/var/home/pranavmakhijani/anaconda/lib/python3.5/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
480
481 """
--> 482 return array(a, dtype, copy=False, order=order)
483
484 def asanyarray(a, dtype=None, order=None):
ValueError: setting an array element with a sequence.
后续步骤: 我想在我的数据集中包含所有其他变量作为训练特征。