我正在使用Python 3.5中的TFLearn为模式识别问题构建DNN。我的输入形状为[50,300],所以粗略地说我的训练集是一个数组列表,每个数组由50个元素组成,这些元素是300个元素的数组。
我正在构建的所有数组都是NumPy数组。
这里是我正在使用的代码:
training = np.array(training)
# create train and test lists
train_x = list(training[:,0])
print(train_x[0])
train_y = list(training[:,1])
# reset underlying graph data
tf.reset_default_graph()
mean = int(len(train_x[0])/len(train_y[0]))
net = tflearn.input_data(shape=[None, 50, 300])
net = tflearn.fully_connected(net, mean)
net = tflearn.fully_connected(net, mean)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)
# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
# Start training (apply gradient descent algorithm)
model.fit(train_x, train_y, show_metric=True)
但我得到的错误如下:
Traceback (most recent call last):
File "/Users/Foo/Bar/test.py", line 82, in <module>
model.fit(train_x, train_y, show_metric=True)
File "/usr/local/lib/python3.5/site-packages/tflearn/models/dnn.py", line 215, in fit
callbacks=callbacks)
File "/usr/local/lib/python3.5/site-packages/tflearn/helpers/trainer.py", line 336, in fit
show_metric)
File "/usr/local/lib/python3.5/site-packages/tflearn/helpers/trainer.py", line 777, in _train
feed_batch)
File "/usr/local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run
np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
File "/usr/local/lib/python3.5/site-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
可能我不能很好地定义网络的形状。我该如何解决?
感谢您的建议
答案 0 :(得分:1)
定义网络没有任何问题,但问题是X和Y,首先它们是列表,但它们应该是数组,如果你首先修复它然后检查你所适合的数组的形状会更好。
检查一下:
# create train and test lists
train_x = training[:,0].reshape([-1,50,300])
print(train_x.shape)
train_y = training[:,1]