【发布时间】:2020-03-09 23:09:07
【问题描述】:
我正在研究共指解析模型,并试图将大量数据矩阵输入到我的 CNN 输入层。出于说明目的,我截断了我的数据以使用更易于管理的数字。
格式化数据函数
EMBEDDING_DIM = 400
...
@staticmethod
def get_train_data(data: DefaultDict[ClusteredDictKey, PreCoCoreferenceDatapoint], embedding_model) -> Tuple[List[Tensor], List[Tensor]]:
"""
(n_samples, n_words, n_attributes (word embedding, pos, etc))
[ [ [ word_embedding, pos ] ] ]
xtrain[sentence_sample][word_position][attribute]
xtrain[0][0] -> first word's attributes in first sentence
xtrain[37][5] -> sixth word's attributes in 38th sentence
xtrain[0][0][0] -> word_embedding
xtrain[0][0][1] -> pos one-hot encoding
"""
xtrain = []
ytrain = []
pos_onehot = PreCoParser.get_pos_onehot() # dictionary mapping POS to one-hot encoding
for key, value in data.items():
training_data = []
sentence_embeddings = PreCoParser.get_embedding_for_sent(key.sentence, embedding_model) # Returns tensor (ndarray) of shape: (tokens_in_sent, EMBEDDING_DIM)
pos = PreCoParser.get_pos_onehot_for_sent(key.sentence, pos_onehot) # Returns tensor (ndarray) of shape: (45,)
assert sentence_embeddings.shape == (len(key.sentence), EMBEDDING_DIM)
assert pos.shape == (len(key.sentence), 45)
for i, embedding in enumerate(sentence_embeddings):
training_data.append(np.asarray([embedding, np.asarray(pos[i])]))
cluster_indices = list(sum([cluster.indices for cluster in value], ()))
# Delete every third element to remove sentence index
del cluster_indices[0::3]
if len(training_data) > 0:
xtrain.append(np.asarray(training_data))
ytrain.append(np.asarray(cluster_indices) / len(key.sentence)) # normalize output data
gc.collect()
return (np.asarray(xtrain), np.asarray(ytrain))
缩写问题
简而言之,我有一个能够成功运行以下断言的 NumPy 数组:
assert self.xtrain[0][0][0].shape == (EMBEDDING_DIM,)
至少对我而言,这意味着该数组包含 4 个维度,最终向量包含 EMBEDDING_DIM 个元素(在我的情况下为 400 个)。
但是,运行以下代码会产生奇怪的结果:
>>> self.xtrain.shape
(500,)
>>> self.xtrain[0].shape # on sentence with 11 words
(11,2)
>>> self.xtrain[0][0].shape # two attributes
(2,)
>>> self.xtrain[0][0][0].shape
(400,)
>>> self.xtrain[0][0][1].shape
(45,)
500 指的是我截断的样本数(并且所有输出都符合我的预期)。此外,当通过一个简单的 Keras Conv2D 输入层输入这些数据时,我会遇到以下错误:
self.model.fit(self.xtrain, self.ytrain, epochs=1)
File "/usr/local/lib/python3.7/site-packages/keras/engine/training.py", line 1154, in fit
batch_size=batch_size)
File "/usr/local/lib/python3.7/site-packages/keras/engine/training.py", line 579, in _standardize_user_data
exception_prefix='input')
File "/usr/local/lib/python3.7/site-packages/keras/engine/training_utils.py", line 135, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (499, 1)
我很乐意发布更多代码,需要,但目前的任何帮助将不胜感激!
【问题讨论】:
-
检查每个级别的
dtype。看来xtrain是object的一维数组。一,也许所有元素都是(11,2)数组,对象也是如此。这是最低级别 400 和 45 形状的混合,这会阻止您一直得到一个 n-d 数组。 -
你说得对,我的 dtypes 都是混合的。从外到内分别是:
object、object、float32、uint8。我假设我的下一步是将dtype标准化为float32? -
当尺寸不匹配时你会得到对象 dtype