【发布时间】:2021-10-11 19:06:00
【问题描述】:
尝试为基于文本的多标签分类问题训练单层神经网络。
model= Sequential()
model.add(Dense(20, input_dim=400, kernel_initializer='he_uniform', activation='relu'))
model.add(Dense(9, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(x_train, y_train, verbose=0, epochs=100)
得到错误:
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
x_train 是一个 300 维 word2vec 矢量化文本数据,每个实例填充到 400 长度。包含462条记录。
对训练数据的观察如下:
print('#### Shape of input numpy array #####')
print(x_train.shape)
print('#### Shape of each element in the array #####')
print(x_train[0].shape)
print('#### Object type for input data #####')
print(type(x_train))
print('##### Object type for first element of input data ####')
print(type(x_train[0]))
#### Shape of input numpy array #####
(462,)
#### Shape of each element in the array #####
(400, 300)
#### Object type for input data #####
<class 'numpy.ndarray'>
##### Object type for first element of input data ####
<class 'numpy.ndarray'>
【问题讨论】:
-
我打赌
x_train的dtype是object。这通常是从形状不同的数组列表中创建一个数组的结果。您显示第一个元素的形状。但这是所有 462 的形状吗?np.stack(x_train)是做什么的? -
print(type(x))通常不是很有用。print(x.dtype)提供更多信息。如果dtype是object,那么它包含对象,例如其他数组。如果float是一个数字数组,可能tensorflow可以使用。 -
@hpaulj 是的,dtype 是对象。
标签: python arrays numpy tensorflow neural-network